Example usage for org.jfree.chart.renderer.category AreaRenderer setSeriesItemLabelGenerator

List of usage examples for org.jfree.chart.renderer.category AreaRenderer setSeriesItemLabelGenerator

Introduction

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

Prototype

@Override
public void setSeriesItemLabelGenerator(int series, CategoryItemLabelGenerator generator) 

Source Link

Document

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

Usage

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 {/*from  w ww.  j  a va2s  .co  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;
}