Hi,
I have a simple question related to the way I could refresh data of a serie dynamically.
Suppose I have a thread that receive an array of double a few times per second. The size of the array is for example 1000. What is the best and fastest way to refresh my serie and make sure I will see all arrays in my chart and make sure that the Eclipse IDE won't be freeze by the thread and by the update of the chart?
I made the following example, but I have several problem with it.
1- The updates freeze the IDE
2- The refresh of the serie is pretty slow
3- The update of the chart starts only at the end of the processing of the array. If i replace the for loop by a infite loop, the UI is freeze infititely
4- Make sure the IDE have enough cpu to continue to work.
You can answer me at the following address:
lapointe_vincent_1975@hotmail.com
Thanks,
Vincent
Example (based on LargeSeriesExample.java):
public class SampleView extends ViewPart {
public SampleView() {
}
public void createPartControl(Composite parent) {
final Chart chart = new Chart(parent, SWT.NONE);
chart.getTitle().setText("Large Series Example");
chart.getAxisSet().getXAxis(0).getTitle().setText("Data Points");
chart.getAxisSet().getYAxis(0).getTitle().setText("Amplitude");
final ILineSeries lineSeries = (ILineSeries) chart.getSeriesSet().createSeries(SeriesType.LINE, "line series1");
lineSeries.setSymbolType(PlotSymbolType.NONE);
lineSeries.setYSeries(getSeries(0, i));
// adjust the axis range
chart.getAxisSet().adjustRange();
parent.getShell().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
for (int t=0; t< 100; t++) {
lineSeries.setYSeries(getSeries((double)t, 0));
chart.redraw();
chart.update();
}
}
}
);
}
private static double[] getSeries(double t, double y) {
double[] series = new double[1000];
for (int i = 0; i < series.length; i++) {
series[i] = y + Math.sin(t + i * 33 * Math.PI/ series.length) + Math.sin(t + i * 15 * Math.PI/ series.length);
}
return series;
}
public void setFocus() {
// TODO Auto-generated method stub
}
}
Hi Vincent,
If I understand your question correctly, this is a generic issue regarding the usage of Display#asyncExec(). You can see the same behavior (i.e. IDE freeze) with the following code without using SWTChart.
public class SampleView extends ViewPart {
@Override
public void createPartControl(Composite parent) {
parent.getShell().getDisplay().asyncExec(new Runnable() {
public void run() {
while (true) {
// do nothing
}
}
});
}
@Override
public void setFocus() {
// do nothing
}
}
Since UI-thread is blocked by run() method above, IDE doesn't have a chance to update itself.
1) It is not good idea to use Display#asyncExec() for the operation which may take long time like manipulating model. The asyncExec() should be used only for the operation redrawing/updating widgets like Control#update(), and the run() should return asap.
2) Instead of repeating something within run() for long time, it would be better to invoke asyncExec() multiple times for short time.
3) In the case of repeating the UI updates so frequently, all of UI updates would be not always necessary. You could use org.eclipse.core.runtime.jobs.Job to schedule the job to update and cancel the already scheduled same job.
I hope this answers your question.
Best Regards,
Yoshitaka