Android aChartEngine XY Chart

I am using aChartEngine 0.7.0 graphics library for Android. I have an XY work schedule that dynamically changes every second when new data is received. However, he draws a line chart from left to right and eventually gets out of sight. The only way to view new data is to scroll to the right.

Does anyone know how to draw a line, draw new data on the left side, so that the old data eventually scrolls right out of sight. Basically, change the direction of the line chart?

+4
source share
3 answers

I really decided to go with the natural flow direction of aChartEngine dynamic line diagrams. I managed to block the graphic image so that the line no longer scrolls from the screen out of sight. I also delete elements from the beginning of the series in order to keep a certain time period visible without a graph shrinking to a mass of lines. It really works very well.

Here you see a video showing the graphics in action: http://www.youtube.com/watch?v=mZMrOc5QaG0

+1
source

My code is:

private XYMultipleSeriesDataset HRDataset = new XYMultipleSeriesDataset(); private XYMultipleSeriesRenderer HeartRateRenderer = new XYMultipleSeriesRenderer(); private XYSeries HRCurrentSeries; private GraphicalView HRChartView; 

in onResume ():

  if (HRChartView == null) { LinearLayout layout = (LinearLayout) findViewById(R.id.HRchart); HRChartView = ChartFactory.getLineChartView(this, HRDataset, HeartRateRenderer); layout.addView(HRChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // boolean enabled = HRDataset.getSeriesCount() > 0; // setSeriesEnabled(enabled); } else { HRChartView.repaint(); } 

in onCreate ():

  HeartRateRenderer.setAxisTitleTextSize(16); HeartRateRenderer.setChartTitleTextSize(20); HeartRateRenderer.setLabelsTextSize(15); HeartRateRenderer.setLegendTextSize(15); HeartRateRenderer.setMargins(new int[] {20, 30, 15, 0}); HeartRateRenderer.setAxesColor(Color.YELLOW); String seriesTitle = "Heart Rate"; XYSeries series = new XYSeries(seriesTitle); HRDataset.addSeries(series); HRCurrentSeries = series; XYSeriesRenderer renderer = new XYSeriesRenderer(); renderer.setColor(Color.RED); HeartRateRenderer.addSeriesRenderer(renderer); 

I have a service that receives data from a Blue Tooth device. When new data appears, I call this function:

 public void setupHRChart(double x, double y) { HRCurrentSeries.add(x, y); if (HRChartView != null) { HRChartView.repaint(); } } 

in manifest:

  <activity android:name="org.achartengine.GraphicalActivity" /> 

in my layout:

  <LinearLayout android:id="@+id/HRchart" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="300px" android:layout_weight="1" /> 

- Good luck that helps.

+3
source

I also remove items from the beginning of the series to maintain a visible amount of time

The problem with this solution is that you are really losing data ...

How about using setXAxisMax and setXAxisMin renderers as follows:

0) set XAxisMax to a value, for example 10, and set the XAxisMin value to 0

1) count the "ticks" in the xTick variable

2) when xTick> XAxisMax:

2.1) set XAxisMax to xTick

2.2), set XAxisMin to what was + 1

3) display the graph again

Thus, we actually shift the "visible part" of the graph (playing with Xmin and Xmax), but do not lose any data (that is, we can still scroll to see the previous data points):

 renderer.setXAxisMin(0.0); renderer.setXAxisMax(10); xTick = 0; lastMinX = 0; private void refreshChart(double lastValue) { /* check if we need to shift the x axis */ if (xTick > getRenderer().getXAxisMax()) { getRenderer().setXAxisMax(xTick); getRenderer().setXAxisMin(++lastMinX); } series.add(xTick++, lastValue); mChartView.repaint(); } 
+1
source

All Articles