Java Code Examples for org.jfree.chart.plot.PiePlot#setLegendLabelGenerator()

The following examples show how to use org.jfree.chart.plot.PiePlot#setLegendLabelGenerator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PiePlotTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Draws a pie chart where the label generator returns null.
 */
public void testDrawWithNullLegendLabels() {
    DefaultPieDataset dataset = new DefaultPieDataset();
    dataset.setValue("L1", 12.0);
    dataset.setValue("L2", 11.0);
    JFreeChart chart = ChartFactory.createPieChart("Test", dataset, true);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLegendLabelGenerator(new NullLegendLabelGenerator());
    boolean success = false;
    try {
        BufferedImage image = new BufferedImage(200 , 100,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
        g2.dispose();
        success = true;
    }
    catch (Exception e) {
      success = false;
    }
    assertTrue(success);
}
 
Example 2
Source File: PiePlotTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Draws a pie chart where the label generator returns null.
 */
public void testDrawWithNullLegendLabels() {
    DefaultPieDataset dataset = new DefaultPieDataset();
    dataset.setValue("L1", 12.0);
    dataset.setValue("L2", 11.0);
    JFreeChart chart = ChartFactory.createPieChart("Test", dataset, true, 
            false, false);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLegendLabelGenerator(new NullLegendLabelGenerator());
    boolean success = false;
    try {
        BufferedImage image = new BufferedImage(200 , 100, 
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
        g2.dispose();
        success = true;
    }
    catch (Exception e) {
      success = false;
    }
    assertTrue(success);
}
 
Example 3
Source File: DefaultChartTheme.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 */
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;
}
 
Example 4
Source File: GenericChartTheme.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 */
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;
}
 
Example 5
Source File: SimpleChartTheme.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 */
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;
}
 
Example 6
Source File: MinimapPanel.java    From dsworkbench with Apache License 2.0 4 votes vote down vote up
private void renderChartInfo() {
    HashMap<Object, Marker> marks = new HashMap<>();
    DefaultPieDataset dataset = buildDataset(marks);

    JFreeChart chart = ChartFactory.createPieChart(
            null, // chart title
            dataset, // data
            true, // include legend
            true,
            false);
    chart.setBackgroundPaint(null);
    //chart.setBorderStroke(null);
    chart.setBorderVisible(false);
    final PiePlot plot = (PiePlot) chart.getPlot();
    // plot.setBackgroundPaint(null);
    //  plot.setShadowPaint(null);

    for(Object o: marks.keySet()) {
        if (iCurrentView == ID_ALLY_CHART) {
            Ally a = (Ally) o;
            plot.setSectionPaint(a.getTag(), marks.get(a).getMarkerColor());
        } else {
            Tribe t = (Tribe) o;
            plot.setSectionPaint(t.getName(), marks.get(t).getMarkerColor());
        }
    }
    //plot.setCircular(true);
    //  plot.setMaximumLabelWidth(30.0);
 /*
     * plot.setLabelGenerator(new StandardPieSectionLabelGenerator( "{0} = {2}", NumberFormat.getNumberInstance(),
     * NumberFormat.getPercentInstance()));
     */
    //   chart.getLegend().setVerticalAlignment(VerticalAlignment.CENTER);
    //  chart.getLegend().setPosition(RectangleEdge.RIGHT);
    // plot.setMaximumLabelWidth(20.0);
    plot.setLabelGenerator(null);
    plot.setBackgroundPaint(Constants.DS_BACK);
    /*
     * plot.setInteriorGap(0.0); plot.setLabelGap(0.0);
     */
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
            "{0} = {2}", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()));

    /*
     * plot.getL plot.setLabelFont(g2d.getFont().deriveFont(10.0f));
     */


    //plot.setLabelGenerator(null);

    //plot.setMaximumLabelWidth(30.0);
    //plot.getLabelDistributor().distributeLabels(10.0, 20.0);
    //chart.draw(g2d, new Rectangle2D.Float(20, 20, 100, 100));

    //  g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    plot.setOutlineVisible(false);
    mChartImage = chart.createBufferedImage(getWidth(), getHeight());
    //chart.draw(g2d, new Rectangle2D.Float(50, 50, 400, 400));
    //g2d.drawImage(bi, 30, 30, null);

    //  g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));

    //bi = chart.createBufferedImage(240, 240);
    // g2d.drawImage(bi, 30, 30, null);
}
 
