Search in sources :

Example 1 with StackedBarRenderer

use of org.jfree.chart.renderer.category.StackedBarRenderer in project pentaho-platform by pentaho.

the class JFreeChartEngine method createBarChart.

private static JFreeChart createBarChart(final CategoryDatasetChartDefinition chartDefinition) {
    // TODO Make the following accessible from the chartDefinition
    String categoryAxisLabel = null;
    String valueAxisLabel = null;
    boolean tooltips = true;
    boolean urls = true;
    // -----------------------------------------------------------
    String title = chartDefinition.getTitle();
    boolean legend = chartDefinition.isLegendIncluded();
    PlotOrientation orientation = chartDefinition.getOrientation();
    CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
    ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
    BarRenderer renderer = null;
    // Determine the type of renderer to use
    if (chartDefinition.isStacked() || chartDefinition.isThreeD()) {
        if (chartDefinition.isStacked() && chartDefinition.isThreeD()) {
            renderer = new StackedBarRenderer3D();
        } else if (chartDefinition.isStacked()) {
            renderer = new StackedBarRenderer();
        } else {
            renderer = new BarRenderer3D();
        }
    } else {
        renderer = new BarRenderer();
    }
    if (orientation == PlotOrientation.HORIZONTAL) {
        ItemLabelPosition position1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT);
        renderer.setPositiveItemLabelPosition(position1);
        ItemLabelPosition position2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_RIGHT);
        renderer.setNegativeItemLabelPosition(position2);
    } else if (orientation == PlotOrientation.VERTICAL) {
        ItemLabelPosition position1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER);
        renderer.setPositiveItemLabelPosition(position1);
        ItemLabelPosition position2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER);
        renderer.setNegativeItemLabelPosition(position2);
    }
    if (tooltips) {
        renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
    }
    if (urls) {
        renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator());
    }
    if (chartDefinition.getMaxBarWidth() != null) {
        renderer.setMaximumBarWidth(chartDefinition.getMaxBarWidth().doubleValue());
    }
    CategoryPlot plot = new CategoryPlot(chartDefinition, categoryAxis, valueAxis, renderer);
    JFreeChartEngine.updatePlot(plot, chartDefinition);
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    return chart;
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) NumberAxis(org.jfree.chart.axis.NumberAxis) StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer) StackedBarRenderer3D(org.jfree.chart.renderer.category.StackedBarRenderer3D) BarRenderer(org.jfree.chart.renderer.category.BarRenderer) StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer) StandardCategoryURLGenerator(org.jfree.chart.urls.StandardCategoryURLGenerator) CategoryPlot(org.jfree.chart.plot.CategoryPlot) JFreeChart(org.jfree.chart.JFreeChart) StackedBarRenderer3D(org.jfree.chart.renderer.category.StackedBarRenderer3D) BarRenderer3D(org.jfree.chart.renderer.category.BarRenderer3D) CategoryAxis(org.jfree.chart.axis.CategoryAxis) StandardCategoryToolTipGenerator(org.jfree.chart.labels.StandardCategoryToolTipGenerator) ValueAxis(org.jfree.chart.axis.ValueAxis) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition)

Example 2 with StackedBarRenderer

use of org.jfree.chart.renderer.category.StackedBarRenderer in project dhis2-core by dhis2.

the class DefaultChartService method getStackedBarRenderer.

/**
 * Returns a stacked bar renderer.
 */
private StackedBarRenderer getStackedBarRenderer() {
    StackedBarRenderer renderer = new StackedBarRenderer();
    for (int i = 0; i < COLORS.length; i++) {
        renderer.setSeriesPaint(i, COLORS[i]);
        renderer.setShadowVisible(false);
    }
    return renderer;
}
Also used : StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer)

Example 3 with StackedBarRenderer

use of org.jfree.chart.renderer.category.StackedBarRenderer in project SIMVA-SoS by SESoS.

the class ChartFactory method createStackedBarChart.

