Example usage for org.jfree.chart.plot PiePlot setSectionPaint

List of usage examples for org.jfree.chart.plot PiePlot setSectionPaint

Introduction

In this page you can find the example usage for org.jfree.chart.plot PiePlot setSectionPaint.

Prototype

public void setSectionPaint(int section, Paint paint) 

Source Link

Document

Sets the paint used to fill a section of the pie and sends a PlotChangeEvent to all registered listeners.

Usage

From source file:lucee.runtime.tag.Chart.java

private void chartPie() throws PageException, IOException {
    // do dataset
    DefaultPieDataset dataset = new DefaultPieDataset();
    ChartSeriesBean csb = _series.get(0);

    ChartDataBean cdb;//from   w  ww  .j a v  a  2s.  c o m

    List datas = csb.getDatas();
    if (sortxaxis)
        Collections.sort(datas);
    Iterator itt = datas.iterator();
    while (itt.hasNext()) {
        cdb = (ChartDataBean) itt.next();
        dataset.setValue(cdb.getItemAsString(), cdb.getValue());
    }

    JFreeChart chart = show3d ? ChartFactory.createPieChart3D(title, dataset, false, true, true)
            : ChartFactory.createPieChart(title, dataset, false, true, true);

    Plot p = chart.getPlot();
    PiePlot pp = (PiePlot) p;

    Font _font = getFont();
    pp.setLegendLabelGenerator(new PieSectionLegendLabelGeneratorImpl(_font, chartwidth));
    pp.setBaseSectionOutlinePaint(Color.GRAY); // border
    pp.setLegendItemShape(new Rectangle(7, 7));
    pp.setLabelFont(new Font(font, 0, 11));
    pp.setLabelLinkPaint(COLOR_333333);
    pp.setLabelLinkMargin(-0.05);
    pp.setInteriorGap(0.123);
    pp.setLabelGenerator(new PieSectionLabelGeneratorImpl(labelFormat));

    databackgroundcolor = backgroundcolor;

    setBackground(chart, p);
    setBorder(chart, p);
    setLegend(chart, p, _font);
    set3d(p);
    setFont(chart, _font);
    setTooltip(chart);
    setScale(chart);

    // Slice Type and colors
    boolean doSclice = pieslicestyle == PIE_SLICE_STYLE_SLICED;
    Color[] colors = csb.getColorlist();
    Iterator it = csb.getDatas().iterator();
    int count = 0;
    while (it.hasNext()) {
        cdb = (ChartDataBean) it.next();
        if (doSclice)
            pp.setExplodePercent(cdb.getItemAsString(), 0.13);

        if (count < colors.length) {
            pp.setSectionPaint(cdb.getItemAsString(), colors[count]);
        }
        count++;
    }

    writeOut(chart);
}

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

private JFreeChart createPieChart() throws QueryException {
    pieDataset = new DefaultPieDataset();
    String chartTitle = replaceParameters(chart.getTitle().getTitle());
    chartTitle = StringUtil.getI18nString(chartTitle, I18nUtil.getLanguageByName(chart, language));
    JFreeChart jfreechart = ChartFactory.createPieChart(chartTitle, pieDataset, true, true, false);

    // hide border
    jfreechart.setBorderVisible(false);//from   w w w .j  a v a  2s .c  om

    // title
    setTitle(jfreechart);

    PiePlot plot = (PiePlot) jfreechart.getPlot();
    plot.setForegroundAlpha(transparency);
    // a start angle used to create similarities between flash chart and this jfreechart
    plot.setStartAngle(330);
    // legend label will contain the text and the value
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0} = {1}"));

    // no shadow
    plot.setShadowXOffset(0);
    plot.setShadowYOffset(0);

    DecimalFormat decimalformat;
    DecimalFormat percentageFormat;
    if (chart.getYTooltipPattern() == null) {
        decimalformat = new DecimalFormat("#");
        percentageFormat = new DecimalFormat("0.00%");
    } else {
        decimalformat = new DecimalFormat(chart.getYTooltipPattern());
        percentageFormat = decimalformat;
    }
    boolean showValues = (chart.getShowYValuesOnChart() == null) ? false : chart.getShowYValuesOnChart();
    if (showValues) {
        // label will contain also the percentage formatted with two decimals
        plot.setLabelGenerator(
                new StandardPieSectionLabelGenerator("{0} ({2})", decimalformat, percentageFormat));
    }

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

    createChart(null, new Object[1]);

    // after chart creation we can set slices colors
    List<Comparable> keys = pieDataset.getKeys();
    List<Color> colors = chart.getForegrounds();
    for (int i = 0, size = colors.size(); i < keys.size(); i++) {
        plot.setSectionPaint(keys.get(i), colors.get(i % size));
        plot.setLabelFont(chart.getFont());
    }

    return jfreechart;
}

