简体   繁体   中英

Is there a way I can get the Bar Width in px/dp's of an MP Android BarChart?

I want to create a "table" (which is build with a recycle view) below my chart. But I want the RecycleView to have the same number of elements of my chart (In this case 7) Is there a way to get the column width of the Bar Chart so I can assign the same width to my Recycle View Cells?

在此处输入图片说明

At the end (if it is posible) I want the two scroll to be synced.

The bar width in pixels is the difference between the value bar width x position (wp) and the label starting position (xp):

        Transformer transformer = bChart.getTransformer(YAxis.AxisDependency.LEFT);
        float bw = (float) transformer.getPixelForValues(bChart.getBarData().getBarWidth(), 0).x;
        float xp = (float) transformer.getPixelForValues(0, 0).x;
        float barWidthInPixels = bw - xp;

As you can see from the javadoc, BarData#getBarWidth() returns a the bar width in chart values (not in pixels).

You can translate between chart values (x and y values on the chart) and pixel values (for the Android handset's screen) using a Transformer . Something like the following:

float barWidth = barData.getBarWidth();
Transformer transformer = mChart.getViewPortHandler().getTransformer(AxisDependency.LEFT);
MPPointD barDimensPoint = transformer.getPixelForValues(barWidth, 0);
float barWidthInPixels = (float) barDimensPoint.x;

It's essentially the opposite operation from this question where the requirement is to translate pixels into values on the chart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM