List of usage examples for org.jfree.data.time MovingAverage createPointMovingAverage
public static TimeSeries createPointMovingAverage(TimeSeries source, String name, int pointCount)
From source file:com.zimbra.perf.chart.ChartUtil.java
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);/*from w ww . j a va 2 s. c o m*/ } 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); }
From source file:org.atomserver.testutils.plot.PerfDataSet.java
public XYDataset getXYDataSet(boolean normalizeTimes, List methodNamesToPlot, String methodNameSuffix, boolean computeMovingAverage, int numPointsInAvg, boolean plotTPM) { long lMinDate = 0L; if (normalizeTimes) { lMinDate = minDate.getTime();// ww w . ja va2s . c o m } TimeSeriesCollection tsc = new TimeSeriesCollection(); for (Map.Entry<String, List<PerfData>> entry : dataSet.entrySet()) { String methodName = entry.getKey(); if (methodNamesToPlot != null) { if (!methodNamesToPlot.contains(methodName)) continue; } if (methodNameSuffix != null) { methodName += " " + methodNameSuffix; } TimeSeries series = new TimeSeries(methodName, Second.class); List<PerfData> perfDataList = entry.getValue(); for (PerfData perfData : perfDataList) { Date datetime = perfData.getDateTime(); Double value = 0.0; if (plotTPM) value = perfData.getTPM(); else value = perfData.getAvgResp(); if (normalizeTimes) { long lDatetime = datetime.getTime(); long lNormalizedTime = lDatetime - lMinDate; datetime = new Date(lNormalizedTime); if (log.isTraceEnabled()) log.trace("[ " + lDatetime + ", " + lMinDate + ", " + lNormalizedTime + "] normalized time = " + datetime); } series.add(new Second(datetime), value); } if (computeMovingAverage) { TimeSeries movingAvg = MovingAverage.createPointMovingAverage(series, methodName + " moving average", numPointsInAvg); tsc.addSeries(movingAvg); } else { tsc.addSeries(series); } } return tsc; }