我正在使用createLineChart
创建图形。这会显示为任何折线图。
我想应用XYSplineRenderer
,以使线条弯曲。有可能吗?
这是代码。这只是一个简单的CreateLineChart
,所以我想知道是否有可能应用该Renderer,因为其中一个值是String
并且是一个值范围。
public class Intento1 extends JFrame {
JFreeChart Grafica;
JFreeChart Grafica2;
JFreeChart Grafica3;
DefaultCategoryDataset Datos = new DefaultCategoryDataset();
DefaultCategoryDataset Datos2 = new DefaultCategoryDataset();
//XYDataset Datos2 = new XYDataset();
//XYSplineRenderer render = new XYSplineRenderer();
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Intento1 frame = new Intento1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Intento1() {
Datos.addValue(1, "Frecuencia", "Menos de 46");
Datos.addValue(1, "Frecuencia", "46-55");
Datos.addValue(3, "Frecuencia", "56-65");
Datos.addValue(7, "Frecuencia", "66-75");
Datos.addValue(11, "Frecuencia", "76-85");
Datos.addValue(21, "Frecuencia", "86-95");
Datos.addValue(28, "Frecuencia", "96-105");
Datos.addValue(16, "Frecuencia", "106-115");
Datos.addValue(22, "Frecuencia", "116-125");
Datos.addValue(7, "Frecuencia", "126-135");
Datos.addValue(1, "Frecuencia", "136-145");
Datos.addValue(2, "Frecuencia", "146 o Mas");
Datos2.addValue(0.008, "Frecuencia Relativa", "Menos de 46");
Datos2.addValue(0.008, "Frecuencia Relativa", "46-55");
Datos2.addValue(0.025, "Frecuencia Relativa", "56-65");
Datos2.addValue(0.058, "Frecuencia Relativa", "66-75");
Datos2.addValue(0.092, "Frecuencia Relativa", "76-85");
Datos2.addValue(0.175, "Frecuencia Relativa", "86-95");
Datos2.addValue(0.234, "Frecuencia Relativa", "96-105");
Datos2.addValue(0.134, "Frecuencia Relativa", "106-115");
Datos2.addValue(0.183, "Frecuencia Relativa", "116-125");
Datos2.addValue(0.058, "Frecuencia Relativa", "126-135");
Datos2.addValue(0.008, "Frecuencia Relativa", "136-145");
Datos2.addValue(0.017, "Frecuencia Relativa", "146 o Mas");
//Grafica.getXYPlot().setRenderer(render);
Grafica = ChartFactory.createLineChart("Variable continua"
, "Produccion", "Frecuencia", Datos, PlotOrientation.VERTICAL,
true,true,false);
Grafica2 = ChartFactory.createLineChart("Variable continua"
, "Produccion", "Frecuencia", Datos2, PlotOrientation.VERTICAL,
true,true,false);
Grafica3=ChartFactory.createLineChart ("Variablew", "Produccion", "Frecuencia", Datos2, PlotOrientation.VERTICAL,
true, true, false);
CategoryPlot plot = Grafica.getCategoryPlot();
//XYPlot awa = Grafica.getPlot();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnGraficar = new JButton("Graficar");
btnGraficar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ChartPanel Panel = new ChartPanel(Grafica);
JFrame Ventana = new JFrame("JFreeChart");
Ventana.getContentPane().add(Panel);
Ventana.pack();
Ventana.setVisible(true);
Ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
btnGraficar.setBounds(160, 36, 89, 23);
contentPane.add(btnGraficar);
JButton btnGraficarRelativas = new JButton("Graficar Relativas");
btnGraficarRelativas.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ChartPanel Panel = new ChartPanel(Grafica2);
JFrame Ventana = new JFrame("JFreeChart");
Ventana.getContentPane().add(Panel);
Ventana.pack();
Ventana.setVisible(true);
Ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
btnGraficarRelativas.setBounds(160, 95, 89, 23);
contentPane.add(btnGraficarRelativas);
}
}
答案 0 :(得分:1)
没有现有的ChartFactory
使用XYSplineRenderer
,我不确定您现有的数据集。我从example开始,对this之类的正态分布进行了采样,得到了here所示的原始示例。您可能需要调整precision
以适合您的数据。您可以根据需要使用How can I change the tickLabel
on a NumberAxis
…?中建议的任何方法来更改域轴标签。
我的数据集使用
int String String
仔细阅读一下,第一个String
是系列密钥,我将其标记为"Frequency"
;第二个似乎是数字范围。这个新示例说明了如何使用更高的精度和SymbolAxis
,它应该可以在十几个范围内更好地工作。
更新示例:
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* @see https://stackoverflow.com/a/55418498/230513
* @see https://stackoverflow.com/a/14790187/230513
* @see https://stackoverflow.com/a/40167139/230513
*/
public class SplineTest {
public static final String Title = "Spline Test";
public static void main(String[] args) {
EventQueue.invokeLater(new SplineTest()::display);
}
private void display() {
XYSeries series = new XYSeries("Frequency");
series.add(0, 1);
series.add(1, 1);
series.add(2, 3);
series.add(3, 7);
series.add(4, 11);
series.add(5, 21);
series.add(6, 28);
series.add(7, 16);
series.add(8, 22);
series.add(9, 7);
series.add(10, 1);
series.add(11, 2);
XYDataset dataset = new XYSeriesCollection(series);
String[] labels = new String[series.getItemCount()];
labels[0] = "<46";
labels[1] = "46-55";
labels[2] = "56-65";
labels[3] = "66-75";
labels[4] = "76-85";
labels[5] = "86-95";
labels[6] = "96-105";
labels[7] = "106-115";
labels[8] = "116-125";
labels[9] = "126-135";
labels[10] = "136-145";
labels[11] = ">146";
NumberAxis domain = new SymbolAxis("X", labels);
NumberAxis range = new NumberAxis("Y");
XYSplineRenderer r = new XYSplineRenderer(8);
XYPlot xyplot = new XYPlot(dataset, domain, range, r);
JFreeChart chart = new JFreeChart(xyplot);
ChartPanel chartPanel = new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
JFrame frame = new JFrame(Title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(chartPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}