📄 piechart.java
字号:
package view;//Copyright (C) 2008 Harald Unander, Wang Wenjuan//// This file is part of WlanTV.//// WlanTV is free software: you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation, either version 3 of the License, or// (at your option) any later version.//// WlanTV is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with WlanTV. If not, see <http://www.gnu.org/licenses/>.import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Insets;import java.awt.Rectangle;import java.awt.Shape;import java.awt.geom.Arc2D;import java.awt.geom.Rectangle2D;import javax.swing.BorderFactory;import javax.swing.JPanel;import main.Main;import model.DataSet;@SuppressWarnings("serial")public class PieChart extends JPanel { public PieChart(DataSet[] dataSet) { for (DataSet ds : dataSet) add(new Chart(ds)); } private class Chart extends JPanel { DataSet dataSet; Graphics2D g2d; public Chart(DataSet dataSet) { this.dataSet = dataSet; setBorder(BorderFactory.createTitledBorder(dataSet.getName())); } @Override public void paint(Graphics g) { super.paint(g); g2d = (Graphics2D) g; Insets is = getInsets(); int height = getHeight()-is.top-is.bottom; if (height > 150) height = 150; Rectangle r = new Rectangle(is.left,is.top,getWidth()-is.left-is.right,height); drawPie(g2d, r, dataSet); } public void drawPie(Graphics2D g, Rectangle area, DataSet slices) { float col = 0.05f; float step = 0.13f; double total = 0.0D; for (long val : dataSet.a) { total += val; } double curValue = 0.0D; double startAngle = 0; float lblX = area.x+area.height+7; float lblY = area.y; for (int i=0; i<dataSet.getSize(); i++) { long value = dataSet.a[i]; String label = dataSet.labels[i]; startAngle = curValue * 360 / total; double arcAngle = value * 360 / total;// System.out.print(startAngle+"/"+arcAngle); Shape s = new Arc2D.Double(area.x+10, area.y+10, area.height-20, area.height-20, startAngle, arcAngle, Arc2D.PIE); Color c = Color.getHSBColor(col, 0.8f, 0.8f); g.setPaint(c); g.fill(s); g.setPaint(Color.BLACK); g.draw(s); if (value != 0) { Shape s1 = new Rectangle2D.Float(lblX+5, lblY, 10,10); g.setPaint(c); g.fill(s1); g.setPaint(Color.BLACK); g.draw(s1); g.setFont(new Font("Helvetica",Font.BOLD,9)); String pLabel; if (dataSet.isTimePie()) pLabel = label + " : " + (long)Math.round(100*value/total) + "%";// pLabel = (long)Math.round(100*value/total) + "% : "+label; else pLabel = label + " : " + value;// pLabel = value + " : " + label; g.setFont(Main.SMALLFONT); g.drawString(pLabel, lblX+25, lblY+9); lblY+=15; } col+=step; curValue += value; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -