Example usage for org.jfree.chart.renderer.category GroupedStackedBarRenderer setBaseItemLabelGenerator

List of usage examples for org.jfree.chart.renderer.category GroupedStackedBarRenderer setBaseItemLabelGenerator

Introduction

In this page you can find the example usage for org.jfree.chart.renderer.category GroupedStackedBarRenderer setBaseItemLabelGenerator.

Prototype

@Override
public void setBaseItemLabelGenerator(CategoryItemLabelGenerator generator) 

Source Link

Document

Sets the base item label generator and sends a RendererChangeEvent to all registered listeners.

Usage

From source file:org.jfree.chart.demo.StackedBarChartDemo5.java

private static JFreeChart createChart(CategoryDataset categorydataset) {
    JFreeChart jfreechart = ChartFactory.createStackedBarChart("Stacked Bar Chart Demo 5", "Category", "Value",
            categorydataset, PlotOrientation.VERTICAL, true, true, false);
    GroupedStackedBarRenderer groupedstackedbarrenderer = new GroupedStackedBarRenderer();
    KeyToGroupMap keytogroupmap = new KeyToGroupMap("G1");
    keytogroupmap.mapKeyToGroup("S1", "G1");
    keytogroupmap.mapKeyToGroup("S2", "G1");
    keytogroupmap.mapKeyToGroup("S3", "G2");
    keytogroupmap.mapKeyToGroup("S4", "G3");
    groupedstackedbarrenderer.setSeriesToGroupMap(keytogroupmap);
    groupedstackedbarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    groupedstackedbarrenderer.setBaseItemLabelsVisible(true);
    groupedstackedbarrenderer.setPositiveItemLabelPositionFallback(
            new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
    groupedstackedbarrenderer.setItemMargin(0.10000000000000001D);
    SubCategoryAxis subcategoryaxis = new SubCategoryAxis("Category / Group");
    subcategoryaxis.setCategoryMargin(0.050000000000000003D);
    subcategoryaxis.addSubCategory("G1");
    subcategoryaxis.addSubCategory("G2");
    subcategoryaxis.addSubCategory("G3");
    CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
    categoryplot.setDomainAxis(subcategoryaxis);
    categoryplot.setRenderer(groupedstackedbarrenderer);
    return jfreechart;
}

From source file:net.sf.dynamicreports.design.transformation.chartcustomizer.GroupedStackedBarRendererCustomizer.java

@Override
public void customize(JFreeChart chart, ReportParameters reportParameters) {
    this.seriesColors = new LinkedHashMap<String, Paint>();
    this.map = null;
    Set<String> groups = new LinkedHashSet<String>();
    CategoryDataset dataset = chart.getCategoryPlot().getDataset();

    for (int i = 0; i < dataset.getRowCount(); i++) {
        String rowKey = (String) dataset.getRowKey(i);
        String group = StringUtils.substringBefore(rowKey, GROUP_SERIES_KEY);
        String series = StringUtils.substringAfter(rowKey, GROUP_SERIES_KEY);
        if (map == null) {
            map = new KeyToGroupMap(group);
        }//from  w w w  .  ja  v  a  2  s . c o m
        map.mapKeyToGroup(rowKey, group);
        groups.add(group);
        if (!seriesColors.containsKey(series)) {
            Paint paint = chart.getCategoryPlot().getDrawingSupplier().getNextPaint();
            seriesColors.put(series, paint);
        }
    }

    DefaultCategoryDataset newDataset = new DefaultCategoryDataset();
    for (Object column : dataset.getColumnKeys()) {
        for (String group : groups) {
            for (String series : seriesColors.keySet()) {
                try {
                    Number value = dataset.getValue(group + GROUP_SERIES_KEY + series, (Comparable<?>) column);
                    newDataset.addValue(value, group + GROUP_SERIES_KEY + series, (Comparable<?>) column);
                } catch (UnknownKeyException e) {
                    newDataset.addValue(0, group + GROUP_SERIES_KEY + series, (Comparable<?>) column);
                }
            }

        }
    }
    dataset = newDataset;

    GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
    renderer.setSeriesToGroupMap(map);

    StackedBarRenderer categoryRenderer = (StackedBarRenderer) chart.getCategoryPlot().getRenderer();
    renderer.setBaseItemLabelsVisible(categoryRenderer.getBaseItemLabelsVisible());
    renderer.setBaseItemLabelFont(categoryRenderer.getBaseItemLabelFont());
    renderer.setBaseItemLabelPaint(categoryRenderer.getBaseItemLabelPaint());
    renderer.setBaseItemLabelGenerator(categoryRenderer.getBaseItemLabelGenerator());
    renderer.setShadowVisible(categoryRenderer.getShadowsVisible());

    renderer.setItemMargin(0.10);
    renderer.setDrawBarOutline(false);
    for (int i = 0; i < dataset.getRowCount(); i++) {
        String rowKey = (String) dataset.getRowKey(i);
        String score = StringUtils.substringAfter(rowKey, GROUP_SERIES_KEY);
        renderer.setSeriesPaint(i, seriesColors.get(score));
    }

    CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();
    SubCategoryAxis newDomainAxis = new SubCategoryAxis(domainAxis.getLabel());
    newDomainAxis.setLabelFont(domainAxis.getLabelFont());
    newDomainAxis.setTickLabelFont(domainAxis.getTickLabelFont());
    newDomainAxis.setLabelPaint(domainAxis.getLabelPaint());
    newDomainAxis.setTickLabelPaint(domainAxis.getTickLabelPaint());
    newDomainAxis.setAxisLinePaint(domainAxis.getAxisLinePaint());
    newDomainAxis.setTickMarkPaint(domainAxis.getTickMarkPaint());
    newDomainAxis.setTickLabelsVisible(domainAxis.isTickLabelsVisible());
    newDomainAxis.setTickMarksVisible(domainAxis.isTickMarksVisible());
    newDomainAxis.setCategoryMargin(0.05);
    for (String group : groups) {
        newDomainAxis.addSubCategory(group);
    }

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setDomainAxis(newDomainAxis);
    plot.setRenderer(renderer);

    LegendItemCollection legendItems = new LegendItemCollection();
    for (String item : seriesColors.keySet()) {
        legendItems.add(new LegendItem(item, seriesColors.get(item)));
    }
    plot.setFixedLegendItems(legendItems);

    chart.getCategoryPlot().setDataset(dataset);
}

From source file:it.eng.spagobi.engines.chart.bo.charttypes.barcharts.StackedBarGroup.java

/**
 * Inherited by IChart./*from w  w w .ja v  a  2  s.co  m*/
 * 
 * @param chartTitle the chart title
 * @param dataset the dataset
 * 
 * @return the j free chart
 */

public JFreeChart createChart(DatasetMap datasets) {

    logger.debug("IN");
    CategoryDataset dataset = (CategoryDataset) datasets.getDatasets().get("1");

    logger.debug("Get plot orientaton");
    PlotOrientation plotOrientation = PlotOrientation.VERTICAL;
    if (horizontalView) {
        plotOrientation = PlotOrientation.HORIZONTAL;
    }

    JFreeChart chart = ChartFactory.createStackedBarChart(name, // chart title
            categoryLabel, // domain axis label
            valueLabel, // range axis label
            dataset, // data
            plotOrientation, // the plot orientation
            legend, // legend
            true, // tooltips
            false // urls
    );

    chart.setBackgroundPaint(Color.white);
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(color);
    plot.setRangeGridlinePaint(Color.white);
    plot.setDomainGridlinePaint(Color.white);
    plot.setDomainGridlinesVisible(true);

    GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
    KeyToGroupMap map = new KeyToGroupMap("G1");
    int numElForGroup = 0;
    for (int idx = 0; idx < numGroups.intValue(); idx++) {
        for (int j = 0; j < numSerieForGroup.intValue(); j++) {
            try {
                String tmpSubCat = (String) subCategoryNames.get(j + idx * numSerieForGroup.intValue());
                map.mapKeyToGroup(tmpSubCat, "G" + (idx + 1));

            } catch (Exception e) {
                logger.error("out of range error in inserting in stacked bar group: continue anayway", e);
            }
        }
    }

    renderer.setSeriesToGroupMap(map);
    renderer.setItemMargin(0.0);
    renderer.setDrawBarOutline(false);
    renderer.setBaseItemLabelsVisible(true);
    if (percentageValue)
        renderer.setBaseItemLabelGenerator(
                new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat("#,##.#%")));
    else
        renderer.setBaseItemLabelGenerator(new FilterZeroStandardCategoryItemLabelGenerator());
    renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());

    if (maxBarWidth != null) {
        renderer.setMaximumBarWidth(maxBarWidth.doubleValue());
    }

    boolean document_composition = false;
    if (mode.equalsIgnoreCase(SpagoBIConstants.DOCUMENT_COMPOSITION))
        document_composition = true;

    /*
    MyCategoryUrlGenerator mycatUrl=new MyCategoryUrlGenerator(rootUrl);
    mycatUrl.setDocument_composition(document_composition);
    mycatUrl.setCategoryUrlLabel(categoryUrlName);
    mycatUrl.setSerieUrlLabel(serieUrlname);
            
    renderer.setItemURLGenerator(mycatUrl);
     */

    TextTitle title = setStyleTitle(name, styleTitle);
    chart.setTitle(title);
    if (subName != null && !subName.equals("")) {
        TextTitle subTitle = setStyleTitle(subName, styleSubTitle);
        chart.addSubtitle(subTitle);
    }

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

    // set the background color for the chart...
    chart.setBackgroundPaint(color);

    NumberFormat nf = NumberFormat.getNumberInstance(locale);

    // set the range axis to display integers only...
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setLabelFont(new Font(styleXaxesLabels.getFontName(), Font.PLAIN, styleXaxesLabels.getSize()));
    rangeAxis.setLabelPaint(styleXaxesLabels.getColor());
    rangeAxis
            .setTickLabelFont(new Font(styleXaxesLabels.getFontName(), Font.PLAIN, styleXaxesLabels.getSize()));
    rangeAxis.setTickLabelPaint(styleXaxesLabels.getColor());
    rangeAxis.setNumberFormatOverride(nf);
    if (rangeIntegerValues == true) {
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    }
    if (rangeAxisLocation != null) {
        if (rangeAxisLocation.equalsIgnoreCase("BOTTOM_OR_LEFT")) {
            plot.setRangeAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT);
        } else if (rangeAxisLocation.equalsIgnoreCase("BOTTOM_OR_RIGHT")) {
            plot.setRangeAxisLocation(0, AxisLocation.BOTTOM_OR_RIGHT);
        } else if (rangeAxisLocation.equalsIgnoreCase("TOP_OR_RIGHT")) {
            plot.setRangeAxisLocation(0, AxisLocation.TOP_OR_RIGHT);
        } else if (rangeAxisLocation.equalsIgnoreCase("TOP_OR_LEFT")) {
            plot.setRangeAxisLocation(0, AxisLocation.TOP_OR_LEFT);
        }
    }

    int seriesN = dataset.getRowCount();
    int numSerieColored = 0;

    if (orderColorVector != null && orderColorVector.size() > 0) {
        logger.debug("color serie by SERIES_ORDER_COLORS template specification");
        for (int i = 0; i < seriesN; i++) {
            if (orderColorVector.get(i) != null) {
                Color color = orderColorVector.get(i);
                renderer.setSeriesPaint(i, color);
            }
        }
    } else if (colorMap != null) {
        while (numSerieColored < seriesN) {
            for (int i = 1; i <= colorMap.size(); i++) {
                Color color = (Color) colorMap.get("SER" + i);
                Color gradient = new Color(Integer.decode("#FFFFFF").intValue());
                if (gradientMap != null)
                    gradient = (Color) gradientMap.get("SER" + i);

                if (color != null) {
                    Paint p = new GradientPaint(0.0f, 0.0f, color, 0.0f, 0.0f, gradient);

                    //renderer.setSeriesPaint(numSerieColored, color);
                    renderer.setSeriesPaint(numSerieColored, p);
                }
                numSerieColored++;
            }
        }
    }
    renderer.setGradientPaintTransformer(
            new StandardGradientPaintTransformer(GradientPaintTransformType.HORIZONTAL));

    MyStandardCategoryItemLabelGenerator generator = null;
    if (additionalLabels) {
        generator = new MyStandardCategoryItemLabelGenerator(catSerLabels, "{1}", NumberFormat.getInstance());

        double orient = (-Math.PI / 2.0);
        if (styleValueLabels.getOrientation().equalsIgnoreCase("horizontal")) {
            orient = 0.0;
        }
        renderer.setBaseItemLabelFont(
                new Font(styleValueLabels.getFontName(), Font.PLAIN, styleValueLabels.getSize()));
        renderer.setBaseItemLabelPaint(styleValueLabels.getColor());

        renderer.setBaseItemLabelGenerator(generator);
        renderer.setBaseItemLabelsVisible(true);
        //vertical labels          
        renderer.setBasePositiveItemLabelPosition(
                new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, orient));
        renderer.setBaseNegativeItemLabelPosition(
                new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, orient));

        //horizontal labels
        /*
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
           ItemLabelAnchor.CENTER, TextAnchor.CENTER));
        renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(
           ItemLabelAnchor.CENTER, TextAnchor.CENTER));
         */

    }

    SubCategoryAxis domainAxis = new SubCategoryAxis(categoryLabel + " / " + subCategoryLabel);
    String subCatLabel = "";
    for (int j = 1; j <= numGroups.intValue(); j++) {
        if (subCatLabelsMap != null)
            subCatLabel = (String) subCatLabelsMap.get("CAT" + j);
        else
            subCatLabel = subCategoryLabel;

        domainAxis.addSubCategory(subCatLabel);
        domainAxis
                .setLabelFont(new Font(styleYaxesLabels.getFontName(), Font.PLAIN, styleYaxesLabels.getSize()));
        domainAxis.setLabelPaint(styleYaxesLabels.getColor());
        domainAxis.setTickLabelFont(
                new Font(styleYaxesLabels.getFontName(), Font.PLAIN, styleYaxesLabels.getSize()));
        domainAxis.setTickLabelPaint(styleYaxesLabels.getColor());
    }
    plot.setDomainAxis(domainAxis);
    plot.setRenderer(renderer);

    /*
    domainAxis.setCategoryLabelPositions(
    CategoryLabelPositions.createUpRotationLabelPositions(
          Math.PI / 6.0));
     */
    if (legend == true)
        drawLegend(chart);
    logger.debug("OUT");
    return chart;

}