Example 7
Source File: PieChartExpression.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void configureChart( final JFreeChart chart ) {
  super.configureChart( chart );

  final Plot plot = chart.getPlot();
  final PiePlot pp = (PiePlot) plot;
  final PieDataset pieDS = pp.getDataset();
  pp.setDirection( rotationClockwise ? Rotation.CLOCKWISE : Rotation.ANTICLOCKWISE );
  if ( ( explodeSegment != null ) && ( explodePct != null ) ) {
    configureExplode( pp );
  }
  if ( StringUtils.isEmpty( getTooltipFormula() ) == false ) {
    pp.setToolTipGenerator( new FormulaPieTooltipGenerator( getRuntime(), getTooltipFormula() ) );
  }
  if ( StringUtils.isEmpty( getUrlFormula() ) == false ) {
    pp.setURLGenerator( new FormulaPieURLGenerator( getRuntime(), getUrlFormula() ) );
  }

  pp.setIgnoreNullValues( ignoreNulls );
  pp.setIgnoreZeroValues( ignoreZeros );
  if ( Boolean.FALSE.equals( getItemsLabelVisible() ) ) {
    pp.setLabelGenerator( null );
  } else {
    final ExpressionRuntime runtime = getRuntime();
    final Locale locale = runtime.getResourceBundleFactory().getLocale();

    final FastDecimalFormat fastPercent = new FastDecimalFormat( FastDecimalFormat.TYPE_PERCENT, locale, true );
    final FastDecimalFormat fastInteger = new FastDecimalFormat( FastDecimalFormat.TYPE_INTEGER, locale, true );

    final DecimalFormat numFormat = new DecimalFormat( fastInteger.getPattern(), new DecimalFormatSymbols( locale ) );
    numFormat.setRoundingMode( RoundingMode.HALF_UP );

    final DecimalFormat percentFormat =
      new DecimalFormat( fastPercent.getPattern(), new DecimalFormatSymbols( locale ) );
    percentFormat.setRoundingMode( RoundingMode.HALF_UP );

    final StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator( pieLabelFormat,
      numFormat, percentFormat );
    pp.setLabelGenerator( labelGen );

    final StandardPieSectionLabelGenerator legendGen = new StandardPieSectionLabelGenerator( pieLegendLabelFormat,
      numFormat, percentFormat );
    pp.setLegendLabelGenerator( legendGen );
  }

  if ( StringUtils.isEmpty( getLabelFont() ) == false ) {
    pp.setLabelFont( Font.decode( getLabelFont() ) );
  }

  if ( pieDS != null ) {
    final String[] colors = getSeriesColor();
    for ( int i = 0; i < colors.length; i++ ) {
      if ( i < pieDS.getItemCount() ) {
        pp.setSectionPaint( pieDS.getKey( i ), parseColorFromString( colors[ i ] ) );
      } else {
        break;
      }
    }
  }

  if ( shadowPaint != null ) {
    pp.setShadowPaint( shadowPaint );
  }
  if ( shadowXOffset != null ) {
    pp.setShadowXOffset( shadowXOffset.doubleValue() );
  }
  if ( shadowYOffset != null ) {
    pp.setShadowYOffset( shadowYOffset.doubleValue() );
  }
  pp.setCircular( circular );

  if ( isShowBorder() == false || isChartSectionOutline() == false ) {
    chart.setBorderVisible( false );
    chart.getPlot().setOutlineVisible( false );
  }

}