java餅圖
『壹』 java 能不能為一個JFrame中的JPanel添加一個餅形圖
import java.awt.Graphics;
import javax.swing.*;
class mypanel extends JPanel{
public void paint(Graphics g){
super.paint(g);
g.drawOval(50, 50, 100, 50);//畫橢圓其實是畫矩形的內接橢圓,(x,y,width,height)起始坐標,外判告接矩形寬和高,
}
}
public class Jpanel extends JFrame {
Jpanel(){
super("橢圓");
setSize(200,200);
setContentPane(new mypanel());
setVisible(true);
}
public static void main(String[] args) {
new Jpanel();
}
}//餅形圖應該就是特殊的橢圓吧,掘神明想要什麼形狀的 ,改變width和瞎輪height就行了
『貳』 怎麼用JAVA 開發的圓餅圖
java繪制餅圖
<%@ page language="java" contentType="image/png;charset=GBK" pageEncoding="GBK"
import="java.awt.*, javax.imageio.*,java.awt.geom.*,java.awt.image.*"%>
<%!// 繪制餅圖的說明
public void drawTips(String tips, Color color, Arc2D.Double arc2d, Graphics2D g2d) {
Arc2D.Double position = arc2d;
position.setAngleExtent(arc2d.getAngleExtent() / 2);
position.x = arc2d.x - 15;
position.y = arc2d.y - 15;
position.width = arc2d.getWidth() + 50;
position.height = arc2d.getHeight() + 50;
Point2D.Double endPoint = (Point2D.Double) position.getEndPoint();
g2d.setPaint(color);
int stringLength = g2d.getFontMetrics().stringWidth(tips);
if (endPoint.x <= arc2d.getCenterX())
g2d.drawString(tips, (float) endPoint.x - stringLength,
(float) endPoint.y);
else {
g2d.drawString(tips, (float) endPoint.x, (float) endPoint.y);
}
}
%>
<%
// 清空岩滑緩沖區
response.reset();
// 注意這里的MIME類型
response.setContentType("image/png");
//悉棗巧 創建一個 500X375 的圖像
int width = 500, height = 375;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 創建Java2D對象
Graphics2D g2d = image.createGraphics();
// 填充整個背景
g2d.setPaint(Color.WHITE);
g2d.fillRect(0, 0, width, height);
// 繪制陰影,由灰色漸進圓角矩形組成
GradientPaint grayGP = new GradientPaint(0, 0, Color.GRAY, width,
height, new Color(218, 214, 212), false);
g2d.setPaint(grayGP);
RoundRectangle2D.Float bgRR = new RoundRectangle2D.Float(5, 5,
width - 5, height - 5, 50, 50);
g2d.fill(bgRR);
// 繪制漸進藍色圓角矩形背景
GradientPaint blueGP = new GradientPaint(0, 0, new Color(252, 197,
113), width / 2, height / 2, new Color(255, 255, 169), true);
g2d.setPaint(blueGP);
g2d.fillRoundRect(0, 0, width - 5, height - 5, 50, 50);
// 繪制深藍色圓角矩睜鍵形輪廓
BasicStroke bs = new BasicStroke(1.2f);
g2d.setStroke(bs);
g2d.setPaint(new Color(55, 71, 105));
g2d.drawRoundRect(0, 0, width - 5, height - 5, 50, 50);
// 繪制圖表標題
//String chartTitle = "客戶構成分析";
g2d.setColor(Color.BLACK);
g2d.setFont(new Font("宋體", Font.PLAIN, 30));
//int stringLength = g2d.getFontMetrics().stringWidth(chartTitle);
//g2d.drawString(chartTitle, (width - stringLength) / 2, 30);
// 定義圓弧
Arc2D.Double arc2d = new Arc2D.Double();
double startAngle = 30.0, arcAngle = 0.0;
double rectWidth = 295.0, rectHeight = 245.0;
Point2D.Double p2d = new Point2D.Double((width - rectWidth) / 2, 70.0);
int arcType = Arc2D.PIE;
Color color[] = new Color[5];
color[0] = new Color(99, 99, 0);
color[1] = new Color(255, 169, 66);
color[2] = new Color(33, 255, 66);
color[3] = new Color(33, 0, 255);
color[4] = new Color(255, 0, 66);
double bookSales[] = new double[5];
double totalSales = 0.0;
// 定義厚度
int thickness = 25;
for (int i = 0; i < bookSales.length; i++) {
bookSales[i] = 1 + Math.random() * 99;
totalSales += bookSales[i];
}
// 繪制起始角度不大於90度的圓弧
startAngle = 30;
for (int i = 0; i < bookSales.length; i++) {
arcAngle = bookSales[i];
if (startAngle >= 90) {
break;
}
for (int j = thickness; j > 0; j--) {
g2d.setColor(color[i].darker());
arc2d = new Arc2D.Double(p2d.x, p2d.y + j, rectWidth,
rectHeight, startAngle, arcAngle, arcType);
// 填充圓弧
g2d.fill(arc2d);
}
// 更新圓弧的起始角度
startAngle += arcAngle;
}
// 繪制起始角度不小於270度的圓弧
startAngle = 390;
for (int i = bookSales.length - 1; i > 0; i--) {
arcAngle = bookSales[i];
if (startAngle <= 270) {
break;
}
for (int j = thickness; j > 0; j--) {
g2d.setColor(color[i].darker());
arc2d = new Arc2D.Double(p2d.x, p2d.y + j, rectWidth,
rectHeight, startAngle, -arcAngle, arcType);
// 填充圓弧
g2d.fill(arc2d);
}
// 更新圓弧的起始角度
startAngle -= arcAngle;
}
// 繪制起始角度在90度到270度之間的圓弧
startAngle = 30;
for (int i = 0; i < bookSales.length; i++) {
arcAngle = bookSales[i];
if (startAngle > 90 && startAngle < 270) {
for (int j = thickness; j > 0; j--) {
g2d.setColor(color[i].darker());
arc2d = new Arc2D.Double(p2d.x, p2d.y + j, rectWidth,
rectHeight, startAngle + arcAngle, -arcAngle,
arcType);
// 填充圓弧
g2d.fill(arc2d);
}
}
// 更新圓弧的起始角度
startAngle += arcAngle;
}
// 繪制最上面一層圓弧
startAngle = 30;
for (int i = 0; i < bookSales.length; i++) {
arcAngle = bookSales[i];//item.getPercent() * 3.6;
g2d.setColor(color[i]);
arc2d = new Arc2D.Double(p2d.x, p2d.y, rectWidth, rectHeight,
startAngle, arcAngle, arcType);
// 填充圓弧
g2d.fill(arc2d);
// 描繪圓弧輪廓
g2d.setStroke(new BasicStroke(1.2f));
g2d.setPaint(Color.GRAY);
g2d.draw(arc2d);
// 更新圓弧的起始角度
startAngle += arcAngle;
g2d.setFont(new Font("宋體", Font.PLAIN, 12));
}
// 部署圖形
g2d.dispose();
// 利用ImageIO類的write方法對圖像進行編碼
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "PNG", sos);
sos.close();
out.clear();
out = pageContext.pushBody();
%>
『叄』 如何用java整合echarts生成餅圖
首先要把echarts所需的js和swf文件導入進去
//後台拼圖標所需xml
StringBufferoutXml=newStringBuffer();//任務列表xml字元串
@Action("/task/loadMyTaskPercentXml")
()
{
HttpServletResponseres=ServletActionContext.getResponse();
res.setHeader("Cache-Control","no-store");
res.setHeader("Pragma","no-cache");
res.setDateHeader("Expires",0);
res.setContentType("text/xml;charset=GBK");
PrintWriterout=res.getWriter();
StringBufferoutXml=newStringBuffer(
"<?xmlversion="1.0"encoding="GBK"?>
"滾絕);
outXml
.append("<chartbaseFontSize='12'caption='任務統計'subcaption=''yAxisMinValue='51650.1'yAxisMaxValue='71118.3'xaxisname='日期'yaxisname='數量'hovercapbg='FFECAA'hovercapborder='F47E00'formatNumberScale='0'decimalPrecision='0'showvalues='1'numdivlines='10'numVdivlines='0'shownames='1'rotateNames='1'>");
outXml.append("
");
outXml.append("<setname='已完成任務(%)");
outXml.append("'value='"+s1.replace("%","")+"");
outXml.append("'/>");
outXml.append("
");
outXml.append("<setname='未完鏈弊成任務(%)");
outXml.append("'value='"+s2.replace("%","")+"");
outXml.append("'/>");
outXml.append("
");
outXml.append("</chart>
"棚備族);
out.print(outXml.toString());
out.flush();
out.close();
returnnull;
}
在後台進行拼裝xml,並且返回到頁面
<divstyle="display:none"id="taskListGraph">${outXml}</div>
<scripttype="text/javascript">
vartext=document.getElementById("taskListGraph").innerHTML
varchart2=newFusionCharts("${base}/swf/Bar2D.swf?ChartNoDataText=暫無數據&XMLLoadingText=正在載入數據,請稍候","chart02","610","276");
chart2.setDataXML(text);
chart2.render('jdbox');
chart2.addParam("wmode","Opaque");
</script>
這樣圖就生成好了!
『肆』 如何使用java做統計圖表
//參考地址http://echarts..com/去網站下js控制項,下面是餅圖的代碼,下圖是我的代碼效果
functionquery1(housetype,redStatisticsList,text0,cashingSum,tranferSum){
<!--紅包發放數據-->
varmyrodiusred=echarts.init(document.getElementById('知頌碧mainrodius'));//ID
varredHousehold=housetype;
varredMoney=redStatisticsList;
varallMoney=0;//總金額搭舉
$.each(redStatisticsList,function(index,item){
allMoney=allMoney+Number(item.value);
})
optionTwo={
title:{
text:text0,
subtext:"總金額:"+allMoney+" 提現櫻戚總額:"+cashingSum+" 到賬總額:"+tranferSum,
x:'center'
},
tooltip:{
trigger:'item',
formatter:"{a}<br/>{b}({d}%)"
},
legend:{
orient:'vertical',
left:'left',
data:housetype
},
series:[
{
type:'pie',
radius:'55%',
center:['50%','60%'],
data:redMoney,
itemStyle:{
emphasis:{
shadowBlur:10,
shadowOffsetX:0,
shadowColor:'rgba(0,0,0,0.5)'
}
}
}
]
};
myrodiusred.setOption(optionTwo);
}
『伍』 java freechart 餅圖厚度
餅圖的厚度是可以自己設置的 ,默認厚度一般比較厚。
每一個餅圖片區的外廓默認是一條細灰線勾畫出來的。PiePlot類提供了如下選擇項:
完全不顯示片區外廓
通過改變缺宏檔省的值來改變全部的片區外廓
單獨改變部分餅圖的片區外廓
完全關閉/開啟片區外廓
調用該方法可以觸發PlotChangeEvent事件。
片區外廓的控制
在片區外廓顯示的時候,我們可以改變餅圖片區的整個外廓顏色或風格或者單個餅圖片區的
顏色冊鎮或風格。 整個外廓顏色或風格的修改需要在基本層裡面設置,單個餅圖片區的顏色設置
需要在系列層中設置。 在基本層里,如果沒有更高層的顏色設置,則調用已定義的默認設
置。 我們可以使用PiePot類的方法州絕粗來改變我們的設置