I have managed to plot a linear graph. The following is the code:
private JPanel createGraph() {
JPanel panel = new JPanel();
XYSeries series = new XYSeries("MyGraph");
series.add(0, 1);
series.add(1, 2);
series.add(2, 5);
series.add(7, 8);
series.add(9, 10);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createXYLineChart(
"XY Chart",
"x-axis",
"y-axis",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
ChartPanel chartPanel = new ChartPanel(chart);
panel.add(chartPanel);
return panel;
}
However, it is not a smooth curve, but straight lines. How can I make it smooth please?
I believe that you are looking for XYSplineRenderer
You should be able to do
chart.getXYPlot().setRenderer(new XYSplineRenderer());
after your chart is constructed.
Related
I'm making an application in Java using JFreeChart which shows an XY line chart. The problem is that it shows every point of the dataset on the lines, and I don't want to display these points. Any idea on how to remove the points or make them not visible?
This is a sample screenshot:
Here is the code:
JFrame frame = new JFrame();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesStroke(0, new BasicStroke(2.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
new float[] { 2.0f, 4.0f }, 0.0f));
XYDataset ds = createDataset(i, p, capacity);
JFreeChart xylineChart = ChartFactory.createXYLineChart("", "",
"", ds, PlotOrientation.VERTICAL, true, true, false);
XYPlot plot = xylineChart.getXYPlot();
plot.setDomainGridlinePaint(Color.BLACK);
plot.setRangeGridlinePaint(Color.BLACK);
plot.setBackgroundPaint(Color.WHITE);
plot.setRenderer(renderer);
ChartPanel cp = new ChartPanel(xylineChart);
frame.getContentPane().add(cp);
By default, the no-argument constructor of XYLineAndShapeRenderer "Creates a new renderer with both lines and shapes visible." To remove the points, you can
Use the alternative constructor to specify the desired combination—lines without shapes, as shown here and in ChartFactory.createXYLineChart:
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false);
Invoke setSeriesShapesVisible(), as #abhinavxeon suggests here and the author suggests here:
renderer.setSeriesShapesVisible(0, false)
I'm using JFreeChart v1.0.19 to create the following time series chart:
...using the following code:
final String chartTitle = "";
final XYDataset dataset = createDataset1();
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
chartTitle, "Year End", "Turnover", dataset, true, false, false);
final XYPlot plot = chart.getXYPlot();
final NumberAxis axis2 = new NumberAxis("Cash");
axis2.setAutoRangeIncludesZero(false);
plot.setRangeAxis(1, axis2);
plot.setDataset(1, createDataset2());
plot.mapDatasetToRangeAxis(1, 1);
final StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
renderer1.setSeriesPaint(0, Color.blue);
renderer1.setSeriesShape(0, ShapeUtilities.createDiamond(5));
renderer1.setSeriesShapesFilled(0, true);
plot.setRenderer(0, renderer1);
final StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
renderer2.setSeriesPaint(0, Color.black);
renderer2.setSeriesShape(0, ShapeUtilities.createDiamond(5));
renderer2.setSeriesShapesFilled(0, true);
plot.setRenderer(1, renderer2);
chart.getPlot().setBackgroundPaint(Color.WHITE);
chart.getXYPlot().setDomainGridlinePaint(Color.LIGHT_GRAY);
chart.getXYPlot().setRangeGridlinePaint(Color.LIGHT_GRAY);
...but I can't get the data points to show shapes. Is this possible?
It was my renderer that was the problem. Changing from StandardXYItemRenderer to XYLineAndShapeRenderer e.g.
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true,true);
...did the trick. The two boolean args in the constructor are for switching on/off lines and shapes, respectively.
if i have a my Jpanel and a my JFreeChart. How can I add this Chart in to the JPanel?
XYSeries series = new XYSeries("XYGraph");
series.add(1, 1);
series.add(1, 2);
series.add(2, 1);
series.add(3, 9);
series.add(4, 10);
// Add the series to your data set
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
// Generate the graph
JFreeChart chart = ChartFactory.createXYLineChart(
"XY Chart", // Title
"x-axis", // x-axis Label
"y-axis", // y-axis Label
dataset, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
true, // Show Legend
true, // Use tooltips
false // Configure chart to generate URLs?
);
and now, how can i add chart in my JPanle?
From an old java forums thread
JPanel jPanel1 = new JPanel();
jPanel1.setLayout(new java.awt.BorderLayout());
..
ChartPanel CP = new ChartPanel(chart);
..
jPanel1.add(CP,BorderLayout.CENTER);
jPanel1.validate();
Here is a good tutorial to lead you further : How to embed JFreeChart in JPanel
JFreeChart chart = new JFreeChart("Cos(x) and Cos^2(x) versus x", parent);
ChartPanel myChart = new ChartPanel(chart);
myChart.setMouseWheelEnabled(true);
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel1.add(myChart,BorderLayout.CENTER);
jPanel1.validate();
I was having the same problem but I was able to identify a way to solve this.
JPanel panel = new JPanel();
panel.setBackground(new Color(255, 102, 51));
panel.setBounds(50, 64, 955, 888);
frame.getContentPane().add(panel);
panel.setLayout(null); //Absolute Layout
XYSeries series = new XYSeries("XY Chart");
XYSeriesCollection dataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Testing Chart", "Date", "Average Profit", dataset);
ChartPanel chartPanel = new ChartPanel((JFreeChart) null);
chartPanelH2S.setChart(chart);
chartPanelH2S.setBounds(39, 193, 419, 309);
panel.add(chartPanel);
frame.setVisible(true);
Hope this helps
Im trying to change the background color to my bar chart and so far nothing seems to be working
Here is my code below:
JFreeChart expbarchart = ChartFactory.createBarChart("Monthly Expenditures", "Expenditure Type", "Amount (£)", barexp, PlotOrientation.VERTICAL, false, true, false);
ChartPanel expframe = new ChartPanel(expbarchart);
expframe.setLocation(695, 49);
expframe.setSize(641,500);
expframe.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(173, 216, 230), null));
graphpanel.add(expframe);
I have tried doing .setbackground() and it does not seem to work
Thanks
BarChartDemo1 shows you how to set the chart background paint:
chart.setBackgroundPaint(new Color(173, 216, 230));
It also shows you how to set a ChartTheme that you can change:
StandardChartTheme theme = new StandardChartTheme("JFree/Shadow", true);
Color color = new Color(173, 216, 230);
theme.setPlotBackgroundPaint(color);
theme.setChartBackgroundPaint(color.brighter());
ChartFactory.setChartTheme(theme);
try this and customize as per your need.
// create the chart...
JFreeChart chart = ChartFactory.createLineChart(
"# of Sales by Month", // chart title
"Month", // domain axis label
"# of Sales", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
if(subTitle != null && !subTitle.isEmpty())
chart.addSubtitle(new TextTitle(subTitle));
chart.setBackgroundPaint(Color.BLUE);
// Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);
// chart.setBackgroundPaint(p);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRangePannable(true);
plot.setRangeGridlinesVisible(true);
plot.setBackgroundAlpha(1);
plot.setBackgroundPaint(Color.BLUE);
// Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);
// plot.setBackgroundPaint(p);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
ChartUtilities.applyCurrentTheme(chart);
You have to use JFreeChart.getPlot().setBackgroundPaint(Color.XXXXXX); like this:
public static void main(String[] args) {
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("LoggedIn" +": "+ 5, 10);
pieDataset.setValue("LoggedOut" +": "+ 8, 17);
JFreeChart jfc = ChartFactory.createPieChart("title", pieDataset, false, false, false );
jfc.getPlot().setBackgroundPaint(Color.BLUE);
ChartPanel chart = new ChartPanel(jfc);
JFrame frame = new JFrame();
frame.add(chart);
frame.pack();
frame.setVisible(true);
}
Of course, you can catch the desired color by several different methods regarding your needs, for example:
Color color1 = "anyComponent".getBackgroundColor();
and then apply
jfc.getPlot().setBackgroundPaint(color1);
I hope it helps!
Let's say I've generated a chart with the following code:
private ChartPanel createChart(){
XYSeries series1 = new XYSeries("First");
XYSeries series2 = new XYSeries("Second");
XYSeries series3 = new XYSeries("Third");
series1.add(0.0, 5.5);
series1.add(5, 10);
series1.add(10, 5.5);
series2.add(0.0, 2);
series2.add(5, 2);
series2.add(10, 7);
series3.add(0.0, 10);
series3.add(5, 5);
series3.add(10, 6);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
dataset.addSeries(series2);
dataset.addSeries(series3);
JFreeChart chart = ChartFactory.createXYLineChart("line chart example",
"X", "Y", dataset, PlotOrientation.VERTICAL, true, true, false);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
return chartPanel;
}
Now I would like to modify a value previously inserted in an XYSeries without build another chart (no other call to createXYLineChart), and make the chart automatically updated.
How do this?
Just look at the api.
There are many different methods, depending if you know the index of the element in the series, or if you know the x-value, but want to change the y-value.
I assume the chart will be redrawn since all the methods throw a SeriesChangeEvent.