Android图表控件MPAndroidChart——曲线图LineChart(多条曲线)动态添加数据

时间:2025-04-02 17:33:32
public class DynamicLineChartManager { private LineChart lineChart; private YAxis leftAxis; private YAxis rightAxis; private XAxis xAxis; private LineData lineData; private LineDataSet lineDataSet; private List<ILineDataSet> lineDataSets = new ArrayList<>(); private SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式   private List<String> timeList = new ArrayList<>(); //存储x轴的时间 //一条曲线 public DynamicLineChartManager(LineChart mLineChart, String name, int color) { this.lineChart = mLineChart; leftAxis = lineChart.getAxisLeft(); rightAxis = lineChart.getAxisRight(); xAxis = lineChart.getXAxis(); initLineChart(); initLineDataSet(name, color); } //多条曲线 public DynamicLineChartManager(LineChart mLineChart, List<String> names, List<Integer> colors) { this.lineChart = mLineChart; leftAxis = lineChart.getAxisLeft(); rightAxis = lineChart.getAxisRight(); xAxis = lineChart.getXAxis(); initLineChart(); initLineDataSet(names, colors); } /** * 初始化LineChar */ private void initLineChart() { lineChart.setDrawGridBackground(false); //显示边界 lineChart.setDrawBorders(true); //折线图例 标签 设置 Legend legend = lineChart.getLegend(); (.LINE); (11f); //显示位置 (.BOTTOM); (.LEFT); (.HORIZONTAL); (false); //X轴设置显示位置在底部 xAxis.setPosition(.BOTTOM); xAxis.setGranularity(1f); xAxis.setLabelCount(10); xAxis.setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float value, AxisBase axis) { return timeList.get((int) value % timeList.size()); } }); //保证Y轴从0开始,不然会上移一点 leftAxis.setAxisMinimum(0f); rightAxis.setAxisMinimum(0f); } /** * 初始化折线(一条线) * * @param name * @param color */ private void initLineDataSet(String name, int color) { lineDataSet = new LineDataSet(null, name); lineDataSet.setLineWidth(1.5f); lineDataSet.setCircleRadius(1.5f); lineDataSet.setColor(color); lineDataSet.setCircleColor(color); lineDataSet.setHighLightColor(color); //设置曲线填充 lineDataSet.setDrawFilled(true); lineDataSet.setAxisDependency(.LEFT); lineDataSet.setValueTextSize(10f); lineDataSet.setMode(.CUBIC_BEZIER); //添加一个空的 LineData lineData = new LineData(); lineChart.setData(lineData); lineChart.invalidate(); } /** * 初始化折线(多条线) * * @param names * @param colors */ private void initLineDataSet(List<String> names, List<Integer> colors) { for (int i = 0; i < (); i++) { lineDataSet = new LineDataSet(null, (i)); lineDataSet.setColor((i)); lineDataSet.setLineWidth(1.5f); lineDataSet.setCircleRadius(1.5f); lineDataSet.setColor((i)); lineDataSet.setDrawFilled(true); lineDataSet.setCircleColor((i)); lineDataSet.setHighLightColor((i)); lineDataSet.setMode(.CUBIC_BEZIER); lineDataSet.setAxisDependency(.LEFT); lineDataSet.setValueTextSize(10f); lineDataSets.add(lineDataSet); } //添加一个空的 LineData lineData = new LineData(); lineChart.setData(lineData); lineChart.invalidate(); } /** * 动态添加数据(一条折线图) * * @param number */ public void addEntry(int number) { //最开始的时候才添加 lineDataSet(一个lineDataSet 代表一条线) if (lineDataSet.getEntryCount() == 0) { lineData.addDataSet(lineDataSet); } lineChart.setData(lineData); //避免集合数据过多,及时清空(做这样的处理,并不知道有没有用,但还是这样做了) if (timeList.size() > 11) { timeList.clear(); } timeList.add(df.format(System.currentTimeMillis())); Entry entry = new Entry(lineDataSet.getEntryCount(), number); lineData.addEntry(entry, 0); //通知数据已经改变 lineData.notifyDataChanged(); lineChart.notifyDataSetChanged(); //设置在曲线图中显示的最大数量 lineChart.setVisibleXRangeMaximum(10); //移到某个位置 lineChart.moveViewToX(lineData.getEntryCount() - 5); } /** * 动态添加数据(多条折线图) * * @param numbers */ public void addEntry(List<Integer> numbers) { if (lineDataSets.get(0).getEntryCount() == 0) { lineData = new LineData(lineDataSets); lineChart.setData(lineData); } if (timeList.size() > 11) { timeList.clear(); } timeList.add(df.format(System.currentTimeMillis())); for (int i = 0; i < (); i++) { Entry entry = new Entry(lineDataSet.getEntryCount(), (i)); lineData.addEntry(entry, i); lineData.notifyDataChanged(); lineChart.notifyDataSetChanged(); lineChart.setVisibleXRangeMaximum(6); lineChart.moveViewToX(lineData.getEntryCount() - 5); } } /** * 设置Y轴值 * * @param max * @param min * @param labelCount */ public void setYAxis(float max, float min, int labelCount) { if (max < min) { return; } leftAxis.setAxisMaximum(max); leftAxis.setAxisMinimum(min); leftAxis.setLabelCount(labelCount, false); rightAxis.setAxisMaximum(max); rightAxis.setAxisMinimum(min); rightAxis.setLabelCount(labelCount, false); lineChart.invalidate(); } /** * 设置高限制线 * * @param high * @param name */ public void setHightLimitLine(float high, String name, int color) { if (name == null) { name = "高限制线"; } LimitLine hightLimit = new LimitLine(high, name); (4f); (10f); (color); (color); leftAxis.addLimitLine(hightLimit); lineChart.invalidate(); } /** * 设置低限制线 * * @param low * @param name */ public void setLowLimitLine(int low, String name) { if (name == null) { name = "低限制线"; } LimitLine hightLimit = new LimitLine(low, name); (4f); (10f); leftAxis.addLimitLine(hightLimit); lineChart.invalidate(); } /** * 设置描述信息 * * @param str */ public void setDescription(String str) { Description description = new Description(); (str); lineChart.setDescription(description); lineChart.invalidate(); } }