modify dataset of an already built chart with Java and JFreeChart - java

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.

Related

I need to put the line chart in the JFrame, so they become one instead of opening a jframe and a line chart [duplicate]

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

Too much data in DomainAxis, another way of setTickUnit

I am creating chart like this:
DefaultCategoryDataset cumulativeResultData = new DefaultCategoryDataset();
DefaultCategoryDataset indexResultData = new DefaultCategoryDataset();
then in for loop im adding data to each Dataset using cumulativeResultData.addValue
CategoryPlot plot = new CategoryPlot();
plot.setDomainAxis(new CategoryAxis("Days"));
plot.setRangeAxis(new NumberAxis("Result"));
plot.setOrientation(PlotOrientation.VERTICAL);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
LineAndShapeRenderer renderer2 = new LineAndShapeRenderer(true, false);
LineAndShapeRenderer renderer = new LineAndShapeRenderer(true, false);
plot.setRenderer(renderer);
plot.setDataset(cumulativeResultData);
ValueAxis rangeAxis2 = new NumberAxis("Index price");
rangeAxis2.setRange(minRange - 1, maxRange + 1);
plot.setRangeAxis(1, rangeAxis2);
plot.setRenderer(1, renderer2);
plot.setDataset(1, indexResultData);
plot.mapDatasetToRangeAxis(1, 1);
JFreeChart chart = new JFreeChart(plot);
But what i get is not what i expected to get, please take a look in the picture bellow.
The domainAxis has to much data and it is blurred. Is any way to repair this? That only every for example 20th element shows on the domainAxis?
http://imgur.com/PkxSZl8
You should only use the CategoryPlot class when your x-axis will show categorical data. It looks like you have time series data, and for that you can use the XYPlot class with a DateAxis for the x-axis. There is also the TimeSeriesCollection class that you can probably use for your dataset.

Change the Lines Style and colour of a JFreeChart

I have 3 series:
XYSeries s1 = new XYSeries("one");
s1.add(1,0);
s1.add(2,1);
XYSeries s2 = new XYSeries("two");
s1.add(3,0);
s1.add(4,1);
XYSeries s3 = new XYSeries("three");
s1.add(5,0);
s1.add(6,1);
Which i plot as a XYLineChart:
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);
dataset.addSeries(s3);
JFreeChart chart = ChartFactory.createXYLineChart("title", "x", "y", dataset, PlotOrientation.VERTICAL, false, false, false);
and add to a panel:
ChartPanel cp = new ChartPanel(chart);
panel_1.add(cp, BorderLayout.CENTER);
panel_1.validate();
How do i make it so series s1 and s2 are the same colour, and s3 is different?
You can use the renderer's setSeriesPaint() method to specify the series and paint. Also consider using a custom DrawingSupplier, mentioned here. More generally, you can override the desired get*Paint() methods to establish any desired color scheme, as shown here for getItemFillPaint()

How to make plot a curve using JFreeChart?

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.

Display X Values on the XYLineChart

I want to display the X values on to the XY line chart As shown below:
How can i do this?
I want to display it like this:
Here's my code to display the line Graph:
public class LineChartDemo6{
public static void main(String arg[]){
XYSeries series = new XYSeries("Average Weight");
series.add(20.0, 20.0);
series.add(40.0, 20.0);
series.add(55.0, 20.0);
series.add(70.0, 20.0);
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart
("XYLine Chart using JFreeChart", "Age", "Weight",
xyDataset, PlotOrientation.VERTICAL, true, true, false);
ChartFrame frame1=new ChartFrame("XYLine Chart",chart);
frame1.setVisible(true);
frame1.setSize(300,300);
}
}
You can add an XYItemLabelGenerator to your plot's renderer, as shown in this example and this example. It looks like ArgumentIndex {1} is the domain value.
Addendum: Your example works fine; it just needs a little extra margin.
ValueAxis range = plot.getRangeAxis();
range.setUpperMargin(0.20);

Categories