/** Add anomalies to the plot. */ public void addAnomalies(XYPlot plot, ArrayList<Anomaly> anomalyList) { for (Anomaly a : anomalyList) { IntervalSequence is = a.intervals; for (Interval i : is) { ValueMarker marker = new ValueMarker(i.index); marker.setPaint(Color.black); plot.addDomainMarker(marker); } } }
@Override public void addLogMarker(LogMarker e) { if (combinedPlot != null) { for (Object plot : combinedPlot.getSubplots()) { ValueMarker vm = new ValueMarker(e.getTimestamp() - localTimeOffset); vm.setLabel(e.getLabel()); ((org.jfree.chart.plot.XYPlot) plot).addDomainMarker(vm); } } }
/** * Draws a range marker. * * @param g2 the graphics device. * @param plot the plot. * @param axis the value axis. * @param marker the marker. * @param dataArea the area for plotting data (not including 3D effect). */ @Override public void drawRangeMarker( Graphics2D g2, CategoryPlot plot, ValueAxis axis, Marker marker, Rectangle2D dataArea) { Rectangle2D adjusted = new Rectangle2D.Double( dataArea.getX(), dataArea.getY() + getYOffset(), dataArea.getWidth() - getXOffset(), dataArea.getHeight() - getYOffset()); if (marker instanceof ValueMarker) { ValueMarker vm = (ValueMarker) marker; double value = vm.getValue(); Range range = axis.getRange(); if (!range.contains(value)) { return; } GeneralPath path = null; PlotOrientation orientation = plot.getOrientation(); if (orientation == PlotOrientation.HORIZONTAL) { float x = (float) axis.valueToJava2D(value, adjusted, plot.getRangeAxisEdge()); float y = (float) adjusted.getMaxY(); path = new GeneralPath(); path.moveTo(x, y); path.lineTo((float) (x + getXOffset()), y - (float) getYOffset()); path.lineTo((float) (x + getXOffset()), (float) (adjusted.getMinY() - getYOffset())); path.lineTo(x, (float) adjusted.getMinY()); path.closePath(); } else if (orientation == PlotOrientation.VERTICAL) { float y = (float) axis.valueToJava2D(value, adjusted, plot.getRangeAxisEdge()); float x = (float) dataArea.getX(); path = new GeneralPath(); path.moveTo(x, y); path.lineTo(x + (float) this.xOffset, y - (float) this.yOffset); path.lineTo((float) (adjusted.getMaxX() + this.xOffset), y - (float) this.yOffset); path.lineTo((float) (adjusted.getMaxX()), y); path.closePath(); } g2.setPaint(marker.getPaint()); g2.fill(path); g2.setPaint(marker.getOutlinePaint()); g2.draw(path); } else { super.drawRangeMarker(g2, plot, axis, marker, adjusted); // TODO: draw the interval marker with a 3D effect } }
public static void writeChart( PointTimeSeriesCollection pointTimeSeriesCollection, boolean showLegend, OutputStream out, int width, int height, long from, long to) throws IOException { // 创建主题样式 StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 设置标题字体 standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 18)); // 设置图例的字体 standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 14)); // 设置轴向的字体 standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 14)); // 应用主题样式 ChartFactory.setChartTheme(standardChartTheme); 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 { XYSplineRenderer spline = new XYSplineRenderer(); spline.setBaseShapesVisible(false); renderer = spline; } 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); }
/** * Draws a range marker. * * @param g2 the graphics device. * @param plot the plot. * @param axis the value axis. * @param marker the marker. * @param dataArea the area for plotting data (not including 3D effect). */ public void drawRangeMarker( Graphics2D g2, CategoryPlot plot, ValueAxis axis, Marker marker, Rectangle2D dataArea) { Rectangle2D adjusted = new Rectangle2D.Double( dataArea.getX(), dataArea.getY() + getYOffset(), dataArea.getWidth() - getXOffset(), dataArea.getHeight() - getYOffset()); if (marker instanceof ValueMarker) { ValueMarker vm = (ValueMarker) marker; double value = vm.getValue(); Range range = axis.getRange(); if (!range.contains(value)) { return; } GeneralPath path = null; PlotOrientation orientation = plot.getOrientation(); if (orientation == PlotOrientation.HORIZONTAL) { float x = (float) axis.valueToJava2D(value, adjusted, plot.getRangeAxisEdge()); float y = (float) adjusted.getMaxY(); path = new GeneralPath(); path.moveTo(x, y); path.lineTo((float) (x + getXOffset()), y - (float) getYOffset()); path.lineTo((float) (x + getXOffset()), (float) (adjusted.getMinY() - getYOffset())); path.lineTo(x, (float) adjusted.getMinY()); path.closePath(); } else if (orientation == PlotOrientation.VERTICAL) { float y = (float) axis.valueToJava2D(value, adjusted, plot.getRangeAxisEdge()); float x = (float) dataArea.getX(); path = new GeneralPath(); path.moveTo(x, y); path.lineTo(x + (float) this.xOffset, y - (float) this.yOffset); path.lineTo((float) (adjusted.getMaxX() + this.xOffset), y - (float) this.yOffset); path.lineTo((float) (adjusted.getMaxX()), y); path.closePath(); } g2.setPaint(marker.getPaint()); g2.fill(path); g2.setPaint(marker.getOutlinePaint()); g2.draw(path); String label = marker.getLabel(); RectangleAnchor anchor = marker.getLabelAnchor(); if (label != null) { Font labelFont = marker.getLabelFont(); g2.setFont(labelFont); g2.setPaint(marker.getLabelPaint()); Point2D coordinates = calculateRangeMarkerTextAnchorPoint( g2, orientation, dataArea, path.getBounds2D(), marker.getLabelOffset(), LengthAdjustmentType.EXPAND, anchor); TextUtilities.drawAlignedString( label, g2, (float) coordinates.getX(), (float) coordinates.getY(), marker.getLabelTextAnchor()); } } else { super.drawRangeMarker(g2, plot, axis, marker, adjusted); // TODO: draw the interval marker with a 3D effect } }
/** * Draws a marker for the range axis. * * @param g2 the graphics device (not <code>null</code>). * @param plot the plot (not <code>null</code>). * @param axis the range axis (not <code>null</code>). * @param marker the marker to be drawn (not <code>null</code>). * @param dataArea the area inside the axes (not <code>null</code>). */ public void drawRangeMarker( Graphics2D g2, CategoryPlot plot, ValueAxis axis, Marker marker, Rectangle2D dataArea) { if (marker instanceof ValueMarker) { ValueMarker vm = (ValueMarker) marker; double value = vm.getValue(); Range range = axis.getRange(); if (!range.contains(value)) { return; } PlotOrientation orientation = plot.getOrientation(); double v = axis.valueToJava2D(value, dataArea, plot.getRangeAxisEdge()); Line2D line = null; if (orientation == PlotOrientation.HORIZONTAL) { line = new Line2D.Double(v, dataArea.getMinY(), v, dataArea.getMaxY()); } else if (orientation == PlotOrientation.VERTICAL) { line = new Line2D.Double(dataArea.getMinX(), v, dataArea.getMaxX(), v); } g2.setPaint(marker.getPaint()); g2.setStroke(marker.getStroke()); g2.draw(line); String label = marker.getLabel(); RectangleAnchor anchor = marker.getLabelAnchor(); if (label != null) { Font labelFont = marker.getLabelFont(); g2.setFont(labelFont); g2.setPaint(marker.getLabelPaint()); Point2D coordinates = calculateRangeMarkerTextAnchorPoint( g2, orientation, dataArea, line.getBounds2D(), marker.getLabelOffset(), LengthAdjustmentType.EXPAND, anchor); TextUtilities.drawAlignedString( label, g2, (float) coordinates.getX(), (float) coordinates.getY(), marker.getLabelTextAnchor()); } } else if (marker instanceof IntervalMarker) { IntervalMarker im = (IntervalMarker) marker; double start = im.getStartValue(); double end = im.getEndValue(); Range range = axis.getRange(); if (!(range.intersects(start, end))) { return; } // don't draw beyond the axis range... start = range.constrain(start); end = range.constrain(end); double v0 = axis.valueToJava2D(start, dataArea, plot.getRangeAxisEdge()); double v1 = axis.valueToJava2D(end, dataArea, plot.getRangeAxisEdge()); PlotOrientation orientation = plot.getOrientation(); Rectangle2D rect = null; if (orientation == PlotOrientation.HORIZONTAL) { rect = new Rectangle2D.Double(v0, dataArea.getMinY(), v1 - v0, dataArea.getHeight()); } else if (orientation == PlotOrientation.VERTICAL) { rect = new Rectangle2D.Double( dataArea.getMinX(), Math.min(v0, v1), dataArea.getWidth(), Math.abs(v1 - v0)); } Paint p = marker.getPaint(); if (p instanceof GradientPaint) { GradientPaint gp = (GradientPaint) p; GradientPaintTransformer t = im.getGradientPaintTransformer(); if (t != null) { gp = t.transform(gp, rect); } g2.setPaint(gp); } else { g2.setPaint(p); } g2.fill(rect); String label = marker.getLabel(); RectangleAnchor anchor = marker.getLabelAnchor(); if (label != null) { Font labelFont = marker.getLabelFont(); g2.setFont(labelFont); g2.setPaint(marker.getLabelPaint()); Point2D coordinates = calculateRangeMarkerTextAnchorPoint( g2, orientation, dataArea, rect, marker.getLabelOffset(), marker.getLabelOffsetType(), anchor); TextUtilities.drawAlignedString( label, g2, (float) coordinates.getX(), (float) coordinates.getY(), marker.getLabelTextAnchor()); } } }