Search in sources :

Example 61 with TimeSeriesCollection

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();
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) MTGDashBoard(org.magic.api.interfaces.MTGDashBoard) MagicEdition(org.magic.api.beans.MagicEdition) TimeSeriesDataItem(org.jfree.data.time.TimeSeriesDataItem) IOException(java.io.IOException) Date(java.util.Date) JFreeChart(org.jfree.chart.JFreeChart) IOException(java.io.IOException) XYTextAnnotation(org.jfree.chart.annotations.XYTextAnnotation) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) Day(org.jfree.data.time.Day) SimpleDateFormat(java.text.SimpleDateFormat)

Example 62 with TimeSeriesCollection

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);
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) NumberAxis(org.jfree.chart.axis.NumberAxis) TimeSeries(org.jfree.data.time.TimeSeries) Color(java.awt.Color) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) IValueTime(com.serotonin.m2m2.view.stats.IValueTime) ValueMarker(org.jfree.chart.plot.ValueMarker) Marker(org.jfree.chart.plot.Marker) JFreeChart(org.jfree.chart.JFreeChart) XYTextAnnotation(org.jfree.chart.annotations.XYTextAnnotation) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) XYStepRenderer(org.jfree.chart.renderer.xy.XYStepRenderer) ValueMarker(org.jfree.chart.plot.ValueMarker)

Example 63 with TimeSeriesCollection

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);
}
Also used : NumberAxis(org.jfree.chart.axis.NumberAxis) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) Paint(java.awt.Paint) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer)

Example 64 with TimeSeriesCollection

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);
        }
    }
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) Iterator(java.util.Iterator) Paint(java.awt.Paint)

Example 65 with TimeSeriesCollection

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;
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) Constructor(java.lang.reflect.Constructor) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException) Date(java.util.Date) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)90 TimeSeries (org.jfree.data.time.TimeSeries)66 JFreeChart (org.jfree.chart.JFreeChart)35 XYPlot (org.jfree.chart.plot.XYPlot)25 Date (java.util.Date)23 Day (org.jfree.data.time.Day)20 DateAxis (org.jfree.chart.axis.DateAxis)17 Month (org.jfree.data.time.Month)14 SimpleDateFormat (java.text.SimpleDateFormat)13 NumberAxis (org.jfree.chart.axis.NumberAxis)12 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)12 RegularTimePeriod (org.jfree.data.time.RegularTimePeriod)11 Map (java.util.Map)9 Color (java.awt.Color)7 Font (java.awt.Font)7 File (java.io.File)7 List (java.util.List)7 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)7 LocalDate (java.time.LocalDate)6 BasicStroke (java.awt.BasicStroke)5