use of org.jfree.data.time.TimeSeriesCollection in project MtgDesktopCompanion by nicho92.
the class HistoryPricesPanel method refresh.
private void refresh() {
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries series1 = new TimeSeries(title);
if (showAll) {
for (MTGDashBoard d : MTGControler.getInstance().getDashBoards()) {
TimeSeries series = new TimeSeries(d.getName());
Map<Date, Double> mapTime;
try {
mapTime = d.getPriceVariation(mc, me);
if (mapTime != null) {
for (Entry<Date, Double> da : mapTime.entrySet()) series.add(new Day(da.getKey()), da.getValue().doubleValue());
dataset.addSeries(series);
}
} catch (IOException e) {
MTGLogger.printStackTrace(e);
}
}
} else {
for (Entry<Date, Double> d : map.entrySet()) series1.add(new Day(d.getKey()), d.getValue().doubleValue());
dataset.addSeries(series1);
}
JFreeChart chart = ChartFactory.createTimeSeriesChart("Price Variation", "Date", "Price", dataset, true, true, false);
if (showEdition)
try {
for (MagicEdition edition : MTGControler.getInstance().getEnabledProviders().loadEditions()) {
Date d = new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(edition.getReleaseDate() + " 00:00");
TimeSeriesDataItem item = series1.getDataItem(new Day(d));
if (item != null) {
double x = item.getPeriod().getFirstMillisecond();
double y = item.getValue().doubleValue();
XYTextAnnotation annot = new XYTextAnnotation(edition.getId(), x, y);
annot.setToolTipText(edition.getSet());
XYPlot plot = (XYPlot) chart.getPlot();
plot.addAnnotation(annot);
}
}
} catch (Exception e) {
MTGLogger.printStackTrace(e);
}
pane.setChart(chart);
pane.addMouseWheelListener(mwe -> {
if (mwe.getWheelRotation() > 0) {
pane.zoomOutDomain(0.5, 0.5);
} else if (mwe.getWheelRotation() < 0) {
pane.zoomInDomain(1.5, 1.5);
}
});
this.add(pane, BorderLayout.CENTER);
chart.fireChartChanged();
}
use of org.jfree.data.time.TimeSeriesCollection in project ma-core-public by infiniteautomation.
the class ImageChartUtils method writeChart.
public static void writeChart(PointTimeSeriesCollection pointTimeSeriesCollection, boolean showLegend, OutputStream out, int width, int height, long from, long to) throws IOException {
JFreeChart chart = ChartFactory.createTimeSeriesChart(null, null, null, null, showLegend, false, false);
chart.setBackgroundPaint(SystemSettingsDao.getColour(SystemSettingsDao.CHART_BACKGROUND_COLOUR));
XYPlot plot = chart.getXYPlot();
((DateAxis) plot.getDomainAxis()).setTimeZone(pointTimeSeriesCollection.getTimeZone());
plot.setBackgroundPaint(SystemSettingsDao.getColour(SystemSettingsDao.PLOT_BACKGROUND_COLOUR));
Color gridlines = SystemSettingsDao.getColour(SystemSettingsDao.PLOT_GRIDLINE_COLOUR);
plot.setDomainGridlinePaint(gridlines);
plot.setRangeGridlinePaint(gridlines);
((NumberAxis) plot.getRangeAxis()).setAutoRangeStickyZero(false);
double numericMin = 0;
double numericMax = 1;
int numericSeriesCount = pointTimeSeriesCollection.getNumericSeriesCount();
if (pointTimeSeriesCollection.hasNumericData()) {
for (int i = 0; i < numericSeriesCount; i++) {
NumericTimeSeries nts = pointTimeSeriesCollection.getNumericTimeSeries(i);
AbstractXYItemRenderer renderer;
if (nts.getPlotType() == DataPointVO.PlotTypes.STEP)
renderer = new XYStepRenderer();
else if (nts.getPlotType() == DataPointVO.PlotTypes.LINE)
renderer = new XYLineAndShapeRenderer(true, false);
else {
// XYCardinalSplineRenderer spline = new XYCardinalSplineRenderer(.5d, 16);
// XYSmoothLineAndShapeRenderer spline = new XYSmoothLineAndShapeRenderer();
// XYSplineRenderer spline = new XYSplineRenderer();
// spline.setBaseShapesVisible(false);
// renderer = spline;
renderer = new XYLineAndShapeRenderer(true, false);
}
if (nts.getPaint() != null)
renderer.setSeriesPaint(0, nts.getPaint(), false);
if (nts.getStroke() != null)
renderer.setSeriesStroke(0, nts.getStroke(), false);
plot.setDataset(i, new TimeSeriesCollection(nts.getTimeSeries()));
plot.setRenderer(i, renderer);
}
numericMin = plot.getRangeAxis().getLowerBound();
numericMax = plot.getRangeAxis().getUpperBound();
if (!pointTimeSeriesCollection.hasMultiplePoints()) {
// If this chart displays a single point, check if there should be a range description.
TimeSeries timeSeries = pointTimeSeriesCollection.getNumericTimeSeries(0).getTimeSeries();
String desc = timeSeries.getRangeDescription();
if (!StringUtils.isBlank(desc)) {
// Replace any HTML entities with Java equivalents
desc = StripEntities.stripHTMLEntities(desc, ' ');
plot.getRangeAxis().setLabel(desc);
}
}
} else
plot.getRangeAxis().setVisible(false);
if (pointTimeSeriesCollection.getRangeMarkers() != null) {
boolean rangeAdjusted = false;
for (Marker marker : pointTimeSeriesCollection.getRangeMarkers()) {
plot.addRangeMarker(marker);
if (marker instanceof ValueMarker) {
ValueMarker vm = (ValueMarker) marker;
if (numericMin > vm.getValue()) {
numericMin = vm.getValue();
rangeAdjusted = true;
}
if (numericMax < vm.getValue()) {
numericMax = vm.getValue();
rangeAdjusted = true;
}
}
}
if (rangeAdjusted) {
double adj = (numericMax - numericMin);
plot.getRangeAxis().setLowerBound(numericMin - adj * plot.getRangeAxis().getLowerMargin());
plot.getRangeAxis().setUpperBound(numericMax + adj * plot.getRangeAxis().getUpperMargin());
}
}
int discreteValueCount = pointTimeSeriesCollection.getDiscreteValueCount();
double interval = (numericMax - numericMin) / (discreteValueCount + 1);
int intervalIndex = 1;
if (pointTimeSeriesCollection.hasDiscreteData()) {
for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);
XYStepRenderer renderer = new XYStepRenderer();
TimeSeries ts = new TimeSeries(dts.getName(), null, null);
for (IValueTime vt : dts.getValueTimes()) addMillisecond(ts, vt.getTime(), numericMin + (interval * (dts.getValueIndex(vt.getValue()) + intervalIndex)));
if (dts.getPaint() != null)
renderer.setSeriesPaint(0, dts.getPaint(), false);
if (dts.getStroke() != null)
renderer.setSeriesStroke(0, dts.getStroke(), false);
plot.setDataset(numericSeriesCount + i, new TimeSeriesCollection(ts, pointTimeSeriesCollection.getTimeZone()));
plot.setRenderer(numericSeriesCount + i, renderer);
intervalIndex += dts.getDiscreteValueCount();
}
}
if (from > 0)
plot.getDomainAxis().setLowerBound(from);
if (to > 0)
plot.getDomainAxis().setUpperBound(to);
if (pointTimeSeriesCollection.hasDiscreteData()) {
// Add the value annotations.
double annoX = plot.getDomainAxis().getLowerBound();
intervalIndex = 1;
for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);
for (int j = 0; j < dts.getDiscreteValueCount(); j++) {
XYTextAnnotation anno = new XYTextAnnotation(" " + dts.getValueText(j), annoX, numericMin + (interval * (j + intervalIndex)));
if (!pointTimeSeriesCollection.hasNumericData() && intervalIndex + j == discreteValueCount)
// This prevents the top label from getting cut off
anno.setTextAnchor(TextAnchor.TOP_LEFT);
else
anno.setTextAnchor(TextAnchor.BOTTOM_LEFT);
anno.setPaint(((AbstractRenderer) plot.getRenderer(numericSeriesCount + i)).lookupSeriesPaint(0));
plot.addAnnotation(anno);
}
intervalIndex += dts.getDiscreteValueCount();
}
}
// Return the image.
ChartUtilities.writeChartAsPNG(out, chart, width, height);
}
use of org.jfree.data.time.TimeSeriesCollection in project AndrOBD by fr3ts0n.
the class ObdDataPlotter method addSeries.
/**
* add a new series to the graph
*
* @param series The new series to be added
*/
public synchronized void addSeries(TimeSeries series) {
if (oneRangePerSeries) {
// get paint for current axis/range/...
Paint currPaint = DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE[raIndex % DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE.length];
XYPlot plot = (XYPlot) chart.getPlot();
// set dataset
plot.setDataset(raIndex, new TimeSeriesCollection(series));
// ** set axis
NumberAxis axis = new NumberAxis();
axis.setTickLabelFont(legendFont);
axis.setAxisLinePaint(currPaint);
axis.setTickLabelPaint(currPaint);
axis.setTickMarkPaint(currPaint);
// ** set axis in plot
plot.setRangeAxis(raIndex, axis);
plot.setRangeAxisLocation(raIndex, raIndex % 2 == 0 ? AxisLocation.TOP_OR_LEFT : AxisLocation.BOTTOM_OR_RIGHT);
plot.mapDatasetToRangeAxis(raIndex, raIndex);
// ** create renderer
XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
renderer.setBaseToolTipGenerator(toolTipGen);
renderer.setSeriesPaint(0, currPaint);
// ** set renderer in plot
plot.setRenderer(raIndex, renderer);
raIndex++;
}
dataset.addSeries(series);
}
use of org.jfree.data.time.TimeSeriesCollection in project AndrOBD by fr3ts0n.
the class ObdDataPlotter method setGraphTime.
/**
* Setter for property graphTime.
*
* @param graphTime New value of property graphTime.
*/
@SuppressWarnings("rawtypes")
public synchronized void setGraphTime(int graphTime) {
TimeSeries currSer;
TimeSeriesCollection currDs;
XYPlot currPlot = (XYPlot) chart.getPlot();
this.graphTime = graphTime;
// lop through all datasets
for (int i = currPlot.getDatasetCount(); i >= 0; --i) {
currDs = (TimeSeriesCollection) currPlot.getDataset(i);
// Update all series within dataset
Iterator it = currDs.getSeries().iterator();
while (it.hasNext()) {
currSer = (TimeSeries) it.next();
currSer.setMaximumItemAge(graphTime);
}
}
}
use of org.jfree.data.time.TimeSeriesCollection in project xwiki-platform by xwiki.
the class TimeSeriesCollectionFactory method create.
public XYDataset create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {
String dataSeries = params.getString(ChartParams.SERIES);
TimeSeriesCollection dataset = new TimeSeriesCollection();
Class timePeriodClass = params.getClass(ChartParams.TIME_PERIOD_CLASS);
if (timePeriodClass == null) {
timePeriodClass = Day.class;
}
DateFormat format = params.getDateFormat(ChartParams.DATE_FORMAT);
if (format == null) {
format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
}
if (dataSeries.equals("columns")) {
if (!dataSource.hasHeaderColumn()) {
throw new GenerateException("Header column required");
}
for (int column = 0; column < dataSource.getColumnCount(); column++) {
String seriesName;
if (dataSource.hasHeaderRow()) {
seriesName = dataSource.getHeaderRowValue(column);
} else {
seriesName = "Series " + (column + 1);
}
TimeSeries series = new TimeSeries(seriesName, timePeriodClass);
for (int row = 0; row < dataSource.getRowCount(); row++) {
RegularTimePeriod period;
try {
Date date = format.parse(dataSource.getHeaderColumnValue(row));
Constructor ctor = timePeriodClass.getConstructor(new Class[] { Date.class });
period = (RegularTimePeriod) ctor.newInstance(new Object[] { date });
} catch (Exception e) {
throw new GenerateException(e);
}
series.add(period, dataSource.getCell(row, column));
}
dataset.addSeries(series);
}
} else if (dataSeries.equals("rows")) {
if (!dataSource.hasHeaderRow()) {
throw new GenerateException("Header row required");
}
for (int row = 0; row < dataSource.getRowCount(); row++) {
String seriesName;
if (dataSource.hasHeaderColumn()) {
seriesName = dataSource.getHeaderColumnValue(row);
} else {
seriesName = "Series " + (row + 1);
}
TimeSeries series = new TimeSeries(seriesName, timePeriodClass);
for (int column = 0; column < dataSource.getColumnCount(); column++) {
RegularTimePeriod period;
try {
Date date = format.parse(dataSource.getHeaderRowValue(column));
Constructor ctor = timePeriodClass.getConstructor(new Class[] { Date.class });
period = (RegularTimePeriod) ctor.newInstance(new Object[] { date });
} catch (Exception e) {
throw new GenerateException(e);
}
series.add(period, dataSource.getCell(row, column));
}
dataset.addSeries(series);
}
} else {
throw new GenerateException("Invalid series parameter:" + dataSeries);
}
return dataset;
}
Aggregations