Java Code Examples for org.jfree.data.time.TimeSeriesCollection#addSeries()
The following examples show how to use
org.jfree.data.time.TimeSeriesCollection#addSeries() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultipleAxisChart.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void addSeries(String yaxisName, TimeSeries series) { NumberAxis axis2 = new NumberAxis(yaxisName); axis2.setAutoRangeIncludesZero(false); XYPlot plot = (XYPlot) chart.getPlot(); plot.setRangeAxis(axisNum, axis2); plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT); TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(series); plot.setDataset(axisNum, dataset); plot.mapDatasetToRangeAxis(axisNum, axisNum); XYItemRenderer renderer2 = new StandardXYItemRenderer(); plot.setRenderer(axisNum, renderer2); axisNum++; }
Example 2
Source File: TimeSeriesCollectionTests.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Some tests for the indexOf() method. */ public void testIndexOf() { TimeSeries s1 = new TimeSeries("S1"); TimeSeries s2 = new TimeSeries("S2"); TimeSeriesCollection dataset = new TimeSeriesCollection(); assertEquals(-1, dataset.indexOf(s1)); assertEquals(-1, dataset.indexOf(s2)); dataset.addSeries(s1); assertEquals(0, dataset.indexOf(s1)); assertEquals(-1, dataset.indexOf(s2)); dataset.addSeries(s2); assertEquals(0, dataset.indexOf(s1)); assertEquals(1, dataset.indexOf(s2)); dataset.removeSeries(s1); assertEquals(-1, dataset.indexOf(s1)); assertEquals(0, dataset.indexOf(s2)); TimeSeries s2b = new TimeSeries("S2"); assertEquals(0, dataset.indexOf(s2b)); }
Example 3
Source File: TimeSeriesCollectionTests.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Some checks for the {@link TimeSeriesCollection#removeSeries(int)} * method. */ public void testRemoveSeries_int() { TimeSeriesCollection c1 = new TimeSeriesCollection(); TimeSeries s1 = new TimeSeries("Series 1"); TimeSeries s2 = new TimeSeries("Series 2"); TimeSeries s3 = new TimeSeries("Series 3"); TimeSeries s4 = new TimeSeries("Series 4"); c1.addSeries(s1); c1.addSeries(s2); c1.addSeries(s3); c1.addSeries(s4); c1.removeSeries(2); assertTrue(c1.getSeries(2).equals(s4)); c1.removeSeries(0); assertTrue(c1.getSeries(0).equals(s2)); assertEquals(2, c1.getSeriesCount()); }
Example 4
Source File: HouseholdsPanel.java From computational-economy with GNU General Public License v3.0 | 6 votes |
protected ChartPanel createUtilityFunctionMechanicsPanel(final Currency currency) { final TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection(); timeSeriesCollection.addSeries(ApplicationContext.getInstance().getModelRegistry() .getNationalEconomyModel(currency).householdsModel.budgetModel.getTimeSeries()); for (final ConvexFunctionTerminationCause terminationCause : ConvexFunctionTerminationCause.values()) { timeSeriesCollection.addSeries(ApplicationContext.getInstance().getModelRegistry() .getNationalEconomyModel(currency).householdsModel.convexFunctionTerminationCauseModels .get(terminationCause).getTimeSeries()); } // budget is correct here final JFreeChart chart = ChartFactory.createTimeSeriesChart("Utility Function Mechanics", "Date", "Budget Spent", timeSeriesCollection, true, true, false); configureChart(chart); return new ChartPanel(chart); }
Example 5
Source File: SWTMultipleAxisDemo1.java From SIMVA-SoS with Apache License 2.0 | 6 votes |
/** * 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; }
Example 6
Source File: TimeSeriesCollectionTests.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Tests the remove series method. */ public void testRemoveSeries() { TimeSeriesCollection c1 = new TimeSeriesCollection(); TimeSeries s1 = new TimeSeries("Series 1"); TimeSeries s2 = new TimeSeries("Series 2"); TimeSeries s3 = new TimeSeries("Series 3"); TimeSeries s4 = new TimeSeries("Series 4"); c1.addSeries(s1); c1.addSeries(s2); c1.addSeries(s3); c1.addSeries(s4); c1.removeSeries(s3); TimeSeries s = c1.getSeries(2); boolean b1 = s.equals(s4); assertTrue(b1); }
Example 7
Source File: CandlestickChart.java From ta4j-origins with MIT License | 5 votes |
/** * Builds an additional JFreeChart dataset from a ta4j time series. * @param series a time series * @return an additional dataset */ private static TimeSeriesCollection createAdditionalDataset(TimeSeries series) { ClosePriceIndicator indicator = new ClosePriceIndicator(series); TimeSeriesCollection dataset = new TimeSeriesCollection(); org.jfree.data.time.TimeSeries chartTimeSeries = new org.jfree.data.time.TimeSeries("Btc price"); for (int i = 0; i < series.getTickCount(); i++) { Tick tick = series.getTick(i); chartTimeSeries.add(new Second(new Date(tick.getEndTime().toEpochSecond() * 1000)), indicator.getValue(i).toDouble()); } dataset.addSeries(chartTimeSeries); return dataset; }
Example 8
Source File: ChartFactory.java From graylog-plugin-aggregates with GNU General Public License v3.0 | 5 votes |
public static JFreeChart generateTimeSeriesChart(String title, List<HistoryAggregateItem> history, String timespan, Calendar cal) throws ParseException { TimeSeries series = initializeSeries(timespan, cal, history); TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(series); IntervalXYDataset idataset = new XYBarDataset(dataset, 1); JFreeChart chart = org.jfree.chart.ChartFactory.createXYBarChart( title, // Title "Date/time", // X-axis Label true, "Hits", // Y-axis Label idataset, // Dataset PlotOrientation.VERTICAL, true, // Show legend true, // Use tooltips false // Generate URLs ); chart.setBackgroundPaint(Color.WHITE); chart.setBorderPaint(Color.BLACK); XYPlot plot = (XYPlot)chart.getPlot(); plot.setBackgroundPaint(Color.LIGHT_GRAY); plot.getRenderer().setSeriesPaint(0, Color.BLUE); plot.setDomainGridlinePaint(Color.GRAY); plot.setRangeGridlinePaint(Color.GRAY); chart.removeLegend(); return chart; }
Example 9
Source File: MoneyPanel.java From computational-economy with GNU General Public License v3.0 | 5 votes |
protected ChartPanel createMoneyVelocityPanel() { final TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection(); for (final Currency currency : Currency.values()) { timeSeriesCollection.addSeries(ApplicationContext.getInstance().getModelRegistry() .getNationalEconomyModel(currency).moneyVelocityModel.getTimeSeries()); } final JFreeChart chart = ChartFactory.createTimeSeriesChart("Velocity of Money", "Date", "Velocity of Money", timeSeriesCollection, true, true, false); configureChart(chart); return new ChartPanel(chart); }
Example 10
Source File: CombinedXYPlotDemo1.java From ECG-Viewer with GNU General Public License v2.0 | 5 votes |
/** * Creates a sample dataset. You wouldn't normally hard-code the * population of a dataset in this way (it would be better to read the * values from a file or a database query), but for a self-contained demo * this is the least complicated solution. * * @return A sample dataset. */ private static IntervalXYDataset createDataset2() { TimeSeriesCollection dataset = new TimeSeriesCollection(); TimeSeries series1 = new TimeSeries("Series 2"); series1.add(new Month(1, 2005), 1200); series1.add(new Month(2, 2005), 1400); series1.add(new Month(3, 2005), 1500); series1.add(new Month(4, 2005), 1700); series1.add(new Month(5, 2005), 1600); series1.add(new Month(6, 2005), 2400); series1.add(new Month(7, 2005), 2100); series1.add(new Month(8, 2005), 2200); series1.add(new Month(9, 2005), 800); series1.add(new Month(10, 2005), 2350); series1.add(new Month(11, 2005), 500); series1.add(new Month(12, 2005), 700); series1.add(new Month(1, 2006), 900); series1.add(new Month(2, 2006), 1500); series1.add(new Month(3, 2006), 2100); series1.add(new Month(4, 2006), 2200); series1.add(new Month(5, 2006), 1900); series1.add(new Month(6, 2006), 3000); series1.add(new Month(7, 2006), 3780); series1.add(new Month(8, 2006), 4000); series1.add(new Month(9, 2006), 4500); series1.add(new Month(10, 2006), 7000); series1.add(new Month(11, 2006), 5500); series1.add(new Month(12, 2006), 6000); series1.add(new Month(1, 2007), 6500); dataset.addSeries(series1); return dataset; }
Example 11
Source File: CombinedXYPlotDemo1.java From SIMVA-SoS with Apache License 2.0 | 5 votes |
/** * Creates a sample dataset. You wouldn't normally hard-code the * population of a dataset in this way (it would be better to read the * values from a file or a database query), but for a self-contained demo * this is the least complicated solution. * * @return A sample dataset. */ private static IntervalXYDataset createDataset2() { TimeSeriesCollection dataset = new TimeSeriesCollection(); TimeSeries series1 = new TimeSeries("Series 2"); series1.add(new Month(1, 2005), 1200); series1.add(new Month(2, 2005), 1400); series1.add(new Month(3, 2005), 1500); series1.add(new Month(4, 2005), 1700); series1.add(new Month(5, 2005), 1600); series1.add(new Month(6, 2005), 2400); series1.add(new Month(7, 2005), 2100); series1.add(new Month(8, 2005), 2200); series1.add(new Month(9, 2005), 800); series1.add(new Month(10, 2005), 2350); series1.add(new Month(11, 2005), 500); series1.add(new Month(12, 2005), 700); series1.add(new Month(1, 2006), 900); series1.add(new Month(2, 2006), 1500); series1.add(new Month(3, 2006), 2100); series1.add(new Month(4, 2006), 2200); series1.add(new Month(5, 2006), 1900); series1.add(new Month(6, 2006), 3000); series1.add(new Month(7, 2006), 3780); series1.add(new Month(8, 2006), 4000); series1.add(new Month(9, 2006), 4500); series1.add(new Month(10, 2006), 7000); series1.add(new Month(11, 2006), 5500); series1.add(new Month(12, 2006), 6000); series1.add(new Month(1, 2007), 6500); dataset.addSeries(series1); return dataset; }
Example 12
Source File: MoneyPanel.java From computational-economy with GNU General Public License v3.0 | 5 votes |
protected ChartPanel createCreditUtilizationRatePanel() { final TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection(); for (final Currency currency : Currency.values()) { timeSeriesCollection.addSeries(ApplicationContext.getInstance().getModelRegistry() .getNationalEconomyModel(currency).creditUtilizationRateModel.getTimeSeries()); } final JFreeChart chart = ChartFactory.createTimeSeriesChart("Credit Utilization Rate", "Date", "Credit Utilization Rate", timeSeriesCollection, true, true, false); configureChart(chart); return new ChartPanel(chart); }
Example 13
Source File: MultipleAxisChart.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * A demonstration application showing how to create a time series chart * with multiple axes. * * @param title1 the frame title. * @param xaxis1 xaxis title * @param yaxis1 yaxis title * @param series1 the data */ public MultipleAxisChart(String title1, String xaxis1, String yaxis1, TimeSeries series1) { TimeSeriesCollection dataset1 = new TimeSeriesCollection(); dataset1.addSeries(series1); chart = ChartFactory.createTimeSeriesChart(title1, xaxis1, yaxis1, dataset1, true, true, false); // chart.addSubtitle(new TextTitle("Four datasets and four range axes.")); XYPlot plot = (XYPlot) chart.getPlot(); plot.setOrientation(PlotOrientation.VERTICAL); this.setLayout(new BorderLayout()); this.add(new ChartPanel(chart), BorderLayout.CENTER); }
Example 14
Source File: SWTTimeSeriesDemo.java From openstock with GNU General Public License v3.0 | 4 votes |
/** * 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; }
Example 15
Source File: DiagramRenderer.java From SensorWebClient with GNU General Public License v2.0 | 4 votes |
public TimeSeriesCollection createDataset(Map<String, OXFFeatureCollection> entireCollMap, TimeseriesProperties prop, String observedProperty, boolean compress) { TimeSeriesCollection dataset = new TimeSeriesCollection(); OXFFeatureCollection obsColl = entireCollMap.get(prop.getOffering() + "@" + prop.getServiceUrl()); String foiID = prop.getFeature(); String obsPropID = prop.getPhenomenon(); String procID = prop.getProcedure(); // only if the observation concerns the observedProperty, it // will be added to the dataset if (obsPropID.equals(observedProperty)) { String[] foiIds = new String[] { foiID }; String[] procedureIds = new String[] { procID }; String[] observedPropertyIds = new String[] { obsPropID }; ObservationSeriesCollection seriesCollection = new ObservationSeriesCollection(obsColl, foiIds, observedPropertyIds, procedureIds, true); // // now let's put in the date-value pairs. // ! But put it only in if it differs from the previous // one ! // TimeSeries timeSeries = new TimeSeries(prop.getTimeseriesId(), FixedMillisecond.class); TimeseriesFactory factory = new TimeseriesFactory(seriesCollection); if (seriesCollection.getSortedTimeArray().length > 0) { if (compress) { timeSeries = factory.compressToTimeSeries(prop.getTimeseries(), isOverview, prop.getGraphStyle()); } else { timeSeries = factory.createTimeSeries(prop.getTimeseries(), prop.getGraphStyle()); } } dataset.addSeries(timeSeries); } dataset.setDomainIsPointsInTime(true); return dataset; }
Example 16
Source File: ComplexChartPlotter.java From Design-Patterns-and-SOLID-Principles-with-Java with MIT License | 4 votes |
@Override void doPlot(Report report, OutputStream out) throws IOException { List<String> labels = report.getLabels(); List<List<Report.Field>> records = report.getRecords(); String dateLabel = labels.get(dateIndex); List<String> valueLabels = labels .stream() .filter(l -> !l.equals(dateLabel)) .collect(Collectors.toList()); TimeSeriesCollection seriesCollection = new TimeSeriesCollection(); for (String valueLabel : valueLabels) { seriesCollection.addSeries(new TimeSeries(valueLabel)); } for (List<Report.Field> record : records) { Report.Field dateField = record.get(dateIndex); Day day = Day.parseDay(dateField.getAsString()); List<Number> values = record.stream() .filter(f -> !f.equals(dateField)) .map(Report.Field::getAsNumber) .collect(Collectors.toList()); for (int j = 0; j < values.size(); j++) { Number value = values.get(j); TimeSeries series = seriesCollection.getSeries(j); series.add(day, value); } } JFreeChart chart; if (chartType == ChartType.LINE) { chart = createTimeSeriesChart( reportType.getDisplayName(), dateLabel, null, seriesCollection ); } else if (chartType == ChartType.BAR) { chart = createXYBarChart( reportType.getDisplayName(), dateLabel, true, null, seriesCollection ); } else { throw new IllegalStateException(String.format("Chart type %s not handled.", chartType)); } writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT); }
Example 17
Source File: SWTTimeSeriesDemo.java From astor with GNU General Public License v2.0 | 4 votes |
/** * 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", Month.class); 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", Month.class); 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), 180.5); s2.add(new Month(11, 2001), 140.1); s2.add(new Month(12, 2001), 110.3); // 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; }
Example 18
Source File: TimeSeriesChartDemo1.java From ccu-historian with GNU General Public License v3.0 | 4 votes |
/** * 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); // ****************************************************************** // More than 150 demo applications are included with the JFreeChart // Developer Guide...for more information, see: // // > http://www.object-refinery.com/jfreechart/guide.html // // ****************************************************************** TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(s1); dataset.addSeries(s2); return dataset; }
Example 19
Source File: TimesheetDisciplineChartBuilder.java From projectforge-webapp with GNU General Public License v3.0 | 4 votes |
/** * Ein Diagramm, welches über die letzten n Tage die Tage visualisiert, die zwischen Zeitberichtsdatum und Zeitpunkt der tatsächlichen * Buchung liegen. * @param timesheetDao * @param userId * @param forLastNDays * @param shape e. g. new Ellipse2D.Float(-3, -3, 6, 6) or null, if no marker should be printed. * @param stroke e. g. new BasicStroke(3.0f). * @param showAxisValues * @return */ public JFreeChart create(final TimesheetDao timesheetDao, final Integer userId, final short forLastNDays, final boolean showAxisValues) { final DayHolder dh = new DayHolder(); final TimesheetFilter filter = new TimesheetFilter(); filter.setStopTime(dh.getDate()); dh.add(Calendar.DATE, -forLastNDays); filter.setStartTime(dh.getDate()); filter.setUserId(userId); filter.setOrderType(OrderDirection.ASC); final List<TimesheetDO> list = timesheetDao.getList(filter); final TimeSeries planSeries = new TimeSeries("Soll"); final TimeSeries actualSeries = new TimeSeries("Ist"); final Iterator<TimesheetDO> it = list.iterator(); TimesheetDO current = null; if (it.hasNext() == true) { current = it.next(); } long numberOfBookedDays = 0; long totalDifference = 0; for (int i = 0; i <= forLastNDays; i++) { long difference = 0; long totalDuration = 0; // Weight for average. while (current != null && (dh.isSameDay(current.getStartTime()) == true || current.getStartTime().before(dh.getDate()) == true)) { final long duration = current.getWorkFractionDuration(); difference += (current.getCreated().getTime() - current.getStartTime().getTime()) * duration; totalDuration += duration; if (it.hasNext() == true) { current = it.next(); } else { current = null; break; } } final double averageDifference = difference > 0 ? ((double) difference) / totalDuration / 86400000 : 0; // In days. final Day day = new Day(dh.getDayOfMonth(), dh.getMonth() + 1, dh.getYear()); if (averageDifference > 0) { planSeries.add(day, PLANNED_AVERAGE_DIFFERENCE_BETWEEN_TIMESHEET_AND_BOOKING); // plan average // (PLANNED_AVERAGE_DIFFERENCE_BETWEEN_TIMESHEET_AND_BOOKING // days). actualSeries.add(day, averageDifference); totalDifference += averageDifference; numberOfBookedDays++; } dh.add(Calendar.DATE, 1); } averageDifferenceBetweenTimesheetAndBooking = numberOfBookedDays > 0 ? new BigDecimal(totalDifference).divide(new BigDecimal( numberOfBookedDays), 1, RoundingMode.HALF_UP) : BigDecimal.ZERO; final TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(actualSeries); dataset.addSeries(planSeries); final XYChartBuilder cb = new XYChartBuilder(null, null, null, dataset, false); final XYDifferenceRenderer diffRenderer = new XYDifferenceRenderer(cb.getRedFill(), cb.getGreenFill(), true); diffRenderer.setSeriesPaint(0, cb.getRedMarker()); diffRenderer.setSeriesPaint(1, cb.getGreenMarker()); cb.setRenderer(0, diffRenderer).setStrongStyle(diffRenderer, false, actualSeries, planSeries); cb.setDateXAxis(true).setYAxis(true, "days"); return cb.getChart(); }
Example 20
Source File: ComplexChartPlotter.java From Design-Patterns-and-SOLID-Principles-with-Java with MIT License | 4 votes |
@Override void doPlot(Report report, OutputStream out) throws IOException { List<String> labels = report.getLabels(); List<List<Report.Field>> records = report.getRecords(); String dateLabel = labels.get(dateIndex); List<String> valueLabels = labels .stream() .filter(l -> !l.equals(dateLabel)) .collect(Collectors.toList()); TimeSeriesCollection seriesCollection = new TimeSeriesCollection(); for (String valueLabel : valueLabels) { seriesCollection.addSeries(new TimeSeries(valueLabel)); } for (List<Report.Field> record : records) { Report.Field dateField = record.get(dateIndex); Day day = Day.parseDay(dateField.getAsString()); List<Number> values = record.stream() .filter(f -> !f.equals(dateField)) .map(Report.Field::getAsNumber) .collect(Collectors.toList()); for (int j = 0; j < values.size(); j++) { Number value = values.get(j); TimeSeries series = seriesCollection.getSeries(j); series.add(day, value); } } JFreeChart chart; if (chartType == ChartType.LINE) { chart = createTimeSeriesChart( reportType.getDisplayName(), dateLabel, null, seriesCollection ); } else if (chartType == ChartType.BAR) { chart = createXYBarChart( reportType.getDisplayName(), dateLabel, true, null, seriesCollection ); } else { throw new IllegalStateException(String.format("Chart type %s not handled.", chartType)); } writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT); }