java饼图_java swing绘制饼图

package com.cs.ff;

import java.awt.Color;

import java.awt.FontMetrics;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.Toolkit;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.geom.Arc2D;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JFrame;

import javax.swing.JPanel;

/**

* 绘制3D效果的饼图

*

* @author Biao

*/

@SuppressWarnings("serial")

public class Z3 extends JPanel {

private double[] data; // 在饼图中显示的数据

private Color[] defaultColors; // 预定义饼图的颜色

private Z2[] pies;

private int shadowDepth = 8;

private int shiftAngle = -30;

private int selectedPieIndex = -1; // 鼠标点击是选中的Arc, -1为没有选中

public Z3() {

data = new double[] { 20.72, 6.56, 3.74, 10.26, 15.38, 5.69, 10.72, 15.38, 6.15, 18.0 };

defaultColors = createColors();

int x = 50;

int y = 50;

int w = 380;

int h = (int) (w * 0.618); // 黄金分割

pies = createPies(x, y, w, h, shadowDepth, shiftAngle, data, defaultColors);

// 取得鼠标选中的饼图的下标

addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

selectedPieIndex = -1;

for (int i = 0; i < pies.length; ++i) {

if (pies[i].getArc().contains(e.getX(), e.getY())) {

selectedPieIndex = i;

break;

}

}

repaint();

}

});

}

private Color[] createColors() {

// 返回16进制的值颜色

List colors = new ArrayList();

colors.add(Color.decode("#FF7321"));

colors.add(Color.decode("#169800"));

colors.add(Color.decode("#00E500"));

colors.add(Color.decode("#D0F15A"));

colors.add(Color.decode("#AA6A2D"));

colors.add(Color.decode("#BFDD89"));

colors.add(Color.decode("#E2FF55"));

colors.add(Color.decode("#D718A5"));

colors.add(Color.decode("#00DBFF"));

colors.add(Color.decode("#00FF00"));

return colors.toArray(new Color[0]);

}

public static Z2[] createPies(int x,

int y,

int w,

int h,

int shadowDepth,

double shiftAngle,

double[] data,

Color[] colors) {

double sum = 0;

for (double d : data) {

sum += d;

}

// 初始化Pies

double arcAngle = 0;

double startAngle = shiftAngle;

Z2[] pies = new Z2[data.length];

for (int i = 0; i < data.length; ++i) {

arcAngle = data[i] * 360 / sum; // 使用百分比计算角度

if (i + 1 == data.length) {

arcAngle = 360 + shiftAngle - startAngle; // 保证闭合

arcAngle = arcAngle > 0 ? arcAngle : 0;

}

Arc2D.Double arc = new Arc2D.Double(x, y, w, h, startAngle, arcAngle, Arc2D.PIE);

pies[i] = new Z2(arc, colors[i % colors.length], data[i], shadowDepth, 30);

startAngle += arcAngle;

}

return pies;

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

if (pies != null) {

drawPies(g2d, pies, selectedPieIndex);

}

}

private void drawPies(Graphics2D g2d, Z2[] pies, int selectedIndex) {

int startIndex = 0; // 从第几个饼图开始绘制

int endIndex = pies.length; // 要画的饼图的数量.

boolean closed = (endIndex - startIndex == pies.length) ? true : false;

boolean selected = (selectedIndex >= startIndex && selectedIndex < endIndex) ? true : false;

FontMetrics metrics = g2d.getFontMetrics();

// 一次性绘制完3D效果,然后再绘制饼图的效果比绘制饼图的同时绘制好

for (int i = startIndex; i < endIndex; ++i) {

if (i != selectedIndex) {

Z2 p = pies[i];

g2d.setColor(p.getColor().darker());

g2d.fill(p.getFrontSite());

}

}

// 如果没有闭合时,且选中的不是第一块,则第一块画左面

if (!closed && selectedIndex != startIndex) {

g2d.setColor(pies[startIndex].getColor().darker());

g2d.fill(pies[startIndex].getLeftSite());

}

// 如果没有闭合时,且选中的不是最后一块,则最后一块画右面

if (!closed && selectedIndex + 1 != endIndex) {

g2d.setColor(pies[endIndex - 1].getColor().darker());

g2d.fill(pies[endIndex - 1].getRightSite());

}

// 有饼图被选中

if (selected) {

int prevIndex = selectedIndex > startIndex ? (selectedIndex - 1) : endIndex - 1;

int nextIndex = (selectedIndex + 1) >= endIndex ? startIndex : (selectedIndex + 1);

// 前一个画右墙

g2d.setColor(pies[prevIndex].getColor().darker());

g2d.fill(pies[prevIndex].getRightSite());

// 后一个画左墙

g2d.setColor(pies[nextIndex].getColor().darker());

g2d.fill(pies[nextIndex].getLeftSite());

}

// 最后再绘制饼图的上面部分,把不需要的部分隐藏掉

for (int i = startIndex; i < endIndex; ++i) {

if (i != selectedIndex) {

Z2 p = pies[i];

g2d.setColor(p.getColor());

g2d.fill(p.getArc());

int sw = metrics.stringWidth(p.getLabel()) / 2;

int sh = (metrics.getAscent()) / 2;

int x = (int) (p.getLabelPosition().getX() - sw);

int y = (int) (p.getLabelPosition().getY() + sh);

g2d.setColor(Color.BLACK);

g2d.drawString(p.getLabel(), x, y);

}

}

// 绘制被选中的饼图

if (selected) {

Z2 p = pies[selectedIndex].getSelectedPie();

g2d.setColor(p.getColor().darker());

g2d.fill(p.getFrontSite());

g2d.fill(p.getLeftSite());

g2d.fill(p.getRightSite());

g2d.setColor(p.getColor());

g2d.fill(p.getArc());

int sw = metrics.stringWidth(p.getLabel()) / 2;

int sh = (metrics.getAscent()) / 2;

int x = (int) (p.getLabelPosition().getX() - sw);

int y = (int) (p.getLabelPosition().getY() + sh);

g2d.setColor(Color.BLACK);

g2d.drawString(p.getLabel(), x, y);

}

}

private static void createGuiAndShow() {

JFrame frame = new JFrame("Pie with 3D Effect");

frame.getContentPane().add(new Z3());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

int sw = Toolkit.getDefaultToolkit().getScreenSize().width;

int sh = Toolkit.getDefaultToolkit().getScreenSize().height;

int w = 500;

int h = 400;

int x = (sw - w) / 2;

int y = (sh - h) / 2 - 40;

x = x > 0 ? x : 0;

y = y > 0 ? y : 0;

frame.setBounds(x, y, w, h);

frame.setVisible(true);

}

public static void main(String[] args) {

createGuiAndShow();

}

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值