How to not draw zero values on a linechart while showing their X axis values on MPAndroidChart?

纵然是瞬间 提交于 2019-12-04 14:13:08

问题


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:

The desired graph should look similar to this graph:

How can I achieve this behavior?


回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/40448081/how-to-not-draw-zero-values-on-a-linechart-while-showing-their-x-axis-values-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!