Search in sources :

Example 1 with AbstractXYItemRenderer

use of org.jfree.chart.renderer.xy.AbstractXYItemRenderer in project SIMVA-SoS by SESoS.

the class XYPlot method drawDomainGridlines.

/**
 * Draws the gridlines for the plot, if they are visible.
 *
 * @param g2  the graphics device.
 * @param dataArea  the data area.
 * @param ticks  the ticks.
 *
 * @see #drawRangeGridlines(Graphics2D, Rectangle2D, List)
 */
protected void drawDomainGridlines(Graphics2D g2, Rectangle2D dataArea, List ticks) {
    // no renderer, no gridlines...
    if (getRenderer() == null) {
        return;
    }
    // draw the domain grid lines, if any...
    if (isDomainGridlinesVisible() || isDomainMinorGridlinesVisible()) {
        Stroke gridStroke = null;
        Paint gridPaint = null;
        Iterator iterator = ticks.iterator();
        boolean paintLine;
        while (iterator.hasNext()) {
            paintLine = false;
            ValueTick tick = (ValueTick) iterator.next();
            if ((tick.getTickType() == TickType.MINOR) && isDomainMinorGridlinesVisible()) {
                gridStroke = getDomainMinorGridlineStroke();
                gridPaint = getDomainMinorGridlinePaint();
                paintLine = true;
            } else if ((tick.getTickType() == TickType.MAJOR) && isDomainGridlinesVisible()) {
                gridStroke = getDomainGridlineStroke();
                gridPaint = getDomainGridlinePaint();
                paintLine = true;
            }
            XYItemRenderer r = getRenderer();
            if ((r instanceof AbstractXYItemRenderer) && paintLine) {
                ((AbstractXYItemRenderer) r).drawDomainLine(g2, this, getDomainAxis(), dataArea, tick.getValue(), gridPaint, gridStroke);
            }
        }
    }
}
Also used : ValueTick(org.jfree.chart.axis.ValueTick) BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) Iterator(java.util.Iterator) Paint(java.awt.Paint) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer)

Example 2 with AbstractXYItemRenderer

use of org.jfree.chart.renderer.xy.AbstractXYItemRenderer in project pentaho-platform by pentaho.

the class JFreeChartEngine method createXYSeriesCollectionChart.

private static JFreeChart createXYSeriesCollectionChart(final XYSeriesCollectionChartDefinition chartDefinition) {
    JFreeChart chart = null;
    // TODO Make the following accessible from the chartDefinition
    String domainAxisLabel = null;
    String rangeAxisLabel = null;
    boolean tooltips = true;
    boolean urls = true;
    // -----------------------------------------------------------
    String title = chartDefinition.getTitle();
    boolean legend = chartDefinition.isLegendIncluded();
    NumberAxis domainAxis = chartDefinition.isThreeD() ? new NumberAxis3D(domainAxisLabel) : new NumberAxis(domainAxisLabel);
    domainAxis.setAutoRangeIncludesZero(chartDefinition.isDomainIncludesZero());
    domainAxis.setAutoRangeStickyZero(chartDefinition.isDomainStickyZero());
    ValueAxis rangeAxis = new NumberAxis(rangeAxisLabel);
    AbstractXYItemRenderer renderer = null;
    switch(chartDefinition.getChartType()) {
        case LINE_CHART_TYPE:
            renderer = chartDefinition.isThreeD() ? new XYLine3DRenderer() : new XYLineAndShapeRenderer(true, false);
            ((XYLineAndShapeRenderer) renderer).setShapesVisible(chartDefinition.isMarkersVisible());
            ((XYLineAndShapeRenderer) renderer).setBaseShapesFilled(chartDefinition.isMarkersVisible());
            break;
        case AREA_CHART_TYPE:
            renderer = new XYAreaRenderer();
            break;
        case STEP_CHART_TYPE:
            renderer = new XYStepRenderer();
            break;
        case STEP_AREA_CHART_TYPE:
            renderer = new XYStepAreaRenderer();
            break;
        case DIFFERENCE_CHART_TYPE:
            renderer = new XYDifferenceRenderer();
            break;
        case DOT_CHART_TYPE:
            renderer = new XYDotRenderer();
            ((XYDotRenderer) renderer).setDotHeight(chartDefinition.getDotHeight());
            ((XYDotRenderer) renderer).setDotWidth(chartDefinition.getDotWidth());
            break;
        default:
            // should log an error if invalid chart type passed in - at least return null for no renderer
            return null;
    }
    if (renderer != null && legend) {
        renderer.setLegendItemURLGenerator(new StandardXYSeriesLabelGenerator());
    }
    if (tooltips) {
        XYToolTipGenerator generator = new StandardXYToolTipGenerator(chartDefinition.getTooltipContent(), new DecimalFormat(chartDefinition.getTooltipXFormat()), new DecimalFormat(chartDefinition.getTooltipYFormat()));
        renderer.setToolTipGenerator(generator);
    }
    if (urls) {
        renderer.setURLGenerator(new StandardXYURLGenerator());
    }
    renderer.setStroke(JFreeChartEngine.getLineStyleStroke(chartDefinition.getLineStyle(), chartDefinition.getLineWidth()));
    XYPlot plot = new XYPlot(chartDefinition, domainAxis, rangeAxis, renderer);
    JFreeChartEngine.updatePlot(plot, chartDefinition);
    chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    return chart;
}
Also used : NumberAxis(org.jfree.chart.axis.NumberAxis) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) StandardXYSeriesLabelGenerator(org.jfree.chart.labels.StandardXYSeriesLabelGenerator) DecimalFormat(java.text.DecimalFormat) NumberAxis3D(org.jfree.chart.axis.NumberAxis3D) XYStepAreaRenderer(org.jfree.chart.renderer.xy.XYStepAreaRenderer) XYDotRenderer(org.jfree.chart.renderer.xy.XYDotRenderer) XYDifferenceRenderer(org.jfree.chart.renderer.xy.XYDifferenceRenderer) JFreeChart(org.jfree.chart.JFreeChart) XYAreaRenderer(org.jfree.chart.renderer.xy.XYAreaRenderer) XYLine3DRenderer(org.jfree.chart.renderer.xy.XYLine3DRenderer) StandardXYToolTipGenerator(org.jfree.chart.labels.StandardXYToolTipGenerator) XYPlot(org.jfree.chart.plot.XYPlot) ValueAxis(org.jfree.chart.axis.ValueAxis) XYStepRenderer(org.jfree.chart.renderer.xy.XYStepRenderer) StandardXYURLGenerator(org.jfree.chart.urls.StandardXYURLGenerator) StandardXYToolTipGenerator(org.jfree.chart.labels.StandardXYToolTipGenerator) XYToolTipGenerator(org.jfree.chart.labels.XYToolTipGenerator)

