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

前端 未结 6 553
情话喂你
情话喂你 2020-12-10 01:31

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:

6条回答
  •  渐次进展
    2020-12-10 01:46

    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

提交回复
热议问题