我有这样的哈希映射
LinkedHashMaphash = 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);
我想通过MPAndroidChart设计一个LineChart,但它不工作,图表无效,没有任何显示!似乎我的代码无效.
这是我的代码:
private void setChartData(LinkedHashMaplastMonthMediaLikes) { ArrayList xVals = new ArrayList<>(); for(Map.Entry entry : lastMonthMediaLikes.entrySet()) { xVals.add(entry.getKey()); } ArrayList vals1 = new ArrayList<>(); int i = 0; for(Map.Entry 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); }
请指教,我该怎么办?
你是通过设置数据setData(...)
,但你没有刷新的View
调用invalidate()
.
这应该做的工作:
LineChart chart = ...; chart.setData(...); // set data chart.invalidate(); // refresh after setting data