以下是代码:
public Component getCombinedTemperatureChart(Context context) {
/**
* Title for each line
*/
String[] titles = new String[]{"Temp 1", "Temp 2"};
/**
* x-axis values
*/
List<double[]> x = new ArrayList<double[]>();
for (int i = 0; i < titles.length; i++) {
x.add(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
}
/**
* Value sets
*/
List<double[]> values = new ArrayList<double[]>();
values.add(new double[]{12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,
13.9});
values.add(new double[]{9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10});
/**
* Colors and Styles for renderer
*/
int[] colors = new int[]{Color.GREEN, Color.rgb(200, 150, 0)};
PointStyle[] styles = new PointStyle[]{PointStyle.CIRCLE, PointStyle.DIAMOND};
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
renderer.setPointSize(5.5f);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);
r.setLineWidth(5);
r.setFillPoints(true);
}
setChartSettings(renderer, "Weather data", "Month", "Temperature", 0.5, 12.5, 0, 40,
Color.LTGRAY, Color.LTGRAY);
renderer.setXLabels(12);
renderer.setYLabels(10);
renderer.setShowGrid(true);
renderer.setXLabelsAlign(Paint.Align.CENTER);
renderer.setYLabelsAlign(Paint.Align.RIGHT);
XYValueSeries sunSeries = new XYValueSeries("Demo 1");
sunSeries.add(1f, 35, 4.3);
sunSeries.add(2f, 35, 4.9);
sunSeries.add(3f, 35, 5.9);
sunSeries.add(4f, 35, 8.8);
sunSeries.add(5f, 35, 10.8);
sunSeries.add(6f, 35, 11.9);
sunSeries.add(7f, 35, 13.6);
sunSeries.add(8f, 35, 12.8);
sunSeries.add(9f, 35, 11.4);
sunSeries.add(10f, 35, 9.5);
sunSeries.add(11f, 35, 7.5);
sunSeries.add(12f, 35, 5.5);
XYSeriesRenderer lightRenderer = new XYSeriesRenderer();
lightRenderer.setColor(Color.YELLOW);
XYSeries waterSeries = new XYSeries("Demo 2");
waterSeries.add(1, 16);
waterSeries.add(2, 15);
waterSeries.add(3, 16);
waterSeries.add(4, 17);
waterSeries.add(5, 20);
waterSeries.add(6, 23);
waterSeries.add(7, 25);
waterSeries.add(8, 25.5);
waterSeries.add(9, 26.5);
waterSeries.add(10, 24);
waterSeries.add(11, 22);
waterSeries.add(12, 18);
XYSeries waterSeries2 = new XYSeries("Demo 3");
waterSeries2.add(1, 15);
waterSeries2.add(2, 14);
waterSeries2.add(3, 14);
waterSeries2.add(4, 15);
waterSeries2.add(5, 18);
waterSeries2.add(6, 22);
waterSeries2.add(7, 24);
waterSeries2.add(8, 25);
waterSeries2.add(9, 24);
waterSeries2.add(10, 21);
waterSeries2.add(11, 18);
waterSeries2.add(12, 16);
renderer.setBarSpacing(0.3);
XYSeriesRenderer sunSeries1 = new XYSeriesRenderer();
sunSeries1.setColor(0xff0011cc);
sunSeries1.setChartValuesTextAlign(Paint.Align.CENTER);
XYSeriesRenderer waterRenderer1 = new XYSeriesRenderer();
waterRenderer1.setColor(0xff0099cc);
waterRenderer1.setChartValuesTextAlign(Paint.Align.CENTER);
XYSeriesRenderer waterRenderer2 = new XYSeriesRenderer();
waterRenderer2.setColor(0xff9933cc);
waterRenderer2.setChartValuesTextAlign(Paint.Align.RIGHT);
XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
dataset.addSeries(0, sunSeries);
dataset.addSeries(0, waterSeries);
dataset.addSeries(0, waterSeries2);
renderer.addSeriesRenderer(0, lightRenderer);
renderer.addSeriesRenderer(0, waterRenderer1);
renderer.addSeriesRenderer(0, waterRenderer2);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.argb(50, 250, 210, 190));
waterRenderer1.setDisplayChartValues(true);
waterRenderer1.setChartValuesTextSize(10);
waterRenderer2.setDisplayChartValues(true);
waterRenderer2.setChartValuesTextSize(10);
ChartFactory.getLineChartView(context, dataset, renderer).setZoomRate(5f);
ChartFactory.getLineChartView(context, dataset, renderer).getCurrentSeriesAndPoint();
return ChartFactory.getLineChartView(context, dataset, renderer).getPeer();
}
此方法将图表作为组件返回。我以下列形式称呼此方法:
chart = demo.getCombinedTemperatureChart(context);
addComponent(BorderLayout.CENTER, chart);
使用较旧的库,图表可以正确呈现,但是使用新的CN1aChartEngine库,我遇到了这个例外:
java.lang.NullPointerException
at org.achartengine.chart.XYChart.draw(XYChart.java:374)
at org.achartengine.GraphicalView.onDraw(GraphicalView.java:195)
at org.achartengine.compat.View$Peer.run(View.java:208)
at com.codename1.ui.Display$1.run(Display.java:775)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
[Task Thread] 0:0:0,0 - java.lang.NullPointerException
[Task Thread] 0:0:0,1 - java.lang.NullPointerException
在调试时我得到了这个,当form.show()
方法返回图表组件时getCombinedTemperatureChart(Context context)
调用时,会发生此异常。
我不明白为什么更新的CN1aChartEngine库会发生这种异常。没什么好帮的。
答案 0 :(得分:1)
看起来您正在使用较旧的1.0.x系列(即使用双鱼座)。我已经复制了问题(在那一行)并修复了它。你可以下载最新的 https://github.com/shannah/CN1aChartEngine/releases/tag/1.0.2