From source file:org.oscarehr.web.reports.ocan.NeedRatingOverTimeReportGenerator.java

private JFreeChart generateNeedsOverTimeChart() {

    CategoryDataset dataset = this.createDataset();

    JFreeChart chart = ChartFactory.createStackedBarChart("Needs over Time (Consumer and Staff)", // chart title
            "Assessments", // x axis label
            "# of Domain", // y axis label
            dataset, // data
            PlotOrientation.VERTICAL, true, // include legend
            true, // tooltips
            false // urls
    );//from w w  w. ja  v  a  2 s . com

    chart.setBackgroundPaint(Color.LIGHT_GRAY);

    GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
    KeyToGroupMap map = new KeyToGroupMap("G1");
    map.mapKeyToGroup("Consumer (Unknown)", "G1");
    map.mapKeyToGroup("Consumer (No Needs)", "G1");
    map.mapKeyToGroup("Consumer (Met Needs)", "G1");
    map.mapKeyToGroup("Consumer (Unmet Needs)", "G1");

    map.mapKeyToGroup("Staff (Unknown)", "G2");
    map.mapKeyToGroup("Staff (No Needs)", "G2");
    map.mapKeyToGroup("Staff (Met Needs)", "G2");
    map.mapKeyToGroup("Staff (Unmet Needs)", "G2");

    renderer.setSeriesToGroupMap(map);
    renderer.setItemMargin(0.1);
    //renderer.setItemLabelsVisible(true);
    renderer.setMaximumBarWidth(15);

    Paint p1 = Color.GREEN;
    renderer.setSeriesPaint(0, p1);
    renderer.setSeriesPaint(4, p1);
    renderer.setSeriesPaint(7, p1);

    Paint p2 = Color.BLUE;
    renderer.setSeriesPaint(1, p2);
    renderer.setSeriesPaint(5, p2);
    renderer.setSeriesPaint(8, p2);

    Paint p3 = new Color(255, 255, 153);
    renderer.setSeriesPaint(2, p3);
    renderer.setSeriesPaint(6, p3);
    renderer.setSeriesPaint(9, p3);

    Paint p4 = Color.ORANGE;
    renderer.setSeriesPaint(3, p4);
    renderer.setSeriesPaint(7, p4);
    renderer.setSeriesPaint(10, p4);

    SubCategoryAxis domainAxis = new SubCategoryAxis("Assessments");
    domainAxis.setCategoryMargin(0.05);
    domainAxis.addSubCategory("Consumer");
    domainAxis.addSubCategory("Staff");
    domainAxis.setMinorTickMarkInsideLength(10);
    domainAxis.setMinorTickMarkInsideLength(10);
    domainAxis.setMinorTickMarksVisible(true);

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setDomainAxis(domainAxis);
    plot.setRenderer(renderer);

    LegendItemCollection result = new LegendItemCollection();
    LegendItem item1 = new LegendItem("Unknown", Color.GREEN);
    LegendItem item2 = new LegendItem("No Needs", Color.BLUE);
    LegendItem item3 = new LegendItem("Met Needs", new Color(255, 255, 153));
    LegendItem item4 = new LegendItem("Unmet Needs", Color.ORANGE);

    result.add(item1);
    result.add(item2);
    result.add(item3);
    result.add(item4);

    plot.setFixedLegendItems(result);
    plot.setBackgroundPaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.black);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setRange(new Range(0, 24), true, false);
    rangeAxis.setTickUnit(new NumberTickUnit(4));

    plot.getDomainAxis().setCategoryMargin(0.35);

    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBasePositiveItemLabelPosition(
            new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.BASELINE_LEFT));

    return chart;
}