Example usage for org.jfree.data.xy XYSeries remove

List of usage examples for org.jfree.data.xy XYSeries remove

Introduction

In this page you can find the example usage for org.jfree.data.xy XYSeries remove.

Prototype

public XYDataItem remove(Number x) 

Source Link

Document

Removes an item with the specified x-value and sends a SeriesChangeEvent to all registered listeners.

Usage

From source file:Graph_with_jframe_and_arduino.java

public static void main(String[] args) {

    // create and configure the window
    JFrame window = new JFrame();
    window.setTitle("Sensor Graph GUI");
    window.setSize(600, 400);//from  w ww . jav  a 2  s  .c om
    window.setLayout(new BorderLayout());
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // create a drop-down box and connect button, then place them at the top of the window
    JComboBox<String> portList_combobox = new JComboBox<String>();
    Dimension d = new Dimension(300, 100);
    portList_combobox.setSize(d);
    JButton connectButton = new JButton("Connect");
    JPanel topPanel = new JPanel();
    topPanel.add(portList_combobox);
    topPanel.add(connectButton);
    window.add(topPanel, BorderLayout.NORTH);
    //pause button
    JButton Pause_btn = new JButton("Start");

    // populate the drop-down box
    SerialPort[] portNames;
    portNames = SerialPort.getCommPorts();
    //check for new port available
    Thread thread_port = new Thread() {
        @Override
        public void run() {
            while (true) {
                SerialPort[] sp = SerialPort.getCommPorts();
                if (sp.length > 0) {
                    for (SerialPort sp_name : sp) {
                        int l = portList_combobox.getItemCount(), i;
                        for (i = 0; i < l; i++) {
                            //check port name already exist or not
                            if (sp_name.getSystemPortName().equalsIgnoreCase(portList_combobox.getItemAt(i))) {
                                break;
                            }
                        }
                        if (i == l) {
                            portList_combobox.addItem(sp_name.getSystemPortName());
                        }

                    }

                } else {
                    portList_combobox.removeAllItems();

                }
                portList_combobox.repaint();

            }

        }

    };
    thread_port.start();
    for (SerialPort sp_name : portNames)
        portList_combobox.addItem(sp_name.getSystemPortName());

    //for(int i = 0; i < portNames.length; i++)
    //   portList.addItem(portNames[i].getSystemPortName());

    // create the line graph
    XYSeries series = new XYSeries("line 1");
    XYSeries series2 = new XYSeries("line 2");
    XYSeries series3 = new XYSeries("line 3");
    XYSeries series4 = new XYSeries("line 4");
    for (int i = 0; i < 100; i++) {
        series.add(x, 0);
        series2.add(x, 0);
        series3.add(x, 0);
        series4.add(x, 10);
        x++;
    }

    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series);
    dataset.addSeries(series2);
    XYSeriesCollection dataset2 = new XYSeriesCollection();
    dataset2.addSeries(series3);
    dataset2.addSeries(series4);

    //create jfree chart
    JFreeChart chart = ChartFactory.createXYLineChart("Sensor Readings", "Time (seconds)", "Arduino Reading",
            dataset);
    JFreeChart chart2 = ChartFactory.createXYLineChart("Sensor Readings", "Time (seconds)", "Arduino Reading 2",
            dataset2);

    //color render for chart 1
    XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
    r1.setSeriesPaint(0, Color.RED);
    r1.setSeriesPaint(1, Color.GREEN);
    r1.setSeriesShapesVisible(0, false);
    r1.setSeriesShapesVisible(1, false);

    XYPlot plot = chart.getXYPlot();
    plot.setRenderer(0, r1);
    plot.setRenderer(1, r1);

    plot.setBackgroundPaint(Color.WHITE);
    plot.setDomainGridlinePaint(Color.DARK_GRAY);
    plot.setRangeGridlinePaint(Color.blue);

    //color render for chart 2
    XYLineAndShapeRenderer r2 = new XYLineAndShapeRenderer();
    r2.setSeriesPaint(0, Color.BLUE);
    r2.setSeriesPaint(1, Color.ORANGE);
    r2.setSeriesShapesVisible(0, false);
    r2.setSeriesShapesVisible(1, false);

    XYPlot plot2 = chart2.getXYPlot();
    plot2.setRenderer(0, r2);
    plot2.setRenderer(1, r2);

    ChartPanel cp = new ChartPanel(chart);
    ChartPanel cp2 = new ChartPanel(chart2);

    //multiple graph container
    JPanel graph_container = new JPanel();
    graph_container.setLayout(new BoxLayout(graph_container, BoxLayout.X_AXIS));
    graph_container.add(cp);
    graph_container.add(cp2);

    //add chart panel in main window
    window.add(graph_container, BorderLayout.CENTER);
    //window.add(cp2, BorderLayout.WEST);

    window.add(Pause_btn, BorderLayout.AFTER_LAST_LINE);
    Pause_btn.setEnabled(false);
    //pause btn action
    Pause_btn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (Pause_btn.getText().equalsIgnoreCase("Pause")) {

                if (chosenPort.isOpen()) {
                    try {
                        Output.write(0);
                    } catch (IOException ex) {
                        Logger.getLogger(Graph_with_jframe_and_arduino.class.getName()).log(Level.SEVERE, null,
                                ex);
                    }
                }

                Pause_btn.setText("Start");
            } else {
                if (chosenPort.isOpen()) {
                    try {
                        Output.write(1);
                    } catch (IOException ex) {
                        Logger.getLogger(Graph_with_jframe_and_arduino.class.getName()).log(Level.SEVERE, null,
                                ex);
                    }
                }

                Pause_btn.setText("Pause");
            }
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    });

    // configure the connect button and use another thread to listen for data
    connectButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            if (connectButton.getText().equals("Connect")) {
                // attempt to connect to the serial port
                chosenPort = SerialPort.getCommPort(portList_combobox.getSelectedItem().toString());
                chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
                if (chosenPort.openPort()) {
                    Output = chosenPort.getOutputStream();
                    connectButton.setText("Disconnect");
                    Pause_btn.setEnabled(true);
                    portList_combobox.setEnabled(false);
                }

                // create a new thread that listens for incoming text and populates the graph
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        Scanner scanner = new Scanner(chosenPort.getInputStream());
                        while (scanner.hasNextLine()) {
                            try {
                                String line = scanner.nextLine();
                                int number = Integer.parseInt(line);
                                series.add(x, number);
                                series2.add(x, number / 2);
                                series3.add(x, number / 1.5);
                                series4.add(x, number / 5);

                                if (x > 100) {
                                    series.remove(0);
                                    series2.remove(0);
                                    series3.remove(0);
                                    series4.remove(0);
                                }

                                x++;
                                window.repaint();
                            } catch (Exception e) {
                            }
                        }
                        scanner.close();
                    }
                };
                thread.start();
            } else {
                // disconnect from the serial port
                chosenPort.closePort();
                portList_combobox.setEnabled(true);
                Pause_btn.setEnabled(false);
                connectButton.setText("Connect");

            }
        }
    });

    // show the window
    window.setVisible(true);
}

