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;
}
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();
}
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);
}
}
}
}
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);
}
}
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);
}
}
Aggregations