List of usage examples for org.jfree.data.time TimeSeries add
public void add(RegularTimePeriod period, Number value)
From source file:name.wramner.jmstools.analyzer.DataProvider.java
/** * Get a base64-encoded image for inclusion in an img tag with a chart with number of produced and consumed messages * per second./*w w w . j a va2s . c om*/ * * @return chart as base64 string. */ public String getBase64MessagesPerSecondImage() { TimeSeries timeSeriesConsumed = new TimeSeries("Consumed"); TimeSeries timeSeriesProduced = new TimeSeries("Produced"); TimeSeries timeSeriesTotal = new TimeSeries("Total"); for (PeriodMetrics m : getMessagesPerSecond()) { Second second = new Second(m.getPeriodStart()); timeSeriesConsumed.add(second, m.getConsumed()); timeSeriesProduced.add(second, m.getProduced()); timeSeriesTotal.add(second, m.getConsumed() + m.getProduced()); } TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection(timeSeriesConsumed); timeSeriesCollection.addSeries(timeSeriesProduced); timeSeriesCollection.addSeries(timeSeriesTotal); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { JFreeChart chart = ChartFactory.createTimeSeriesChart("Messages per second (TPS)", "Time", "Messages", timeSeriesCollection); chart.getPlot().setBackgroundPaint(Color.WHITE); ChartUtilities.writeChartAsPNG(bos, chart, 1024, 500); } catch (IOException e) { throw new UncheckedIOException(e); } return "data:image/png;base64," + Base64.getEncoder().encodeToString(bos.toByteArray()); }
From source file:it.marcoberri.mbmeteo.action.chart.GetMinAndMax.java
/** * Processes requests for both HTTP/* w w w . j a va2 s . co m*/ * <code>GET</code> and * <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { log.debug("start : " + this.getClass().getName()); final HashMap<String, String> params = getParams(request.getParameterMap()); final Integer dimy = Default.toInteger(params.get("dimy"), 600); final Integer dimx = Default.toInteger(params.get("dimx"), 800); final String from = Default.toString(params.get("from") + " 00:00:00", "1970-01-01 00:00:00"); final String to = Default.toString(params.get("to") + " 23:59:00", "2030-01-01 23:59:00"); final String field = Default.toString(params.get("field"), "outdoorTemperature"); final String period = Default.toString(params.get("period"), "day"); request.getSession().setAttribute("from", params.get("from")); request.getSession().setAttribute("to", params.get("to")); final String cacheKey = getCacheKey(params); if (cacheReadEnable) { final Query q = ds.find(Cache.class); q.filter("cacheKey", cacheKey).filter("servletName", this.getClass().getName()); final Cache c = (Cache) q.get(); if (c == null) { log.info("cacheKey:" + cacheKey + " on servletName: " + this.getClass().getName() + " not found"); } if (c != null) { final GridFSDBFile imageForOutput = MongoConnectionHelper.getGridFS() .findOne(new ObjectId(c.getGridId())); if (imageForOutput != null) { ds.save(c); try { response.setHeader("Content-Length", "" + imageForOutput.getLength()); response.setHeader("Content-Disposition", "inline; filename=\"" + imageForOutput.getFilename() + "\""); final OutputStream out = response.getOutputStream(); final InputStream in = imageForOutput.getInputStream(); final byte[] content = new byte[(int) imageForOutput.getLength()]; in.read(content); out.write(content); in.close(); out.close(); return; } catch (Exception e) { log.error(e); } } else { log.error("file not in db"); } } } final String formatIn = getFormatIn(period); final String formatOut = getFormatOut(period); final Query q = ds.createQuery(MapReduceMinMax.class).disableValidation(); final Date dFrom = DateTimeUtil.getDate("yyyy-MM-dd hh:mm:ss", from); final Date dTo = DateTimeUtil.getDate("yyyy-MM-dd hh:mm:ss", to); final List<Date> datesIn = getRangeDate(dFrom, dTo); final HashSet<String> datesInString = new HashSet<String>(); for (Date d : datesIn) { datesInString.add(DateTimeUtil.dateFormat(formatIn, d)); } if (datesIn != null && !datesIn.isEmpty()) { q.filter("_id in", datesInString); } q.order("_id"); final List<MapReduceMinMax> mapReduceResult = q.asList(); final TimeSeries serieMin = new TimeSeries("Min"); final TimeSeries serieMax = new TimeSeries("Max"); for (MapReduceMinMax m : mapReduceResult) { try { final Date tmpDate = DateTimeUtil.getDate(formatIn, m.getId().toString()); if (tmpDate == null) { continue; } final Millisecond t = new Millisecond(tmpDate); ChartEnumMinMaxHelper chartEnum = ChartEnumMinMaxHelper.getByFieldAndType(field, "min"); Method method = m.getClass().getMethod(chartEnum.getMethod()); Number n = (Number) method.invoke(m); serieMin.add(t, n); chartEnum = ChartEnumMinMaxHelper.getByFieldAndType(field, "max"); method = m.getClass().getMethod(chartEnum.getMethod()); n = (Number) method.invoke(m); serieMax.add(t, n); } catch (IllegalAccessException ex) { log.error(ex); } catch (IllegalArgumentException ex) { log.error(ex); } catch (InvocationTargetException ex) { log.error(ex); } catch (NoSuchMethodException ex) { log.error(ex); } catch (SecurityException ex) { log.error(ex); } } final ChartEnumMinMaxHelper chartData = ChartEnumMinMaxHelper.getByFieldAndType(field, "min"); final TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(serieMin); dataset.addSeries(serieMax); final JFreeChart chart = ChartFactory.createTimeSeriesChart("Max/Min", "", chartData.getUm(), dataset, true, false, false); final XYPlot plot = (XYPlot) chart.getPlot(); final DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat(formatOut)); axis.setVerticalTickLabels(true); if (field.toUpperCase().indexOf("PRESSURE") != -1) { plot.getRangeAxis().setRange(chartPressureMin, chartPressureMax); } final File f = File.createTempFile("mbmeteo", ".jpg"); ChartUtilities.saveChartAsJPEG(f, chart, dimx, dimy); try { if (cacheWriteEnable) { final GridFSInputFile gfsFile = MongoConnectionHelper.getGridFS().createFile(f); gfsFile.setFilename(f.getName()); gfsFile.save(); final Cache c = new Cache(); c.setServletName(this.getClass().getName()); c.setCacheKey(cacheKey); c.setGridId(gfsFile.getId().toString()); ds.save(c); } response.setContentType("image/jpeg"); response.setHeader("Content-Length", "" + f.length()); response.setHeader("Content-Disposition", "inline; filename=\"" + f.getName() + "\""); final OutputStream out = response.getOutputStream(); final FileInputStream in = new FileInputStream(f.toString()); final int size = in.available(); final byte[] content = new byte[size]; in.read(content); out.write(content); in.close(); out.close(); } catch (Exception e) { log.error(e); } finally { f.delete(); } }
From source file:org.projectforge.plugins.liquidityplanning.LiquidityChartBuilder.java
/** * @param forecast/*from w ww . j a v a 2 s .co m*/ * @param settings (next days) * @return */ public JFreeChart createBarChart(final LiquidityForecast forecast, final LiquidityForecastSettings settings) { Validate.isTrue(settings.getNextDays() > 0 && settings.getNextDays() < 500); final LiquidityForecastCashFlow cashFlow = new LiquidityForecastCashFlow(forecast, settings.getNextDays()); final TimeSeries accumulatedSeriesExpected = new TimeSeries( I18n.getString("plugins.liquidityplanning.forecast.expected")); final TimeSeries creditSeries = new TimeSeries(I18n.getString("plugins.liquidityplanning.common.credit")); final TimeSeries debitSeries = new TimeSeries(I18n.getString("plugins.liquidityplanning.common.debit")); double accumulatedExpected = settings.getStartAmount().doubleValue(); final DayHolder dh = new DayHolder(); final Date lower = dh.getDate(); for (int i = 0; i < settings.getNextDays(); i++) { final Day day = new Day(dh.getDayOfMonth(), dh.getMonth() + 1, dh.getYear()); if (i > 0) { accumulatedExpected += cashFlow.getDebitsExpected()[i - 1].doubleValue() + cashFlow.getCreditsExpected()[i - 1].doubleValue(); } accumulatedSeriesExpected.add(day, accumulatedExpected); creditSeries.add(day, cashFlow.getCreditsExpected()[i].doubleValue()); debitSeries.add(day, cashFlow.getDebitsExpected()[i].doubleValue()); dh.add(Calendar.DATE, 1); } dh.add(Calendar.DATE, -1); final XYChartBuilder cb = new XYChartBuilder(ChartFactory.createXYBarChart(null, null, false, null, null, PlotOrientation.VERTICAL, false, false, false)); int counter = 0; final TimeSeriesCollection xyDataSeries = new TimeSeriesCollection(); xyDataSeries.addSeries(accumulatedSeriesExpected); final XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer(true, true); lineRenderer.setSeriesPaint(0, cb.getRedMarker()); lineRenderer.setSeriesVisibleInLegend(0, true); cb.setRenderer(counter, lineRenderer).setDataset(counter++, xyDataSeries).setStrongStyle(lineRenderer, false, accumulatedSeriesExpected); final TimeSeriesCollection cashflowSet = new TimeSeriesCollection(); cashflowSet.addSeries(debitSeries); cashflowSet.addSeries(creditSeries); final XYBarRenderer barRenderer = new XYBarRenderer(.2); barRenderer.setSeriesPaint(0, cb.getGreenFill()); barRenderer.setSeriesPaint(1, cb.getRedFill()); barRenderer.setShadowVisible(false); cb.setRenderer(counter, barRenderer).setDataset(counter++, cashflowSet); cb.setDateXAxis(true).setDateXAxisRange(lower, dh.getDate()).setYAxis(true, null); return cb.getChart(); }
From source file:org.jfree.data.time.junit.TimeSeriesTest.java
/** * Test for bug report 1864222./*from w ww . ja v a 2 s . c o m*/ */ public void testBug1864222() { TimeSeries s = new TimeSeries("S"); s.add(new Day(19, 8, 2005), 1); s.add(new Day(31, 1, 2006), 1); boolean pass = true; try { s.createCopy(new Day(1, 12, 2005), new Day(18, 1, 2006)); } catch (CloneNotSupportedException e) { pass = false; } assertTrue(pass); }
From source file:org.jfree.data.time.junit.TimeSeriesTest.java
/** * Check that the item bounds are determined correctly when there is a * maximum item count and a new value is added. */// w ww. j a v a2 s.co m public void testDelete_RegularTimePeriod() { TimeSeries s1 = new TimeSeries("S1"); s1.add(new Year(2010), 1.1); s1.add(new Year(2011), 2.2); s1.add(new Year(2012), 3.3); s1.add(new Year(2013), 4.4); s1.delete(new Year(2010)); s1.delete(new Year(2013)); assertEquals(2.2, s1.getMinY(), EPSILON); assertEquals(3.3, s1.getMaxY(), EPSILON); }
From source file:org.jfree.data.time.junit.TimeSeriesTest.java
/** * A test for the clear method./*from ww w .j a va 2 s . c om*/ */ public void testClear() { TimeSeries s1 = new TimeSeries("S1"); s1.add(new Year(2009), 1.1); s1.add(new Year(2010), 2.2); assertEquals(2, s1.getItemCount()); s1.clear(); assertEquals(0, s1.getItemCount()); assertTrue(Double.isNaN(s1.getMinY())); assertTrue(Double.isNaN(s1.getMaxY())); }
From source file:org.jfree.data.time.junit.TimeSeriesTest.java
/** * Some checks for the update(RegularTimePeriod...method). *///w w w . j a v a2s .c o m public void testUpdate_RegularTimePeriod() { TimeSeries s1 = new TimeSeries("S1"); s1.add(new Year(2010), 1.1); s1.add(new Year(2011), 2.2); s1.add(new Year(2012), 3.3); s1.update(new Year(2012), 4.4); assertEquals(4.4, s1.getMaxY(), EPSILON); s1.update(new Year(2010), 0.5); assertEquals(0.5, s1.getMinY(), EPSILON); s1.update(new Year(2012), null); assertEquals(2.2, s1.getMaxY(), EPSILON); s1.update(new Year(2010), null); assertEquals(2.2, s1.getMinY(), EPSILON); }
From source file:org.jfree.data.time.junit.TimeSeriesTest.java
/** * Some checks for the delete(int, int) method. *///w ww. j a v a 2s. co m public void testDelete3() { TimeSeries s1 = new TimeSeries("S1"); s1.add(new Year(2011), 1.1); s1.add(new Year(2012), 2.2); s1.add(new Year(2013), 3.3); s1.add(new Year(2014), 4.4); s1.add(new Year(2015), 5.5); s1.add(new Year(2016), 6.6); s1.delete(2, 5); assertEquals(2, s1.getItemCount()); assertEquals(new Year(2011), s1.getTimePeriod(0)); assertEquals(new Year(2012), s1.getTimePeriod(1)); assertEquals(1.1, s1.getMinY(), EPSILON); assertEquals(2.2, s1.getMaxY(), EPSILON); }
From source file:org.jfree.data.time.junit.TimeSeriesTest.java
/** * Checks that the min and max y values are updated correctly when copying * a subset.//from ww w. jav a2 s. c om * * @throws java.lang.CloneNotSupportedException */ public void testCreateCopy3() throws CloneNotSupportedException { TimeSeries s1 = new TimeSeries("S1"); s1.add(new Year(2009), 100.0); s1.add(new Year(2010), 101.0); s1.add(new Year(2011), 102.0); assertEquals(100.0, s1.getMinY(), EPSILON); assertEquals(102.0, s1.getMaxY(), EPSILON); TimeSeries s2 = s1.createCopy(0, 1); assertEquals(100.0, s2.getMinY(), EPSILON); assertEquals(101.0, s2.getMaxY(), EPSILON); TimeSeries s3 = s1.createCopy(1, 2); assertEquals(101.0, s3.getMinY(), EPSILON); assertEquals(102.0, s3.getMaxY(), EPSILON); }
From source file:org.jfree.data.time.junit.TimeSeriesTest.java
/** * Test the setMaximumItemCount() method to ensure that it removes items * from the series if necessary.// w ww .j a v a 2s . c om */ public void testSetMaximumItemCount() { TimeSeries s1 = new TimeSeries("S1"); s1.add(new Year(2000), 13.75); s1.add(new Year(2001), 11.90); s1.add(new Year(2002), null); s1.add(new Year(2005), 19.32); s1.add(new Year(2007), 16.89); assertTrue(s1.getItemCount() == 5); s1.setMaximumItemCount(3); assertTrue(s1.getItemCount() == 3); TimeSeriesDataItem item = s1.getDataItem(0); assertTrue(item.getPeriod().equals(new Year(2002))); assertEquals(16.89, s1.getMinY(), EPSILON); assertEquals(19.32, s1.getMaxY(), EPSILON); }