package com.positive.charts.plot.xy;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import com.positive.charts.axis.ValueAxis;
import com.positive.charts.data.util.DatasetUtilities;
import com.positive.charts.data.xy.TableXYDataset;
import com.positive.charts.data.xy.XYDataItem;
import com.positive.charts.data.xy.XYDataset;
import com.positive.charts.data.xy.XYSeries;
import com.positive.charts.data.xy.XYSeriesCollection;
import com.positive.charts.plot.PlotRenderingInfo;
import com.positive.charts.plot.PlotState;
import com.positive.charts.renderer.xy.XYItemRenderer;
/**
* Realtime XY plot is designed to work with data gathered in realtime and
* requiring fastest possible display. The domain axis of this plot represents
* real time.
* <p>
* Special realtime renderers must be used.
*/
public class RealtimeXYPlot extends XYPlot {
/** A back buffer image to draw on. */
private Image backBuffer;
public void dispose() {
if (this.backBuffer != null) {
this.backBuffer.dispose();
}
super.dispose();
}
public void draw(final GC gc, final Rectangle area, final Point anchor,
final PlotState parentState, final PlotRenderingInfo info) {
// TODO Auto-generated method stub
super.draw(gc, area, anchor, parentState, info);
}
public boolean render(final GC gc, final Rectangle dataArea,
final int index, final PlotRenderingInfo info) {
final XYDataset dataset = this.getDataset(index);
if (DatasetUtilities.isEmptyOrNull(dataset)) {
return false;
}
XYItemRenderer renderer = this.getRenderer(index);
if (renderer == null) {
renderer = this.getRenderer();
}
final ValueAxis domainAxis = this.getDomainAxisForDataset(index);
final ValueAxis rangeAxis = this.getRangeAxisForDataset(index);
final double lowerBound = domainAxis.getLowerBound();
final double upperBound = domainAxis.getUpperBound();
List series;
if (dataset instanceof TableXYDataset) {
series = ((TableXYDataset) dataset).getSeries();
} else if (dataset instanceof XYSeriesCollection) {
series = ((XYSeriesCollection) dataset).getSeries();
} else {
return false;
}
for (final Iterator iter = series.iterator(); iter.hasNext();) {
final XYSeries s = (XYSeries) iter.next();
final List items = s.getItems();
if (items.size() == 0) {
continue;
}
int fromIndex = Collections.binarySearch(items, new XYDataItem(
lowerBound, 0));
if (fromIndex < 0) {
fromIndex = -fromIndex - 2;
}
if (fromIndex < 0) {
fromIndex = 0;
}
int toIndex = Collections.binarySearch(items, new XYDataItem(
upperBound, 0));
if (toIndex < 0) {
toIndex = -toIndex;
}
if (toIndex >= items.size()) {
toIndex = items.size() - 1;
}
final List subSeries = items.subList(fromIndex, toIndex);
renderer.drawSeriesItems(gc, dataArea, info, this, domainAxis,
rangeAxis, dataset, dataset.indexOf(s.getKey()), subSeries);
}
return true;
}
}