MPAndroidChart is not updating

Ali Hasanzade picture Ali Hasanzade · Dec 14, 2015 · Viewed 8.8k times · Source

I have a hashmap like this

LinkedHashMap<String, String> hash = new LinkedHashMap<>();
hash.put("2015-05-03", 100);
hash.put("2015-05-04", 110);
hash.put("2015-05-05", 90);
hash.put("2015-05-06", 40);

I want to design a LineChart via MPAndroidChart but it is not working, diagram is not valid, nothing to show!it seems my codes are invalid.
here are my codes :

private void setChartData(LinkedHashMap<String, String> lastMonthMediaLikes)
    {

        ArrayList<String> xVals = new ArrayList<>();

        for(Map.Entry<String, String> entry : lastMonthMediaLikes.entrySet())
        {
            xVals.add(entry.getKey());
        }



        ArrayList<Entry> vals1 = new ArrayList<>();
        int i = 0;
        for(Map.Entry<String, String> entry : lastMonthMediaLikes.entrySet())
        {
            vals1.add(new Entry(Float.valueOf(entry.getValue()), i));
            i++;
        }

        // create a dataset and give it a type
        LineDataSet set1 = new LineDataSet(vals1, "DataSet 1");
        set1.setDrawCubic(true);
        set1.setCubicIntensity(0.2f);
        //set1.setDrawFilled(true);
        set1.setDrawCircles(false);
        set1.setLineWidth(1.8f);
        set1.setCircleSize(4f);
        set1.setCircleColor(Color.WHITE);
        set1.setHighLightColor(Color.rgb(244, 117, 117));
        set1.setColor(Color.WHITE);
        set1.setFillColor(Color.WHITE);
        set1.setFillAlpha(100);
        set1.setDrawHorizontalHighlightIndicator(false);
        set1.setFillFormatter(new FillFormatter()
        {
            @Override
            public float getFillLinePosition(LineDataSet dataSet, LineDataProvider dataProvider)
            {
                return -10;
            }
        });

        // create a data object with the datasets
        LineData data = new LineData(xVals, set1);
        //data.setValueTypeface(tf);
        data.setValueTextSize(9f);
        data.setDrawValues(false);

        // set data
        chart.setData(data);
    }

Please advice, what should i do?

Answer

Philipp Jahoda picture Philipp Jahoda · Dec 14, 2015

You are setting the data via setData(...) but you are not refreshing the View by calling invalidate().

This should do the job:

LineChart chart = ...;

chart.setData(...); // sets data & also calls notifyDataSetChanged()
chart.invalidate(); // refreshes chart