/**
 * Creates a stacked bar chart with default settings.  The chart object
 * returned by this method uses a {@link CategoryPlot} instance as the
 * plot, with a {@link CategoryAxis} for the domain axis, a
 * {@link NumberAxis} as the range axis, and a {@link StackedBarRenderer}
 * as the renderer.
 *
 * @param title  the chart title (<code>null</code> permitted).
 * @param domainAxisLabel  the label for the category axis
 *                         (<code>null</code> permitted).
 * @param rangeAxisLabel  the label for the value axis
 *                        (<code>null</code> permitted).
 * @param dataset  the dataset for the chart (<code>null</code> permitted).
 * @param orientation  the orientation of the chart (horizontal or
 *                     vertical) (<code>null</code> not permitted).
 * @param legend  a flag specifying whether or not a legend is required.
 * @param tooltips  configure chart to generate tool tips?
 * @param urls  configure chart to generate URLs?
 *
 * @return A stacked bar chart.
 */
public static JFreeChart createStackedBarChart(String title, String domainAxisLabel, String rangeAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) {
    ParamChecks.nullNotPermitted(orientation, "orientation");
    CategoryAxis categoryAxis = new CategoryAxis(domainAxisLabel);
    ValueAxis valueAxis = new NumberAxis(rangeAxisLabel);
    StackedBarRenderer renderer = new StackedBarRenderer();
    if (tooltips) {
        renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
    }
    if (urls) {
        renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator());
    }
    CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
    plot.setOrientation(orientation);
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    currentTheme.apply(chart);
    return chart;
}
Also used : NumberAxis(org.jfree.chart.axis.NumberAxis) StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer) CategoryAxis(org.jfree.chart.axis.CategoryAxis) StandardCategoryToolTipGenerator(org.jfree.chart.labels.StandardCategoryToolTipGenerator) ValueAxis(org.jfree.chart.axis.ValueAxis) StandardCategoryURLGenerator(org.jfree.chart.urls.StandardCategoryURLGenerator) CategoryPlot(org.jfree.chart.plot.CategoryPlot)

Example 4 with StackedBarRenderer

use of org.jfree.chart.renderer.category.StackedBarRenderer in project cubrid-manager by CUBRID.

the class CombinedBarTimeSeriesChart method createBarChart.

/**
	 * Create a bar chart instance.
	 * 
	 * @return a bar chart instance
	 */
