`
lushuaiyin
  • 浏览: 673409 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ChartDirector 官方的例子,加上了我的注释,比jfreechart简单些

 
阅读更多
package chartdirecttest;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import ChartDirector.*;
//implements DemoModule
public class simplepie 
{
    //Name of demo program窗口title
    public String toString() { return "Simple Pie Chart"; }

    //Number of charts produced in this demo图标个数设置
    public int getNoOfCharts() { return 1; }

    //Main code for creating charts产生一个图表视图
    public void createChart(ChartViewer viewer, int index)
    {
        // The data for the pie chart图表数据
        double[] data = {25, 18, 15, 12, 8, 30, 35};

        // The labels for the pie chart图表各项名字
        String[] labels = {"Labor", "Licenses", "Taxes", "Legal", "Insurance",
            "Facilities", "Production"};

        // Create a PieChart object of size 360 x 300 pixels视图的宽和长
        PieChart c = new PieChart(360, 300);

        // Set the center of the pie at (180, 140) and the radius to 100 pixels图形的坐标(从图形中心定位),半径
        c.setPieSize(180, 140, 100);

        // Set the pie data and the pie labels把数据项以及对应的数据添加到图表上
        c.setData(data, labels);

        // Output the chart把图表转换成图像然后放到视图上去
        viewer.setImage(c.makeImage());

        //include tool tip for the chart提示(目前表示没看懂这部分代码)
        viewer.setImageMap(c.getHTMLImageMap("clickable", "",
            "title='{label}: US${value}K ({percent}%)'"));
    }

    //Allow this module to run as standalone program for easy testing测试的main函数
    public static void main(String[] args)
    {
        //Instantiate an instance of this demo module
    	simplepie demo = new simplepie();//  DemoModule

        //Create and set up the main window
        JFrame frame = new JFrame(demo.toString());
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);} });
        frame.getContentPane().setBackground(Color.white);

        // Create the chart and put them in the content pane
        ChartViewer viewer = new ChartViewer();
        demo.createChart(viewer, 0);
        frame.getContentPane().add(viewer);

        // Display the window
        frame.pack();
        frame.setVisible(true);
    }
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics