我想制作一个折线图,但是我几乎不知道如何使用图表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;
}
}
我同意图表包是相当低的水平。我们可能想提供一些“用户友好”的东西。
我建议在略有简化的新图表演示中使用折线图演示代码:https : //www.codenameone.com/blog/charts-demo-revisited.html
在此处查看产生此折线图的代码:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句