private JFreeChart createBarChart() {
    bardataset = new DefaultCategoryDataset();
    if (valueMap != null) {
        for (String key : valueMap.keySet()) {
            bardataset.addValue(0D, key, "");
        }
    }
    bardataset.addValue(barMax, "100", "");
    JFreeChart chart = ChartFactory.createStackedBarChart("", "", "", bardataset, PlotOrientation.VERTICAL, false, false, false);
    chart.setBorderVisible(false);
    chart.setBorderStroke(new BasicStroke(0.0f));
    chart.setBackgroundPaint(Color.BLACK);
    //plot
    CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    categoryplot.setOutlineVisible(false);
    RectangleInsets rectangleInsets = new RectangleInsets();
    categoryplot.setAxisOffset(rectangleInsets);
    categoryplot.setBackgroundPaint(Color.BLACK);
    categoryplot.setDomainGridlinesVisible(false);
    categoryplot.setRangeGridlinesVisible(false);
    //renderer
    StackedBarRenderer stackedbarrenderer = (StackedBarRenderer) categoryplot.getRenderer(0);
    stackedbarrenderer.setDrawBarOutline(false);
    stackedbarrenderer.setMaximumBarWidth(0.6);
    stackedbarrenderer.setItemMargin(0);
    //painter
    StandardBarPainter painter = new StandardBarPainter() {

        private static final long serialVersionUID = -3124115075260902181L;

        public void paintBar(Graphics2D g2, BarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base) {
            Paint itemPaint = renderer.getItemPaint(row, column);
            GradientPaintTransformer t = renderer.getGradientPaintTransformer();
            if (t != null && itemPaint instanceof GradientPaint) {
                itemPaint = t.transform((GradientPaint) itemPaint, bar);
            }
            g2.setPaint(itemPaint);
            double height = bar.getHeight();
            double width = bar.getWidth();
            double x = bar.getBounds2D().getX();
            double y = bar.getBounds2D().getY();
            int barNumber = (int) (height / 2 + 0.5);
            if (height < 1 && height > 0.5) {
                barNumber = 1;
            }
            for (int i = 0; i < barNumber; i++) {
                RectangularShape subBarLeft = new Rectangle2D.Double(x, y, width / 2, 0.8);
                g2.fill(subBarLeft);
                RectangularShape subBarRight = new Rectangle2D.Double(x + width / 2 + 1, y, width / 2, 0.8);
                g2.fill(subBarRight);
                y += 2;
            }
            if (renderer.isDrawBarOutline()) {
                Stroke stroke = renderer.getItemOutlineStroke(row, column);
                Paint paint = renderer.getItemOutlinePaint(row, column);
                if (stroke != null && paint != null) {
                    g2.setStroke(stroke);
                    g2.setPaint(paint);
                    g2.draw(bar);
                }
            }
        }
    };
    stackedbarrenderer.setBarPainter(painter);
    stackedbarrenderer.setSeriesPaint(0, Color.GREEN);
    stackedbarrenderer.setSeriesPaint(1, Color.RED);
    int backPaintOrder = 1;
    if (valueMap != null) {
        backPaintOrder = valueMap.size();
    }
    stackedbarrenderer.setSeriesPaint(backPaintOrder, new Color(136, 200, 135));
    stackedbarrenderer.setShadowVisible(false);
    stackedbarrenderer.setDrawBarOutline(false);
    //categoryAxis
    categoryAxis = categoryplot.getDomainAxis();
    categoryAxis.setAxisLineVisible(false);
    //	categoryAxis.setCategoryMargin(0);
    categoryAxis.setMinorTickMarksVisible(false);
    categoryAxis.setTickLabelsVisible(false);
    categoryAxis.setTickMarksVisible(false);
    categoryAxis.setLabelPaint(Color.GREEN);
    categoryAxis.setLabelFont(new Font("", 0, 10));
    //valueAxis
    ValueAxis valueAxis = categoryplot.getRangeAxis();
    valueAxis.setVisible(false);
    return chart;
}
Also used : BasicStroke(java.awt.BasicStroke) BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer) GradientPaintTransformer(org.jfree.ui.GradientPaintTransformer) Color(java.awt.Color) BarRenderer(org.jfree.chart.renderer.category.BarRenderer) StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer) GradientPaint(java.awt.GradientPaint) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) JFreeChart(org.jfree.chart.JFreeChart) CategoryPlot(org.jfree.chart.plot.CategoryPlot) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) StandardBarPainter(org.jfree.chart.renderer.category.StandardBarPainter) ValueAxis(org.jfree.chart.axis.ValueAxis) RectangularShape(java.awt.geom.RectangularShape) RectangleInsets(org.jfree.ui.RectangleInsets) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) RectangleEdge(org.jfree.ui.RectangleEdge)

Example 5 with StackedBarRenderer

use of org.jfree.chart.renderer.category.StackedBarRenderer in project pentaho-platform by pentaho.

the class JFreeChartEngine method createBarLineChart.

