CSDN博客

img smilelily

struts开发实践-柱形图、饼状图实例

发表于2004/4/13 14:14:00  1031人阅读

分类: struts开发

 struts开发实践-柱形图、饼状图实例

本案主要功能是完成柱形图、饼状图的绘制,并将柱形图、饼状图生成jpg图形并显示。

1。调用的主要函数说明:请参考曲线图部分的说明

2。曲线图绘制文件

/***********program StatRectFrame begin*********************/

package test;

import java.awt.*;

import java.awt.geom.*;

import java.awt.image.*;

import javax.swing.JFrame;

import java.lang.Math;

/**

 * 绘图:柱形图;饼形图

 */

public class StatRectFrame

    extends JFrame {

  private int[] result;

  private String[] title;

  private int statStyle;

  private Color[] color = {

      Color.blue, new Color(255, 92, 0), new Color(255, 0, 30),

      new Color(0, 255, 30), new Color(255, 0, 240), Color.cyan};

    /**入口参数:结果集,标题集,统计类型:柱状图 or 饼状图 */

  public StatRectFrame(int[] result, String[] title, int statStyle) {

    this.result = result;

    this.title = title;

    this.statStyle = statStyle;

  }

 

  public void paint(Graphics g) {

    Graphics2D g2 = (Graphics2D) g;

    Dimension dim = this.getSize();

    if (statStyle == 1) {//饼状图

      g2.setColor(Color.white);

      g2.fillRect(0, 0, dim.width, dim.height);

      int total = result[result.length - 1];

      int begTangle = 0;

      int endTangle = 0;

      g2.setColor(Color.black);

      int pointx=dim.width/2;//圆心x

      int pointy=dim.height/2;//圆心y

 

      int r=150;//半径

      long[] pointsx=new long[result.length-1];

      long[] pointsy=new long[result.length-1];

      pointsx[0]=pointx+r;

      pointsy[0]=pointy;

      for (int i = 1; i < result.length - 1; i++) {

        if (i >= 6) {

          g2.setColor(color[i % 6]);

        }

        else {

          g2.setColor(color[i]);

        }

 

        if (result[i] > 0) {

          int percent=Math.round((result[i]*100f)/total);

          endTangle = Math.round(percent*360f/100f);

          title[i]=title[i]+"("+percent+"%)";

          g2.fillArc( pointx-r,pointy-r, 2*r, 2*r,

                     begTangle, endTangle);

 

          //新中心点

          pointsx[i]=Math.round(Math.cos(Math.toRadians(begTangle+endTangle*0.5))*r+pointx);

          pointsy[i]=Math.round(pointy-Math.sin(Math.toRadians(begTangle+endTangle*0.5))*r);

          //延长点

          long pointsxEx=Math.round(Math.cos(Math.toRadians(begTangle+endTangle*0.5))*(r+50)+pointx);

          long pointsyEx=Math.round(pointy-Math.sin(Math.toRadians(begTangle+endTangle*0.5))*(r+50));

          g2.setColor(Color.black);

          g2.draw(new Line2D.Double(pointsx[i], pointsy[i], pointsxEx, pointsyEx));

          //画文本框

          g2.setColor(new Color(251,249,206));

          if(((int)pointsxEx)<pointx){

            g2.fillRect( (int) pointsxEx-110, (int) pointsyEx - 10, 110, 20);

            g2.setColor(Color.black);

            g2.drawString(title[i],(int)pointsxEx-110,(int)pointsyEx+5);

          }

          else{

            g2.fillRect( (int) pointsxEx, (int) pointsyEx - 10, 110, 20);

            g2.setColor(Color.black);

            g2.drawString(title[i],(int)pointsxEx,(int)pointsyEx+5);

          }

          begTangle = begTangle + endTangle;

        }

      }

 

    }

    else {//柱状图

      // make sure to fill in the background color

      g2.setColor(Color.white);

      g2.fillRect(0, 0, dim.width, dim.height);

 

      // draw the x and y axis

      g2.setPaint(Color.black);

      g2.setStroke(new BasicStroke(2.0f));

      g2.draw(new Line2D.Double(10, 10, 10, dim.height - 120));

      g2.draw(new Line2D.Double(10, dim.height - 120,

                                dim.width, dim.height - 120));

 

      //画柱形图

      Font font = g2.getFont().deriveFont(12.0f);

      g2.setFont(font);

      // font.layoutGlyphVector()

      int height = Math.round(dim.height - 120 - 30) / result[0];

      for (int i = 1; i < result.length - 1; i++) {

        g2.setPaint(Color.black);

        g2.drawString("" + result[i], 20 + (i - 1) * 30,

                      dim.height - 120 - height * result[i] - 20);

        //draw the title

        //print one by one;

        for (int j = 1; j <= Math.round(title[i].length() / 2); j++) {

          g2.drawString(title[i].substring( (j - 1) * 2, j * 2),

                        20 + (i - 1) * 30, dim.height - 120 + j * 20);

        }

        if (i >= 6) {

          g2.setColor(color[i % 6]);

        }

        else {

          g2.setColor(color[i]);

        }

        g2.fillRect(20 + (i - 1) * 30, dim.height - 120 - height * result[i],

                    20, height * result[i]);

      }

    }

 

  }

 

}/*****************program end************************/

3。生成jpg图形并显示:与曲线图中介绍的部分类似,不再赘述。

 

 

 

 

 

0 0

相关博文

我的热门文章

img
取 消
img