数据的饼图与柱图

数据(数组)的饼图与柱图

耿祥义

摘要: 养成使用接口的好习惯,掌握BufferedImage和ImageIO类。

一、数据的饼图

对于数组。比如:

double a [] = ;

那么数组a的饼图是分别用饼状图形显示数组各个单元的数值占整个数组数据之和的百分比。例如:

二、数据的柱状图

数组a的柱状图形(柱高)显示数组各个单元的数值,例如:

三、视频讲解

https://share.weiyun.com/X9Jv3dyJ

复制到浏览器地址栏访问即可。

1. 接口(提供BufferedImage图像)

Drawer.java

public interface Drawer {

//用数组data绘制一幅图像

BufferedImage getImage(double data[],String dataMess[]);

}

2.主类

import javax.imageio.ImageIO;

public class MainClass {

public static void main(String args[]){

Drawer drawer = new DrawPieChart();

double a [] = ;

String mess[] =

{"不及格","及格","中等","良好","优秀"};

BufferedImage imagePie =

drawer.getImage(a,mess);

drawer = new DrawBarChart();

BufferedImage imageBar =

drawer.getImage(a,mess);

File filePie = new File("pie.jpg");

File fileBar = new File("bar.bmp");

try {

boolean boo =

ImageIO.write(imagePie,"jpeg",filePie);

boo = ImageIO.write(imageBar,"bmp",fileBar);

if(boo){

(""+filePie.getAbsolutePath());

(""+fileBar.getAbsolutePath());

Runtime ce=Runtime.getRuntime();

ce.exec("mspaint "+filePie.getAbsolutePath());

ce.exec("mspaint "+fileBar.getAbsolutePath());

}

}

catch(Exception exp){}

}

}

3.实现接口的类(提供数组的饼图)

import java.awt.Graphics2D;

import java.awt.Graphics;

import java.awt.Color;

import java.awt.Font;

//绘制饼图

public class DrawPieChart implements Drawer {

public BufferedImage getImage(double data[],String dataMess[]){

double sum = 0;

for(double item:data){

sum += item;

}

int width=1000, height=800;

BufferedImage image =

new BufferedImage

(width,height,BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();

g.fillRect(0, 0, width, height);

Graphics2D g_2d=(Graphics2D)g;

double angSt = 0; //饼弧的开始角度

double angExt = 360;//饼弧的度数

double radius = width/4;

int x = 20;

int y = 20;

Arc2D arc=

new Arc2D.Double

(x,y,2*radius,2*radius,angSt,angExt,Arc2D.PIE);

for(int i = 0;i

angSt = 0;

for(int m=0;m

angSt = angSt+(data[m]/sum)*360;

}

angExt = (data[i]/sum)*360;

arc.setAngleStart(angSt); //设置饼弧的开始角度

arc.setAngleExtent(angExt); //设置饼弧度数

g_2d.setXORMode(Color.white);

if(i%5 ==0){

g_2d.setColor(Color.red);

}

else if(i%5==1){

g_2d.setColor(Color.blue);

}

else if(i%5 ==2){

g_2d.setColor(Color.green);

}

else if(i%5 ==3){

g_2d.setColor(Color.yellow);

}

else if(i%5 ==4){

g_2d.setColor(Color.cyan);

}

g_2d.fill(arc); //绘制饼图

angSt = angSt+angExt/2;

double left_x =

x+radius+radius*Math.cos(angSt*Math.PI/180);

double left_y =

y+radius-radius*Math.sin(angSt*Math.PI/180);

g_2d.setFont

(new Font("",Font.BOLD,20));

g_2d.setColor(Color.black);

String persent =

String.format("%.2f",(data[i]/sum)*100);

if(angSt270){

Line2D line =

new Line2D.Double

(left_x,left_y,2*radius+x+5,left_y);

g_2d.draw(line);

g_2d.drawString

(dataMess[i]+""+

persent+"%(数据:"+data[i]+

")",(int)(2*radius+x+5),(int)left_y);

}

else {

g_2d.drawString

(dataMess[i]+""+persent+

"%(数据:"+data[i]+")",(int)left_x,(int)left_y);

}

}

return image;

}

}

3.实现接口的类(提供数组的柱状图)

import java.awt.Graphics2D;

import java.awt.Graphics;

import java.awt.Color;

import java.awt.Font;

import java.util.Arrays;

//绘制柱状图

public class DrawBarChart implements Drawer {

public BufferedImage getImage(double data[],String dataMess[]){

double sum = 0;

for(double item:data){

sum += item;

}

double copyData []=

Arrays.copyOf(data,data.length);

int width=1000, height=300;

BufferedImage image =

new BufferedImage

(width,height,BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();

g.setColor(Color.cyan);

g.fillRect(0, 0, width, height);

Graphics2D g_2d=(Graphics2D)g;

int startX = 10;

double x = startX;//矩形的左上角x坐标

double y = height-20;//矩形的左上角y坐标

double w = width/data.length/3,

h =0;//矩形的宽和高

double step = w/2;

Rectangle2D rect=

new Rectangle2D.Double(x,y,w,h);

for(int i = 0;i

x = x+w+step;

y = height-data[i]/sum*height;

h = data[i]/sum*height;

rect.setRect(x,y,w,h);

if(i%3 ==0){

g_2d.setColor(Color.red);

}

else if(i%3==1){

g_2d.setColor(Color.blue);

}

else if(i%3 ==2){

g_2d.setColor(Color.green);

}

g_2d.fill(rect); //填充矩形

g_2d.setFont

(new Font("宋体",Font.PLAIN,16));

g_2d.setColor(Color.black);

g_2d.drawString

(dataMess[i]+":"+copyData[i],(int)x,(int)y);

}

Line2D line =

new Line2D.Double(startX,0,startX,height);

g_2d.draw(line);

g_2d.drawString

("数据代数和:"+sum,(int)startX,20);

return image;

}

}

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20200524A0JG0U00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

扫码关注云+社区

领取腾讯云代金券