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

hcp picture hcp · Dec 28, 2012 · Viewed 11.5k times · Source

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.

Answer

Andy Gaskell picture Andy Gaskell · Aug 8, 2013

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