I am using JFreeChart library to plot something in an Eclipse view and currently my code for view initialization looks as follows:
@Override
public void createPartControl(Composite parent)
{
JFreeChart chart = null;
// Created via different object
chart = graph.createLineChart("Test Graph", "x", "y");
ChartComposite frame = new ChartComposite(parent, SWT.NONE, chart, true);
frame.setRangeZoomable(false);
parent.layout(true);
}
I wanted to add scrolling to my graph when the user zooms in, but so far all the examples that I found (in the demo package, here, and other sources) are based on Swing dealing with JPanes and ChartPanels.
How can I achieve the same in the createPartControl()
function of my view where I have only a Composite
. Is it possible to do it in SWT only or should I mix (need to figure out how) SWT with SWING or AWT?
Thank you
You might look at
SlidingXYDataset
, mentioned here, or the paging approach shown here.In addition to the accepted answer I'd like to post an answer myself, because I ran into a couple of issues regarding to
SlidingXYDataset
:The implementation allows to specify the number of points in the dataset that can be shown at any given time and that is OK for most use cases. But in the case where the data points are sparsed very uniformly in the dataset sliding thorugh data will cause the domain axis to change its range.
For example if the window size is 5 datapoints and first 5 are 2 units appart and the next 5 are 20 units appart the X axis visible range size will change when your are sliding.
So you can see what the behaviour will look like when all the points are ploted at arbitrary locations. This may or may not be a desired behaviour. In my case I needed to have a fixed visible range of the X axis (for example 200 units) and to keep it as such all the time.
For some reason I was not able to create a
SlidingXYDataset
by giving it an emptyXYSeriesCollection/TimeSeriesCollection
. I needed to start with an empty dataset and update it in real time and I had to add useless data to start with. But that's maybe because I was doing something wrong.Having said that above, there might be workarounds for it, but I went and implemented the zooming and scrollbar in a different than the accepted answer's way.
Also this example shows how to sync the slider when user performs a zoom action on the graph.
First: you need to create an
XYLineChart
and set the desired range (The example is elaborated for X axis only, but can be applied for Y axis as well)Second: you need to put both the chart and a scroll bar inside the parent layout. In SWT you can do this for example through the user of FormLayout
Now every time you move the slider, you need to capture that event and change the chart's domain range with the to methods according to you slider data:
Third: Finaly you need to override the zoom method of the ChartComposite either by extending this object or right above when you create a new one
Hope this implementation can find it's use in other people's use cases.