From source file:edu.jhuapl.graphs.jfreechart.JFreeChartPieGraphSource.java

@SuppressWarnings("unchecked")
@Override//from   w ww  .ja v  a 2s. c o m
public void initialize() throws GraphException {
    String title = getParam(GraphSource.GRAPH_TITLE, String.class, DEFAULT_TITLE);
    boolean legend = getParam(GraphSource.GRAPH_LEGEND, Boolean.class, DEFAULT_GRAPH_LEGEND);
    boolean graphToolTip = getParam(GraphSource.GRAPH_TOOL_TIP, Boolean.class, DEFAULT_GRAPH_TOOL_TIP);
    boolean graphDisplayLabel = getParam(GraphSource.GRAPH_DISPLAY_LABEL, Boolean.class, false);
    boolean legendBorder = getParam(GraphSource.LEGEND_BORDER, Boolean.class, DEFAULT_LEGEND_BORDER);
    boolean graphBorder = getParam(GraphSource.GRAPH_BORDER, Boolean.class, DEFAULT_GRAPH_BORDER);
    Font titleFont = getParam(GraphSource.GRAPH_FONT, Font.class, DEFAULT_GRAPH_TITLE_FONT);
    String noDataMessage = getParam(GraphSource.GRAPH_NO_DATA_MESSAGE, String.class,
            DEFAULT_GRAPH_NO_DATA_MESSAGE);
    PieGraphData pieGraphData = makeDataSet();
    Map<Comparable, Paint> colors = pieGraphData.colors;

    this.chart = ChartFactory.createPieChart(title, pieGraphData.data, false, graphToolTip, false);

    Paint backgroundColor = getParam(GraphSource.BACKGROUND_COLOR, Paint.class, DEFAULT_BACKGROUND_COLOR);
    Paint plotColor = getParam(JFreeChartTimeSeriesGraphSource.PLOT_COLOR, Paint.class, backgroundColor);
    Paint labelColor = getParam(JFreeChartTimeSeriesGraphSource.GRAPH_LABEL_BACKGROUND_COLOR, Paint.class,
            DEFAULT_BACKGROUND_COLOR);

    this.chart.setBackgroundPaint(backgroundColor);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(plotColor);
    plot.setNoDataMessage(noDataMessage);

    if (!graphDisplayLabel) {
        plot.setLabelGenerator(null);
    } else {
        plot.setInteriorGap(0.001);
        plot.setMaximumLabelWidth(.3);
        //           plot.setIgnoreNullValues(true);
        plot.setIgnoreZeroValues(true);
        plot.setShadowPaint(null);
        //           plot.setOutlineVisible(false);
        //TODO use title font?
        Font font = plot.getLabelFont();
        plot.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
        plot.setLabelFont(font.deriveFont(font.getSize2D() * .75f));
        plot.setLabelBackgroundPaint(labelColor);
        plot.setLabelShadowPaint(null);
        plot.setLabelOutlinePaint(null);
        plot.setLabelGap(0.001);
        plot.setLabelLinkMargin(0.0);
        plot.setLabelLinksVisible(true);
        //           plot.setSimpleLabels(true);
        //           plot.setCircular(true);
    }

    if (!graphBorder) {
        plot.setOutlineVisible(false);
    }

    if (title != null && !"".equals(title)) {
        TextTitle title1 = new TextTitle();
        title1.setText(title);
        title1.setFont(titleFont);
        title1.setPadding(3, 2, 5, 2);
        chart.setTitle(title1);
    } else {
        chart.setTitle((TextTitle) null);
    }
    plot.setLabelPadding(new RectangleInsets(1, 1, 1, 1));
    //Makes a wrapper for the legend to remove the border around it
    if (legend) {
        LegendTitle legend1 = new LegendTitle(chart.getPlot());
        BlockContainer wrapper = new BlockContainer(new BorderArrangement());

        if (legendBorder) {
            wrapper.setFrame(new BlockBorder(1, 1, 1, 1));
        } else {
            wrapper.setFrame(new BlockBorder(0, 0, 0, 0));
        }

        BlockContainer items = legend1.getItemContainer();
        items.setPadding(2, 10, 5, 2);
        wrapper.add(items);
        legend1.setWrapper(wrapper);
        legend1.setPosition(RectangleEdge.BOTTOM);
        legend1.setHorizontalAlignment(HorizontalAlignment.CENTER);

        if (params.get(GraphSource.LEGEND_FONT) instanceof Font) {
            legend1.setItemFont(((Font) params.get(GraphSource.LEGEND_FONT)));
        }

        chart.addSubtitle(legend1);
        plot.setLegendLabelGenerator(new PieGraphLabelGenerator());
    }

    for (Comparable category : colors.keySet()) {
        plot.setSectionPaint(category, colors.get(category));
    }

    plot.setToolTipGenerator(new PieGraphToolTipGenerator(pieGraphData));
    plot.setURLGenerator(new PieGraphURLGenerator(pieGraphData));

    initialized = true;
}

