2014-01-20 22 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