Example 3 with AbstractXYItemRenderer

use of org.jfree.chart.renderer.xy.AbstractXYItemRenderer in project ma-core-public by infiniteautomation.

the class ImageChartUtils method writeChart.

public static void writeChart(PointTimeSeriesCollection pointTimeSeriesCollection, boolean showLegend, OutputStream out, int width, int height, long from, long to) throws IOException {
    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 {
                // XYCardinalSplineRenderer spline = new XYCardinalSplineRenderer(.5d, 16);
                // XYSmoothLineAndShapeRenderer spline = new XYSmoothLineAndShapeRenderer();
                // XYSplineRenderer spline = new XYSplineRenderer();
                // spline.setBaseShapesVisible(false);
                // renderer = spline;
                renderer = new XYLineAndShapeRenderer(true, false);
            }
            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);
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) NumberAxis(org.jfree.chart.axis.NumberAxis) TimeSeries(org.jfree.data.time.TimeSeries) Color(java.awt.Color) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) IValueTime(com.serotonin.m2m2.view.stats.IValueTime) ValueMarker(org.jfree.chart.plot.ValueMarker) Marker(org.jfree.chart.plot.Marker) JFreeChart(org.jfree.chart.JFreeChart) XYTextAnnotation(org.jfree.chart.annotations.XYTextAnnotation) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) XYStepRenderer(org.jfree.chart.renderer.xy.XYStepRenderer) ValueMarker(org.jfree.chart.plot.ValueMarker)

Example 4 with AbstractXYItemRenderer

use of org.jfree.chart.renderer.xy.AbstractXYItemRenderer in project SIMVA-SoS by SESoS.

the class XYPlot method getDataRange.

/**
 * Returns the range for the specified axis.
 *
 * @param axis  the axis.
 *
 * @return The range.
 */