From source file:com.appnativa.rare.ui.chart.jfreechart.ChartHandler.java

private void customizePiePlot(ChartDefinition cd, PiePlot plot, PieCollection pie) {
    PlotInformation pi = cd.getPlotInformation();

    plot.setBackgroundPaint(null);// w w  w .  ja v a2s. c o  m
    plot.setOutlineVisible(false);
    customizeBasicPlot(plot, pi);

    List<RenderableDataItem> rows = cd.getSeries();
    int len = (rows == null) ? 0 : rows.size();
    ChartDataItem di;
    iComponentPainter bp;
    Paint p = null;

    if (len > 0) {
        rows = rows.get(0).getItems();
        len = rows.size();
    }

    for (int i = 0; i < len; i++) {
        di = (ChartDataItem) rows.get(i);
        bp = di.getComponentPainter();

        iPlatformPaint pp;

        if (bp == null) {
            p = di.getBackground();
        } else if (bp.getBackgroundPainter() != null) {
            pp = bp.getBackgroundPainter().getPaint(100, 100);
            p = (pp == null) ? null : pp.getPaint();
        }

        if (p == null) {
            p = getDefaultColor(i);
        }

        plot.setSectionPaint(di.getDomainValue().toString(), p);
    }

    if (pie.getExplodedPiece() != null) {
        plot.setExplodePercent(pie.getExplodedPiece(), 0.50);
    }

    plot.setLabelBackgroundPaint(null);
    plot.setLabelOutlinePaint(null);
    plot.setLabelShadowPaint(null);

    UIColor fg = cd.getTextColor(plotLabelColor);
    UIFont font = cd.getTextFont(plotLabelFont);

    plot.setLabelFont(font);
    plot.setLabelPaint(fg);

    if (cd.isShowPlotLabels()) {
        String format = (pi == null) ? null : pi.getLabelsFormat();

        plot.setLabelGenerator(new PieLabelGenerator(cd, format));
    }

    if (cd.isShowToolTips()) {
        plot.setToolTipGenerator(new PieToolTipLabelGenerator(cd, false));
    }
}

From source file:com.naryx.tagfusion.cfm.tag.awt.cfCHART.java

private PiePlot getPiePlot(List<cfCHARTSERIESData> series, String pieSliceStyle, boolean bShow3D)
        throws cfmBadFileException {
    org.jfree.data.general.DefaultPieDataset dataset = new org.jfree.data.general.DefaultPieDataset();
    cfCHARTSERIESData seriesData = series.get(0);
    int num = seriesData.getNumItems();
    for (int j = 0; j < num; j++) {
        dataset.setValue(seriesData.getItemName(j), seriesData.getItemValue(j));
    }/*from w ww  . j  ava  2s.c  om*/

    // Create a pie plot
    PiePlot plot;
    if (bShow3D) {
        plot = new PiePlot3D(dataset);
        ((PiePlot3D) plot).setDepthFactor(0.1);
    } else {
        if (seriesData.getType().equals("pie")) {
            // It's a 2D pie chart
            plot = new PiePlot(dataset);
        } else {
            // It's a 2D ring chart
            plot = new org.jfree.chart.plot.RingPlot(dataset);
            ((org.jfree.chart.plot.RingPlot) plot).setSeparatorsVisible(false);
        }
    }

    // If a colorList attribute was specified on the cfchartseries tag
    // then set the section paint for each slice of the pie.
    List<String> colorList = seriesData.getColorList();
    if (colorList != null) {
        for (int i = 0; i < colorList.size(); i++) {
            plot.setSectionPaint(i, convertStringToColor(colorList.get(i)));
        }
    }

    // Don't display a shadow
    plot.setShadowPaint(null);

    // Only display the name in the Legend
    plot.setLegendLabelGenerator(new org.jfree.chart.labels.StandardPieSectionLabelGenerator("{0}"));

    // Don't draw an outline around the chart
    plot.setOutlinePaint(null);

    if (pieSliceStyle.equals("sliced")) {
        for (int j = 0; j < num; j++)
            plot.setExplodePercent(j, .1);
    }

    return plot;
}

