2
我想製作折線圖,但我幾乎不知道如何使用圖表API。我已經調整了製作餅圖的代碼,以便嘗試製作折線圖。我真的很感謝實施方面的幫助,或者如果有關於如何製作很好的折線圖的教程。如何在cn1中製作折線圖
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;
}
}
喜曬。非常感謝評論。我確實看到這個演示,然後在項目中下載它,這些類表示導入com.codename1.charts ....不存在。如何在演示類中啓用圖表包? – roblin
演示源代碼應該在IDE的最新版本中,所以你應該可以在New-> Project-> Codename One/Demos中看到3.5+插件。 –
啊,我們走吧!非常感謝。 – roblin