Reputation: 1
I am a bit puzzled at the moment. I am displaying a LineChart using the MPAndroidChart library and define the chart in the xml file of my fragment.
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/activity_linechart"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
This works all well but somehow my last data point is not displayed when showing this on my fragment view.
[Last data point not displayed]
The input data is:
X | Y |
---|---|
0.004377362 | -0.01 |
0.001105259 | -0.005 |
0.002050623 | -0.005 |
8.42E-04 | 0 |
0.002581482 | 0 |
0.002581482 | 0.005 |
4.62E-04 | 0.005 |
0.002114198 | 0.01 |
I was pretty sure it is me having done something incorrect (still may well be a likely option). However, if I add one more data point to the above array (floats)
X | Y |
---|---|
0.0022 | 0.015 |
then the missing data point and the new one are displayed correctly - but the first data point now disappears from the chart.
[All data displayed correctly]
I define my chart and input data as
private LineChart lineChart;
private List<Entry> entries = new ArrayList<Entry>();
To display my chart layout, I use in onCreateView:
View view = inflater.inflate(R.layout.fragment_chart, container, false);
lineChart = (LineChart) view.findViewById(R.id.activity_linechart);
The chart input entries are added as x,y positions in a loop
for (int i = 0; i < X.length; i++) {
entries.add(new Entry(X[i], Y[i]));
}
I do then add the data to the chart by
Log.d("ChartData", "onCreate: " + entries.toString());
LineDataSet dataSet = new LineDataSet(entries, "Label");
LineData lineData = new LineData(dataSet);
lineChart.setData(lineData);
lineChart.invalidate(); // refresh chart
There is some formatting going on before adding the data to the chart such as setting axis min/max etc but I doubt this is the issue here?!
Would anyone have an idea what is going on? I am at a loss and hope of getting some new ideas I could try to get this working. API of my project is 22.
Edit.......
So, I came to the conclusion that either the LineChart library has a bug or is not intended to be used for data as arranged in my case... I ran quite a few more trials such as this set of points:
x | y |
---|---|
0 | -0.01 |
0.001275207 | -0.01 |
7.41E-04 | -0.005 |
2.01E-04 | -0.005 |
-2.60E-04 | 0 |
-7.44E-04 | 0 |
-7.44E-04 | 0.005 |
0.001199546 | 0.005 |
0.001006149 | 0.01 |
0 | 0.01 |
which gives the error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.composites.clt, PID: 4408
java.lang.NegativeArraySizeException: -10
at com.github.mikephil.charting.utils.Transformer.generateTransformedValuesLine(Transformer.java:178)
at com.github.mikephil.charting.renderer.LineChartRenderer.drawValues(LineChartRenderer.java:549)
at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:278)
at android.view.View.draw(View.java:15231)
at android.view.View.updateDisplayListIfDirty(View.java:14167)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15234)
at android.widget.FrameLayout.draw(FrameLayout.java:598)
at android.view.View.updateDisplayListIfDirty(View.java:14167)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at androidx.fragment.app.FragmentContainerView.drawChild(FragmentContainerView.java:246)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at androidx.fragment.app.FragmentContainerView.dispatchDraw(FragmentContainerView.java:234)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at androidx.constraintlayout.widget.ConstraintLayout.dispatchDraw(ConstraintLayout.java:1975)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15234)
at android.widget.FrameLayout.draw(FrameLayout.java:598)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2650)
at android.view.View.updateDisplayListIfDirty(View.java:14167)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCa
but when switching X and Y as input, the data displays well. Haven't gotten to the root cause yet but believe that somehow the chart bounds are not calculated correctly... or does someone have a better idea? Would someone know a straightforward way of fixing this?
Many thanks already! Cheers, Frank
Upvotes: 0
Views: 1386
Reputation: 49
I think the problem is your visible range not define correctly. Try adding this to your graph setup:
lineChart.setVisibleXRangeMaximum(x);
x
variable being the number of data point will be display. Just put any number you want to display on your graph.
Upvotes: 0