I am successfully displaying pie chart using achart engine.I want to customize my labels text size according to the screen size.Thanks in advance.
This problem comes down to resolution. achartengine seems to have been designed with raw pixels in mind while display quality and pixel density has increased dramatically over the last couple years. The labels from the achartengine sample are tiny on my HTC One!
Android has introduced scale-independent pixels. The key to using them with achartengine is the TypedValue
class. Here's what I did:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float val = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, metrics);
I got 18 from "text size medium" in the link above.
You can then use val
anywhere you want to use medium sized text. For example:
renderer.setLabelsTextSize(val);
This a summarised version of what I did:
public static final int TEXT_SIZE_XHDPI = 24;
public static final int TEXT_SIZE_HDPI = 20;
public static final int TEXT_SIZE_MDPI = 18;
public static final int TEXT_SIZE_LDPI = 13;
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
switch (getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_XHIGH:
renderer.setMargins(new int[] { 40, 90, 25, 10 });
renderer.setAxisTitleTextSize(Constants.TEXT_SIZE_XHDPI);
renderer.setChartTitleTextSize(Constants.TEXT_SIZE_XHDPI);
renderer.setLabelsTextSize(Constants.TEXT_SIZE_XHDPI);
renderer.setLegendTextSize(Constants.TEXT_SIZE_XHDPI);
break;
case DisplayMetrics.DENSITY_HIGH:
renderer.setMargins(new int[] { 30, 50, 20, 10 });
renderer.setAxisTitleTextSize(Constants.TEXT_SIZE_HDPI);
renderer.setChartTitleTextSize(Constants.TEXT_SIZE_HDPI);
renderer.setLabelsTextSize(Constants.TEXT_SIZE_HDPI);
renderer.setLegendTextSize(Constants.TEXT_SIZE_HDPI);
break;
default:
renderer.setMargins(new int[] { 30, 50, 20, 10 });
renderer.setAxisTitleTextSize(Constants.TEXT_SIZE_LDPI);
renderer.setChartTitleTextSize(Constants.TEXT_SIZE_LDPI);
renderer.setLabelsTextSize(Constants.TEXT_SIZE_LDPI);
renderer.setLegendTextSize(Constants.TEXT_SIZE_LDPI);
break;
}
So I basically customised the margins and text sizes of the chart depending of the device's screen density. Note: I excluded the medium density case for now.
I wrote two methods called _dp and _sp to convert to dp and sp values.
private DisplayMetrics displayMetrics;
private boolean isPortrait;
int _dp(float pixels) {
return (int)(pixels * displayMetrics.density);
}
float _sp(float pixels) {
return (pixels * displayMetrics.scaledDensity);
}
In your onCreate you need to set the displayMetrics value:
displayMetrics = (this.getResources()).getDisplayMetrics();
Then you just use _sp and _dp to get the sp/dp value. For instance to set a font scaled to 18
renderer.setAxisTitleTextSize(_sp(18));
And to set margins with dp values:
renderer.setMargins(new int[] {_dp(25), _dp(30), _dp(35), _dp(20)});
NOTE: I made one change to this code. It is possible to get a 0 value for these functions -- which is a real problem if you are dividing by the return value. I added a test to return 1 if the return value is < 1. ie: return ((rv < 1) ? 1 : v);
You can set the labels text size this way:
renderer.setLabelsTextSize(size);
You will have to find the best logic for finding the best proportion between the screen size and the labels size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With