Example usage for org.jfree.chart.labels StandardCategoryItemLabelGenerator StandardCategoryItemLabelGenerator

List of usage examples for org.jfree.chart.labels StandardCategoryItemLabelGenerator StandardCategoryItemLabelGenerator

Introduction

In this page you can find the example usage for org.jfree.chart.labels StandardCategoryItemLabelGenerator StandardCategoryItemLabelGenerator.

Prototype

public StandardCategoryItemLabelGenerator(String labelFormat, NumberFormat formatter,
        NumberFormat percentFormatter) 

Source Link

Document

Creates a new generator with the specified number formatter.

Usage

From source file:org.sonar.plugins.scmstats.charts.StackedBarChart3D.java

private JFreeChart createChart(CategoryDataset categorydataset) {
    JFreeChart jfreechart = ChartFactory.createStackedBarChart3D(null, "Authors", "Activity", categorydataset,
            PlotOrientation.HORIZONTAL, true, true, false);
    CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();

    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    numberaxis.setNumberFormatOverride(new DecimalFormat("0%"));

    StackedBarRenderer3D stackedbarrenderer3d = (StackedBarRenderer3D) categoryplot.getRenderer();
    stackedbarrenderer3d.setRenderAsPercentages(true);
    stackedbarrenderer3d.setDrawBarOutline(false);
    stackedbarrenderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{3}",
            NumberFormat.getIntegerInstance(), new DecimalFormat("0.0%")));
    stackedbarrenderer3d.setBaseItemLabelsVisible(true);
    stackedbarrenderer3d/*  w  ww . j  av  a  2  s.  c o m*/
            .setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
    stackedbarrenderer3d
            .setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));

    stackedbarrenderer3d.setSeriesPaint(0, Color.decode("#66CD00"));
    stackedbarrenderer3d.setSeriesPaint(1, Color.decode("#4F94CD"));
    stackedbarrenderer3d.setSeriesPaint(2, Color.decode("#FF4040"));
    return jfreechart;
}

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

private JFreeChart createChart(CategoryDataset categorydataset) {
    JFreeChart jfreechart = ChartFactory.createStackedBarChart3D("Stacked Bar Chart 3D Demo 2", "Category",
            "Value", categorydataset, PlotOrientation.VERTICAL, true, true, false);
    CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
    StackedBarRenderer3D stackedbarrenderer3d = (StackedBarRenderer3D) categoryplot.getRenderer();
    stackedbarrenderer3d.setRenderAsPercentages(true);
    stackedbarrenderer3d.setDrawBarOutline(false);
    stackedbarrenderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{3}",
            NumberFormat.getIntegerInstance(), new DecimalFormat("0.0%")));
    stackedbarrenderer3d.setBaseItemLabelsVisible(true);
    stackedbarrenderer3d/*from   w w  w.  j  a  v a2  s. com*/
            .setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
    stackedbarrenderer3d
            .setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
    return jfreechart;
}

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

private JFreeChart createChart(CategoryDataset categorydataset) {
    JFreeChart jfreechart = ChartFactory.createStackedBarChart3D("Stacked Bar Chart 3D Demo 4", "Category",
            "Value", categorydataset, PlotOrientation.HORIZONTAL, true, true, false);
    CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
    StackedBarRenderer3D stackedbarrenderer3d = (StackedBarRenderer3D) categoryplot.getRenderer();
    stackedbarrenderer3d.setRenderAsPercentages(true);
    stackedbarrenderer3d.setDrawBarOutline(false);
    stackedbarrenderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{3}",
            NumberFormat.getIntegerInstance(), new DecimalFormat("0.0%")));
    stackedbarrenderer3d.setBaseItemLabelsVisible(true);
    stackedbarrenderer3d/*w ww. j a  v  a2s .c o m*/
            .setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
    stackedbarrenderer3d
            .setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
    return jfreechart;
}

From source file:ro.nextreports.engine.chart.JFreeChartExporter.java

