How to continuously update Y-axis values in MPAndroidChart

帅比萌擦擦* 提交于 2021-01-22 08:36:53

问题


I want my axis in LineChart to adjust it's maximum and minimum values real-time. Functions like resetAxisMaxValue() and resetAxisMinValue() work well when new data's Y-value is increasing(both positive and negative), however, once the signal gets low again Y-value max and min never drops to the smaller values so it can show all new data on the full canvas of the graph. I wanted to check whether there is method to do so automatically, or whether I should use brute force. Thank you.


回答1:


Alex's answer is a bit old, so...

MPAndroidChart has a method called setAutoScaleMinMaxEnabled(boolean enabled). It enables your chart to automatically adjusts the chart's scale. You could use it simply like this.

chart.setAutoScaleMinMaxEnabled(true);



回答2:


I post the solution I have implemented. It works well real time.

Add the following lines to the function where you append data to the graph.

float maxVisiblePoint = -9999999;
float minVisiblePoint = 9999999;

//removing last element from the chart and finding max and min visible value
if(dataSet.getEntryCount() == 650) {
     mChartData.removeXValue(0);
     dataSet.removeEntry(0);

     for (Entry entry : dataSet.getYVals()) {
         if (entry.getVal() > maxVisiblePoint) maxVisiblePoint = entry.getVal();
         if (entry.getVal() < minVisiblePoint) minVisiblePoint = entry.getVal();
         entry.setXIndex(entry.getXIndex() - 1);
     }

//autogain is checked
if (autoScaleChecked) {
    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setAxisMaxValue(maxVisiblePoint);
    leftAxis.setAxisMinValue(minVisiblePoint);
}
//static gain
else {
      YAxis leftAxis = mChart.getAxisLeft();
      leftAxis.setAxisMaxValue(5882400f);
      leftAxis.setAxisMinValue(-5882400f);
}
// let the chart know it's data has changed
mChart.notifyDataSetChanged();

Feel free to ask me in case you face some problems. Enjoy!



来源:https://stackoverflow.com/questions/31934301/how-to-continuously-update-y-axis-values-in-mpandroidchart

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