Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Can't refresh rather than build
MidnightJa
I have a chart that will be refreshed very fast over a period of several seconds, and I want to minimze the flicker effect. I saw in the BIRT documentation that I could do a refresh rather than build and render on the Generator. But if I call refresh() on the Generator, I get a SWTException with the message "Graphic is disposed".
I can't find any way to avoid this, or even a suitable guard that prevents refresh() from being called when the exception will be thrown. Can someone tell me how do do a refresh as the documentation suggests? The documentation says to "Refer to the chart demo that illustrates how an animated chart may be computed with minimal overhead", but I haven't been able to find any such demo.
Find more posts tagged with
Comments
MidnightJa
Here's my code, which I should have posted yesterday. The code is adapted from an example I found online. It's a test case that plots a sine wave and changes the period of the wave every 500 msec. The code works, but there is some flicker with each update. If you un-comment the line in paintControl() that sets firstRender to false, it will attempt to refresh rather than build and render after the first paint operation. Then the SWTException I mentioned earlier will be thrown on gr.refresh(state).<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>public class SampleChartView extends ViewPart {
/**
* The ID of the view as specified by the extension.
*/
public static final String ID = "net.midnightjava.charttest.views.SampleChartView";
private static float p = 5;
private static GeneratedChartState state;
private static final class MyPaintListener implements PaintListener
{
boolean firstRender = true;
public void paintControl(PaintEvent e)
{
IDeviceRenderer deviceRenderer = null;
try {
deviceRenderer = PluginSettings.instance().getDevice("dv.SWT");
} catch (ChartException e1) {
e1.printStackTrace();
}
deviceRenderer
.setProperty(IDeviceRenderer.GRAPHICS_CONTEXT, e.gc);
Rectangle rect = ((Composite) e.widget).getClientArea();
Bounds bounds = BoundsImpl.create(rect.x + 2,
rect.y + 2,
rect.width - 4,
rect.height - 4);
bounds.scale(72d /
deviceRenderer.getDisplayServer().getDpiResolution());
Chart chart = null;
if (firstRender) {
chart = createSimpleLineChart();
}
Generator gr = Generator.instance();
try
{
if (firstRender) {
state = gr.build(deviceRenderer.getDisplayServer(),
chart,
bounds,
new RunTimeContext());
gr.render(deviceRenderer, state);
} else {
gr.refresh(state);
}
//firstRender = false;
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
private Canvas canvas;
public SampleChartView() {
Job animator = new Job("Animator Job") {
@Override
protected IStatus run(IProgressMonitor monitor) {
if (p == 10) {
p = 5;
} else {
p++;
}
if (!PlatformUI.getWorkbench().getDisplay().isDisposed()) {
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
if (canvas != null && !canvas.isDisposed()) {
canvas.redraw();
}
}
});
}
schedule(500);
return Status.OK_STATUS;
}
};
animator.setSystem(true);
animator.schedule();
}
public void createPartControl(final Composite parent) {
canvas = new Canvas(parent, SWT.NONE);
canvas.addPaintListener(new MyPaintListener());
}
public static final Chart createSimpleLineChart()
{
ChartWithAxes cwaBar = ChartWithAxesImpl.create();
cwaBar.getBlock().setBackground(ColorDefinitionImpl.BLACK());
Plot plot = cwaBar.getPlot();
plot.getOutline().setVisible(false);
cwaBar.getTitle().getLabel().setVisible(false);
Legend lg = cwaBar.getLegend();
LineAttributes lia = lg.getOutline();
lg.getText().getFont().setSize(16);
lia.setStyle(LineStyle.SOLID_LITERAL);
lg.getInsets().set(10, 5, 0, 0);
lg.getOutline().setVisible(false);
lg.setAnchor(Anchor.NORTH_LITERAL);
lg.setVisible(false);
AxisImpl xAxisPrimary = (AxisImpl) cwaBar.getPrimaryBaseAxes()[0];
xAxisPrimary.setType(AxisType.LINEAR_LITERAL);
xAxisPrimary.getMajorGrid().getTickAttributes().setVisible(false);
xAxisPrimary.getOrigin().setType(IntersectionType.MIN_LITERAL);
xAxisPrimary.getTitle().setVisible(false);
xAxisPrimary.getLabel().setVisible(false);
xAxisPrimary.getLineAttributes().setVisible(false);
AxisImpl yAxisPrimary = (AxisImpl) cwaBar.getPrimaryOrthogonalAxis(xAxisPrimary);
yAxisPrimary.getMajorGrid().getTickAttributes().setVisible(false);
yAxisPrimary.getTitle().setVisible(false);
yAxisPrimary.setPercent(false);
yAxisPrimary.getLabel().setVisible(false);
yAxisPrimary.getLineAttributes().setVisible(false);
Vector<Integer> vs = new Vector<Integer>();
for (int i = 1; i <= 512; i++) {
vs.add(i);
}
ArrayList<Double> vn1 = new ArrayList<Double>();
for (float i = 1; i <= 512; i++) {
vn1.add(40 * Math.sin(i / p) + 40);
}
NumberDataSet categoryValues = NumberDataSetImpl.create(vs);
NumberDataSet orthoValues1 = NumberDataSetImpl.create(vn1);
Series seCategory = SeriesImpl.create();
seCategory.setDataSet(categoryValues);
seCategory.setVisible(false);
LineSeries ls = (LineSeries) LineSeriesImpl.create();
ls.setDataSet(orthoValues1);
ls.getLineAttributes().setColor(ColorDefinitionImpl.GREEN());
ls.getMarkers().get(0).setVisible(false);
SeriesDefinition sdX = SeriesDefinitionImpl.create();
sdX.getSeriesPalette().shift(0);
xAxisPrimary.getSeriesDefinitions().add(sdX);
SeriesDefinition sdY = SeriesDefinitionImpl.create();
sdY.getSeriesPalette().shift(0);
yAxisPrimary.getSeriesDefinitions().add(sdY);
sdX.getSeries().add(seCategory);
sdY.getSeries().add(ls);
return cwaBar;
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
}</pre>