Search in sources :

Example 46 with TimeSeries

use of org.jfree.data.time.TimeSeries 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);
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod) Day(org.jfree.data.time.Day) Test(org.junit.Test)

Example 47 with TimeSeries

use of org.jfree.data.time.TimeSeries 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);
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FixedMillisecond(org.jfree.data.time.FixedMillisecond) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ValueAxis(org.jfree.chart.axis.ValueAxis) ArrayList(java.util.ArrayList) List(java.util.List) SeriesException(org.jfree.data.general.SeriesException) JFreeChart(org.jfree.chart.JFreeChart) Date(java.util.Date) LogarithmicAxis(org.jfree.chart.axis.LogarithmicAxis) XYPlot(org.jfree.chart.plot.XYPlot) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with TimeSeries

use of org.jfree.data.time.TimeSeries in project dbeaver by dbeaver.

the class DashboardRendererBase method generateSampleSeries.

protected void generateSampleSeries(DashboardContainer container, TimeSeriesCollection dataset) {
    TimeSeries seriesSin = new TimeSeries("Sin");
    long startTime = System.currentTimeMillis() - 1000 * 60 * 60 * 2;
    for (int i = 0; i < 100; i++) {
        seriesSin.add(new TimeSeriesDataItem(new FixedMillisecond(startTime + i * 60 * 1000), Math.sin(0.1 * i) * 100));
    }
    dataset.addSeries(seriesSin);
    TimeSeries seriesCos = new TimeSeries("Cos");
    for (int i = 0; i < 100; i++) {
        seriesCos.add(new TimeSeriesDataItem(new FixedMillisecond(startTime + i * 60 * 1000), Math.cos(0.1 * i) * 100));
    }
    dataset.addSeries(seriesCos);
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) FixedMillisecond(org.jfree.data.time.FixedMillisecond) TimeSeriesDataItem(org.jfree.data.time.TimeSeriesDataItem)

Example 49 with TimeSeries

use of org.jfree.data.time.TimeSeries 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());
    }
}
Also used : SeriesDefinition(org.apache.qpid.disttest.charting.definition.SeriesDefinition) TimeSeries(org.jfree.data.time.TimeSeries) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) TimeSeriesDataItem(org.jfree.data.time.TimeSeriesDataItem) SeriesRow(org.apache.qpid.disttest.charting.seriesbuilder.SeriesRow) Test(org.junit.Test)

Example 50 with TimeSeries

use of org.jfree.data.time.TimeSeries 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)

Aggregations

TimeSeries (org.jfree.data.time.TimeSeries)55 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)39 JFreeChart (org.jfree.chart.JFreeChart)12 Date (java.util.Date)11 XYPlot (org.jfree.chart.plot.XYPlot)11 Month (org.jfree.data.time.Month)9 Day (org.jfree.data.time.Day)8 TimeSeriesDataItem (org.jfree.data.time.TimeSeriesDataItem)8 BasicStroke (java.awt.BasicStroke)7 Color (java.awt.Color)7 Map (java.util.Map)7 RegularTimePeriod (org.jfree.data.time.RegularTimePeriod)6 IOException (java.io.IOException)5 SimpleDateFormat (java.text.SimpleDateFormat)5 List (java.util.List)5 DateAxis (org.jfree.chart.axis.DateAxis)5 NumberAxis (org.jfree.chart.axis.NumberAxis)5 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)5 FixedMillisecond (org.jfree.data.time.FixedMillisecond)5 BufferedImage (java.awt.image.BufferedImage)4