PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.56k stars 9.01k forks source link

Chart doesn't take up full width after redraw #2646

Open mshcheng opened 7 years ago

mshcheng commented 7 years ago

Before i ask my question, let me first say that this is the most powerful and feature-rich charting library I have used on the Android platform. The documentation is great, and i can find lots of resources on stackoverflow as well.

But i recently come across one issue that i don't know it is a bug or i just haven't done the right thing. I have setup this chart object to occupy full width of the layout/screen. On the initial draw with dataset A, the chart appears fine, taking up the full width. Note that the left Y-axis labels are fairly long in this case.

before

When i redraw the chart with different dataset B, with shorter Y-axis labels, the chart doesn't seem to take the full width of the screen (ie. leaving a wide blank space on the left side).

after

But if at the very start, i select dataset B (without selecting dataset A first), the chart is drawn properly without leaving a wide blank space on the left.

proper

Have i missed something here ? any help is appreciated.

patrick-elmquist commented 7 years ago

Hi, are you calling chart.invalidate() after changing the data set?

mshcheng commented 7 years ago

Yup i did. Below is the code that i use to draw the chart with. I basically only change the currentEntries arraylist when i switch between dataset A and B.

currentSet = new LineDataSet(currentEntries, "current_carbon");
currentSet.setColor(ContextCompat.getColor(chart.getActivity(), R.color.current_carbon_line));
currentSet.setLineWidth(1f);
currentSet.setCircleColor(ContextCompat.getColor(chart.getActivity(), R.color.current_carbon_line));
currentSet.setCircleRadius(3f);
currentSet.setDrawCircleHole(true);
currentSet.setDrawValues(false);
dataSets.add(currentSet);
LineData lData = new LineData(dataSets);

XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new MealAxisValueFormatter(chart.getActivity(), chart));
xAxis.setGranularity(1f);
xAxis.setLabelRotationAngle(90);
xAxis.setAxisMinValue(0f);
xAxis.setAxisMaxValue((float) (numOfDays*numOfMeals)-1);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setLabelCount(numOfDays*numOfMeals);
chart.getAxisLeft().setAxisMinValue(0f);
chart.getAxisRight().setEnabled(false);
chart.setDrawBorders(true);

chart.clearValues();
chart.setData(lData);
chart.animateX(1000);
chart.invalidate();
chart.fitScreen();
patrick-elmquist commented 7 years ago

Does it work if you add chart.notifyDataSetChanged()?

mshcheng commented 7 years ago

Tried that just now, still the same.

tamslinn commented 7 years ago

Did work out an answer to this? I am having the same problem, when I redraw with shorter series, so the x axis is reduced in length. Redrawing longer is fine.

tamslinn commented 7 years ago

I got mine to work by recreating the LineData object rather than just amending the values in the LineDataSet

jeremy96yg commented 5 years ago

Hi, I just got that problem, just call calcMinMax() on your dataset to compute again the min and max of axis like that : chart.lineData.dataSets.first().calcMinMax() And then invalidate() to redraw with new axis' min max

bachpx2 commented 4 years ago

@tamslinn thanks for the suggestion, I got mine to work by doing that as well

ak4550 commented 3 years ago

anyone found the solution?