From source file:statistic.graph.gui.Charts.java

private static void initXAxis(XYPlot plot, XYDataset dataset) {
    plot.setDomainAxis(new NumberAxis(plot.getDomainAxis().getLabel()));
    XYSeriesCollection collection = (XYSeriesCollection) dataset;
    double max = Double.NEGATIVE_INFINITY;
    double min = Double.POSITIVE_INFINITY;
    if (collection != null) {
        for (int s = 0; s < collection.getSeriesCount(); s++) {
            for (int d = 0; d < collection.getItemCount(s); d++) {
                XYDataItem data = collection.getSeries(s).getDataItem(d);
                if (data.getX().longValue() == Integer.MAX_VALUE
                        || data.getX().longValue() == Integer.MIN_VALUE) {
                    continue;
                }//from  ww w . j  av a 2s  . c  o m
                if (data.getX().doubleValue() > max) {
                    max = data.getX().doubleValue();
                }
                if (data.getX().doubleValue() < min) {
                    min = data.getX().doubleValue();
                }
            }
        }
        if (min < max) {
            plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            plot.getDomainAxis().setRange(min - 0.5, max + 0.5);
            for (int s = 0; s < collection.getSeriesCount(); s++) {
                XYSeries series = collection.getSeries(s);
                if (series.indexOf(Integer.MIN_VALUE) >= 0) {
                    XYDataItem item = series.remove((Number) Integer.MIN_VALUE);
                    if (series.indexOf(min) < 0) {
                        series.add(min, item.getY());
                    }
                }
                if (series.indexOf(Integer.MAX_VALUE) >= 0) {
                    XYDataItem item = series.remove((Number) Integer.MAX_VALUE);
                    if (series.indexOf(max) < 0) {
                        series.add(max, item.getY());
                    }
                }
            }
        } else {
            plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            plot.getDomainAxis().setRange(0 - 0.5, 1 + 0.5);
        }
    } else {
        plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        plot.getDomainAxis().setRange(0 - 0.5, 1 + 0.5);
    }
}

From source file:de.fhffm.jad.demo.view.LineChartPanel.java

/**
 * Add a value to one of the lines/*from w  w  w .  ja va2 s .com*/
 * @param y
 * @param series
 */
public void addValue(double y, int series) {
    //Grab the line to add a value
    XYSeries current = null;
    if (series == 1) {
        current = series1;
    } else if (series == 2) {
        current = series2;
    } else {
        return;
    }
    current.remove(0);
    current.add(x, y);
}

From source file:umontreal.iro.lecuyer.charts.EmpiricalChart.java

