This is only my second post, so please forgive me if I am not following the rules correctly yet. I am trying to automatically update a line graph, which was created using the JFreeChart lib. I have been attempting this for a few days now and have referred to the api and followed many examples here, here and here.
I have a method that gets the balance of an account and sends it to another class:
public double credit(double d)
{
int bonus = 10;
if (d >= 500){
logData = "CREDITED: + £" + df.format(d) + "\n ********** Whohoo a £10.00 bonus has been added to your balance for depositing at least £500 in a single month **********\n";
currentBalance = currentBalance + d + bonus;
draw.passBalance(currentBalance);
draw.paint(currentBalance); //Send balance to Draw class for the graph (CPB)
} else {
logData = "CREDITED: + £" + df.format(d) + "\n";
currentBalance = currentBalance + d;
}
return currentBalance;
}
The class that receives this deals with the data for the graph:
public class Draw extends ApplicationFrame {
private double balance;
private ChartPanel chartPanel;
private JFreeChart chart;
private XYDataset dataset;
private Draw graph;
public Draw(final String title) {
super(title);
dataset = createDataset();
chart = createChart(dataset);
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(750, 350));
setContentPane(chartPanel);
}
private XYDataset createDataset() {
final XYSeries series1 = new XYSeries("Balance");
series1.add(1.0, balance);
series1.add(2.0, balance);
series1.add(3.0, 100.00);
series1.add(4.0, 100.00);
series1.add(5.0, 100.00);
series1.add(6.0, 100.00);
series1.add(7.0, 100.00);
series1.add(8.0, 100.00);
series1.add(9.0, 100.00);
series1.add(10.0, 100.00);
series1.add(11.0, 100.00);
series1.add(12.0, 100.00);
System.out.println(balance);
final XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
return dataset;
}
private JFreeChart createChart(final XYDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createXYLineChart(
"Account Balance", // chart title
"Month", // x axis label
"Balance", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
false, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.white);
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.lightGray);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(1, true);
plot.setRenderer(renderer);
return chart;
}
public double passBalance(double d)
{
return balance = d;
}
public void paint(double d)
{
//System.out.println(balance);
createDataset();
}
}
It is my understanding that the chart will be redrawn since all the methods throw a SeriesChangeEvent, but this doesn't appear to be the case.
I'm relatively new to Java and would greatly appreciate any help at all!!!
Thanks