From source file:org.jfree.chart.ChartFactory.java

/**
 * Creates a pie chart with default settings that compares 2 datasets.
 * The color of each section will be determined by the move from the value
 * for the same key in {@code previousDataset}. ie if value1 &gt; value2
 * then the section will be in green (unless {@code greenForIncrease}
 * is {@code false}, in which case it would be {@code red}).
 * Each section can have a shade of red or green as the difference can be
 * tailored between 0% (black) and percentDiffForMaxScale% (bright
 * red/green)./*from w w w . j  ava  2  s.  com*/
 * <p>
 * For instance if {@code percentDiffForMaxScale} is 10 (10%), a
 * difference of 5% will have a half shade of red/green, a difference of
 * 10% or more will have a maximum shade/brightness of red/green.
 * <P>
 * The chart object returned by this method uses a {@link PiePlot} instance
 * as the plot.
 * <p>
 * Written by <a href="mailto:opensource@objectlab.co.uk">Benoit
 * Xhenseval</a>.
 *
 * @param title  the chart title ({@code null} permitted).
 * @param dataset  the dataset for the chart ({@code null} permitted).
 * @param previousDataset  the dataset for the last run, this will be used
 *                         to compare each key in the dataset
 * @param percentDiffForMaxScale scale goes from bright red/green to black,
 *                               percentDiffForMaxScale indicate the change
 *                               required to reach top scale.
 * @param greenForIncrease  an increase since previousDataset will be
 *                          displayed in green (decrease red) if true.
 * @param locale  the locale ({@code null} not permitted).
 * @param subTitle displays a subtitle with color scheme if true
 * @param showDifference  create a new dataset that will show the %
 *                        difference between the two datasets.
 *
 * @return A pie chart.
 *
 * @since 1.0.7
 */
public static JFreeChart createPieChart(String title, PieDataset dataset, PieDataset previousDataset,
        int percentDiffForMaxScale, boolean greenForIncrease, Locale locale, boolean subTitle,
        boolean showDifference) {

    PiePlot plot = new PiePlot(dataset);
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator(locale));
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));

    plot.setToolTipGenerator(new StandardPieToolTipGenerator(locale));

    List<Comparable> keys = dataset.getKeys();
    DefaultPieDataset series = null;
    if (showDifference) {
        series = new DefaultPieDataset();
    }

    double colorPerPercent = 255.0 / percentDiffForMaxScale;
    for (Comparable key : keys) {
        Number newValue = dataset.getValue(key);
        Number oldValue = previousDataset.getValue(key);

        if (oldValue == null) {
            if (greenForIncrease) {
                plot.setSectionPaint(key, Color.GREEN);
            } else {
                plot.setSectionPaint(key, Color.RED);
            }
            if (showDifference) {
                assert series != null; // suppress compiler warning
                series.setValue(key + " (+100%)", newValue);
            }
        } else {
            double percentChange = (newValue.doubleValue() / oldValue.doubleValue() - 1.0) * 100.0;
            double shade = (Math.abs(percentChange) >= percentDiffForMaxScale ? 255
                    : Math.abs(percentChange) * colorPerPercent);
            if (greenForIncrease && newValue.doubleValue() > oldValue.doubleValue()
                    || !greenForIncrease && newValue.doubleValue() < oldValue.doubleValue()) {
                plot.setSectionPaint(key, new Color(0, (int) shade, 0));
            } else {
                plot.setSectionPaint(key, new Color((int) shade, 0, 0));
            }
            if (showDifference) {
                assert series != null; // suppress compiler warning
                series.setValue(
                        key + " (" + (percentChange >= 0 ? "+" : "")
                                + NumberFormat.getPercentInstance().format(percentChange / 100.0) + ")",
                        newValue);
            }
        }
    }

    if (showDifference) {
        plot.setDataset(series);
    }

    JFreeChart chart = new JFreeChart(title, plot);

    if (subTitle) {
        TextTitle subtitle = new TextTitle("Bright " + (greenForIncrease ? "red" : "green") + "=change >=-"
                + percentDiffForMaxScale + "%, Bright " + (!greenForIncrease ? "red" : "green") + "=change >=+"
                + percentDiffForMaxScale + "%", new Font("SansSerif", Font.PLAIN, 10));
        chart.addSubtitle(subtitle);
    }
    currentTheme.apply(chart);
    return chart;
}

From source file:org.jfree.chart.ChartFactory.java

/**
 * Creates a pie chart with default settings that compares 2 datasets.
 * The color of each section will be determined by the move from the value
 * for the same key in {@code previousDataset}. ie if value1 &gt; value2
 * then the section will be in green (unless {@code greenForIncrease}
 * is {@code false}, in which case it would be {@code red}).
 * Each section can have a shade of red or green as the difference can be
 * tailored between 0% (black) and percentDiffForMaxScale% (bright
 * red/green).//from   w ww  .jav a 2  s .  c om
 * <p>
 * For instance if {@code percentDiffForMaxScale} is 10 (10%), a
 * difference of 5% will have a half shade of red/green, a difference of
 * 10% or more will have a maximum shade/brightness of red/green.
 * <P>
 * The chart object returned by this method uses a {@link PiePlot} instance
 * as the plot.
 * <p>
 * Written by <a href="mailto:opensource@objectlab.co.uk">Benoit
 * Xhenseval</a>.
 *
 * @param title  the chart title ({@code null} permitted).
 * @param dataset  the dataset for the chart ({@code null} permitted).
 * @param previousDataset  the dataset for the last run, this will be used
 *                         to compare each key in the dataset
 * @param percentDiffForMaxScale scale goes from bright red/green to black,
 *                               percentDiffForMaxScale indicate the change
 *                               required to reach top scale.
 * @param greenForIncrease  an increase since previousDataset will be
 *                          displayed in green (decrease red) if true.
 * @param subTitle displays a subtitle with color scheme if true
 * @param showDifference  create a new dataset that will show the %
 *                        difference between the two datasets.
 *
 * @return A pie chart.
 */
public static JFreeChart createPieChart(String title, PieDataset dataset, PieDataset previousDataset,
        int percentDiffForMaxScale, boolean greenForIncrease, boolean subTitle, boolean showDifference) {

    PiePlot plot = new PiePlot(dataset);
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator());
    plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));

    plot.setToolTipGenerator(new StandardPieToolTipGenerator());

    List<Comparable> keys = dataset.getKeys();
    DefaultPieDataset series = null;
    if (showDifference) {
        series = new DefaultPieDataset();
    }

    double colorPerPercent = 255.0 / percentDiffForMaxScale;
    for (Comparable key : keys) {
        Number newValue = dataset.getValue(key);
        Number oldValue = previousDataset.getValue(key);

        if (oldValue == null) {
            if (greenForIncrease) {
                plot.setSectionPaint(key, Color.GREEN);
            } else {
                plot.setSectionPaint(key, Color.RED);
            }
            if (showDifference) {
                assert series != null; // suppresses compiler warning
                series.setValue(key + " (+100%)", newValue);
            }
        } else {
            double percentChange = (newValue.doubleValue() / oldValue.doubleValue() - 1.0) * 100.0;
            double shade = (Math.abs(percentChange) >= percentDiffForMaxScale ? 255
                    : Math.abs(percentChange) * colorPerPercent);
            if (greenForIncrease && newValue.doubleValue() > oldValue.doubleValue()
                    || !greenForIncrease && newValue.doubleValue() < oldValue.doubleValue()) {
                plot.setSectionPaint(key, new Color(0, (int) shade, 0));
            } else {
                plot.setSectionPaint(key, new Color((int) shade, 0, 0));
            }
            if (showDifference) {
                assert series != null; // suppresses compiler warning
                series.setValue(
                        key + " (" + (percentChange >= 0 ? "+" : "")
                                + NumberFormat.getPercentInstance().format(percentChange / 100.0) + ")",
                        newValue);
            }
        }
    }

    if (showDifference) {
        plot.setDataset(series);
    }

    JFreeChart chart = new JFreeChart(title, plot);

    if (subTitle) {
        TextTitle subtitle;
        subtitle = new TextTitle("Bright " + (greenForIncrease ? "red" : "green") + "=change >=-"
                + percentDiffForMaxScale + "%, Bright " + (!greenForIncrease ? "red" : "green") + "=change >=+"
                + percentDiffForMaxScale + "%", new Font("SansSerif", Font.PLAIN, 10));
        chart.addSubtitle(subtitle);
    }
    currentTheme.apply(chart);
    return chart;
}