前言 :
這裡要建立一個旋轉的 PieChart, 關鍵在於使用 javax.swing.Timer 搭配 javax.awt.event.ActionListener 建立的內部類別 Rotator 動態改變 Pie Chart 的角度, 使效果就像 Pie Chart 在旋轉. 而 PieChart 使用的資料容器為 PieDataset 而建立圖表方法為 createPieChart().
範例代碼 :
使用Packages : jcommon-1.0.16.jar, jfreechart-1.0.13.jar.
執行結果 :
這裡要建立一個旋轉的 PieChart, 關鍵在於使用 javax.swing.Timer 搭配 javax.awt.event.ActionListener 建立的內部類別 Rotator 動態改變 Pie Chart 的角度, 使效果就像 Pie Chart 在旋轉. 而 PieChart 使用的資料容器為 PieDataset 而建立圖表方法為 createPieChart().
範例代碼 :
使用Packages : jcommon-1.0.16.jar, jfreechart-1.0.13.jar.
- PieChartDemo4.java :
- package john.jfreechart.piechart;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.text.NumberFormat;
- import javax.swing.Timer;
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.ChartPanel;
- import org.jfree.chart.JFreeChart;
- import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
- import org.jfree.chart.plot.PiePlot;
- import org.jfree.data.general.DefaultPieDataset;
- import org.jfree.data.general.PieDataset;
- import org.jfree.ui.ApplicationFrame;
- import org.jfree.ui.RefineryUtilities;
- /**
- * Source : http://www.koders.com/java/fidCA9ADCF38E1E1B2825657905E9A4FC95579E0E6B.aspx
- * @author John-Lee
- */
- public class PieChartDemo4 extends ApplicationFrame{
- /**
- * Default constructor.
- *
- * @param title the frame title.
- */
- public PieChartDemo4(String title) {
- super(title);
- PieDataset dataset = createDataset(14);
- // create the chart...
- JFreeChart chart = ChartFactory.createPieChart(
- "Pie Chart Demo 4", // chart title
- dataset, // dataset
- false, // include legend
- true,
- false
- );
- // set the background color for the chart...
- chart.setBackgroundPaint(new Color(222, 222, 255));
- PiePlot plot = (PiePlot) chart.getPlot();
- plot.setBackgroundPaint(Color.white);
- plot.setCircular(true);
- // For the label format, use {0} where the pie section key should be inserted, {1}
- // for the absolute section value and {2} for the percent amount of the pie section,
- // e.g. "{0} = {1} ({2})" will display as apple = 120 (5%).
- plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} = {2}"));
- plot.setNoDataMessage("No data available");
- // add the chart to a panel...
- ChartPanel chartPanel = new ChartPanel(chart);
- chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
- setContentPane(chartPanel);
- Rotator rotator = new Rotator(plot);
- rotator.start();
- }
- /**
- * Creates a sample dataset.
- *
- * @param sections the number of sections.
- *
- * @return A sample dataset.
- */
- private PieDataset createDataset(int sections) {
- DefaultPieDataset result = new DefaultPieDataset();
- for (int i = 0; i < sections; i++) {
- double value = 100.0 * Math.random();
- result.setValue("Section " + i, value);
- }
- return result;
- }
- /**
- * Starting point for the demonstration application.
- *
- * @param args ignored.
- */
- public static void main(String[] args) {
- PieChartDemo4 demo = new PieChartDemo4("Pie Chart Demo 4");
- demo.pack();
- RefineryUtilities.centerFrameOnScreen(demo);
- demo.setVisible(true);
- }
- /**
- * The rotator.
- *
- * @author David Gilbert
- */
- static class Rotator extends Timer implements ActionListener {
- /** The plot. */
- private PiePlot plot;
- /** The angle. */
- private int angle = 270;
- /**
- * Constructor.
- *
- * @param plot the plot.
- */
- Rotator(PiePlot plot) {
- super(100, null);
- this.plot = plot;
- addActionListener(this); // every 100ms, trigger self.
- }
- /**
- * Modifies the starting angle.
- * Ref:
- * - javax.swing.Timer : http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html
- * - javax.awt.event.ActionListener : http://download.oracle.com/javase/6/docs/api/java/awt/event/ActionListener.html
- * @param event the action event.
- */
- public void actionPerformed(ActionEvent event) {
- this.plot.setStartAngle(angle);
- this.angle = this.angle + 1;
- if (this.angle == 360) {
- this.angle = 0;
- }
- }
- }
- }
執行結果 :
This message was edited 5 times. Last update was at 02/06/2011 18:05:26
沒有留言:
張貼留言