MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

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

Open BruceParmele opened this issue 11 years ago • 18 comments

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

BruceParmele avatar May 20 '15 17:05 BruceParmele

Are you calling invalidate() after making changes?

PhilJay avatar May 20 '15 17:05 PhilJay

Yes sir.

BruceParmele avatar May 20 '15 18:05 BruceParmele

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

PhilJay avatar May 20 '15 18:05 PhilJay

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

BruceParmele avatar May 20 '15 18:05 BruceParmele

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

PhilJay avatar May 20 '15 18:05 PhilJay

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

BruceParmele avatar May 20 '15 19:05 BruceParmele

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

PhilJay avatar May 20 '15 19:05 PhilJay

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

BruceParmele avatar May 20 '15 19:05 BruceParmele

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.

BruceParmele avatar May 20 '15 19:05 BruceParmele

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

PhilJay avatar May 20 '15 19:05 PhilJay

Correct. Intended.

BruceParmele avatar May 20 '15 19:05 BruceParmele

Try calling setContentView(...) somewhere earlier.

PhilJay avatar May 20 '15 19:05 PhilJay

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

BruceParmele avatar May 20 '15 19:05 BruceParmele

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.

BruceParmele avatar May 20 '15 19:05 BruceParmele

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.

PhilJay avatar May 20 '15 20:05 PhilJay

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

BruceParmele avatar May 20 '15 20:05 BruceParmele

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.

Pranoy1c avatar Jun 14 '18 23:06 Pranoy1c

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() ?

MasonDarcy avatar Jun 08 '20 15:06 MasonDarcy