Search in sources :

Example 21 with XYSeries

use of org.jfree.data.xy.XYSeries in project gephi by gephi.

the class ChartsUtils method regress.

/********Private methods*********/
/**
     * Calculates linear regression points from a XYSeriesCollection data set
     * Code obtained from http://pwnt.be/2009/08/17/simple-linear-regression-with-jfreechart
     */
private static XYDataset regress(XYSeriesCollection data) {
    // Determine bounds
    double xMin = Double.MAX_VALUE, xMax = 0;
    for (int i = 0; i < data.getSeriesCount(); i++) {
        XYSeries ser = data.getSeries(i);
        for (int j = 0; j < ser.getItemCount(); j++) {
            double x = ser.getX(j).doubleValue();
            if (x < xMin) {
                xMin = x;
            }
            if (x > xMax) {
                xMax = x;
            }
        }
    }
    // Create 2-point series for each of the original series
    XYSeriesCollection coll = new XYSeriesCollection();
    for (int i = 0; i < data.getSeriesCount(); i++) {
        XYSeries ser = data.getSeries(i);
        int n = ser.getItemCount();
        double sx = 0, sy = 0, sxx = 0, sxy = 0, syy = 0;
        for (int j = 0; j < n; j++) {
            double x = ser.getX(j).doubleValue();
            double y = ser.getY(j).doubleValue();
            sx += x;
            sy += y;
            sxx += x * x;
            sxy += x * y;
            syy += y * y;
        }
        double b = (n * sxy - sx * sy) / (n * sxx - sx * sx);
        double a = sy / n - b * sx / n;
        XYSeries regr = new XYSeries(ser.getKey());
        regr.add(xMin, a + b * xMin);
        regr.add(xMax, a + b * xMax);
        coll.addSeries(regr);
    }
    return coll;
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) XYSeriesCollection(org.jfree.data.xy.XYSeriesCollection)

Example 22 with XYSeries

use of org.jfree.data.xy.XYSeries in project openblocks by mikaelhg.

the class CLineGraph method getCSV.

public String getCSV() {
    lock = true;
    int max = 0;
    List<List<String>> columns = new ArrayList<List<String>>();
    for (int i = 0; i < chartData.getSeriesNum(); i++) {
        if (i == 0) {
            //{name, v1, v2, v3}
            List<String> time = new ArrayList<String>();
            XYSeries s = ((XYSeriesCollection) chart.getXYPlot().getDataset()).getSeries(i);
            max = Math.max(max, s.getItemCount());
            time.add("TIME,");
            for (Object o : s.getItems()) {
                XYDataItem item = (XYDataItem) o;
                time.add(item.getX() + ",");
            }
            columns.add(time);
        }
        //{name, v1, v2, v3}
        List<String> values = new ArrayList<String>();
        XYSeries s = ((XYSeriesCollection) chart.getXYPlot().getDataset()).getSeries(i);
        max = Math.max(max, s.getItemCount());
        values.add(s.getKey().toString() + ",");
        for (Object o : s.getItems()) {
            XYDataItem item = (XYDataItem) o;
            values.add(item.getY() + ",");
        }
        columns.add(values);
    }
    StringBuilder output = new StringBuilder();
    for (int i = 0; i < max; i++) {
        for (int j = 0; j < columns.size(); j++) {
            if (i < columns.get(j).size()) {
                output.append(columns.get(j).get(i));
            } else {
                output.append(",");
            }
        }
        output.append("\n");
    }
    lock = false;
    return output.toString();
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) XYSeriesCollection(org.jfree.data.xy.XYSeriesCollection) XYDataItem(org.jfree.data.xy.XYDataItem)

Example 23 with XYSeries

use of org.jfree.data.xy.XYSeries in project openblocks by mikaelhg.

the class CLineGraph method clearValues.

/**
     * Clear the graph starting from the startTime.
     * @param startTime an x-value on the graph
     */
public void clearValues(int index, double startTime) {
    if (!lock) {
        XYSeries s = ((XYSeriesCollection) chart.getXYPlot().getDataset()).getSeries(index);
        int i = s.indexOf(startTime);
        if (i >= 0) {
            int total = s.getItemCount();
            for (; i < total; total--) {
                s.remove(i);
            }
        }
    }
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) XYSeriesCollection(org.jfree.data.xy.XYSeriesCollection)

Example 24 with XYSeries

use of org.jfree.data.xy.XYSeries in project openblocks by mikaelhg.

the class CLineGraph method updateValues.

/**
     * Updates the values for the specified seriesName with the given values
     * @param seriesName the name a series in the graph
     * @param index the index of the desired series to update
     * @param time the time at which to update
     * @param value the value to update
     */
public void updateValues(String seriesName, int index, double time, double value) {
    if (!lock) {
        XYSeries s = ((XYSeriesCollection) chart.getXYPlot().getDataset()).getSeries(index);
        s.setKey(seriesName);
        s.addOrUpdate(time, value);
    }
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) XYSeriesCollection(org.jfree.data.xy.XYSeriesCollection)

Example 25 with XYSeries

use of org.jfree.data.xy.XYSeries in project openblocks by mikaelhg.

the class CLineGraph method updateSeriesNameAt.

/**
     * Updates the series name at the specified index
     * @param seriesName the new seriesName to set at the specified index
     * @param index the desired index to set the new seriesName to
     */
public void updateSeriesNameAt(String seriesName, int index) {
    if (!lock) {
        XYSeries s = ((XYSeriesCollection) chart.getXYPlot().getDataset()).getSeries(index);
        s.setKey(seriesName);
    }
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) XYSeriesCollection(org.jfree.data.xy.XYSeriesCollection)

Aggregations

XYSeries (org.jfree.data.xy.XYSeries)25 XYSeriesCollection (org.jfree.data.xy.XYSeriesCollection)24 JFreeChart (org.jfree.chart.JFreeChart)17 DecimalFormat (java.text.DecimalFormat)11 NumberFormat (java.text.NumberFormat)11 HashMap (java.util.HashMap)6 Map (java.util.Map)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Node (org.gephi.graph.api.Node)1 XYPlot (org.jfree.chart.plot.XYPlot)1 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)1 XYSplineRenderer (org.jfree.chart.renderer.xy.XYSplineRenderer)1 XYDataItem (org.jfree.data.xy.XYDataItem)1