Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mpandroidchart - How can I avoid the repeated values in Y-Axis?

I want to avoid the repeated values and the -0 values in Y-Axis, avoiding the image situation.

I have these ideas to solve this, but any solution:

  1. Limit the zoom before having repeated values in YAxis, therefore stop the infinite zoom-in on the chart
  2. Get the YAxis values and remove the duplicate values.

YAxis with duplicate values

like image 519
rafaelasguerra Avatar asked Sep 14 '15 16:09

rafaelasguerra


3 Answers

Though this is an older question I'd like to add to it for future reference. Newer versions of the library have a little known feature that resolves the duplicated labels, called granularity. This is way simpler to use than the older solutions (though to be fair, this wasn't available at the time those were posted).

You can always check the latest AxisBase Javadocs (3.0.0-beta1) for a more detailed explanation. Here are the relevant methods:

  • setGranularity(float granularity): Set a minimum interval for the axis when zooming in. The axis is not allowed to go below that limit. This can be used to avoid label duplicating when zooming in.
  • setGranularityEnabled(boolean enabled): Enabled/disable granularity control on axis value intervals. If enabled, the axis interval is not allowed to go below a certain granularity.

So in your case you'd need to set the granularity to 0.1f since you have one decimal point. The following snippet of code should avoid the repeated values on the axis:

YAxis yAxis = mChart.getAxisLeft();
yAxis.setGranularityEnabled(true);
yAxis.setGranularity(0.1f);
like image 197
TR4Android Avatar answered Oct 22 '22 22:10

TR4Android


tl;dr You can do this by changing the label count in onChartScale.

First, you want your listener set up:

chart.setOnChartGestureListener(this); // set a listener ;)

You try to get the top/bottom drawn values and check what gets drawn on the screen. Apply some basic calculations, and you're set.

The following code will draw 2 labels if the zoom level gets (too) high and up to 9 otherwise:

@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
    final YAxis yAxis = mChart.getAxisLeft();
    final Transformer transformer = mChart.getTransformer(YAxis.AxisDependency.LEFT);

    // ...minor dirty hack
    final PointD top = transformer.getValuesByTouchPoint(0, 0);
    final PointD bottom = transformer.getValuesByTouchPoint(0, mChart.getHeight());
    final int diff = (int)(top.y - bottom.y);

    // draw 2-9 axis labels
    final int count = Math.min(9, Math.max(diff, 2));

    Log.d("scale", String.format("scale %f: diff %d and count %d", scaleY, diff, count));

    // "force" the count, for there are drawing issues where none get drawn on high zoom levels (just try it out)
    yAxis.setLabelCount(count, true);
}

// todo implement other interface methods

The value formatter and everything else stays the same.

And some ugly screenshot to show that it works :D

Zoomed in chart with 2 labels

like image 3
David Medenjak Avatar answered Oct 22 '22 22:10

David Medenjak


code like this:

    mchart.setAutoScaleMinMaxEnabled(false);
    mchart.getAxisLeft().setAxisMaxValue(10);
    mchart.getAxisLeft().setAxisMinValue(5);
    mchart.getAxisRight().setAxisMaxValue(10);
    mchart.getAxisRight().setAxisMinValue(5);

or:

boolean a = isReady;
 mchart.getAxisLeft().setFormater(new format(float b){ return "" ;})

When u get data :

    mchart.setAutoScaleMinMaxEnabled(true);
    mchart.getAxisLeft().resetAxisMaxValue();
    mchart.getAxisLeft().resetAxisMinValue();
    mchart.getAxisRight().resetAxisMaxValue();
    mchart.getAxisRight().resetAxisMinValue(5);

I have no code by my hand.

like image 2
tiny sunlight Avatar answered Oct 22 '22 22:10

tiny sunlight