JFreeChart - PieChartTest.java

import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.Rotation;
import org.jfree.chart.labels.PieToolTipGenerator;
import org.jfree.chart.labels.StandardPieItemLabelGenerator;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import java.awt.Insets;
import rewin.tools.reportStat.PiePlot3DCustom;
import rewin.tools.reportStat.CustomLegend;

public class PieChartTest extends ApplicationFrame {
  public PieChartTest(String s) {
    super(s);
    PieDataset pieDataset = createDataset();
    JFreeChart freeChart = createChart(pieDataset);
    ChartPanel chartPanel = new ChartPanel(freeChart);
    chartPanel.setPreferredSize(new Dimension(180, 120));
    setContentPane(chartPanel);
  }

  private static PieDataset createDataset() {
    DefaultPieDataset dataset = new DefaultPieDataset();
    System.out.println("dataset.getItemCount() = " + dataset.getItemCount());
    //dataset.setValue("Nokia", new Double(40));
    //System.out.println("dataset.getItemCount() = " + dataset.getItemCount());
    dataset.setValue("Moto", new Double(30));
    dataset.setValue("三星", new Double(20));
    dataset.setValue("西门子", new Double(10));
    dataset.setValue("波导", new Double(5));
    dataset.setValue("联想", new Double(5));
    dataset.setValue("其他", new Double(5));
    //dataset.setValue("无投诉", new Double(1));
    System.out.println("dataset.getItemCount() = " + dataset.getItemCount());
    return dataset;
  }

  private static JFreeChart createChart(PieDataset pieDataset) {
    //JFreeChart freeChart = ChartFactory.createPieChart3D("手机黑榜", pieDataset, true, false, false);
    //JFreeChart freeChart = ChartFactory.createPieChart3D("", pieDataset, true, false, false);

    PiePlot3DCustom piePlot3D = new PiePlot3DCustom(pieDataset);
    int index = pieDataset.getIndex("无投诉");
    System.out.println("index = " + index);
    System.out.println(pieDataset.getKey(0));

    piePlot3D.setInsets(new Insets(0, 0, 0, 0));
    //piePlot3D.setSectionPaint(0, new java.awt.Color(0x88EEFA));
    piePlot3D.setSectionPaint(0, new java.awt.Color(0x56D0F5));
    //piePlot3D.setSectionPaint(1, new java.awt.Color(0xBEEC56));
    piePlot3D.setSectionPaint(1, new java.awt.Color(0x51E050));
    //piePlot3D.setSectionPaint(2, new java.awt.Color(0xFACBDB));
    piePlot3D.setSectionPaint(2, new java.awt.Color(0xE6315C));
    //piePlot3D.setSectionPaint(3, new java.awt.Color(0xFEF29E));
    piePlot3D.setSectionPaint(3, new java.awt.Color(0xFCC938));
    //piePlot3D.setSectionPaint(4, new java.awt.Color(0xFD9BF0));
    piePlot3D.setSectionPaint(4, new java.awt.Color(0xED36C2));
    //piePlot3D.setSectionPaint(5, java.awt.Color.orange);
    //piePlot3D.setSectionPaint(6, java.awt.Color.magenta);
    //piePlot3D.setSectionPaint(7, java.awt.Color.black);
    //piePlot3D.setSectionPaint(8, java.awt.Color.lightGray);
    //piePlot3D.setSectionPaint(9, java.awt.Color.red);
    //piePlot3D.setOutlinePaint(java.awt.Color.white);
    if (pieDataset.getItemCount() == 1 && pieDataset.getIndex("无投诉") >= 0) {
      piePlot3D.setSectionPaint(0, java.awt.Color.red);
    }

    piePlot3D.setStartAngle(60D);
    piePlot3D.setDirection(Rotation.CLOCKWISE);
    piePlot3D.setForegroundAlpha(0.8F);
    piePlot3D.setNoDataMessage("No data to display");
    piePlot3D.setLabelGenerator(new CustomLabelGenerator());
    piePlot3D.setBackgroundAlpha(0f);//设置背景透明
    piePlot3D.setOutlinePaint(java.awt.Color.white);//设置边框为白色
    JFreeChart freeChart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, piePlot3D, true);

    freeChart.setBackgroundPaint(java.awt.Color.white);
    freeChart.setBorderVisible(false);//是否显示边框

    CustomLegend legend = new CustomLegend();
    legend.setAnchor(Legend.EAST);
    legend.setOutlinePaint(java.awt.Color.white);//设置边框为白色
    freeChart.setLegend(legend);
    //Legend legend = freeChart.getLegend();
    //StandardLegend legend = (StandardLegend) freeChart.getLegend();
    //legend.setAnchor(Legend.EAST);
    //legend.setOutlinePaint(java.awt.Color.white);

    return freeChart;
  }

  public static void main(String[] args) {
    String str = "#E6315C";
    String s = str.substring(1);
    System.out.println(s);
    int color = Integer.parseInt(s, 16);
    System.out.println(color);
    PieChartTest pieChartTest = new PieChartTest("PieChartTest");
    pieChartTest.pack();
    RefineryUtilities.centerFrameOnScreen(pieChartTest);
    pieChartTest.setVisible(true);
  }

  static class CustomLabelGenerator implements PieSectionLabelGenerator {

    public String generateSectionLabel(PieDataset piedataset, Comparable comparable) {
      return null;
    }

    CustomLabelGenerator() {
    }
  }

}

阅读更多
个人分类: Java
下一篇JFreeChart - BarChartTest.java
想对作者说点什么? 我来说一句

JFreeChart

2010年04月01日 180KB 下载

JfreeChart

2008年06月26日 120KB 下载

JFreeChart 指南 & JFreeChart 示例

2008年12月03日 3.57MB 下载

没有更多推荐了,返回首页

关闭
关闭