加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:12:41 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 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

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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,w,h);
         g.drawString("Please provide some data!",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<data.length; i++)
         total += data[i].value;
      for (int i=0; i<data.length; 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,h,angle,rad);
         angle += rad;
      }
   }

   public Dimension getMinimumSize() {
      return new Dimension(100,120);
   }
   public Dimension getPreferredSize() {
      return new Dimension(200,240);
   }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读