0

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;

}

Korba
  • 435
  • 1
  • 4
  • 18
  • Did you 5 mods even read the question? @Yogi is asking for something different to the question you linked to. Please moderate properly or don't moderate at all. – David Gilbert Sep 25 '14 at 19:23
  • Voting to reopen in deference to the [author](http://stackoverflow.com/users/2592874/david-gilbert), who will soon be a mod, himself! @Yogi: You may be looking for `XYSeries#clear()`. – trashgod Sep 25 '14 at 22:04
  • Thanks @trashgod . XYSeries#clear suits my use case. However mystsery still remains with why the plotting begins from RIGHT-BOTTOM corner though I have used xyPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); and xyPlot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT); Any thoughts about it? – Korba Sep 26 '14 at 00:31
  • @DavidGilbert may have some insight; in the interim, please edit your question to include a [complete example](http://stackoverflow.com/help/mcve). – trashgod Sep 26 '14 at 01:23
  • I have added the related functions which deal with chart creation and adding points to same. Hope this is complete as expected. – Korba Sep 26 '14 at 02:59
  • 1
    AxisLocation.BOTTOM_OR_LEFT just specifies the location of the axis (at the bottom if the PlotOrientation is horizontal, and at the left if the PlotOrientation is vertical - for the x-axis, switch around for the y-axis). The position of your new data points is determined only by the current bounds of the axes - you should probably reset the range manually on the x-axis each time you clear the series. – David Gilbert Sep 26 '14 at 05:23
  • Please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that shows your revised approach based on the comments posted. – trashgod Oct 09 '14 at 02:33

0 Answers0