private void fixZeroPoint() {
    // reset the first point (x0, 0) with x0 at the beginning of x-axis
    double xmin = Math.min(XAxis.getAxis().getRange().getLowerBound(), XAxis.getTwinAxisPosition());
    XYSeriesCollection col = (XYSeriesCollection) dataset.getSeriesCollection();
    for (int i = 0; i < col.getSeriesCount(); i++) {
        XYSeries ser = col.getSeries(i);
        ser.remove(0); // remove temporary 0-point
        ser.add(xmin, 0); // replace
    }/*w w  w  .j  a  va2 s  .  c om*/
}

From source file:com.anrisoftware.prefdialog.miscswing.multichart.freechart.FreechartXYChart.java

private void updateDeletedData(int row0, int row1, int offset) {
    XYSeriesCollection series = getCategory();
    for (int col = 0; col < series.getSeriesCount(); col++) {
        XYSeries xyseries = series.getSeries(col);
        for (int row = row1; row >= row0; row--) {
            xyseries.remove(row);
        }/* ww w.j av  a2  s.  c o m*/
    }
}

From source file:com.jbombardier.console.charts.XYTimeChartPanel.java

@SuppressWarnings("unchecked")
public void removeOldDataPoints() {
    List<XYSeries> series = xyseriescollection.getSeries();
    for (XYSeries xySeries : series) {
        while (xySeries.getItemCount() > 0) {
            XYDataItem xyDataItem = xySeries.getDataItem(0);
            long itemTime = xyDataItem.getX().longValue();
            if (mostRecentTimeValue - timePeriod > itemTime) {
                xySeries.remove(0);
            } else {
                // Fast exit, the items will be in time order
                break;
            }//from   w  ww.j av  a 2  s  . com
        }
    }
}

From source file:org.jfree.data.xy.DefaultTableXYDataset.java

/**
 * Removes the items from all series for a given x value.
 *
 * @param x  the x-value./*from   w w  w .ja va  2 s . c o  m*/
 */
public void removeAllValuesForX(Number x) {
    ParamChecks.nullNotPermitted(x, "x");
    boolean savedState = this.propagateEvents;
    this.propagateEvents = false;
    for (int s = 0; s < this.data.size(); s++) {
        XYSeries series = (XYSeries) this.data.get(s);
        series.remove(x);
    }
    this.propagateEvents = savedState;
    this.xPoints.remove(x);
    fireDatasetChanged();
}

From source file:org.jfree.data.xy.XYSeriesTest.java

/**
 * Simple test for the remove() method.//  ww  w . j av  a2s .  c o m
 */
@Test
public void testRemove() {
    XYSeries s1 = new XYSeries("Series 1");
    s1.add(1.0, 1.0);
    s1.add(2.0, 2.0);
    s1.add(3.0, 3.0);
    assertEquals(3, s1.getItemCount());

    s1.remove(new Double(2.0));
    assertEquals(new Double(3.0), s1.getX(1));

    s1.remove(0);
    assertEquals(new Double(3.0), s1.getX(0));
}

From source file:com.unicornlabs.kabouter.gui.power.PowerPanel.java

/**
 *
 * @param newLog//from   www. j a  v  a2s .  c om
 */
public void handleNewPowerLog(Powerlog newLog) {
    //If the new log is from the currently focussed device
    XYSeries focussedSeries = myDataSeriesMap.get(newLog.getId().getDeviceid());

    //If we are focussed on this device, the returned series won't be null
    if (focussedSeries != null) {

        //Add the new datapoint
        focussedSeries.add(newLog.getId().getLogtime().getTime(), newLog.getPower());

        //While we have too many data points for a live graph
        while ((long) (newLog.getId().getLogtime().getTime() - focussedSeries.getMinX()) > MAX_DATA_POINTS_LIVE
                * 1000) {
            //Remove the first data point
            focussedSeries.remove(0);
        }

    }
}

From source file:org.jfree.data.xy.XYSeriesTest.java

/**
 * Some checks for the remove(int) method.
 *///from  w  w  w  .ja  v a  2  s  . c  o m
@Test
public void testRemove2() {
    XYSeries s1 = new XYSeries("S1");
    s1.add(1.0, 1.1);
    s1.add(2.0, 2.2);
    s1.add(3.0, 3.3);
    s1.add(4.0, 4.4);
    s1.add(5.0, 5.5);
    s1.add(6.0, 6.6);
    assertEquals(6, s1.getItemCount());
    assertEquals(1.0, s1.getMinX(), EPSILON);
    assertEquals(6.0, s1.getMaxX(), EPSILON);
    assertEquals(1.1, s1.getMinY(), EPSILON);
    assertEquals(6.6, s1.getMaxY(), EPSILON);

    s1.remove(5);
    assertEquals(5, s1.getItemCount());
    assertEquals(1.0, s1.getMinX(), EPSILON);
    assertEquals(5.0, s1.getMaxX(), EPSILON);
    assertEquals(1.1, s1.getMinY(), EPSILON);
    assertEquals(5.5, s1.getMaxY(), EPSILON);
}