use of org.jfree.data.time.TimeSeriesCollection in project SIMVA-SoS by SESoS.
the class SWTMultipleAxisDemo1 method createDataset.
/**
* Creates a sample dataset.
*
* @param name the dataset name.
* @param base the starting value.
* @param start the starting period.
* @param count the number of values to generate.
*
* @return The dataset.
*/
private static XYDataset createDataset(String name, double base, RegularTimePeriod start, int count) {
TimeSeries series = new TimeSeries(name);
RegularTimePeriod period = start;
double value = base;
for (int i = 0; i < count; i++) {
series.add(period, value);
period = period.next();
value = value * (1 + (Math.random() - 0.495) / 10.0);
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series);
return dataset;
}
use of org.jfree.data.time.TimeSeriesCollection in project SIMVA-SoS by SESoS.
the class SWTTimeSeriesDemo method createDataset.
/**
* Creates a dataset, consisting of two series of monthly data.
*
* @return The dataset.
*/
private static XYDataset createDataset() {
TimeSeries s1 = new TimeSeries("L&G European Index Trust");
s1.add(new Month(2, 2001), 181.8);
s1.add(new Month(3, 2001), 167.3);
s1.add(new Month(4, 2001), 153.8);
s1.add(new Month(5, 2001), 167.6);
s1.add(new Month(6, 2001), 158.8);
s1.add(new Month(7, 2001), 148.3);
s1.add(new Month(8, 2001), 153.9);
s1.add(new Month(9, 2001), 142.7);
s1.add(new Month(10, 2001), 123.2);
s1.add(new Month(11, 2001), 131.8);
s1.add(new Month(12, 2001), 139.6);
s1.add(new Month(1, 2002), 142.9);
s1.add(new Month(2, 2002), 138.7);
s1.add(new Month(3, 2002), 137.3);
s1.add(new Month(4, 2002), 143.9);
s1.add(new Month(5, 2002), 139.8);
s1.add(new Month(6, 2002), 137.0);
s1.add(new Month(7, 2002), 132.8);
TimeSeries s2 = new TimeSeries("L&G UK Index Trust");
s2.add(new Month(2, 2001), 129.6);
s2.add(new Month(3, 2001), 123.2);
s2.add(new Month(4, 2001), 117.2);
s2.add(new Month(5, 2001), 124.1);
s2.add(new Month(6, 2001), 122.6);
s2.add(new Month(7, 2001), 119.2);
s2.add(new Month(8, 2001), 116.5);
s2.add(new Month(9, 2001), 112.7);
s2.add(new Month(10, 2001), 101.5);
s2.add(new Month(11, 2001), 106.1);
s2.add(new Month(12, 2001), 110.3);
s2.add(new Month(1, 2002), 111.7);
s2.add(new Month(2, 2002), 111.0);
s2.add(new Month(3, 2002), 109.6);
s2.add(new Month(4, 2002), 113.2);
s2.add(new Month(5, 2002), 111.6);
s2.add(new Month(6, 2002), 108.8);
s2.add(new Month(7, 2002), 101.6);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);
return dataset;
}
use of org.jfree.data.time.TimeSeriesCollection in project SIMVA-SoS by SESoS.
the class JFreeChartTest method testSerialization4.
/**
* Serialize a time seroes chart, restore it, and check for equality.
*/
@Test
public void testSerialization4() {
RegularTimePeriod t = new Day();
TimeSeries series = new TimeSeries("Series 1");
series.add(t, 36.4);
t = t.next();
series.add(t, 63.5);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series);
JFreeChart c1 = ChartFactory.createTimeSeriesChart("Test", "Date", "Value", dataset);
JFreeChart c2 = (JFreeChart) TestUtilities.serialised(c1);
assertEquals(c1, c2);
}
use of org.jfree.data.time.TimeSeriesCollection in project zm-mailbox by Zimbra.
the class ChartUtil method createJFReeChart.
private List<JFreeChart> createJFReeChart(ChartSettings cs) {
double minValue = Double.MAX_VALUE;
double maxValue = Double.MIN_VALUE;
double d = 0;
double count = 0;
double total = 0;
TimeSeriesCollection data = new TimeSeriesCollection();
ArrayList<ChartSettings> syntheticSettings = new ArrayList<ChartSettings>();
for (GroupPlotSettings gps : cs.getGroupPlots()) {
String groupBy = gps.getGroupBy();
DataColumn dc = new DataColumn(gps.getInfile(), groupBy);
StringSeries groupBySeries = mStringSeries.get(dc);
dc = new DataColumn(gps.getInfile(), gps.getDataColumn());
DataSeries ds = mDataSeries.get(dc);
int idx = 0;
Map<String, List<Integer>> groups = new HashMap<String, List<Integer>>();
for (StringEntry e : groupBySeries.dataCollection) {
String g = e.getVal();
List<Integer> indices = groups.get(g);
if (indices == null) {
indices = new ArrayList<Integer>();
groups.put(g, indices);
}
indices.add(idx);
idx++;
}
for (Map.Entry<String, List<Integer>> g : groups.entrySet()) {
String groupByValue = g.getKey();
if (gps.getIgnoreSet().contains(groupByValue))
continue;
List<Integer> indices = g.getValue();
DataSeries syntheticDS = new DataSeries();
DataColumn c = new DataColumn(gps.getInfile(), GROUP_PLOT_SYNTHETIC + groupByValue + ":" + gps.getDataColumn());
for (int i : indices) {
Entry e = ds.get(i);
syntheticDS.AddEntry(e.getTimestamp(), e.getVal());
}
mDataSeries.put(c, syntheticDS);
PlotSettings syntheticPlot = new PlotSettings(groupByValue, c.getInfile(), c.getColumn(), gps.getShowRaw(), gps.getShowMovingAvg(), gps.getMovingAvgPoints(), gps.getMultiplier(), gps.getDivisor(), gps.getNonNegative(), gps.getPercentTime(), gps.getDataFunction(), gps.getAggregateFunction(), gps.getOptional(), null, null);
cs.addPlot(syntheticPlot);
if (cs.getOutDocument() != null) {
ChartSettings s = new ChartSettings(String.format(cs.getTitle(), groupByValue), cs.getCategory(), String.format(cs.getOutfile(), groupByValue), cs.getXAxis(), cs.getYAxis(), cs.getAllowLogScale(), cs.getPlotZero(), cs.getWidth(), cs.getHeight(), null, cs.getTopPlots(), cs.getTopPlotsType());
s.addPlot(syntheticPlot);
syntheticSettings.add(s);
}
}
}
if (cs.getOutDocument() != null && cs.getGroupPlots().size() != 0) {
ArrayList<JFreeChart> charts = new ArrayList<JFreeChart>();
for (ChartSettings c : syntheticSettings) {
charts.addAll(createJFReeChart(c));
c.setOutDocument(cs.getOutDocument());
}
mSyntheticChartSettings.addAll(syntheticSettings);
return charts;
}
List<PlotSettings> plots = cs.getPlots();
if (cs.getTopPlots() > 0 && plots.size() > cs.getTopPlots()) {
String aggregateFunction = cs.getTopPlotsType().name().toLowerCase();
System.out.println(String.format("Reducing %d to %d plots for chart '%s'", plots.size(), cs.getTopPlots(), cs.getTitle()));
ArrayList<PlotAggregatePair> aggregates = new ArrayList<PlotAggregatePair>();
for (PlotSettings ps : plots) {
DataColumn dc = new DataColumn(ps.getInfile(), ps.getDataColumn());
String key = ps.getInfile() + ":" + ps.getDataColumn() + ":" + ps.getAggregateFunction();
PlotDataIterator pdIter = new PlotDataIterator(ps, mDataSeries.get(dc));
double aggregate = mAggregator.compute(pdIter, aggregateFunction, mAggregateStartAt, mAggregateEndAt, key);
aggregates.add(new PlotAggregatePair(ps, aggregate));
}
Collections.sort(aggregates);
while (aggregates.size() > cs.getTopPlots()) {
PlotAggregatePair pair = aggregates.remove(0);
plots.remove(pair.ps);
}
}
for (PlotSettings ps : plots) {
String columnName = ps.getDataColumn();
if (columnName == null) {
columnName = RATIO_PLOT_SYNTHETIC + ps.getRatioTop() + "/" + ps.getRatioBottom();
String infile = ps.getInfile();
String[] top = ps.getRatioTop().split("\\+");
String[] bottom = ps.getRatioBottom().split("\\+");
DataColumn[] ratioTop = new DataColumn[top.length];
DataColumn[] ratioBottom = new DataColumn[bottom.length];
for (int i = 0, j = top.length; i < j; i++) ratioTop[i] = new DataColumn(infile, top[i]);
for (int i = 0, j = bottom.length; i < j; i++) ratioBottom[i] = new DataColumn(infile, bottom[i]);
DataSeries[] topData = new DataSeries[ratioTop.length];
DataSeries[] bottomData = new DataSeries[ratioBottom.length];
for (int i = 0, j = ratioTop.length; i < j; i++) topData[i] = mDataSeries.get(ratioTop[i]);
for (int i = 0, j = ratioBottom.length; i < j; i++) bottomData[i] = mDataSeries.get(ratioBottom[i]);
DataSeries ds = new DataSeries();
for (int i = 0, j = topData[0].size(); i < j; i++) {
double topValue = 0.0;
double bottomValue = 0.0;
double ratio = 0.0;
Entry lastEntry = null;
for (int m = 0, n = topData.length; m < n; m++) {
Entry e = topData[m].get(i);
topValue += e.getVal();
}
for (int m = 0, n = bottomData.length; m < n; m++) {
Entry e = bottomData[m].get(i);
bottomValue += e.getVal();
lastEntry = e;
}
if (bottomValue != 0.0) {
ratio = topValue / bottomValue;
}
// should never be null
assert lastEntry != null;
ds.AddEntry(lastEntry.getTimestamp(), ratio);
}
mDataSeries.put(new DataColumn(infile, columnName), ds);
ps.setDataColumn(columnName);
}
DataColumn dc = new DataColumn(ps.getInfile(), ps.getDataColumn());
DataSeries ds = mDataSeries.get(dc);
TimeSeries ts = new TimeSeries(ps.getLegend(), FixedMillisecond.class);
int numSamples = 0;
for (PlotDataIterator pdIter = new PlotDataIterator(ps, ds); pdIter.hasNext(); numSamples++) {
Pair<Date, Double> entry = pdIter.next();
Date tstamp = entry.getFirst();
double val = entry.getSecond().doubleValue();
if (val != 0 || cs.getPlotZero()) {
if (d < minValue)
minValue = val;
if (d > maxValue)
maxValue = val;
count++;
total += val;
try {
ts.addOrUpdate(new FixedMillisecond(tstamp), val);
} catch (SeriesException e) {
e.printStackTrace(System.out);
}
}
}
if (numSamples == 0 && ps.getOptional()) {
System.out.format("Skipping optional plot %s (no data sample found)\n\n", ps.getLegend());
continue;
}
System.out.format("Adding %d %s points to %s.\n\n", ds.size(), ps.getLegend(), cs.getOutfile());
if (ps.getShowRaw()) {
data.addSeries(ts);
}
if (ps.getShowMovingAvg()) {
int numPoints = ps.getMovingAvgPoints();
if (numPoints == PlotSettings.DEFAULT_PLOT_MOVING_AVG_POINTS) {
// Display 200 points for moving average.
// Divide the total number of points by 200 to
// determine the number of samples to average
// for each point.
numPoints = ts.getItemCount() / 200;
}
if (numPoints >= 2) {
TimeSeries ma = MovingAverage.createPointMovingAverage(ts, ps.getLegend() + " (moving avg)", numPoints);
data.addSeries(ma);
} else {
System.out.println("Not enough data to display moving average for " + ps.getLegend());
data.addSeries(ts);
}
}
}
// Create chart
boolean legend = (data.getSeriesCount() > 1);
JFreeChart chart = ChartFactory.createTimeSeriesChart(null, cs.getXAxis(), cs.getYAxis(), data, legend, false, false);
// Make Y-axis logarithmic if a spike was detected
if (cs.getAllowLogScale() && (minValue > 0) && (maxValue > 0) && (maxValue > 20 * (total / count))) {
if (maxValue / minValue > 100) {
XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis oldAxis = plot.getRangeAxis();
LogarithmicAxis newAxis = new LogarithmicAxis(oldAxis.getLabel());
plot.setRangeAxis(newAxis);
}
}
mChartMap.put(cs, chart);
return Arrays.asList(chart);
}
use of org.jfree.data.time.TimeSeriesCollection in project qpid-broker-j by apache.
the class TimeSeriesBuilderCallbackTest method testAddPointToSeries.
@Test
public void testAddPointToSeries() {
TimeSeriesHolder timeSeriesHolder = new TimeSeriesHolder();
SeriesDefinition seriesDefinition = mock(SeriesDefinition.class);
when(seriesDefinition.getSeriesLegend()).thenReturn(SERIES_LEGEND);
timeSeriesHolder.beginSeries(seriesDefinition);
timeSeriesHolder.addDataPointToSeries(seriesDefinition, new SeriesRow(_dates[0], _values[0]));
timeSeriesHolder.addDataPointToSeries(seriesDefinition, new SeriesRow(_dates[1], _values[1]));
timeSeriesHolder.addDataPointToSeries(seriesDefinition, new SeriesRow(_dates[2], _values[2]));
timeSeriesHolder.endSeries(seriesDefinition);
TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) timeSeriesHolder.getPopulatedDataset();
TimeSeries actualTimeSeries = timeSeriesCollection.getSeries(SERIES_LEGEND);
for (int i = 0; i < NUMBER_OF_DATA_POINTS; i++) {
TimeSeriesDataItem dataItem0 = actualTimeSeries.getDataItem(i);
assertEquals(_dates[i].getTime(), dataItem0.getPeriod().getMiddleMillisecond());
assertEquals(_values[i], dataItem0.getValue());
}
}
Aggregations