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

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

Introduction

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

Prototype

public XYDataItem addOrUpdate(Number x, Number y) 

Source Link

Document

Adds or updates an item in the series and sends a SeriesChangeEvent to all registered listeners.

Usage

From source file:msi.gama.outputs.layers.ChartLayerStatement.java

/**
 * @throws GamaRuntimeException/*from ww  w .j av a  2s  .c  om*/
 * @param scope
 * @param cycle
 */
private void computeSeries(final IScope scope, final long cycle) throws GamaRuntimeException {
    if (datas.isEmpty()) {
        return;
    }
    List x = new ArrayList();
    Object obj = datas.get(0).getValue(scope);
    if (type == SERIES_CHART && scope.getAgentScope() instanceof BatchAgent) {
        // if (BatchAgent.class.isAssignableFrom(scope.getClass()))
        obj = ((BatchAgent) scope.getAgentScope()).getRunNumber();
    }

    boolean cumulative = false;
    if (obj instanceof GamaList) {
        x = (GamaList) obj;
    } else {
        x.add(obj);
    }
    for (int i = 0; i < x.size(); i++) {
        history.append(x.get(i));
        history.append(',');
    }
    if (!(type == SERIES_CHART & datas.size() < 2)) {
        for (int i = 0; i < datas.size(); i++) {
            if (!datasfromlists.contains(datas.get(i))) {
                if (type == SERIES_CHART & i == 0) {
                    i++;
                }
                XYPlot plot = (XYPlot) chart.getPlot();
                // DefaultTableXYDataset data = (DefaultTableXYDataset) plot.getDataset(i);
                XYDataset data = plot.getDataset(i);
                XYSeries serie = new XYSeries(0, false, false);
                if (type == SERIES_CHART || type == XY_CHART) {
                    serie = ((DefaultTableXYDataset) data).getSeries(0);
                }
                if (type == SCATTER_CHART) {
                    serie = ((XYSeriesCollection) data).getSeries(0);
                }
                List n = new ArrayList();
                Object o = datas.get(i).getValue(scope);
                if (o instanceof GamaList) {
                    n = (GamaList) o;
                } else {
                    cumulative = true;
                    n.add(o);
                }
                if (!cumulative) {
                    if (type == SERIES_CHART || type == XY_CHART) {
                        final XYSeries nserie = new XYSeries(serie.getKey(), false, false);
                        ((DefaultTableXYDataset) data).removeSeries(0);
                        ((DefaultTableXYDataset) data).addSeries(nserie);
                        serie = nserie;
                        // serie.clear();
                    }
                    if (type == SCATTER_CHART) {
                        final XYSeries nserie = new XYSeries(serie.getKey(), false, true);
                        ((XYSeriesCollection) data).removeSeries(0);
                        ((XYSeriesCollection) data).addSeries(nserie);
                        serie = nserie;
                        // serie.clear();
                    }

                }
                // java.lang.System.out.println("gr"+n);
                for (int j = 0; j < n.size(); j++) {
                    if (type == SERIES_CHART) {
                        double d2 = Cast.asFloat(scope, n.get(j));
                        double d1;
                        if (cumulative) {
                            d1 = Cast.asFloat(scope, x.get(j));
                        } else {
                            d1 = Cast.asFloat(scope, j);
                        }
                        serie.addOrUpdate(d1, d2);
                    } else if (type == XY_CHART || type == SCATTER_CHART) {
                        try {
                            IList<Double> list = GamaListType.staticCast(scope, n.get(j), Types.FLOAT, false);
                            double d1 = list.get(0);
                            double d2 = list.get(1);
                            if (cumulative) {
                                serie.addOrUpdate(d1, d2);
                            } else {
                                serie.addOrUpdate(d1, d2);

                            }
                        } catch (IndexOutOfBoundsException e) {
                            // GamaRuntimeException g = GamaRuntimeException.create(e,scope);
                            // g.addContext("each point value should be a gama-point or a 2-float list, value here: "+(Cast.asList(scope, n.get(j))));
                            GamaRuntimeException g = GamaRuntimeException.error(
                                    "each point value should be a gama-point or a 2-float list, value here: "
                                            + Cast.asList(scope, n.get(j)),
                                    scope);
                            GAMA.reportAndThrowIfNeeded(scope, g, true);
                            // TODO Auto-generated catch block
                        }
                    }
                    history.append(n.get(j));
                    history.append(',');
                }
            }
        }
    }
    history.deleteCharAt(history.length() - 1);
    history.append(Strings.LN);

}