Dynamically building and updating Histograms with JFreeChart
I've got a stream of incoming data that I would like to plot using a simple histogram. I don't know the range of values, or the proper resolution or bin width to use for the histogram.
SimpleHistogramDataset provides some of this functionality, but I don't want to have to deal with catching exceptions in order to add new bins if the new value isn't covered. In addition, it doesn't easily al开发者_开发知识库low me to rebuild the histogram using a different bin width (perhaps an integer multiples of some initial set width).
Is there an easy way to accomplish this with JFreeChart or some alternate charting library, or am I going to have to write my own class here?
It sounds to me like you have a classic MVC scenario. Your chart is the view, and your controller will coordinate rebuilding this view based upon your model.
So you should build your model to reflect the data coming in, and the controller will regenerate the chart based upon this. Consequently the controller can determine whether to modify an existing chart, or rebuild it completely based upon new requirements. The chart implementation (the view) isn't suited to holding the dynamically changing data structures itself.
Before you invoke addObservation()
, you must verify that the value will fit in some bin; use addBin()
accordingly. When the chart is redrawn, the bins will automatically resize to fill the plot area. If this is not the desired behavior, the corresponding XYBarRenderer can be extended. If you have to remove bins, you must use removeAllBins()
. This makes Brian Agnew's MVC suggestion even more cogent.
For reference, this example shows the effect of adding an XY series dynamically. It may give you a sense of how updates occur.
精彩评论