Reputation: 66
I'm using createLineChart
to create a graph. This appears as any line graph.
What I would like is to apply XYSplineRenderer
so that the lines are curved. Is it possible?
Here's the code. It's just a simple CreateLineChart
, so I was wondering if it's possible to apply that Renderer, since one of the values is String
and is a range of values.
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);
}
}
Upvotes: 1
Views: 1124
Reputation: 205785
No existing ChartFactory
uses XYSplineRenderer
, and I was unsure about your existing datasets. I started from this example and sampled a normal distribution like this to get the original example shown here. You may have to adjust the precision
to suit your data. You can alter the domain axis labels as needed using any of the methods suggested in How can I change the tickLabel
on a NumberAxis
…?.
My data set uses
int String String
Reading more closely, the first String
is the series key, which I have labeled "Frequency"
; the second appears to be a numeric range. This new example illustrates using a higher precision and a SymbolAxis
, which should work better for your dozen or so ranges.
Updated example:
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);
}
}
Upvotes: 2