Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Label Text size according to the screen size in a chart engine pie chart in android

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.

like image 434
hcp Avatar asked Dec 28 '12 09:12

hcp


4 Answers

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);
like image 123
Andy Gaskell Avatar answered Nov 14 '22 14:11

Andy Gaskell


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.

like image 22
Ryan Avatar answered Nov 14 '22 15:11

Ryan


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);

like image 3
steven smith Avatar answered Nov 14 '22 15:11

steven smith


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.

like image 2
Dan D. Avatar answered Nov 14 '22 14:11

Dan D.