Example usage for org.jfree.chart.renderer.xy XYDifferenceRenderer setSeriesStroke

List of usage examples for org.jfree.chart.renderer.xy XYDifferenceRenderer setSeriesStroke

Introduction

In this page you can find the example usage for org.jfree.chart.renderer.xy XYDifferenceRenderer setSeriesStroke.

Prototype

public void setSeriesStroke(int series, Stroke stroke) 

Source Link

Document

Sets the stroke used for a series and sends a RendererChangeEvent to all registered listeners.

Usage

From source file:com.hmsinc.epicenter.webapp.chart.ChartService.java

/**
 * @param adapter/*from   www.  j  a  va 2 s.  co m*/
 * @param annotations
 * @return
 */
private static JFreeChart createChart(final AbstractChart adapter, final List<XYAnnotation> annotations) {

    final JFreeChart chart;
    if (adapter.getItems() instanceof XYDataset) {

        chart = ChartFactory.createTimeSeriesChart(adapter.getTitle(), adapter.getXLabel(), adapter.getYLabel(),
                (XYDataset) adapter.getItems(), true, false, false);
        final ValueAxis domainAxis = new DateAxis();
        domainAxis.setLabelFont(LABEL_FONT);
        domainAxis.setTickLabelFont(LABEL_FONT);
        domainAxis.setLowerMargin(0.0);
        domainAxis.setUpperMargin(0.0);

        chart.getXYPlot().setDomainAxis(domainAxis);
        chart.getXYPlot().getRangeAxis().setLabelFont(LABEL_FONT);
        chart.getXYPlot().getRangeAxis().setTickLabelFont(LABEL_FONT);
        chart.getXYPlot().getRangeAxis().setStandardTickUnits(adapter.getRangeTickUnits());

        if (adapter.isAlwaysScaleFromZero()) {
            final double upperBound = chart.getXYPlot().getRangeAxis().getRange().getUpperBound();
            final Range range = new Range(0, upperBound + (upperBound * 0.2));
            chart.getXYPlot().getRangeAxis().setRange(range);
        }

        for (Marker marker : adapter.getMarkers()) {
            chart.getXYPlot().addDomainMarker(marker);
        }

        if (annotations != null) {
            for (XYAnnotation annotation : annotations) {
                if (annotation instanceof XYPointerAnnotation) {
                    ((XYPointerAnnotation) annotation).setFont(ANNOTATION_FONT);
                }
                chart.getXYPlot().addAnnotation(annotation);
            }
        }

        // Add any bands to the chart
        if (adapter instanceof TimeSeriesChart) {
            final TimeSeriesChart tsc = (TimeSeriesChart) adapter;
            for (int i = 0; i < tsc.getBands().size(); i++) {

                final Color c = tsc.getBandColors().get(i);
                final Color fill = tsc.getBandFillColors().get(i);
                final XYDifferenceRenderer renderer = new XYDifferenceRenderer(fill, fill, false);
                renderer.setSeriesPaint(0, c);
                renderer.setSeriesPaint(1, c);
                renderer.setSeriesStroke(0, LineStyle.THICK.getStroke());
                renderer.setSeriesStroke(1, LineStyle.THICK.getStroke());

                chart.getXYPlot().setDataset(i + 1, tsc.getBands().get(i));
                chart.getXYPlot().setRenderer(i + 1, renderer);
            }
        }

    } else if (adapter.getItems() instanceof CategoryDataset) {

        chart = ChartFactory.createBarChart(adapter.getTitle(), adapter.getXLabel(), adapter.getYLabel(),
                (CategoryDataset) adapter.getItems(), PlotOrientation.VERTICAL, true, false, false);
        chart.getCategoryPlot().getDomainAxis().setLabelFont(LABEL_FONT);
        chart.getCategoryPlot().getRangeAxis().setLabelFont(LABEL_FONT);
        chart.getCategoryPlot().getDomainAxis().setTickLabelFont(LABEL_FONT);
        chart.getCategoryPlot().getRangeAxis().setTickLabelFont(LABEL_FONT);

        if (adapter.isAlwaysScaleFromZero()) {
            final double upperBound = chart.getCategoryPlot().getRangeAxis().getRange().getUpperBound();
            final Range range = new Range(0, upperBound + (upperBound * 0.1));
            chart.getCategoryPlot().getRangeAxis().setRange(range);
        }

    } else {
        throw new UnsupportedOperationException("Unsupported chart type: " + adapter.getItems().getClass());
    }

    chart.getPlot().setNoDataMessage(NO_DATA_MESSAGE);

    return chart;
}

From source file:org.projectforge.statistics.TimesheetDisciplineChartBuilder.java

private JFreeChart create(final TimeSeries series1, final TimeSeries series2, final Shape shape,
        final Stroke stroke, final boolean showAxisValues, final String valueAxisUnitKey) {
    final TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series1);//from w w  w. ja  va 2 s  .  com
    dataset.addSeries(series2);
    final JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, dataset, PlotOrientation.VERTICAL,
            true, true, false);

    final XYDifferenceRenderer renderer = new XYDifferenceRenderer(new Color(238, 176, 176),
            new Color(135, 206, 112), true);
    renderer.setSeriesPaint(0, new Color(222, 23, 33));
    renderer.setSeriesPaint(1, new Color(64, 169, 59));
    if (shape != null) {
        renderer.setSeriesShape(0, shape);
        renderer.setSeriesShape(1, shape);
    } else {
        final Shape none = new Rectangle();
        renderer.setSeriesShape(0, none);
        renderer.setSeriesShape(1, none);
    }
    renderer.setSeriesStroke(0, stroke);
    renderer.setSeriesStroke(1, stroke);
    renderer.setSeriesVisibleInLegend(0, false);
    renderer.setSeriesVisibleInLegend(1, false);
    final XYPlot plot = chart.getXYPlot();
    plot.setRenderer(renderer);
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinePaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.lightGray);
    final DateAxis xAxis = new DateAxis();
    xAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    xAxis.setLowerMargin(0.0);
    xAxis.setUpperMargin(0.0);
    xAxis.setVisible(showAxisValues);
    plot.setDomainAxis(xAxis);
    final NumberAxis yAxis;
    if (showAxisValues == true) {
        yAxis = new NumberAxis(PFUserContext.getLocalizedString(valueAxisUnitKey));
    } else {
        yAxis = new NumberAxis();
    }
    yAxis.setVisible(showAxisValues);
    plot.setRangeAxis(yAxis);
    plot.setOutlineVisible(false);
    return chart;
}