Mpandroidchart: Initial LineDataSet Value text size doesn't appear as set in .setValueTextSize()

Created on 20 May 2015  Â·  18Comments  Â·  Source: PhilJay/MPAndroidChart

This is a great library. Thank you PhilJay!

I have set the setTextValueSize to 12f, and setLineWidth to 3f, yet on the first display of the line graph, the value text is very small and the lines are thinner than what I set them to, But on subsequent displays of the graph, the text size and line size appear as intended. I am having trouble trying to fix this. Any help is very much appreciated.
FIRST DRAW
image

SUBSEQUENT DISPLAYS
image

All 18 comments

Are you calling invalidate() after making changes?

Yes sir.

Post the code you use to setup the chart and change the text size

I appreciate your help....

 private final float VALUE_TEXT_SIZE = 12f;

....
        // DIASTOLIC CHART
        LineDataSet diastolicDataset = new LineDataSet(generateData("DIASTOLIC"), "Diastolic");
        diastolicDataset.setColor(Color.BLACK);
        diastolicDataset.setLineWidth(3f);
//        diastolicDataset.setFillColor(Color.WHITE);
        diastolicDataset.setDrawFilled(false);
        diastolicDataset.setDrawCubic(true);
        diastolicDataset.setCircleSize(4f);
        diastolicDataset.setCircleColor(Color.BLUE);
        diastolicDataset.setCircleColorHole(Color.WHITE);
        diastolicDataset.setValueTypeface(Typeface.DEFAULT);
        diastolicDataset.setValueTextSize(VALUE_TEXT_SIZE);

Okay, and where is you call to setData(...) and then your call of invalidate()?

Here is all three graph lines...

...
        // SYSTOLIC CHART
        LineDataSet systolicDataset = new LineDataSet(generateData("SYSTOLIC"), "Systolic");
        systolicDataset.setColor(Color.BLUE);
        systolicDataset.setLineWidth(3f);
        systolicDataset.setDrawFilled(false);
        systolicDataset.setDrawCubic(true);
        systolicDataset.setCircleSize(4f);
        systolicDataset.setCircleColor(Color.BLACK);
        systolicDataset.setCircleColorHole(Color.WHITE);
        systolicDataset.setValueTypeface(Typeface.DEFAULT);
        systolicDataset.setValueTextSize(VALUE_TEXT_SIZE);

        // DIASTOLIC CHART
        LineDataSet diastolicDataset = new LineDataSet(generateData("DIASTOLIC"), "Diastolic");
        diastolicDataset.setColor(Color.BLACK);
        diastolicDataset.setLineWidth(3f);
        diastolicDataset.setDrawFilled(false);
        diastolicDataset.setDrawCubic(true);
        diastolicDataset.setCircleSize(4f);
        diastolicDataset.setCircleColor(Color.BLUE);
        diastolicDataset.setCircleColorHole(Color.WHITE);
        diastolicDataset.setValueTypeface(Typeface.DEFAULT);
        diastolicDataset.setValueTextSize(VALUE_TEXT_SIZE);

        //PULSE CHART
        LineDataSet pulseDataset = new LineDataSet(generateData("PULSE"), "Heart Rate");
        pulseDataset.setColor(Color.RED);
        pulseDataset.setLineWidth(1f);
        pulseDataset.setDrawCubic(true);
        pulseDataset.setCircleSize(3f);
        pulseDataset.setCircleColor(Color.RED);
        pulseDataset.setCircleColorHole(Color.WHITE);
        pulseDataset.setValueTypeface(Typeface.DEFAULT);
        pulseDataset.setValueTextSize(10f);
        pulseDataset.setAxisDependency(YAxis.AxisDependency.RIGHT);

        ArrayList<LineDataSet> dataSets = new ArrayList<>();

        dataSets.add(systolicDataset);
        dataSets.add(diastolicDataset);
        dataSets.add(pulseDataset);

...


        LineData systolicData = new LineData(generateLabels(), systolicDataset);
        systolicData.setValueFormatter(new MyFormatter());
        chart.setData(systolicData);

        LineData diastolicData = new LineData(generateLabels(), diastolicDataset);
        diastolicData.setValueFormatter(new MyFormatter());

        LineData pulseData = new LineData(generateLabels(),  pulseDataset);
        pulseData.setValueFormatter(new MyFormatter());

        LineData bpdata = new LineData(generateLabels(), dataSets);
        chart.setData(bpdata);
        setContentView(chart);
        chart.invalidate();

Why are you creating 4 different LineData objects?
Also, your pulse data text size is set to 10f instead of 12f

Great question! I removed the first three LineData objects, Still have the same issue.

I have this same issue with another app I am making. So i am sure it's something I am not doing properly. I can rotate the screen or back out of the activity and return, and it will correct itself.

Also, your pulse data text size is set to 10f instead of 12f

Correct. Intended.

Try calling setContentView(...) somewhere earlier.

I tried it in three places to no avail from right after the chart instantiation and two more other places.

I am seeing this in the logcat....

05-19 15:10:43.827    4645-4645/com.stoicdev.simplebptrackerpro E/MPChartLib-Utils﹕ Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before calling Utils.convertDpToPixel(...). Otherwise conversion does not take place.

Ok now I know what the issue is.
You either need to call Utils.init(...) sometime before setting up your LineData or move the creation of the chart before it.

I moved the chart creation and she is working great now.
Thanks Phillip!

I was having the exact same problem too and struggled with it for a while. My line width, circle radius, hole radius etc won't work the first time the activity was loaded but if I went back to the Main Activity and went to the Chart Activity again, they would start to work. This was because I was setting up the LineData before creating the chart. Solution was to call Utils.init(...) before setting up the LineData.

I was having the exact same problem too and struggled with it for a while. My line width, circle radius, hole radius etc won't work the first time the activity was loaded but if I went back to the Main Activity and went to the Chart Activity again, they would start to work. This was because I was setting up the LineData before creating the chart. Solution was to call Utils.init(...) before setting up the LineData.

Hello. I am having a similar problem -- could you tell me how to call Utils.init() ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manucheri picture manucheri  Â·  3Comments

blotfi picture blotfi  Â·  3Comments

botondbutuza picture botondbutuza  Â·  3Comments

galex picture galex  Â·  3Comments

mrZizik picture mrZizik  Â·  3Comments