Reputation: 53
I have class Histogram and I would love to update his vaules over time. Could you tell me how to do it with timer? :P I just have 4 bins all the time. Additionaly I have thread in which I change Calculation.n over time is it possible to lock thread until histogram is updated? Although I dont need to do it on timer I just want everytime "n" changes its value histogram to be updated.
public class Histogram extends JPanel {
/**
*
*/
private static final long serialVersionUID = 8582587439869049160L;
public Histogram() {
// TODO Auto-generated constructor stub
setVisible(true);
final HistogramDataset dataSet = new HistogramDataset();
final double particles[] = {Calculation.n,Calculation.n1,Calculation.n2,Calculation.n3};
dataSet.addSeries("How many particles passed", particles, 4);
Timer timer = new Timer(250, null);
timer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
timer.start();
JFreeChart histogram =ChartFactory.createHistogram(
"Histogram",
null,
null,
dataSet,
PlotOrientation.VERTICAL,
true,
true,
false);
ChartPanel chartPanel = new ChartPanel(histogram);
add(chartPanel);
revalidate();
}
Upvotes: 0
Views: 431
Reputation: 5923
I don't think this is possible without implementing your own version of HistogramDataset
, as then histogram is evaluated when a dataset is added.
If you do implement your own version of HistogramDataset
, base your code on HistogramDataset #addSeries
and remember to call fireDatasetChanged()
at the end.
Upvotes: 1
Reputation: 4477
There is an alternative dataset implementation included in JFreeChart: SimpleHistogramDataset. This one allows you to keep adding new values (via the addObservation()/addObservations()
methods).
Upvotes: 1