struts开发实践—柱形图实例

--文章源自一位网友

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

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

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

2。曲线图绘制文件

/program CurveFrame 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, 2r, 2r,

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+endTangle0.5))(r+50)+pointx);

long pointsyEx=Math.round(pointy-Math.sin(Math.toRadians(begTangle+endTangle0.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)

 1<pointx){ (i="" (int="" (int)="" (j="" *="" +="" -="" 0,="" 1)="" 10,="" 110,="" 120="" 120));="" 120,="" 1;="" 2),="" 2);="" 2,="" 20="" 20);="" 30)="" 30,="" <="Math.round(title[i].length()" and="" axis="" background="" basicstroke(2.0f));="" begtangle="begTangle" by="" color="" dim.height="" dim.height);="" dim.width,="" draw="" else="" else{="" endtangle;="" fill="" font="g2.getFont().deriveFont(12.0f);" font.layoutglyphvector()="" for="" g2.draw(new="" g2.drawstring(""="" g2.drawstring(title[i],(int)pointsxex,(int)pointsyex+5);="" g2.drawstring(title[i],(int)pointsxex-110,(int)pointsyex+5);="" g2.drawstring(title[i].substring(="" g2.fillrect(="" g2.fillrect(0,="" g2.setcolor(color.black);="" g2.setcolor(color.white);="" g2.setfont(font);="" g2.setpaint(color.black);="" g2.setstroke(new="" height="" i="" i++)="" if="" in="" int="" j="" j++)="" line2d.double(10,="" make="" one="" one;="" pointsxex,="" pointsxex-110,="" pointsyex="" print="" result.length="" result[0];="" result[i]="" result[i],="" sure="" the="" title="" to="" x="" y="" {="" }="" 柱状图="" 画柱形图="">= 6) { 
 2
 3g2.setColor(color[i % 6]); 
 4
 5} 
 6
 7else { 
 8
 9g2.setColor(color[i]); 
10
11} 
12
13g2.fillRect(20 + (i - 1) * 30, dim.height - 120 - height * result[i], 
14
1520, height * result[i]); 
16
17} 
18
19} 
20
21} 
22
23}/*****************program end************************/ 
24
253。生成jpg图形并显示:与曲线图中介绍的部分类似,不再赘述。</pointx){>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus