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

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

Introduction

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

Prototype

public void setLabelGenerator(PieSectionLabelGenerator generator) 

Source Link

Document

Sets the section label generator and sends a PlotChangeEvent to all registered listeners.

Usage

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

@Override
public void setShowPlotValues(iPlatformComponent chartPanel, ChartDefinition cd) {
    boolean show = cd.isShowPlotLabels();
    ChartInfo info = (ChartInfo) cd.getChartHandlerInfo();

    info.setShowPointLabels(show);// ww  w. ja va2s.  co m

    Plot plot = info.chart.getPlot();

    if (plot instanceof XYPlot) {
        ((XYPlot) plot).getRenderer().setBaseItemLabelsVisible(show, true);
    } else if (plot instanceof PiePlot) {
        PiePlot p = (PiePlot) plot;

        if (show) {
            p.setLabelGenerator((PieSectionLabelGenerator) info.labelGenerator);
        } else {
            info.labelGenerator = p.getLabelGenerator();
            p.setLabelGenerator(null);
        }
    }
}

From source file:org.pentaho.chart.plugin.jfreechart.JFreeChartFactoryEngine.java

public JFreeChart makePieChart(ChartModel chartModel, NamedValuesDataModel dataModel,
        final IChartLinkGenerator linkGenerator) {
    final DefaultPieDataset dataset = new DefaultPieDataset();
    for (NamedValue namedValue : dataModel) {
        if (namedValue.getName() != null) {
            dataset.setValue(namedValue.getName(),
                    scaleNumber(namedValue.getValue(), dataModel.getScalingFactor()));
        }/*ww w .jav  a2s . co  m*/
    }

    boolean showLegend = (chartModel.getLegend() != null) && (chartModel.getLegend().getVisible());

    String title = "";
    if ((chartModel.getTitle() != null) && (chartModel.getTitle().getText() != null)
            && (chartModel.getTitle().getText().trim().length() > 0)) {
        title = chartModel.getTitle().getText();
    }

    final JFreeChart chart = ChartFactory.createPieChart(title, dataset, showLegend, true, false);

    initChart(chart, chartModel);

    final PiePlot jFreePiePlot = (PiePlot) chart.getPlot();

    if (linkGenerator != null) {
        jFreePiePlot.setURLGenerator(new PieURLGenerator() {

            public String generateURL(PieDataset arg0, Comparable arg1, int arg2) {
                return linkGenerator.generateLink(arg1.toString(), arg1.toString(), arg0.getValue(arg1));
            }
        });
    }

    jFreePiePlot.setNoDataMessage("No data available"); //$NON-NLS-1$
    jFreePiePlot.setCircular(true);
    jFreePiePlot.setLabelGap(0.02);

    org.pentaho.chart.model.PiePlot chartBeansPiePlot = (org.pentaho.chart.model.PiePlot) chartModel.getPlot();

    List<Integer> colors = getPlotColors(chartBeansPiePlot);

    int index = 0;
    for (NamedValue namedValue : dataModel) {
        if (namedValue.getName() != null) {
            jFreePiePlot.setSectionPaint(namedValue.getName(),
                    new Color(0x00FFFFFF & colors.get(index % colors.size())));
        }
        index++;
    }

    if (chartBeansPiePlot.getLabels().getVisible()) {
        jFreePiePlot.setLabelGenerator(new StandardPieSectionLabelGenerator());

        Font font = ChartUtils.getFont(chartBeansPiePlot.getLabels().getFontFamily(),
                chartBeansPiePlot.getLabels().getFontStyle(), chartBeansPiePlot.getLabels().getFontWeight(),
                chartBeansPiePlot.getLabels().getFontSize());
        if (font != null) {
            jFreePiePlot.setLabelFont(font);
            if (chartBeansPiePlot.getLabels().getColor() != null) {
                jFreePiePlot.setLabelPaint(new Color(0x00FFFFFF & chartBeansPiePlot.getLabels().getColor()));
            }
            if (chartBeansPiePlot.getLabels().getBackgroundColor() != null) {
                jFreePiePlot.setLabelBackgroundPaint(
                        new Color(0x00FFFFFF & chartBeansPiePlot.getLabels().getBackgroundColor()));
            }
        }
    } else {
        jFreePiePlot.setLabelGenerator(null);
    }

    jFreePiePlot.setStartAngle(-chartBeansPiePlot.getStartAngle());

    return 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);// w  w w  .ja v a 2s. c o m

    // 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:net.sf.jasperreports.engine.fill.DefaultChartTheme.java

/**
 *
 *///from  w ww.ja  v  a 2  s.  c om
protected JFreeChart createPieChart() throws JRException {
    ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
    JFreeChart jfreeChart = ChartFactory.createPieChart(evaluateTextExpression(getChart().getTitleExpression()),
            (PieDataset) getDataset(), isShowLegend(), true, false);

    configureChart(jfreeChart);
    PiePlot piePlot = (PiePlot) jfreeChart.getPlot();
    //plot.setStartAngle(290);
    //plot.setDirection(Rotation.CLOCKWISE);
    //plot.setNoDataMessage("No data to display");
    JRPiePlot jrPiePlot = (JRPiePlot) getPlot();
    boolean isCircular = jrPiePlot.getCircular() == null ? true : jrPiePlot.getCircular();
    piePlot.setCircular(isCircular);

    boolean isShowLabels = jrPiePlot.getShowLabels() == null ? true : jrPiePlot.getShowLabels();

    if (isShowLabels) {
        PieSectionLabelGenerator labelGenerator = (PieSectionLabelGenerator) getLabelGenerator();
        JRItemLabel itemLabel = jrPiePlot.getItemLabel();
        if (labelGenerator != null) {
            piePlot.setLabelGenerator(labelGenerator);
        } else if (jrPiePlot.getLabelFormat() != null) {
            piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(jrPiePlot.getLabelFormat(),
                    NumberFormat.getNumberInstance(getLocale()), NumberFormat.getPercentInstance(getLocale())));
        } // the default section label is just the key, so there's no need to set localized number formats
        //      else if (itemLabel != null && itemLabel.getMask() != null)
        //      {
        //         piePlot.setLabelGenerator(
        //               new StandardPieSectionLabelGenerator(itemLabel.getMask())
        //               );
        //      }

        piePlot.setLabelFont(
                fontUtil.getAwtFont(getFont(itemLabel == null ? null : itemLabel.getFont()), getLocale()));

        if (itemLabel != null && itemLabel.getColor() != null) {
            piePlot.setLabelPaint(itemLabel.getColor());
        } else {
            piePlot.setLabelPaint(getChart().getForecolor());
        }

        if (itemLabel != null && itemLabel.getBackgroundColor() != null) {
            piePlot.setLabelBackgroundPaint(itemLabel.getBackgroundColor());
        } else {
            piePlot.setLabelBackgroundPaint(getChart().getBackcolor());
        }
    } else {
        piePlot.setLabelGenerator(null);
    }

    if (jrPiePlot.getLegendLabelFormat() != null) {
        piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(jrPiePlot.getLegendLabelFormat(),
                NumberFormat.getNumberInstance(getLocale()), NumberFormat.getPercentInstance(getLocale())));
    } // the default legend label is just the key, so there's no need to set localized number formats

    return jfreeChart;
}

From source file:net.sf.fspdfs.chartthemes.spring.GenericChartTheme.java

/**
 *
 *///from  w w  w  .  j  a v  a 2 s. c o m
protected JFreeChart createPieChart() throws JRException {
    ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
    JFreeChart jfreeChart = ChartFactory.createPieChart(
            (String) evaluateExpression(getChart().getTitleExpression()), (PieDataset) getDataset(),
            isShowLegend(), true, false);

    configureChart(jfreeChart, getPlot());
    PiePlot piePlot = (PiePlot) jfreeChart.getPlot();
    //plot.setStartAngle(290);
    //plot.setDirection(Rotation.CLOCKWISE);
    //plot.setNoDataMessage("No data to display");
    JRPiePlot jrPlot = (JRPiePlot) getPlot();
    boolean isCircular = jrPlot.getCircular() == null ? true : jrPlot.getCircular().booleanValue();
    piePlot.setCircular(isCircular);

    PieSectionLabelGenerator labelGenerator = (PieSectionLabelGenerator) getLabelGenerator();
    JRItemLabel itemLabel = jrPlot.getItemLabel();

    if (labelGenerator != null) {
        piePlot.setLabelGenerator(labelGenerator);
    } else if (jrPlot.getLabelFormat() != null) {
        piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(jrPlot.getLabelFormat()));
    }
    //      else if (itemLabel != null && itemLabel.getMask() != null)
    //      {
    //         piePlot.setLabelGenerator(
    //               new StandardPieSectionLabelGenerator(itemLabel.getMask())
    //               );
    //
    //      }

    if (jrPlot.getLegendLabelFormat() != null) {
        piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(jrPlot.getLegendLabelFormat()));
    }

    Integer baseFontSize = (Integer) getDefaultValue(defaultChartPropertiesMap,
            ChartThemesConstants.BASEFONT_SIZE);
    JRFont font = itemLabel != null && itemLabel.getFont() != null ? itemLabel.getFont() : null;

    piePlot.setLabelFont(getFont(new JRBaseFont(getChart(), null), font, baseFontSize));

    if (itemLabel != null && itemLabel.getColor() != null) {
        piePlot.setLabelPaint(itemLabel.getColor());
    } else {
        piePlot.setLabelPaint(getChart().getForecolor());
    }

    if (itemLabel != null && itemLabel.getBackgroundColor() != null) {
        piePlot.setLabelBackgroundPaint(itemLabel.getBackgroundColor());
    } else {
        piePlot.setLabelBackgroundPaint(getChart().getBackcolor());
    }

    return jfreeChart;
}

From source file:net.sf.fspdfs.chartthemes.simple.SimpleChartTheme.java

/**
 *
 *//* w  ww.  j  a v  a  2  s  .  c om*/
protected JFreeChart createPieChart() throws JRException {
    ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
    JFreeChart jfreeChart = ChartFactory.createPieChart(
            (String) evaluateExpression(getChart().getTitleExpression()), (PieDataset) getDataset(),
            isShowLegend(), true, false);

    configureChart(jfreeChart, getPlot());
    PiePlot piePlot = (PiePlot) jfreeChart.getPlot();
    //plot.setStartAngle(290);
    //plot.setDirection(Rotation.CLOCKWISE);
    //plot.setNoDataMessage("No data to display");
    JRPiePlot jrPlot = (JRPiePlot) getPlot();
    boolean isCircular = jrPlot.getCircular() == null ? true : jrPlot.getCircular().booleanValue();
    piePlot.setCircular(isCircular);

    PieSectionLabelGenerator labelGenerator = (PieSectionLabelGenerator) getLabelGenerator();
    JRItemLabel itemLabel = jrPlot.getItemLabel();
    if (labelGenerator != null) {
        piePlot.setLabelGenerator(labelGenerator);
    } else if (jrPlot.getLabelFormat() != null) {
        piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(jrPlot.getLabelFormat()));
    }
    //      else if (itemLabel != null && itemLabel.getMask() != null)
    //      {
    //         piePlot.setLabelGenerator(
    //               new StandardPieSectionLabelGenerator(itemLabel.getMask())
    //               );
    //      }

    if (jrPlot.getLegendLabelFormat() != null) {
        piePlot.setLegendLabelGenerator(
                new StandardPieSectionLabelGenerator(((JRPie3DPlot) getPlot()).getLegendLabelFormat()));
    }

    if (itemLabel != null && itemLabel.getFont() != null) {
        piePlot.setLabelFont(JRFontUtil.getAwtFont(itemLabel.getFont(), getLocale()));
    } else {
        piePlot.setLabelFont(JRFontUtil.getAwtFont(new JRBaseFont(getChart(), null), getLocale()));
    }

    if (itemLabel != null && itemLabel.getColor() != null) {
        piePlot.setLabelPaint(itemLabel.getColor());
    } else {
        piePlot.setLabelPaint(getChart().getForecolor());
    }

    if (itemLabel != null && itemLabel.getBackgroundColor() != null) {
        piePlot.setLabelBackgroundPaint(itemLabel.getBackgroundColor());
    } else {
        piePlot.setLabelBackgroundPaint(getChart().getBackcolor());
    }

    return jfreeChart;
}

From source file:net.sf.jasperreports.chartthemes.spring.GenericChartTheme.java

/**
 *
 *///from   w  w w.  j  ava 2s .  c om
protected JFreeChart createPieChart() throws JRException {
    ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
    JFreeChart jfreeChart = ChartFactory.createPieChart(evaluateTextExpression(getChart().getTitleExpression()),
            (PieDataset) getDataset(), isShowLegend(), true, false);

    configureChart(jfreeChart, getPlot());
    PiePlot piePlot = (PiePlot) jfreeChart.getPlot();
    //plot.setStartAngle(290);
    //plot.setDirection(Rotation.CLOCKWISE);
    //plot.setNoDataMessage("No data to display");
    JRPiePlot jrPiePlot = (JRPiePlot) getPlot();
    boolean isCircular = jrPiePlot.getCircular() == null ? true : jrPiePlot.getCircular();
    piePlot.setCircular(isCircular);

    boolean isShowLabels = jrPiePlot.getShowLabels() == null ? true : jrPiePlot.getShowLabels();

    if (isShowLabels) {
        PieSectionLabelGenerator labelGenerator = (PieSectionLabelGenerator) getLabelGenerator();
        JRItemLabel itemLabel = jrPiePlot.getItemLabel();

        if (labelGenerator != null) {
            piePlot.setLabelGenerator(labelGenerator);
        } else if (jrPiePlot.getLabelFormat() != null) {
            piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(jrPiePlot.getLabelFormat(),
                    NumberFormat.getNumberInstance(getLocale()), NumberFormat.getPercentInstance(getLocale())));
        }
        //      else if (itemLabel != null && itemLabel.getMask() != null)
        //      {
        //         piePlot.setLabelGenerator(
        //               new StandardPieSectionLabelGenerator(itemLabel.getMask())
        //               );
        //
        //      }

        Integer baseFontSize = (Integer) getDefaultValue(defaultChartPropertiesMap,
                ChartThemesConstants.BASEFONT_SIZE);
        JRFont font = itemLabel != null && itemLabel.getFont() != null ? itemLabel.getFont() : null;

        piePlot.setLabelFont(getFont(new JRBaseFont(getChart(), null), font, baseFontSize));

        if (itemLabel != null && itemLabel.getColor() != null) {
            piePlot.setLabelPaint(itemLabel.getColor());
        } else {
            piePlot.setLabelPaint(getChart().getForecolor());
        }

        if (itemLabel != null && itemLabel.getBackgroundColor() != null) {
            piePlot.setLabelBackgroundPaint(itemLabel.getBackgroundColor());
        } else {
            piePlot.setLabelBackgroundPaint(getChart().getBackcolor());
        }
    } else {
        piePlot.setLabelGenerator(null);
    }

    if (jrPiePlot.getLegendLabelFormat() != null) {
        piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(jrPiePlot.getLegendLabelFormat(),
                NumberFormat.getNumberInstance(getLocale()), NumberFormat.getPercentInstance(getLocale())));
    }

    return jfreeChart;
}

From source file:net.sf.jasperreports.chartthemes.simple.SimpleChartTheme.java

/**
 *
 *///from  ww w  .j  a v a2  s  .c o  m
