Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

codenameone - Chart does not render with the new CN1aChartEngine library

Here is the code:

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();

}

This method returns chart as component. I called this method in the form of:

chart =  demo.getCombinedTemperatureChart(context);
addComponent(BorderLayout.CENTER, chart);

With the older library the chart renders correctly but with the new library of CN1aChartEngine, I got this exception:

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

While debugging I get that, this exception occurs when form.show() called not at the moment when the getCombinedTemperatureChart(Context context) method returns the chart component. I didn't get why this exception occurs with the updated CN1aChartEngine library. Little help would be nice.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like you're using the older 1.0.x line (i.e. using Pisces). I have reproduced the problem (in that line) and fixed it. You can download the latest at https://github.com/shannah/CN1aChartEngine/releases/tag/1.0.2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...