2014-01-20 24 views
1

我想要看起來像下面的兩個環圖表:的JFreeChart - 定製RingChart

enter image description here

但RingPlot似乎並不非常定製。我能想出的最好的是這樣的:

enter image description here

做什麼,我想使用JFreeChart的任何機會呢?

回答

4

JFreeChart可以辦最多的事,這應該讓你開始(我可能會納入中心文本特徵爲即將到來的1.0.18版本,這樣的子類是沒有必要的):

package org.jfree.chart.demo; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.GradientPaint; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.geom.Rectangle2D; 
import javax.swing.JPanel; 

import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.plot.PiePlotState; 
import org.jfree.chart.plot.RingPlot; 
import org.jfree.chart.title.TextTitle; 
import org.jfree.data.general.DefaultPieDataset; 
import org.jfree.data.general.PieDataset; 
import org.jfree.text.TextUtilities; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.HorizontalAlignment; 
import org.jfree.ui.RectangleInsets; 
import org.jfree.ui.RefineryUtilities; 
import org.jfree.ui.TextAnchor; 

/** 
* A simple demonstration application showing how to create a ring chart using 
* data from a {@link DefaultPieDataset}. 
*/ 
public class RingChartDemo1 extends ApplicationFrame { 

    private static final long serialVersionUID = 1L; 

    static class CustomRingPlot extends RingPlot { 

     private Font centerTextFont; 

     private Color centerTextColor; 

     public CustomRingPlot(PieDataset dataset) { 
      super(dataset); 
      this.centerTextFont = new Font(Font.SANS_SERIF, Font.BOLD, 24); 
      this.centerTextColor = Color.LIGHT_GRAY; 
     } 

     @Override 
     protected void drawItem(Graphics2D g2, int section, 
       Rectangle2D dataArea, PiePlotState state, int currentPass) { 
      super.drawItem(g2, section, dataArea, state, currentPass); 
      if (currentPass == 1 && section == 0) { 
       Number n = this.getDataset().getValue(section); 
       g2.setFont(this.centerTextFont); 
       g2.setPaint(this.centerTextColor); 
       TextUtilities.drawAlignedString(n.toString(), g2, 
         (float) dataArea.getCenterX(), 
         (float) dataArea.getCenterY(), 
         TextAnchor.CENTER); 
      } 
     } 

    } 

    /** 
    * Default constructor. 
    * 
    * @param title the frame title. 
    */ 
    public RingChartDemo1(String title) { 
     super(title); 
     setContentPane(createDemoPanel()); 
    } 

    /** 
    * Creates a sample dataset. 
    * 
    * @return A sample dataset. 
    */ 
    private static PieDataset createDataset() { 
     DefaultPieDataset dataset = new DefaultPieDataset(); 
     dataset.setValue("A", new Double(210)); 
     dataset.setValue("B", new Double(150)); 
     return dataset; 
    } 

    /** 
    * Creates a chart. 
    * 
    * @param dataset the dataset. 
    * 
    * @return A chart. 
    */ 
    private static JFreeChart createChart(PieDataset dataset) { 
     CustomRingPlot plot = new CustomRingPlot(dataset); 
     JFreeChart chart = new JFreeChart("Custom Ring Chart", 
       JFreeChart.DEFAULT_TITLE_FONT, plot, false); 
     chart.setBackgroundPaint(new GradientPaint(new Point(0, 0), 
       new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY)); 

     // customise the title position and font 
     TextTitle t = chart.getTitle(); 
     t.setHorizontalAlignment(HorizontalAlignment.LEFT); 
     t.setPaint(new Color(240, 240, 240)); 
     t.setFont(new Font("Arial", Font.BOLD, 26)); 

     plot.setBackgroundPaint(null); 
     plot.setOutlineVisible(false); 
     plot.setLabelGenerator(null); 
     plot.setSectionPaint("A", Color.ORANGE); 
     plot.setSectionPaint("B", new Color(100, 100, 100)); 
     plot.setSectionDepth(0.05); 
     plot.setSectionOutlinesVisible(false); 
     plot.setShadowPaint(null); 

     return chart; 

    } 

    /** 
    * Creates a panel for the demo (used by SuperDemo.java). 
    * 
    * @return A panel. 
    */ 
    public static JPanel createDemoPanel() { 
     JFreeChart chart = createChart(createDataset()); 
     chart.setPadding(new RectangleInsets(4, 8, 2, 2)); 
     ChartPanel panel = new ChartPanel(chart); 
     panel.setMouseWheelEnabled(true); 
     panel.setPreferredSize(new Dimension(600, 300)); 
     return panel; 
    } 

    /** 
    * Starting point for the demonstration application. 
    * 
    * @param args ignored. 
    */ 
    public static void main(String[] args) { 
     RingChartDemo1 demo = new RingChartDemo1("JFreeChart: Ring Chart Demo 1"); 
     demo.pack(); 
     RefineryUtilities.centerFrameOnScreen(demo); 
     demo.setVisible(true); 
    } 

} 
+1

謝謝百萬,這很棒。還有一個SSCCE! – Buffalo

+0

BTW:plot.setSeparatorsVisible(false);可以用來擺脫分隔符。 – Buffalo

+0

@大衛我有一個關於如何做一個自定義環形圖的問題,我想你會有條件回答,如果這甚至可能https://stackoverflow.com/questions/37213030/make-a-custom-ring-圖表合jfreecharts – user1010101