我正在使用adhartengine制作动态线图,这正在依靠静态数据,但不处理动态数据。实际,计划和月份是动态的。实际,计划和月份的值。我正在使用以下代码。
private void drawChart() {
XYSeries actualSeries = new XYSeries("Actual");
// Creating an XYSeries for Expense<br />
XYSeries planSeries = new XYSeries("Plan");
// Adding data to Income and Expense Series
for (int i = 0; i <actualList.size; i++) {
actualSeries.add(i,Double.parseDouble(actualList.get(i)));
planSeries.add(i,Double.parseDouble(planList.get(i)));
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset xyMultipleSeriesDataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
xyMultipleSeriesDataset.addSeries(actualSeries);
// Adding Expense Series to dataset
xyMultipleSeriesDataset.addSeries(planSeries);
XYSeriesRenderer actualRenderer = new XYSeriesRenderer();
actualRenderer.setColor(Color.GREEN);
actualRenderer.setPointStyle(PointStyle.CIRCLE);
actualRenderer.setFillPoints(true);
actualRenderer.setLineWidth(2);
actualRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer planRenderer = new XYSeriesRenderer();
planRenderer.setColor(Color.RED);
planRenderer.setPointStyle(PointStyle.CIRCLE);
planRenderer.setFillPoints(true);
planRenderer.setLineWidth(2);
planRenderer.setDisplayChartValues(true);
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setBackgroundColor(Color.WHITE);
multiRenderer.setMarginsColor(Color.WHITE);
multiRenderer.setLabelsColor(Color.RED);
multiRenderer.setPanEnabled(false, false);
/* multiRenderer.setYLabels(5);
multiRenderer.setXLabels(1);
*/ multiRenderer.setXLabelsColor(Color.BLACK);
multiRenderer.setYLabelsColor(0, Color.BLACK);
//* multiRenderer.setShowGridX(true);
//multiRenderer.setShowGridY(true)
multiRenderer.setGridColor(Color.BLACK);
multiRenderer.setChartTitle("Plan vs Actual Curve");
multiRenderer.setXTitle("Months");
multiRenderer.setYTitle("plan/actual");
multiRenderer.setZoomButtonsVisible(false);
for (int i = 0; i < monthList.size(); i++) {
multiRenderer.addXTextLabel(i + 1, monthList.get(i).toString());
}
multiRenderer.addSeriesRenderer(actualRenderer);
multiRenderer.addSeriesRenderer(planRenderer);
// Getting a reference to LinearLayout of the MainActivity Layout<br />
LinearLayout chartContainer = (LinearLayout) mView.findViewById(R.id.chart_container);
// Creating a Line Chart<br />
GraphicalView chart = ChartFactory.getLineChartView(getActivity(), xyMultipleSeriesDataset, multiRenderer);
chartContainer.addView(chart);
if (chart != null) {
chartContainer.removeView(chart);
chart.repaint();
}
}
Thanks in advance
我在进行一些研究后自己解决了我的问题。问题是UI更新问题。一旦图表就绘制了,然后为新数据绘制,我们必须删除该视图,并应创建新的图表视图基于data.below是工作代码。
private void drawChart() {
// Creating an XYSeries for Income
XYSeries actualSeries = new XYSeries("Actual");
// Creating an XYSeries for Expense<br />
XYSeries planSeries = new XYSeries("Plan");
XYSeries cupOneSeries = new XYSeries("Cup1");
XYSeries cupTwoSeries = new XYSeries("Cup2");
XYSeries cupThreeSeries = new XYSeries("Cup3");
// Adding data to Income and Expense Series
Log.e("x axis value in curve", "" + xAxisValueList.size());
Log.e("size of Actual", actualList.size() + "");
Log.e("size of plan", planList.size() + "");
for (int i = 0; i < actualList.size(); i++) {
actualSeries.add(i, Math.round(Double.parseDouble(actualList.get(i))));
planSeries.add(i, Math.round(Double.parseDouble(planList.get(i))));
cupOneSeries.add(i, Math.round(Double.parseDouble(cupOneList.get(i))));
cupTwoSeries.add(i, Math.round(Double.parseDouble(cupTwoList.get(i))));
cupThreeSeries.add(i, Math.round(Double.parseDouble(cupThreeList.get(i))));
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset xyMultipleSeriesDataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
xyMultipleSeriesDataset.addSeries(actualSeries);
// Adding Expense Series to dataset
xyMultipleSeriesDataset.addSeries(planSeries);
xyMultipleSeriesDataset.addSeries(cupOneSeries);
xyMultipleSeriesDataset.addSeries(cupTwoSeries);
xyMultipleSeriesDataset.addSeries(cupThreeSeries);
XYSeriesRenderer actualRenderer = new XYSeriesRenderer();
actualRenderer.setColor(getActivity().getResources().getColor(R.color.logo_blue));
actualRenderer.setPointStyle(PointStyle.CIRCLE);
actualRenderer.setFillPoints(true);
actualRenderer.setLineWidth(2);
actualRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer planRenderer = new XYSeriesRenderer();
planRenderer.setColor(getActivity().getResources().getColor(R.color.logo_orange));
planRenderer.setPointStyle(PointStyle.CIRCLE);
planRenderer.setFillPoints(true);
planRenderer.setLineWidth(2);
planRenderer.setDisplayChartValues(true);
XYSeriesRenderer cupOneRenderer = new XYSeriesRenderer();
cupOneRenderer.setColor(getActivity().getResources().getColor(R.color.black));
cupOneRenderer.setPointStyle(PointStyle.CIRCLE);
cupOneRenderer.setFillPoints(true);
cupOneRenderer.setLineWidth(2);
cupOneRenderer.setDisplayChartValues(true);
XYSeriesRenderer cupTwoRenderer = new XYSeriesRenderer();
cupTwoRenderer.setColor(getActivity().getResources().getColor(R.color.green));
cupTwoRenderer.setPointStyle(PointStyle.CIRCLE);
cupTwoRenderer.setFillPoints(true);
cupTwoRenderer.setLineWidth(2);
cupTwoRenderer.setDisplayChartValues(true);
XYSeriesRenderer cupThreeRenderer = new XYSeriesRenderer();
cupThreeRenderer.setColor(Color.parseColor("#0D8ECF"));
cupThreeRenderer.setPointStyle(PointStyle.CIRCLE);
cupThreeRenderer.setFillPoints(true);
cupThreeRenderer.setLineWidth(2);
cupThreeRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart<br />
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setBackgroundColor(Color.WHITE);
multiRenderer.setMarginsColor(Color.WHITE);
multiRenderer.setLabelsColor(Color.RED);
multiRenderer.setPanEnabled(true,false);
/* multiRenderer.setLabelsTextSize(getActivity().getResources().getDimensionPixelSize(R.dimen.sp_9));
multiRenderer.setLegendTextSize(getActivity().getResources().getDimensionPixelSize(R.dimen.sp_9));
*/ multiRenderer.setChartValuesTextSize(getActivity().getResources().getDimensionPixelSize(R.dimen.sp_9));
multiRenderer.setXLabelsColor(Color.BLACK);
multiRenderer.setYLabelsColor(0, Color.BLACK);
multiRenderer.setShowGridX(true);
multiRenderer.setShowGridY(true);
multiRenderer.setGridColor(Color.BLACK);
multiRenderer.setChartTitle("Plan vs Actual Curve");
multiRenderer.setXTitle("Months");
multiRenderer.setYTitle("plan/actual");
multiRenderer.setZoomButtonsVisible(false);
multiRenderer.setZoomEnabled(true);
for (int i = 0; i < monthList.size(); i++) {
multiRenderer.addXTextLabel(i, monthList.get(i).toString());
}
multiRenderer.addSeriesRenderer(actualRenderer);
multiRenderer.addSeriesRenderer(planRenderer);
multiRenderer.addSeriesRenderer(cupOneRenderer);
multiRenderer.addSeriesRenderer(cupTwoRenderer);
multiRenderer.addSeriesRenderer(cupThreeRenderer);
Log.e("size of actual log",actualList.size()+"");
LinearLayout chartContainer = (LinearLayout) mView.findViewById(R.id.chart_container);
if (monthList.size() > 0) {
// Getting a reference to LinearLayout of the MainActivity Layout<br />
chartContainer.removeAllViews();
//drawing bar chart<br />
chart = ChartFactory.getLineChartView(getActivity(), xyMultipleSeriesDataset, multiRenderer);
//adding the view to the linearlayout<br />
chartContainer.addView(chart);
} else {
chartContainer.removeAllViews();
CustomToast.initToast(getActivity(),"No data available for S-curve");
}
}