java绘制一个饼图_一个简单的绘制饼图的 Java Bean 实例

1.[代码]ChartBean.java

/**

* Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.

* All rights reserved. Software written by Ian F. Darwin and others.

* $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $

*/

package com.darwinsys.charts;

import java.awt.*;

/**

* Simple charting bean. This version just draws a Pie Chart.

*

* It doesn't even label the pie slices; that is left as a

* (non-trivial) exercise for the reader. Please read the

* Technical Report "How Hard can it be to draw Pie Charts?" by Chris

* van Wyck, Purdue/Bell Labs, 1989??, before you decide how easy

* the work is going to be!

*/

public class ChartBean extends Component {

/** The title to print on the chart */

protected String title;

/** the data to draw */

protected ChartData data[];

/** degrees in a circle */

public static final int CIRCLE = 360;

/** a set of colors to draw the pies in */

protected Color[] colors = {

Color.red,

Color.blue,

Color.green,

Color.pink,

Color.orange

};

/** Construct a ChartBean with a title */

public ChartBean(String s) {

title = s;

setBackground(Color.white);

}

/** Construct a ChartBean with no title (no-arg constructor

* required for Beans).

*/

public ChartBean() {

this(null);

}

public void setLabel(String s) {

title = s;

}

public String getLabel() {

return title;

}

public void setData(ChartData[] newStuff) {

data = newStuff;

repaint();

}

public void paint(Graphics g) {

Dimension sz = getSize();

int w = sz.width, h = sz.width;

if (title != null)

g.drawString(title, w/10, (int)(h*.9));

if (data == null || data.length == 0) {

g.drawOval(0, 0, w, h);

g.drawString("Please provide some data!", w/10, h/2);

return;

}

int total = 0;

int angle = 0;

int rad = 0; // "radians" (actually degrees) to draw

int colNum = 0;

for (int i=0; i

total += data[i].value;

for (int i=0; i

rad = (int)(CIRCLE * ((float)data[i].value / (float)total));

// System.out.println("data: "+data[i].name+";"+data[i].value+

// ",rad="+rad);

g.setColor(colors[colNum++]);

colNum%=colors.length; // keep it in bounds

g.fillArc(0, 0, w, h, angle, rad);

angle += rad;

}

}

public Dimension getMinimumSize() {

return new Dimension(100, 120);

}

public Dimension getPreferredSize() {

return new Dimension(200, 240);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值