private static JFreeChart createBarLineChart(final BarLineChartDefinition chartDefinition) {
    // TODO Make the following accessible from the chartDefinition
    String categoryAxisLabel = null;
    String valueAxisLabel = null;
    String secondValueAxisLabel = null;
    boolean tooltips = true;
    boolean urls = true;
    // -----------------------------------------------------------
    String title = chartDefinition.getTitle();
    boolean legend = chartDefinition.isLegendIncluded();
    PlotOrientation orientation = chartDefinition.getOrientation();
    // split BarLineChartDefinition in two Definitions
    CategoryDatasetChartDefinition barsDataset = new CategoryDatasetChartDefinition(chartDefinition.getSession(), chartDefinition.getChartAttributes());
    CategoryDatasetChartDefinition linesDataset = new CategoryDatasetChartDefinition(chartDefinition.getSession(), chartDefinition.getChartAttributes());
    /*
     * try{ barsDataset = (CategoryDatasetChartDefinition)chartDefinition.clone(); linesDataset =
     * (CategoryDatasetChartDefinition)chartDefinition.clone(); }catch(Exception e){}
     */
    // get column and row count of the data set
    int iColumnCount = chartDefinition.getColumnCount();
    int iRowCount = chartDefinition.getRowCount();
    if (iRowCount <= 0) {
        // $NON-NLS-1$
        chartDefinition.setNoDataMessage(Messages.getInstance().getString("CHART.USER_NO_DATA_AVAILABLE"));
    }
    // Loop through columns
    for (int r = 0; r < iRowCount; r++) {
        // check if measure should be include in bar or line dataset
        String strMeasureName = (String) chartDefinition.getRowKey(r);
        boolean bIsBarColumn = JFreeChartEngine.isBarColumn(chartDefinition.getBarColumns(), strMeasureName);
        boolean bIsLineColumn = JFreeChartEngine.isLineColumn(chartDefinition.getLineColumns(), strMeasureName);
        // getting all values
        for (int c = 0; c < iColumnCount; c++) {
            Comparable compColumnName = chartDefinition.getColumnKey(c);
            Number nValue = chartDefinition.getValue(strMeasureName, compColumnName);
            if (bIsBarColumn) {
                barsDataset.addValue(nValue, strMeasureName, compColumnName);
            }
            if (bIsLineColumn) {
                linesDataset.addValue(nValue, strMeasureName, compColumnName);
            }
        }
    }
    if ((iRowCount > 0) && (barsDataset.getRowCount() <= 0) && (linesDataset.getRowCount() <= 0)) {
        // $NON-NLS-1$
        chartDefinition.setNoDataMessage(Messages.getInstance().getString("CHART.USER_INCORRECT_DATA_FORMAT"));
    }
    // Create Axis Objects
    CategoryAxis catAxis = new CategoryAxis(categoryAxisLabel);
    NumberAxis barsAxis = new NumberAxis(valueAxisLabel);
    NumberAxis linesAxis = new NumberAxis(secondValueAxisLabel);
    // set title and font for lines Axis
    linesDataset.setRangeTitle(chartDefinition.getLinesRangeTitle());
    linesDataset.setRangeTitleFont(chartDefinition.getLinesRangeTitleFont());
    if (chartDefinition.getLinesRangeTickFormat() != null) {
        linesAxis.setNumberFormatOverride(chartDefinition.getLinesRangeTickFormat());
    }
    // create renderer
    BarRenderer barRenderer = null;
    LineAndShapeRenderer lineRenderer = null;
    // Determine the type of renderer to use
    if (chartDefinition.isStacked() || chartDefinition.isThreeD()) {
        if (chartDefinition.isStacked() && chartDefinition.isThreeD()) {
            barRenderer = new StackedBarRenderer3D();
            lineRenderer = new LineRenderer3D();
        } else if (chartDefinition.isStacked()) {
            barRenderer = new StackedBarRenderer();
            lineRenderer = new LineAndShapeRenderer();
        } else {
            barRenderer = new BarRenderer3D();
            lineRenderer = new LineRenderer3D();
        }
    } else {
        barRenderer = new BarRenderer();
        lineRenderer = new LineAndShapeRenderer();
    }
    if (orientation == PlotOrientation.HORIZONTAL) {
        ItemLabelPosition position1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT);
        barRenderer.setPositiveItemLabelPosition(position1);
        lineRenderer.setPositiveItemLabelPosition(position1);
        ItemLabelPosition position2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_RIGHT);
        barRenderer.setNegativeItemLabelPosition(position2);
        lineRenderer.setNegativeItemLabelPosition(position2);
    } else if (orientation == PlotOrientation.VERTICAL) {
        ItemLabelPosition position1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER);
        barRenderer.setPositiveItemLabelPosition(position1);
        lineRenderer.setPositiveItemLabelPosition(position1);
        ItemLabelPosition position2 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER);
        barRenderer.setNegativeItemLabelPosition(position2);
        lineRenderer.setNegativeItemLabelPosition(position2);
    }
    if (tooltips) {
        barRenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
        lineRenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
    }
    if (urls) {
        barRenderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator());
        lineRenderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator());
    }
    if (chartDefinition.getMaxBarWidth() != null) {
        barRenderer.setMaximumBarWidth(chartDefinition.getMaxBarWidth().doubleValue());
    }
    // setting some line attributes
    lineRenderer.setStroke(JFreeChartEngine.getLineStyleStroke(chartDefinition.getLineStyle(), chartDefinition.getLineWidth()));
    lineRenderer.setShapesVisible(chartDefinition.isMarkersVisible());
    lineRenderer.setBaseShapesFilled(chartDefinition.isMarkersVisible());
    /*
     * Create plot and make necessary adjustments for overlaid chart
     */
    // create the plot with bar chart
    CategoryPlot plot = new CategoryPlot(barsDataset, catAxis, barsAxis, barRenderer);
    // add line renderer
    plot.setRenderer(1, lineRenderer);
    // add lines dataset, renderer and axis to plot
    plot.setDataset(1, linesDataset);
    plot.setRangeAxis(1, linesAxis);
    // map lines to second axis
    plot.mapDatasetToRangeAxis(1, 1);
    // set rendering order
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    // set location of second axis
    plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
    // standard settings for plots
    JFreeChartEngine.updatePlot(plot, barsDataset);
    // additional settings for second axis
    ValueAxis secondValueAxis = plot.getRangeAxis(1);
    if (secondValueAxis != null) {
        if (chartDefinition.getLinesRangeTitle() != null) {
            secondValueAxis.setLabel(chartDefinition.getLinesRangeTitle());
        }
        if (chartDefinition.getLinesRangeTitleFont() != null) {
            secondValueAxis.setLabelFont(chartDefinition.getLinesRangeTitleFont());
        }
        if (chartDefinition.getLinesRangeTickFont() != null) {
            secondValueAxis.setTickLabelFont(chartDefinition.getLinesRangeTickFont());
        }
        if (chartDefinition.getLinesRangeMinimum() != ValueAxis.DEFAULT_LOWER_BOUND) {
            secondValueAxis.setLowerBound(chartDefinition.getLinesRangeMinimum());
        }
        if (chartDefinition.getLinesRangeMaximum() != ValueAxis.DEFAULT_UPPER_BOUND) {
            secondValueAxis.setUpperBound(chartDefinition.getLinesRangeMaximum());
        }
    }
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    return chart;
}
Also used : XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) LineAndShapeRenderer(org.jfree.chart.renderer.category.LineAndShapeRenderer) PlotOrientation(org.jfree.chart.plot.PlotOrientation) NumberAxis(org.jfree.chart.axis.NumberAxis) LineRenderer3D(org.jfree.chart.renderer.category.LineRenderer3D) StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer) StackedBarRenderer3D(org.jfree.chart.renderer.category.StackedBarRenderer3D) BarRenderer(org.jfree.chart.renderer.category.BarRenderer) StackedBarRenderer(org.jfree.chart.renderer.category.StackedBarRenderer) StandardCategoryURLGenerator(org.jfree.chart.urls.StandardCategoryURLGenerator) GradientPaint(java.awt.GradientPaint) Paint(java.awt.Paint) TexturePaint(java.awt.TexturePaint) CategoryPlot(org.jfree.chart.plot.CategoryPlot) JFreeChart(org.jfree.chart.JFreeChart) StackedBarRenderer3D(org.jfree.chart.renderer.category.StackedBarRenderer3D) BarRenderer3D(org.jfree.chart.renderer.category.BarRenderer3D) CategoryAxis(org.jfree.chart.axis.CategoryAxis) StandardCategoryToolTipGenerator(org.jfree.chart.labels.StandardCategoryToolTipGenerator) ValueAxis(org.jfree.chart.axis.ValueAxis) ItemLabelPosition(org.jfree.chart.labels.ItemLabelPosition)

