How to make a multi-line chart in real time?

I want to make multiple lines in real time using MPAndroidChart .

There is no need to schedule real-time using only one data. (following code)

private void addEntry(int count) { LineData data = mChart.getData(); if (data != null) { LineDataSet set = data.getDataSetByIndex(0); if (set == null) { set = createSet(); data.addDataSet(set); } data.addXValue(""); data.addEntry(new Entry(getPressure(), set.getEntryCount()), 0); data.setDrawValues(false); data.setHighlightEnabled(false); // let the chart know it data has changed mChart.notifyDataSetChanged(); // limit the number of visible entries mChart.setVisibleXRange(0, count); // move to the latest entry mChart.moveViewToX(data.getXValCount() - (count + 1)); } } 

And it is not a problem to make a few lines using the following code.

 private void setData(int count, float range) { ArrayList<String> xValues = new ArrayList<String>(); for (int i = 0 ; i < count ; i++) { xValues.add((1 + i) + ""); } ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>(); for (int k = 0 ; k < 3 ; k++) { ArrayList<Entry> yValues = new ArrayList<Entry>(); for (int i = 0 ; i < count ; i++) { if (k == 0) { yValues.add(new Entry(getSetPressure(), i)); } else if (k == 1) { yValues.add(new Entry(getCurrentPressure(), i)); } else { yValues.add(new Entry(getSuctionPressure(), i)); } } String s; String c; if (k == 0) { s = "Set Pressure"; c = "#ed1f24"; } else if (k == 1) { s = "Current Pressure"; c = "#004bf6"; } else { s = "Suction Pressure"; c = "#ffba00"; } LineDataSet set = new LineDataSet(yValues, s); set.setAxisDependency(YAxis.AxisDependency.LEFT); set.setDrawCubic(false); set.setDrawCircles(false); set.setCircleColor(Color.parseColor(c)); set.setCircleSize(8f); set.setCircleColorHole(Color.BLACK); set.setDrawCircleHole(false); set.setLineWidth(3f); set.setColor(Color.parseColor(c)); set.setDrawHorizontalHighlightIndicator(false); set.setDrawVerticalHighlightIndicator(false); dataSets.add(set); } LineData data = new LineData(xValues, dataSets); data.setDrawValues(false); data.setHighlightEnabled(false); mChart.setData(data); } 

However, I do not know how to make several lines in a line graph in real time.

How to make multiple lines in real time?

+5
source share
3 answers

In fact, you sent the answer to your question in your question.

This line is the key:

 data.addEntry(... , 0); 

0 at the end indicates the index of the dataset into which the record is to be inserted.

So, you just need to create as many empty DataSets as the rows you want to have, and then add your Entries to any DataSet you want using the above method with the correct index.

Here you can find the official wiki entry for dynamic and real data.

+4
source
 private void addEntry(int min, int max) { data = mChart.getData(); if (data != null) { LineDataSet dataSetGraphA = data.getDataSetByIndex(0); LineDataSet dataSetGraphB = data.getDataSetByIndex(1); LineDataSet dataSetGraphC = data.getDataSetByIndex(2); LineDataSet dataSetGraphD = data.getDataSetByIndex(3); // LineDataSet set1= data.getDataSetByIndex(1); // set.addEntry(...); // can be called as well if (dataSetGraphA == null) { dataSetGraphA = setLineDataSet(0); data.addDataSet(dataSetGraphA); } if (dataSetGraphB == null) { dataSetGraphB = setLineDataSet(1); data.addDataSet(dataSetGraphB); } if (dataSetGraphC == null) { dataSetGraphC = setLineDataSet(2); data.addDataSet(dataSetGraphC); } if (dataSetGraphD == null) { dataSetGraphD = setLineDataSet(3); data.addDataSet(dataSetGraphD); } Calendar c = Calendar.getInstance(); // yyyy-MM-dd.HH.mm.ss.SS SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.SS"); String formattedDate = df.format(c.getTime()); // Now formattedDate have current date/time System.out.println("Current time = " + formattedDate); Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show(); // add a new x-value first data.addXValue(formattedDate); data.addEntry( new Entry(randomInRange(min, max), dataSetGraphA .getEntryCount()), 0); data.addEntry( new Entry(randomInRange(min, max), dataSetGraphB .getEntryCount()), 1); data.addEntry( new Entry(randomInRange(min, max), dataSetGraphC .getEntryCount()), 2); data.addEntry( new Entry(randomInRange(min, max), dataSetGraphD .getEntryCount()), 3); // let the chart know it data has changed mChart.notifyDataSetChanged(); mChart.setVisibleXRangeMaximum(6); mChart.setVisibleYRangeMaximum(15, AxisDependency.LEFT); // // // this automatically refreshes the chart (calls invalidate()) mChart.moveViewTo(data.getXValCount() - 7, 50f, AxisDependency.LEFT); } } 
+2
source

Here is a simple example:

 private void addEntry() { LineData data = mChart.getData(); LineDataSet set0 = (LineDataSet) data.getDataSetByIndex(0); LineDataSet set1 = (LineDataSet) data.getDataSetByIndex(1); if (set0 == null || set1 == null) { // creation of null set0 = createSet(); set1 = createSet(); data.addDataSet(set0); data.addDataSet(set1); } data.addXValue(""); data.addEntry(new Entry((float) (Math.random() * 20) + 2f, set0.getEntryCount()), 0); data.addEntry(new Entry((float) (Math.random() * 20) + 2f, set1.getEntryCount()), 1); mChart.notifyDataSetChanged(); mChart.setVisibleXRangeMaximum(6); mChart.moveViewToX(data.getXValCount() - 7); } 
0
source

Source: https://habr.com/ru/post/1231979/


All Articles