美文网首页
Android绘图之AChartEngine的简单应用

Android绘图之AChartEngine的简单应用

作者: 李广凯 | 来源:发表于2019-10-13 09:43 被阅读0次

    这是结合csdn上大佬的写法简化了一下。好多属性没放上,简单参考一下吧。

    先放两张效果图

    image image

    主要代码

    1、折线图

    
    public class LineChartHelper {
    
    private GraphicalViewgraphicalView;
    
        private XYMultipleSeriesDatasetxyMultipleSeriesDataset;//数据集容器
    
        private XYMultipleSeriesRendererxyMultipleSeriesRenderer;//渲染器容器
    
        private XYSeriesxySeries;//单条曲线数据集
    
        private XYSeriesRendererxySeriesRenderer;//单条曲线渲染器
    
        private Contextcontext;
    
        public LineChartHelper(Context context) {
    
            this.context = context;
    
        }
    
    public GraphicalViewgetGraphicalView() {
    
    graphicalView = ChartFactory.getLineChartView(context, xyMultipleSeriesDataset
    
                    , xyMultipleSeriesRenderer);
    
            return graphicalView;
    
        }
    
    public void setXyMultipleSeriesDataset(String chartTitle) {
    
    xyMultipleSeriesDataset =new XYMultipleSeriesDataset();
    
            xySeries =new XYSeries(chartTitle);
    
            xyMultipleSeriesDataset.addSeries(xySeries);
    
        }
    
    public void setXyMultipleSeriesRenderer(String chartTitle) {
    
    xyMultipleSeriesRenderer =new XYMultipleSeriesRenderer();
    
            if (chartTitle !=null) {
    
    xyMultipleSeriesRenderer.setChartTitle(chartTitle);
    
            }
    
    xyMultipleSeriesRenderer.setShowGrid(true);
    
            xyMultipleSeriesRenderer.setXLabelsColor(Color.BLACK);
    
            xyMultipleSeriesRenderer.setYLabelsColor(0, Color.BLACK);
    
            xyMultipleSeriesRenderer.setMarginsColor(Color.WHITE);
    
            xyMultipleSeriesRenderer.setFitLegend(true);//自适应大小
    
            xySeriesRenderer =new XYSeriesRenderer();
    
            xySeriesRenderer.setDisplayChartValues(true);//显示数值
    
            xyMultipleSeriesRenderer.addSeriesRenderer(xySeriesRenderer);
    
        }
    
    public void updateChart(double x, double y) {
    
    xySeries.add(x, y);
    
            xyMultipleSeriesRenderer.setRange(new double[]{x -5, x, 0, 1000});
    
            graphicalView.repaint();
    
        }
    
    }
    
    

    调用

    public void setLineChart(String chartTitle) {
    
            lineChartHelper = new LineChartHelper(MainActivity.this);
            lineChartHelper.setXyMultipleSeriesDataset(chartTitle);
            lineChartHelper.setXyMultipleSeriesRenderer(chartTitle);
    
            graphicalView = lineChartHelper.getGraphicalView();
    
            lineLayout.addView(graphicalView);
        }
    

    2、饼图

    public class PieChartHelper {
    
        private GraphicalView graphicalView;
        private CategorySeries categorySeries;
        private DefaultRenderer defaultRenderer;
        private SimpleSeriesRenderer simpleSeriesRenderer;// 单块饼图渲染
        private Context context;
    
        public PieChartHelper(Context context) {
            this.context = context;
        }
    
        public GraphicalView getGraphicalView() {
            graphicalView = ChartFactory.getPieChartView(context, categorySeries
                    , defaultRenderer);
            return graphicalView;
        }
    
        public void setCategorySeries(String title) {
    
            categorySeries = new CategorySeries(title);
    
        }
    
        public void setDefaultRenderer(String title) {
    
            defaultRenderer = new DefaultRenderer();
    
            defaultRenderer.setChartTitle(title);
    
            defaultRenderer.setDisplayValues(true);
    
            defaultRenderer.setFitLegend(true);
    
        }
    
        public void updateChart(double[] values, double sumval) {
    
            for (int i = 0; i < values.length; i++) {
    
                simpleSeriesRenderer = new SimpleSeriesRenderer();
    
                categorySeries.add("数据" + (i + 1), values[i] / sumval);
    
                simpleSeriesRenderer.setColor(getRandomColor());
    
                simpleSeriesRenderer.setChartValuesFormat(NumberFormat.getPercentInstance());
    
                defaultRenderer.addSeriesRenderer(simpleSeriesRenderer);
    
            }
            graphicalView.repaint();
    
        }
    
        //随机颜色
        public int getRandomColor() {
            Random random = new Random();
            int R = random.nextInt(255);
            int G = random.nextInt(255);
            int B = random.nextInt(255);
    
            return Color.rgb(R, G, B);
        }
    
    }
    

    调用

    public void setPieLayout(String chartTitle) {
    
            pieChartHelper = new PieChartHelper(MainActivity.this);
    
            pieChartHelper.setCategorySeries(chartTitle);
    
            pieChartHelper.setDefaultRenderer(chartTitle);
    
            values = new double[10];
    
            graphicalView = pieChartHelper.getGraphicalView();
    
            pieLayout.addView(graphicalView);
    
        }
    

    3、柱状图

    public class BarChartHelper {
    
        private GraphicalView graphicalView;
        private XYMultipleSeriesDataset xyMultipleSeriesDataset;
        private XYMultipleSeriesRenderer xyMultipleSeriesRenderer;
        private XYSeries xySeries;
        private XYSeriesRenderer xySeriesRenderer;
    
        private Context context;
    
        public BarChartHelper(Context context) {
            this.context = context;
        }
    
        public GraphicalView getGraphicalView() {
            graphicalView = ChartFactory.getBarChartView(context, xyMultipleSeriesDataset,
                    xyMultipleSeriesRenderer, BarChart.Type.DEFAULT);
    
            return graphicalView;
        }
    
        public void setXyMultipleSeriesDataset() {
            xyMultipleSeriesDataset = new XYMultipleSeriesDataset();
        }
    
        public void setXyMultipleSeriesRenderer(String title) {
            xyMultipleSeriesRenderer = new XYMultipleSeriesRenderer();
    
            xyMultipleSeriesRenderer.setChartTitle(title);
            xyMultipleSeriesRenderer.setFitLegend(true);
    
        }
    
        public void updateChart(String[] titles, ArrayList<double[]> values) {
    
            for (int i = 0; i < titles.length; i++) {
                xySeries = new XYSeries(titles[i]);
                double[] yLable = values.get(i);
                for (int j = 0; j < yLable.length; j++) {
                    xySeries.add(j, yLable[j]);
                }
                xyMultipleSeriesDataset.addSeries(xySeries);
    
                xySeriesRenderer = new XYSeriesRenderer();
                xySeriesRenderer.setColor(getRandomColor());
                xySeriesRenderer.setDisplayChartValues(true);
                xyMultipleSeriesRenderer.addSeriesRenderer(xySeriesRenderer);
            }
            graphicalView.repaint();
    
        }
    
        public int getRandomColor() {
            Random random = new Random();
            int R = random.nextInt(255);
            int G = random.nextInt(255);
            int B = random.nextInt(255);
            return Color.rgb(R, G, B);
        }
    
    }
    

    调用

    public void setBarLayout(String title) {
    
            barChartHelper = new BarChartHelper(MainActivity.this);
    
            barChartHelper.setXyMultipleSeriesDataset();
    
            barChartHelper.setXyMultipleSeriesRenderer(title);
    
            barValues = new ArrayList<>();
    
            graphicalView = barChartHelper.getGraphicalView();
    
            barLayout.addView(graphicalView);
    
        }
    

    4、Main函数

    public class MainActivity extends AppCompatActivity {
    
        private GraphicalView graphicalView;
        private LinearLayout lineLayout, pieLayout, barLayout;
        private LineChartHelper lineChartHelper;
        private PieChartHelper pieChartHelper;
        private BarChartHelper barChartHelper;
    
        private Map<String, String> map;
        private String data;
        private int x;
        private int j;
        private double[] values;
        private double sumval;
        private boolean flag = true;
        private ArrayList<double[]> barValues;
        private String[] barTitles = {"1"};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            lineLayout = findViewById(R.id.lineChart);
            pieLayout = findViewById(R.id.pieChart);
            barLayout = findViewById(R.id.barChart);
    
            setLineChart("折线图");
            setPieLayout("饼图");
            setBarLayout("柱状图");
    
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    map = new HashMap<>();
                    map.put("object", "light");
                    map.put("action", "get");
                    Log.i("http", "开启线程");
                    while (true) {
                        try {
                            String httpData = HttpUtil.getJson(MainActivity.this, map);
                            Log.i("http", httpData);
                            JSONObject jsonObject = new JSONObject(httpData);
                            data = jsonObject.getString("pm2.5");
                            Message message = new Message();
                            message.what = 1;
                            handler.sendMessage(message);
                            Thread.sleep(1 * 1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
    
    
        }
    

    5、更新数据

    private Handler handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 1) {
                    Log.i("http", "handler");
                    lineChartHelper.updateChart(x, Double.valueOf(data));//折线图
                    if (j < 10) {
                        values[j] = Double.valueOf(data);
                        sumval = sumval + values[j];
                        j++;
                    }
                    if (j == 10 && flag == true) {
                        pieChartHelper.updateChart(values, sumval);//饼图(前十个数据)
                        flag = false;
                        barValues.add(values);
                        barChartHelper.updateChart(barTitles, barValues);//柱状图(前十个数据)
                    }
                }
                x++;
    
            }
        };
    

    相关文章

      网友评论

          本文标题:Android绘图之AChartEngine的简单应用

          本文链接:https://www.haomeiwen.com/subject/igdhmctx.html