I am trying to plot a real time graph using the data from serial port which arrives approx 1 sample/5 msec. So it is basically a Integer_Value Vs Time graph. Below is code to draw graph using JFreeChar (ScatterChart). In receive event handler of serial port listener I call method xySeries.add(x, y); with appropriate x and y co-ordinates.
Now plot appearance as expected is like 1. start from left end point of chart 2. Plot till right end 3. Restart from left end point of char 4. keep repeating this refresh cycles as I keep receiving continuous data over serial port.
However, I am stuck with problem #3 and #4. How to clean off the chart and restart from left end point? Also in #1 and #2 I am able to plot the points but it starts somewhere in right bottom end of chart. But I have used
xyPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); xyPlot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
which should ideally start from bottom left end of chart, but it does not.
Would appreciate some pointers in this direction
Thank you in advance.
public void addPoint(Number x, Number y) {
Comment:This function is called from Serial Port receive event handler that supplies X,Y values
XYItemRenderer renderer = xyPlot.getRenderer();
//System.out.println("addPoint y = " + y.doubleValue() + " minThresholdMarker" + minThresholdMarker);
if(y.doubleValue() > minThresholdMarker) {
GradientPaint gPaint = new GradientPaint(2.0f, 6.0f, Color.lightGray, 2.0f, 6.0f, Color.green);
xyPlot.setBackgroundPaint(gPaint);
renderer.setSeriesPaint(0, Color.green);
if(!countDownTimerRunning /*&& !bTimeOver */) {
timerCountDown = new Timer(1000, countDownTimeListener);
timerCountDown.setInitialDelay(0);
countDown = TREATMENT_DURATION;
countElapsed = 0;
timerCountDown.start();
countDownTimerRunning = true;
timerDispPanel.setBackground(new Color(240, 230, 140));
}
}
else {
GradientPaint gPaint = new GradientPaint(4.0f, 6.0f, Color.lightGray, 3.0f, 6.0f, Color.lightGray);
xyPlot.setBackgroundPaint(gPaint);
if(countDownTimerRunning) {
stopCountDownTimer();
}
if(patientReady) {
renderer.setSeriesPaint(0, Color.blue);
}
else {
renderer.setSeriesPaint(0, Color.black);
}
timerDispPanel.setBackground(new Color(240, 240, 240));
if(bTimeOver)
bTimeOver = false;
else
bTimeOver = true;
}
xyPlot.setRenderer(renderer);
xySeries.add(x, y);
}
private ChartPanel drawChart() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
xySeries = new XYSeries("");
xySeriesCollection.addSeries(xySeries);
JFreeChart chart = ChartFactory.createScatterPlot("Breathing Co-ordinator", "Time (ms)", "Volume (liters)", xySeriesCollection);
// Get the XY Plot from the scatter chart
xyPlot = (XYPlot) chart.getPlot();
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);
ValueAxis Xaxis = xyPlot.getDomainAxis();
Xaxis.setAutoRange(true);
Xaxis.setFixedAutoRange(80.0);
ValueAxis Yaxis = xyPlot.getRangeAxis();
Yaxis.setFixedAutoRange(6.0);
// Create the renderer
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesPaint(1, Color.blue);
NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
domain.setVerticalTickLabels(false);
NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
range.setVerticalTickLabels(false);
chartPanel_1 = new ChartPanel(chart);
chartPanel_1.setZoomAroundAnchor(true);
chartPanel_1.setMaximumDrawHeight(700);
chartPanel_1.setRefreshBuffer(true);
chartPanel_1.setLocation(0, 0);
chartPanel_1.setSize(1200, 519);
chartPanel_1.setLayout(new BorderLayout(0, 0));
NumberAxis xAxis = (NumberAxis) xyPlot.getDomainAxis();
NumberAxis yAxis = (NumberAxis) xyPlot.getRangeAxis();
yAxis.setRange(-3.0, 4.0);
xyPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
xyPlot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
return chartPanel_1;
}