@Override
public Range getDataRange(ValueAxis axis) {
    Range result = null;
    List<XYDataset> mappedDatasets = new ArrayList<XYDataset>();
    List<XYAnnotation> includedAnnotations = new ArrayList<XYAnnotation>();
    boolean isDomainAxis = true;
    // is it a domain axis?
    int domainIndex = getDomainAxisIndex(axis);
    if (domainIndex >= 0) {
        isDomainAxis = true;
        mappedDatasets.addAll(getDatasetsMappedToDomainAxis(domainIndex));
        if (domainIndex == 0) {
            // grab the plot's annotations
            Iterator iterator = this.annotations.iterator();
            while (iterator.hasNext()) {
                XYAnnotation annotation = (XYAnnotation) iterator.next();
                if (annotation instanceof XYAnnotationBoundsInfo) {
                    includedAnnotations.add(annotation);
                }
            }
        }
    }
    // or is it a range axis?
    int rangeIndex = getRangeAxisIndex(axis);
    if (rangeIndex >= 0) {
        isDomainAxis = false;
        mappedDatasets.addAll(getDatasetsMappedToRangeAxis(rangeIndex));
        if (rangeIndex == 0) {
            Iterator iterator = this.annotations.iterator();
            while (iterator.hasNext()) {
                XYAnnotation annotation = (XYAnnotation) iterator.next();
                if (annotation instanceof XYAnnotationBoundsInfo) {
                    includedAnnotations.add(annotation);
                }
            }
        }
    }
    // of the ranges.
    for (XYDataset d : mappedDatasets) {
        if (d != null) {
            XYItemRenderer r = getRendererForDataset(d);
            if (isDomainAxis) {
                if (r != null) {
                    result = Range.combine(result, r.findDomainBounds(d));
                } else {
                    result = Range.combine(result, DatasetUtilities.findDomainBounds(d));
                }
            } else {
                if (r != null) {
                    result = Range.combine(result, r.findRangeBounds(d));
                } else {
                    result = Range.combine(result, DatasetUtilities.findRangeBounds(d));
                }
            }
            // getAnnotations() method but it should
            if (r instanceof AbstractXYItemRenderer) {
                AbstractXYItemRenderer rr = (AbstractXYItemRenderer) r;
                Collection c = rr.getAnnotations();
                Iterator i = c.iterator();
                while (i.hasNext()) {
                    XYAnnotation a = (XYAnnotation) i.next();
                    if (a instanceof XYAnnotationBoundsInfo) {
                        includedAnnotations.add(a);
                    }
                }
            }
        }
    }
    Iterator it = includedAnnotations.iterator();
    while (it.hasNext()) {
        XYAnnotationBoundsInfo xyabi = (XYAnnotationBoundsInfo) it.next();
        if (xyabi.getIncludeInDataBounds()) {
            if (isDomainAxis) {
                result = Range.combine(result, xyabi.getXRange());
            } else {
                result = Range.combine(result, xyabi.getYRange());
            }
        }
    }
    return result;
}
Also used : XYAnnotation(org.jfree.chart.annotations.XYAnnotation) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) ArrayList(java.util.ArrayList) XYAnnotationBoundsInfo(org.jfree.chart.annotations.XYAnnotationBoundsInfo) Range(org.jfree.data.Range) Paint(java.awt.Paint) Iterator(java.util.Iterator) LegendItemCollection(org.jfree.chart.LegendItemCollection) Collection(java.util.Collection) AxisCollection(org.jfree.chart.axis.AxisCollection) XYDataset(org.jfree.data.xy.XYDataset) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer)

Example 5 with AbstractXYItemRenderer

use of org.jfree.chart.renderer.xy.AbstractXYItemRenderer in project SIMVA-SoS by SESoS.

the class XYPlot method drawZeroDomainBaseline.

/**
 * Draws a base line across the chart at value zero on the domain axis.
 *
 * @param g2  the graphics device.
 * @param area  the data area.
 *
 * @see #setDomainZeroBaselineVisible(boolean)
 *
 * @since 1.0.5
 */
protected void drawZeroDomainBaseline(Graphics2D g2, Rectangle2D area) {
    if (isDomainZeroBaselineVisible()) {
        XYItemRenderer r = getRenderer();
        // AbstractXYItemRenderer (which is lame)
        if (r instanceof AbstractXYItemRenderer) {
            AbstractXYItemRenderer renderer = (AbstractXYItemRenderer) r;
            renderer.drawDomainLine(g2, this, getDomainAxis(), area, 0.0, this.domainZeroBaselinePaint, this.domainZeroBaselineStroke);
        }
    }
}
Also used : AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer)

Aggregations

AbstractXYItemRenderer (org.jfree.chart.renderer.xy.AbstractXYItemRenderer)5 XYItemRenderer (org.jfree.chart.renderer.xy.XYItemRenderer)3 Paint (java.awt.Paint)2 Iterator (java.util.Iterator)2 JFreeChart (org.jfree.chart.JFreeChart)2 NumberAxis (org.jfree.chart.axis.NumberAxis)2 XYPlot (org.jfree.chart.plot.XYPlot)2 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)2 XYStepRenderer (org.jfree.chart.renderer.xy.XYStepRenderer)2 IValueTime (com.serotonin.m2m2.view.stats.IValueTime)1 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Stroke (java.awt.Stroke)1 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LegendItemCollection (org.jfree.chart.LegendItemCollection)1 XYAnnotation (org.jfree.chart.annotations.XYAnnotation)1 XYAnnotationBoundsInfo (org.jfree.chart.annotations.XYAnnotationBoundsInfo)1 XYTextAnnotation (org.jfree.chart.annotations.XYTextAnnotation)1