protected JFreeChart createPieChart() throws JRException {
    ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
    JFreeChart jfreeChart = ChartFactory.createPieChart(evaluateTextExpression(getChart().getTitleExpression()),
            (PieDataset) getDataset(), isShowLegend(), true, false);

    configureChart(jfreeChart, getPlot());
    PiePlot piePlot = (PiePlot) jfreeChart.getPlot();
    //plot.setStartAngle(290);
    //plot.setDirection(Rotation.CLOCKWISE);
    //plot.setNoDataMessage("No data to display");
    JRPiePlot jrPiePlot = (JRPiePlot) getPlot();
    boolean isCircular = jrPiePlot.getCircular() == null ? true : jrPiePlot.getCircular();
    piePlot.setCircular(isCircular);

    boolean isShowLabels = jrPiePlot.getShowLabels() == null ? true : jrPiePlot.getShowLabels();

    if (isShowLabels) {
        PieSectionLabelGenerator labelGenerator = (PieSectionLabelGenerator) getLabelGenerator();
        JRItemLabel itemLabel = jrPiePlot.getItemLabel();
        if (labelGenerator != null) {
            piePlot.setLabelGenerator(labelGenerator);
        } else if (jrPiePlot.getLabelFormat() != null) {
            piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(jrPiePlot.getLabelFormat(),
                    NumberFormat.getNumberInstance(getLocale()), NumberFormat.getPercentInstance(getLocale())));
        }
        //      else if (itemLabel != null && itemLabel.getMask() != null)
        //      {
        //         piePlot.setLabelGenerator(
        //               new StandardPieSectionLabelGenerator(itemLabel.getMask())
        //               );
        //      }

        if (itemLabel != null && itemLabel.getFont() != null) {
            piePlot.setLabelFont(getFontUtil().getAwtFont(itemLabel.getFont(), getLocale()));
        } else {
            piePlot.setLabelFont(getFontUtil().getAwtFont(new JRBaseFont(getChart(), null), getLocale()));
        }

        if (itemLabel != null && itemLabel.getColor() != null) {
            piePlot.setLabelPaint(itemLabel.getColor());
        } else {
            piePlot.setLabelPaint(getChart().getForecolor());
        }

        if (itemLabel != null && itemLabel.getBackgroundColor() != null) {
            piePlot.setLabelBackgroundPaint(itemLabel.getBackgroundColor());
        } else {
            piePlot.setLabelBackgroundPaint(getChart().getBackcolor());
        }
    } else {
        piePlot.setLabelGenerator(null);
    }

    if (jrPiePlot.getLegendLabelFormat() != null) {
        piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
                ((JRPiePlot) getPlot()).getLegendLabelFormat(), NumberFormat.getNumberInstance(getLocale()),
                NumberFormat.getPercentInstance(getLocale())));
    }

    return jfreeChart;
}

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);//from www .  jav  a 2 s . 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:DAO.DataAccessObject.java

/**
 * Gibt ein Kuchendiagramm zur Anzeige von Umsatz Pro Artikelkategorie.
 * @return PieChar/*from ww w  . j a  v a2  s.c o m*/
 */
public JFreeChart gibChartArtikelkategorieAbsatz() {
    //Charts und Datasets.
    JFreeChart pieChart;
    DefaultPieDataset dataset;
    HashMap<String, Double> artikelMenge = null;
    dataset = new DefaultPieDataset();

    //Hole Daten aus DB
    artikelMenge = this.gibUmsatzProKategorie();
    //Iterator
    Iterator<Map.Entry<String, Double>> i = artikelMenge.entrySet().iterator();
    //Iteriere ber alle Eintrge
    while (i.hasNext()) {
        Map.Entry entr = i.next();
        //Artikelkat name
        String artikelkat = (String) entr.getKey();
        //Umsatz
        Double umsatz = (Double) entr.getValue();
        dataset.setValue(artikelkat, umsatz);
    }

    //Diagramm erstellen.
    pieChart = ChartFactory.createPieChart("Umsatz pro Artikelkategorie " + "(Gesamt)", dataset, true, true,
            true);
    PiePlot plot = (PiePlot) pieChart.getPlot();
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1} Eur ({2})"));
    return pieChart;
}