private JFreeChart createBarChart(boolean horizontal, boolean stacked, boolean isCombo) throws QueryException {
    barDataset = new DefaultCategoryDataset();
    String chartTitle = replaceParameters(chart.getTitle().getTitle());
    chartTitle = StringUtil.getI18nString(chartTitle, I18nUtil.getLanguageByName(chart, language));
    Object[] charts;/*ww  w.j a  v a  2s .  co  m*/
    List<String> legends;
    Object[] lineCharts = null;
    String lineLegend = null;
    if (isCombo) {
        lineCharts = new Object[1];
        if (chart.getYColumnsLegends().size() < chart.getYColumns().size()) {
            lineLegend = "";
        } else {
            lineLegend = chart.getYColumnsLegends().get(chart.getYColumns().size() - 1);
        }
        charts = new Object[chart.getYColumns().size() - 1];
        legends = chart.getYColumnsLegends().subList(0, chart.getYColumns().size() - 1);
    } else {
        charts = new Object[chart.getYColumns().size()];
        legends = chart.getYColumnsLegends();
    }
    boolean hasLegend = false;
    for (int i = 0; i < charts.length; i++) {
        String legend = "";
        try {
            legend = replaceParameters(legends.get(i));
            legend = StringUtil.getI18nString(legend, I18nUtil.getLanguageByName(chart, language));
        } catch (IndexOutOfBoundsException ex) {
            // no legend set
        }
        // Important : must have default different legends used in barDataset.addValue
        if ((legend == null) || "".equals(legend.trim())) {
            legend = DEFAULT_LEGEND_PREFIX + String.valueOf(i + 1);
        } else {
            hasLegend = true;
        }
        charts[i] = legend;
    }
    if (isCombo) {
        String leg = "";
        if (lineLegend != null) {
            leg = replaceParameters(lineLegend);
        }
        lineCharts[0] = leg;
    }

    byte style = chart.getType().getStyle();
    JFreeChart jfreechart;

    String xLegend = StringUtil.getI18nString(replaceParameters(chart.getXLegend().getTitle()),
            I18nUtil.getLanguageByName(chart, language));
    String yLegend = StringUtil.getI18nString(replaceParameters(chart.getYLegend().getTitle()),
            I18nUtil.getLanguageByName(chart, language));
    PlotOrientation plotOrientation = horizontal ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL;
    if (stacked) {
        jfreechart = ChartFactory.createStackedBarChart(chartTitle, // chart title
                xLegend, // x-axis Label
                yLegend, // y-axis Label
                barDataset, // data
                plotOrientation, // orientation
                true, // include legend
                true, // tooltips
                false // URLs
        );
    } else {
        switch (style) {
        case ChartType.STYLE_BAR_PARALLELIPIPED:
        case ChartType.STYLE_BAR_CYLINDER:
            jfreechart = ChartFactory.createBarChart3D(chartTitle, // chart title
                    xLegend, // x-axis Label
                    yLegend, // y-axis Label
                    barDataset, // data
                    plotOrientation, // orientation
                    true, // include legend
                    true, // tooltips
                    false // URLs
            );
            break;
        default:
            jfreechart = ChartFactory.createBarChart(chartTitle, // chart title
                    xLegend, // x-axis Label
                    yLegend, // y-axis Label
                    barDataset, // data
                    plotOrientation, // orientation
                    true, // include legend
                    true, // tooltips
                    false // URLs
            );
            break;
        }
    }

    if (style == ChartType.STYLE_BAR_CYLINDER) {
        ((CategoryPlot) jfreechart.getPlot()).setRenderer(new CylinderRenderer());
    }

    // hide legend if necessary
    if (!hasLegend) {
        jfreechart.removeLegend();
    }

    // hide border
    jfreechart.setBorderVisible(false);

    // title
    setTitle(jfreechart);

    // chart colors & values shown on bars
    boolean showValues = (chart.getShowYValuesOnChart() == null) ? false : chart.getShowYValuesOnChart();
    CategoryPlot plot = (CategoryPlot) jfreechart.getPlot();
    plot.setForegroundAlpha(transparency);
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);
    DecimalFormat decimalformat;
    DecimalFormat percentageFormat;
    if (chart.getYTooltipPattern() == null) {
        decimalformat = new DecimalFormat("#");
        percentageFormat = new DecimalFormat("0.00%");
    } else {
        decimalformat = new DecimalFormat(chart.getYTooltipPattern());
        percentageFormat = decimalformat;
    }
    for (int i = 0; i < charts.length; i++) {
        renderer.setSeriesPaint(i, chart.getForegrounds().get(i));
        if (showValues) {
            renderer.setSeriesItemLabelsVisible(i, true);
            renderer.setSeriesItemLabelGenerator(i,
                    new StandardCategoryItemLabelGenerator("{2}", decimalformat, percentageFormat));
        }
    }

    if (showValues) {
        // increase a little bit the range axis to view all item label values over bars
        plot.getRangeAxis().setUpperMargin(0.2);
    }

    // grid axis visibility & colors 
    if ((chart.getXShowGrid() != null) && !chart.getXShowGrid()) {
        plot.setDomainGridlinesVisible(false);
    } else {
        if (chart.getXGridColor() != null) {
            plot.setDomainGridlinePaint(chart.getXGridColor());
        } else {
            plot.setDomainGridlinePaint(Color.BLACK);
        }
    }
    if ((chart.getYShowGrid() != null) && !chart.getYShowGrid()) {
        plot.setRangeGridlinesVisible(false);
    } else {
        if (chart.getYGridColor() != null) {
            plot.setRangeGridlinePaint(chart.getYGridColor());
        } else {
            plot.setRangeGridlinePaint(Color.BLACK);
        }
    }

    // chart background
    plot.setBackgroundPaint(chart.getBackground());

    // labels color
    plot.getDomainAxis().setTickLabelPaint(chart.getXColor());
    plot.getRangeAxis().setTickLabelPaint(chart.getYColor());

    // legend color
    plot.getDomainAxis().setLabelPaint(chart.getXLegend().getColor());
    plot.getRangeAxis().setLabelPaint(chart.getYLegend().getColor());

    // legend font
    plot.getDomainAxis().setLabelFont(chart.getXLegend().getFont());
    plot.getRangeAxis().setLabelFont(chart.getYLegend().getFont());

    // axis color
    plot.getDomainAxis().setAxisLinePaint(chart.getxAxisColor());
    plot.getRangeAxis().setAxisLinePaint(chart.getyAxisColor());

    // hide labels
    if ((chart.getXShowLabel() != null) && !chart.getXShowLabel()) {
        plot.getDomainAxis().setTickLabelsVisible(false);
        plot.getDomainAxis().setTickMarksVisible(false);
    }
    if ((chart.getYShowLabel() != null) && !chart.getYShowLabel()) {
        plot.getRangeAxis().setTickLabelsVisible(false);
        plot.getRangeAxis().setTickMarksVisible(false);
    }

    if (chart.getType().getStyle() == ChartType.STYLE_NORMAL) {
        // no shadow
        renderer.setShadowVisible(false);
        // no gradient
        renderer.setBarPainter(new StandardBarPainter());
    }

    // label orientation
    CategoryAxis domainAxis = plot.getDomainAxis();
    if (chart.getXorientation() == Chart.VERTICAL) {
        domainAxis
                .setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2));
    } else if (chart.getXorientation() == Chart.DIAGONAL) {
        domainAxis
                .setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4));
    } else if (chart.getXorientation() == Chart.HALF_DIAGONAL) {
        domainAxis
                .setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 8));
    }

    // labels fonts
    domainAxis.setTickLabelFont(chart.getXLabelFont());
    plot.getRangeAxis().setTickLabelFont(chart.getYLabelFont());

    createChart(plot.getRangeAxis(), charts);

    if (isCombo) {
        addLineChartOverBar(jfreechart, lineCharts, lineLegend);
    }

    return jfreechart;
}

