Red de conocimiento informático - Material del sitio web - Cómo dibujar un gráfico de líneas en Java

Cómo dibujar un gráfico de líneas en Java

paquete?com.lei.jfreechart;

importar?javax.swing.JPanel;

importar?org.jfree.chart.chart.ChartPanel;

importar ?org.jfree.chart.JFreeChart;

importar?org.jfree.chart.axis.NumberAxis;

importar?org.jfree.chart.plot.CategoryPlot;

importar?org.jfree.chart.plot.PlotOrientation;

importar?org.jfree.data.category.PlotOrientation;

importar?DefaultCategoryDataset;

import?org.jfree.ui.ApplicationFrame;

import?org.jfree.ui.RefineryUtilities;

¿clase pública?LineCharts?extends?ApplicationFrame?{

/** *? */ privado?{

super(s);

setContentPane(createDemoLine() }

público); ?{

LineCharts?fjc?=?new?LineCharts("Gráfico de líneas");

fjc.pack();

RefineryUtilities.centerFrameOnScreen(fjc );

fjc.setVisible(true);

}

/?Generar un panel que muestre el gráfico public?static?JPanel?createDemoLine()? {

JFreeChart?jfreechart?=?createChart(createDataset());

return?new?ChartPanel(jfreechart }

/?Generar el archivo principal; objeto de gráfico JFreeChart public?{

/?Definir objeto de gráfico

JFreeChart?chart?=?ChartFactory.createLineChart("Curva de ventas trimestral",?//nombre del gráfico de líneas "tiempo" ,?/ /?Abscisa nombre "Ventas (millones)", ?//?Nombre de coordenadas verticales linedataset,?//?Data PlotOrientation.VERTICAL,?//?Imagen de visualización horizontal verdadera,?//Contiene leyenda verdadera,?/ /?tooltips false?//?urls );

CategoryPlot?plot?=?chart.getCategoryPlot();

plot.setRangeGridlinesVisible(true);?//Si mostrar la red Grid

plot.setBackgroundAlpha(0.3f);?//Establecer transparencia de fondo

NumberAxis?rangeAxis?=?(NumberAxis)plot.getRangeAxis();

rangeAxis.setStandardTickUnits(NumberAx

is.createIntegerTickUnits());

rangeAxis.setAutoRangeIncludesZero(true);

rangeAxis.setUpperMargin(0.20);

rangeAxis.setLabelAngle(Math.PI? /?2.0);

return? chart; }

/?Generar datos públicos?{

DefaultCategoryDataset?linedataset?=?new?DefaultCategoryDataset();

//?El nombre de cada curva

String?series1?=?new?DefaultCategoryDataset();

String?series1?=?new?" Frigorífico";

String?series2?=?"TV color";

String?series3?=? Lavadora";

//?Nombre del eje horizontal (nombre de columna)

String?type1?=?"Enero";

String?type2?= ?Febrero";

Cadena?type3?=? Marzo";

lineataset.addValue(0.0,?series1,?type1);

lineataset.addValue(4.2,?series1,?type2);

lineataset.addValue(3.9,?Serie 1,?Tipo 3);

lineataset.addValue(1.0,?Serie 2,?Tipo 1);

lineataset.addValue(5.2,? ?Serie 2,?Tipo 2);

linedataset.addValue(7.9,?series2,?type3);

linedataset.addValue(2.0,?series3,?type1); /p>

linedataset.addValue(9.2,?series3,?type2);

linedataset.addValue(8.9,?series3,?type3);

retorno?< / p>