如何在cn1中创建折线图

时间:2016-08-05 17:34:03

标签: codenameone

我想制作折线图但是我几乎不知道如何使用图表API。我已经调整了制作饼图的代码,以便尝试制作折线图。我非常感谢有关实现的帮助,或者是否有关于如何制作线图的教程非常棒。

    package userclasses;

import com.codename1.charts.ChartComponent;
import com.codename1.charts.models.XYMultipleSeriesDataset;
import com.codename1.charts.models.XYSeries;
import com.codename1.charts.renderers.XYMultipleSeriesRenderer;
import com.codename1.charts.util.ColorUtil;
import com.codename1.charts.views.LineChart;
import com.codename1.ui.Form;
import com.codename1.ui.layouts.BorderLayout;

/**
 *
 * @author Robin
 */
public class LineGraph1 {
    private XYMultipleSeriesRenderer buildXYMSRenderer(int[] colors) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setMargins(new int[]{20, 30, 15, 0});

        return renderer;
    }

    protected XYMultipleSeriesDataset buildXYMultipleSeriesDataSeries(String title, double[] xValues, double[] yValues) {
        XYMultipleSeriesDataset series = new XYMultipleSeriesDataset();
        int k = 0;
        XYSeries xys = new XYSeries(title);
        for (int i = 0; i < xValues.length; i++) {
            xys.add(xValues[i], yValues[i]);
        }
 {

        }
        series.addSeries(xys);

        return series ;
    }

    public Form createLineChartForm() {

        // Generate the values
        double[] xValues = new double[]{1, 2, 3, 4, 5};
        double[] yValues = new double[]{20,26,31,21,44};


        // Set up the renderer
        int[] colors = new int[]{ColorUtil.BLUE, ColorUtil.GREEN, ColorUtil.MAGENTA, ColorUtil.YELLOW, ColorUtil.CYAN};
        XYMultipleSeriesRenderer renderer = buildXYMSRenderer(colors);
        renderer.setZoomButtonsVisible(true);
        renderer.setZoomEnabled(true);
        renderer.setChartTitleTextSize(20);
        renderer.setDisplayValues(true);
        renderer.setShowLabels(true);
        //SimpleSeriesRenderer r = renderer.getSeriesRendererAt(0);

        // Create the chart ... pass the values and renderer to the chart object.


        LineChart chart = new LineChart(buildXYMultipleSeriesDataSeries("Project budget", xValues, yValues), renderer);
        //LineChart chart2 = new LineChart(buildXYMultipleSeriesDataSeries("Project budget", values), new XYMultipleSeriesRenderer());
        // Wrap the chart in a Component so we can add it to a form
        ChartComponent c = new ChartComponent(chart);

        // Create a form and show it.
        Form f = new Form("Graph");
        f.setLayout(new BorderLayout());
        f.addComponent(BorderLayout.CENTER, c);
        return f;
    }
}

1 个答案:

答案 0 :(得分:2)

我同意图表包的水平相当低。我们可能希望提供更多用户友好的东西&#34;。

我建议通过新图表演示中的折线图演示代码进行略微简化:https://www.codenameone.com/blog/charts-demo-revisited.html

查看产生此折线图的代码here

enter image description here