8

I'm using MPAndroidChart to display my data in a line graph. For each date I have its own value.

This works just fine. What I want to do now is to not draw the 0 values, but instead draw the line between 2 adjacent non-zero values (like a trend-line), while keep showing the dates on the x-axis for the zero values.

My current graph: My current graph

The desired graph should look similar to this graph: Desired graph - no zero values

How can I achieve this behavior?

limlim
  • 3,115
  • 2
  • 34
  • 46

3 Answers3

7

I'm posting my friend's solution here (worked like a charm):

  1. Create a dataset with 0 values. Draw it but with line of transparent color.
  2. Create a dataset without 0. Draw it with the color that you need.

Put (1) and (2) on the same LineChart.

It will give you an x axis with x values where there are 0 values but will not draw a line for them.

The second dataset will show the line of data points without the 0 values.

limlim
  • 3,115
  • 2
  • 34
  • 46
  • 1
    Great! Can you please post it as an answer to [this question](http://stackoverflow.com/questions/25328151/mpandroidchart-with-null-values) as well? – David Rawson May 08 '17 at 03:57
3

Without drawing two lines like limlim suggested, you have to add only non zero values to your entries, but the x value has to be incremented anyway:

List<SomeClass> values = new ArrayList<>();

int k = 0;
List<Entry> entries = new ArrayList<>();
for (SomeClass v : values){
  if (v.value > 0){
    entries.add(new Entry(k, v.value));
  }
  k += 1;
}
RiccardoCh
  • 1,060
  • 1
  • 13
  • 24
1

There is no need to split dataset to draw empty points for MPAndroidChart:v3.1.0. All you have to do is set those empty points value as Float.NaN (in case of Kotlin).

Ben Tau
  • 11
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33908242) – Maveňツ Mar 01 '23 at 11:38