From source file:ro.nextreports.engine.chart.JFreeChartExporter.java

private JFreeChart addLineChartOverBar(JFreeChart jfreechart, Object[] lineCharts, String lineLegend)
        throws QueryException {
    // first we read data for bar series, so we have to go back at the start of the result set
    try {// w ww .  j a va2s.c o m
        result.getResultSet().beforeFirst();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    isLineCombo = true;
    lineBarDataset = new DefaultCategoryDataset();
    boolean hasLegend = false;
    for (int i = 0; i < lineCharts.length; i++) {
        String legend = "";
        try {
            legend = replaceParameters(lineLegend);
        } catch (IndexOutOfBoundsException ex) {
            // no legend set
        }
        if ((legend != null) && !"".equals(legend.trim())) {
            hasLegend = true;
        }
        lineCharts[i] = legend;
    }

    int index = chart.getYColumns().size() - 1;
    CategoryPlot plot = jfreechart.getCategoryPlot();
    final LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
    plot.setRenderer(1, renderer2);
    renderer2.setSeriesPaint(0, chart.getForegrounds().get(index));

    final ValueAxis axis2 = new NumberAxis("");
    plot.setRangeAxis(1, axis2);
    plot.setDataset(1, lineBarDataset);
    plot.mapDatasetToRangeAxis(1, 1);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    boolean showValues = (chart.getShowYValuesOnChart() == null) ? false : chart.getShowYValuesOnChart();
    DecimalFormat decimalFormat;
    DecimalFormat percentageFormat;
    if (chart.getYTooltipPattern() == null) {
        decimalFormat = new DecimalFormat("#");
        percentageFormat = new DecimalFormat("0.00%");
    } else {
        decimalFormat = new DecimalFormat(chart.getYTooltipPattern());
        percentageFormat = decimalFormat;
    }
    if (showValues) {
        renderer2.setSeriesItemLabelsVisible(0, true);
        renderer2.setSeriesItemLabelGenerator(0,
                new StandardCategoryItemLabelGenerator("{2}", decimalFormat, percentageFormat));
        // increase a little bit the range axis to view all item label values over points
        plot.getRangeAxis(1).setUpperMargin(0.2);
    }

    final HashMap<String, String> formatValues = createChart(chart.getYColumns().subList(index, index + 1),
            plot.getRangeAxis(1), lineCharts);

    isLineCombo = false;
    return jfreechart;
}

From source file:ro.nextreports.engine.chart.JFreeChartExporter.java

private JFreeChart createAreaChart() throws QueryException {
    barDataset = new DefaultCategoryDataset();
    String chartTitle = replaceParameters(chart.getTitle().getTitle());
    chartTitle = StringUtil.getI18nString(chartTitle, I18nUtil.getLanguageByName(chart, language));
    Object[] charts = new Object[chart.getYColumns().size()];
    List<String> legends = chart.getYColumnsLegends();
    boolean hasLegend = false;
    for (int i = 0; i < charts.length; i++) {
        String legend = "";
        try {// w ww.  j  ava2s .  c o m
            legend = replaceParameters(legends.get(i));
            legend = StringUtil.getI18nString(legend, I18nUtil.getLanguageByName(chart, language));
        } catch (IndexOutOfBoundsException ex) {
            // no legend set
        }
        // Important : must have default different legends used in barDataset.addValue
        if ((legend == null) || "".equals(legend.trim())) {
            legend = DEFAULT_LEGEND_PREFIX + String.valueOf(i + 1);
        } else {
            hasLegend = true;
        }
        charts[i] = legend;
    }

    String xLegend = StringUtil.getI18nString(replaceParameters(chart.getXLegend().getTitle()),
            I18nUtil.getLanguageByName(chart, language));
    String yLegend = StringUtil.getI18nString(replaceParameters(chart.getYLegend().getTitle()),
            I18nUtil.getLanguageByName(chart, language));
    byte style = chart.getType().getStyle();
    JFreeChart jfreechart = ChartFactory.createAreaChart("Area Chart", // chart title
            xLegend, // x-axis Label
            yLegend, // y-axis Label
            barDataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips
            false // urls
    );

    // hide legend if necessary
    if (!hasLegend) {
        jfreechart.removeLegend();
    }

    // hide border
    jfreechart.setBorderVisible(false);

    // title
    setTitle(jfreechart);

    // chart colors & values shown on bars
    boolean showValues = (chart.getShowYValuesOnChart() == null) ? false : chart.getShowYValuesOnChart();
    CategoryPlot plot = (CategoryPlot) jfreechart.getPlot();
    plot.setForegroundAlpha(transparency);
    AreaRenderer renderer = (AreaRenderer) plot.getRenderer();
    DecimalFormat decimalformat;
    DecimalFormat percentageFormat;
    if (chart.getYTooltipPattern() == null) {
        decimalformat = new DecimalFormat("#");
        percentageFormat = new DecimalFormat("0.00%");
    } else {
        decimalformat = new DecimalFormat(chart.getYTooltipPattern());
        percentageFormat = decimalformat;
    }
    for (int i = 0; i < charts.length; i++) {
        renderer.setSeriesPaint(i, chart.getForegrounds().get(i));
        if (showValues) {
            renderer.setSeriesItemLabelsVisible(i, true);
            renderer.setSeriesItemLabelGenerator(i,
                    new StandardCategoryItemLabelGenerator("{2}", decimalformat, percentageFormat));
        }
    }

    if (showValues) {
        // increase a little bit the range axis to view all item label values over bars
        plot.getRangeAxis().setUpperMargin(0.2);
    }

    // grid axis visibility & colors 
    if ((chart.getXShowGrid() != null) && !chart.getXShowGrid()) {
        plot.setDomainGridlinesVisible(false);
    } else {
        if (chart.getXGridColor() != null) {
            plot.setDomainGridlinePaint(chart.getXGridColor());
        } else {
            plot.setDomainGridlinePaint(Color.BLACK);
        }
    }
    if ((chart.getYShowGrid() != null) && !chart.getYShowGrid()) {
        plot.setRangeGridlinesVisible(false);
    } else {
        if (chart.getYGridColor() != null) {
            plot.setRangeGridlinePaint(chart.getYGridColor());
        } else {
            plot.setRangeGridlinePaint(Color.BLACK);
        }
    }

    // chart background
    plot.setBackgroundPaint(chart.getBackground());

    // labels color
    plot.getDomainAxis().setTickLabelPaint(chart.getXColor());
    plot.getRangeAxis().setTickLabelPaint(chart.getYColor());

    // legend color
    plot.getDomainAxis().setLabelPaint(chart.getXLegend().getColor());
    plot.getRangeAxis().setLabelPaint(chart.getYLegend().getColor());

    // legend font
    plot.getDomainAxis().setLabelFont(chart.getXLegend().getFont());
    plot.getRangeAxis().setLabelFont(chart.getYLegend().getFont());

    // axis color
    plot.getDomainAxis().setAxisLinePaint(chart.getxAxisColor());
    plot.getRangeAxis().setAxisLinePaint(chart.getyAxisColor());

    // hide labels
    if ((chart.getXShowLabel() != null) && !chart.getXShowLabel()) {
        plot.getDomainAxis().setTickLabelsVisible(false);
        plot.getDomainAxis().setTickMarksVisible(false);
    }
    if ((chart.getYShowLabel() != null) && !chart.getYShowLabel()) {
        plot.getRangeAxis().setTickLabelsVisible(false);
        plot.getRangeAxis().setTickMarksVisible(false);
    }

    // label orientation
    CategoryAxis domainAxis = plot.getDomainAxis();
    if (chart.getXorientation() == Chart.VERTICAL) {
        domainAxis
                .setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2));
    } else if (chart.getXorientation() == Chart.DIAGONAL) {
        domainAxis
                .setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4));
    } else if (chart.getXorientation() == Chart.HALF_DIAGONAL) {
        domainAxis
                .setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 8));
    }

    // labels fonts
    plot.getDomainAxis().setTickLabelFont(chart.getXLabelFont());
    plot.getRangeAxis().setTickLabelFont(chart.getYLabelFont());

    createChart(plot.getRangeAxis(), charts);

    return jfreechart;
}