Aggregations

StackedBarRenderer (org.jfree.chart.renderer.category.StackedBarRenderer)6 CategoryPlot (org.jfree.chart.plot.CategoryPlot)5 JFreeChart (org.jfree.chart.JFreeChart)4 NumberAxis (org.jfree.chart.axis.NumberAxis)4 ValueAxis (org.jfree.chart.axis.ValueAxis)4 CategoryAxis (org.jfree.chart.axis.CategoryAxis)3 ItemLabelPosition (org.jfree.chart.labels.ItemLabelPosition)3 StandardCategoryToolTipGenerator (org.jfree.chart.labels.StandardCategoryToolTipGenerator)3 BarRenderer (org.jfree.chart.renderer.category.BarRenderer)3 StandardCategoryURLGenerator (org.jfree.chart.urls.StandardCategoryURLGenerator)3 GradientPaint (java.awt.GradientPaint)2 Paint (java.awt.Paint)2 PlotOrientation (org.jfree.chart.plot.PlotOrientation)2 BarRenderer3D (org.jfree.chart.renderer.category.BarRenderer3D)2 StackedBarRenderer3D (org.jfree.chart.renderer.category.StackedBarRenderer3D)2 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Font (java.awt.Font)1 Graphics2D (java.awt.Graphics2D)1 Stroke (java.awt.Stroke)1