Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Diplay % in the pie chart

julius3419
2005-02-04
2013-01-13
  • julius3419
    julius3419
    2005-02-04

    pls help me solve this problem:

    in the pie chart, the label of each pie just showing the number, eg. 36.44. how i have to append % sign at the end of 36.44 to be 36.44%.

    Any help is appreciated....

     
    • julius3419
      julius3419
      2005-02-04

      my codes are like this:

      DatasetProducer pieData = new DatasetProducer() {
                      public Object produceDataset(Map params) {
                      DefaultPieDataset ds = new DefaultPieDataset();
                      for (int i = 0; i < categories.length; i++) {

                          //  percentVotes[i] = percentVotes[i];
                      //    ds.setValue(categories[i], percentVotes[i]);
                            ds.setValue(categories[i],percentVotes[i]);

                      }
                          return ds;
                      }
                      public String getProducerId() {
                          return "PieDataProducer";
                      }
                      public boolean hasExpired(Map params, java.util.Date since) {
                          return false;
                      }
                  };
                      pageContext.setAttribute("pieData", pieData);

      <cewolf:chart id="pieChart" title="<%=chartTitle%>" type="pie">
              <cewolf:gradientpaint>
                  <cewolf:point x="0" y="0" color="#FFFFFF" />
                  <cewolf:point x="300" y="0" color="#CCCCCC" />
              </cewolf:gradientpaint>
              <cewolf:data>
                  <cewolf:producer id="pieData" />
              </cewolf:data>
          </cewolf:chart>
          <cewolf:img chartid="pieChart" renderer="/cewolf" width="450" height="450"/>

       
    • julius3419
      julius3419
      2005-02-14

      I have follow the suggestion, cant have % in the label though, here is my sample codes:
      In my Jsp Page:
      <%@ page contentType="text/html;charset=UTF-8" language="java" %>
      <%@taglib uri='/WEB-INF/cewolf-1.1.tld' prefix='cewolf' %>

      <HTML>
      <BODY>
      <H1>Pie Chart</H1>
      <HR>
      <%
      String chartTitle = "Pie Chart: Percentage of The Votes For Contestants";
      %>
      <jsp:useBean id="pageViews" class="com.PieChart"/>
      <cewolf:chart id="pieChart" title="<%=chartTitle%>" type="pie">
      <cewolf:gradientpaint>
      <cewolf:point x="0" y="0" color="#FFFFFF" />
      <cewolf:point x="300" y="0" color="#DDDDFF" />
      </cewolf:gradientpaint>
      <cewolf:data>
      <cewolf:producer id="pageViews" />
      </cewolf:data>
      </cewolf:chart>
      <cewolf:img chartid="pieChart" renderer="/cewolf" width="450" height="450"/>
      </BODY>
      </HTML>

      In my Java:
      package com;

      import de.laures.cewolf.DatasetProducer;
      import de.laures.cewolf.ChartPostProcessor;

      import java.io.Serializable;
      import java.util.Date;
      import java.util.Map;
      import java.math.BigDecimal;

      import org.jfree.data.general.DefaultPieDataset;
      import org.jfree.chart.plot.Plot;
      import org.jfree.chart.plot.PiePlot;
      import org.jfree.chart.JFreeChart;
      import org.jfree.chart.labels.StandardPieItemLabelGenerator;

      //public class PieChart implements DatasetProducer, Serializable {
      public class PieChart implements DatasetProducer, Serializable, ChartPostProcessor{
      final String[] categories = {"Contestant 1", "Contestant 2", "Contestant 3", "Contestant 4", "Contestant 5", "Contestant 6",
      "Contestant 7", "Contestant 8", "Contestant 9", "Contestant 10", "Contestant 11", "Contestant 12"};

      // String chartTitle1 = "Bar Chart: Number Of The Votes For Contestants";
      // final String[] CategoryValue = {"contestant 1","contestant 2", "contestant 3","contestant 4","contestant 5","contestant 6",
      // "contestant 7","contestant 8","contestant 9","contestant 10","contestant 11","contestant 12"};
      final int[] votes= new int[12];
      int votesCount=0;
      int totalVotes=0;
      final double[] percentVotes=new double[12];
      double[] perVotes=new double[12];

      // out.println(percentVotes[c]);
      public void processChart(Object chart, Map params) {
      PiePlot plot = (PiePlot)((JFreeChart) chart).getPlot();

      plot.setLabelGenerator(new StandardPieItemLabelGenerator("{2}")); 

      //#1 plot.setLabelGenerator(new StandardPieItemLabelGenerator("{0} = {1} ({2})"));
      //#2 plot.setLabelGenerator(new StandardPieItemLabelGenerator("{1}={2}"));
      // plot.setLabelGenerator();
      }

      public Object produceDataset(Map params) {
      for(int i=0; i<12;i++){
      votes[i] = (int)(Math.random() * 10 + 1);
      totalVotes += votes[i];
      }

      for(int c=0; c<12;c++){
      perVotes[c] = (double)(votes[c]*100);
      percentVotes[c] = perVotes[c]/totalVotes;

      int decimalPlace = 2;
      BigDecimal bd = new BigDecimal(percentVotes[c]);
      bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
      percentVotes[c] = bd.doubleValue();
      }
      DefaultPieDataset ds = new DefaultPieDataset();

      for (int i = 0; i < categories.length; i++) {

      ds.setValue(categories[i], percentVotes[i]);

      }
      return ds;
      }

      public String getProducerId() {
      return "PieDataProducer";
      }

      public boolean hasExpired(Map params, java.util.Date since) {
      return false;
      }
      }

      Can anyone show me what i need to add?

       
    • Jason Fletcher
      Jason Fletcher
      2005-03-03

      Try changing the line

      plot.setLabelGenerator(new StandardPieItemLabelGenerator("{2}")); 

      to

      plot.setLabelGenerator(new StandardPieItemLabelGenerator("{2}", new DecimalFormat("0"), new DecimalFormat("0%")));