有很多的繪圖類可以用,jpgraph phplot都可以,和mysql沒什么直接關(guān)系,提供兩組數(shù)據(jù)就可以了。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),五指山企業(yè)網(wǎng)站建設(shè),五指山品牌網(wǎng)站建設(shè),網(wǎng)站定制,五指山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,五指山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
從數(shù)據(jù)庫中不是已經(jīng)返回?cái)?shù)據(jù)了嗎,剩下的就是如何解析這些返回的數(shù)據(jù),然后用繪圖命令繪制出來了
public class CategoryItemChart {
public static String generateBarChart(HttpSession session, PrintWriter pw,int w, int h,ArrayList list) {
String filename = null;
try {
CategoryDataset dataset = createDataset(list);
JFreeChart chart = ChartFactory.createBarChart(
"",//圖表標(biāo)題
"",//X軸標(biāo)題
"",//Y軸標(biāo)題
dataset,//數(shù)據(jù)集合
PlotOrientation.VERTICAL,//圖表顯示方向(水平、垂直)
true,//是否使用圖例
true,//是否使用工具提示
false//是否為圖表增加URL
);
/*------------配置圖表屬性--------------*/
chart.setBackgroundPaint(Color.white); // 1,設(shè)置整個(gè)圖表背景顏色
CategoryPlot plot = chart.getCategoryPlot(); /*------------設(shè)定Plot參數(shù)-------------*/
plot.setBackgroundPaint(Color.white); // 2,設(shè)置詳細(xì)圖表的顯示細(xì)節(jié)部分的背景顏色
plot.setDomainGridlinePaint(Color.black); // 3,設(shè)置垂直網(wǎng)格線顏色
plot.setDomainGridlinesVisible(false); // 4,設(shè)置是否顯示垂直網(wǎng)格線
plot.setRangeGridlinePaint(Color.yellow); // 5,設(shè)置水平網(wǎng)格線顏色
plot.setRangeGridlinesVisible(false); //6,設(shè)置是否顯示水平網(wǎng)格線
/*---------將所有數(shù)據(jù)轉(zhuǎn)換為整數(shù)形式---------*/
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
/*---------設(shè)置是否在柱圖的狀態(tài)條上顯示邊框----*/
CategoryItemRenderer renderer = (CategoryItemRenderer) plot.getRenderer();
BarRenderer render = (BarRenderer) plot.getRenderer();
// render.setItemMargin(0.0);
// render.setMinimumBarLength(0.0);
/*---------設(shè)置狀態(tài)條顏色的深淺漸變-----------*/
GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, new Color(255,200, 80), 0.0f, 0.0f, new Color(255, 255, 40));
GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, new Color(50,255, 50), 0.0f, 0.0f, new Color(100, 255, 100));
GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f,0.0f, new Color(255, 100, 100));
GradientPaint gp3 = new GradientPaint(0.0f, 0.0f, new Color(108,108, 255), 0.0f, 0.0f, new Color(150, 150, 200));
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
renderer.setSeriesPaint(2, gp2);
renderer.setSeriesPaint(3, gp3);
/*
*
* 解決柱狀體與圖片邊框的間距問題
*
*
* */
/*------設(shè)置X軸標(biāo)題的傾斜程度----*/
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.E / 6.0));
/*------設(shè)置柱狀體與圖片邊框的左右間距--*/
domainAxis.setLowerMargin(0.06);
domainAxis.setUpperMargin(0.06);
/*------設(shè)置柱狀體與圖片邊框的上下間距---*/
ValueAxis rAxis = plot.getRangeAxis();
rAxis.setUpperMargin(0.3);
rAxis.setLowerMargin(0.3);
/*---------設(shè)置每一組柱狀體之間的間隔---------*/
render.setItemMargin(0.01);
/*
*
* 解決柱狀體與圖片邊框的間距問題
*
*
* */
/*
*
*
* 解決JFREECHART的中文顯示問題
*
*
* */
/*----------設(shè)置消除字體的鋸齒渲染(解決中文問題)--------------*/
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
/*----------設(shè)置標(biāo)題字體--------------------------*/
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑體", Font.PLAIN, 20));
/*------設(shè)置X軸坐標(biāo)上的文字-----------*/
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
/*------設(shè)置X軸的標(biāo)題文字------------*/
domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12));
/*------設(shè)置Y軸坐標(biāo)上的文字-----------*/
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 14));
/*------設(shè)置Y軸的標(biāo)題文字------------*/
rAxis.setLabelFont(new Font("黑體", Font.PLAIN, 12));
/*---------設(shè)置柱狀體上的顯示的字體---------*/
renderer.setBaseItemLabelFont(new Font("宋體", Font.PLAIN, 12));
renderer.setBaseItemLabelGenerator(new LabelGenerator(0.0));
renderer.setBaseItemLabelsVisible(true);
/*
*
*
* 解決JFREECHART的中文顯示問題
*
*
* */
/*------得到chart的保存路徑----*/
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session);
/*------使用printWriter將文件寫出----*/
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
/*-------------設(shè)置柱狀體頂端的數(shù)據(jù)顯示--------------*/
static class LabelGenerator implements CategoryItemLabelGenerator {
private double threshold;
public LabelGenerator(double threshold) {
this.threshold = threshold;
}
public String generateLabel(CategoryDataset dataset, int row, int column) {
String result = null;
final Number value = dataset.getValue(row, column);
if (value != null) {
final double v = value.doubleValue();
if (v this.threshold) {
result = value.toString();
}
}
return result;
}
public String generateRowLabel(CategoryDataset dataset, int row) {
return null;
}
public String generateColumnLabel(CategoryDataset dataset, int column) {
return null;
}
}
/*-----------數(shù)據(jù)封裝-------------*/
private static CategoryDataset createDataset(ArrayList list) {
String s1 = "1";
String s2 = "2";
String c1 = "1";
String c2 = "2";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(44, s1, c1);
dataset.setValue(48, s2, c2);
return dataset;
}
}
比較完整的一個(gè)得到柱圖的代碼,保存路徑是臨時(shí)文件,怎么從數(shù)據(jù)庫取值應(yīng)該會吧。把dataset處理一下就可以了。
這個(gè)最好先去官網(wǎng)看看 demo 哪里面有代碼 很詳細(xì)。
等看懂 demo 后 就去試試謝謝。
你只要把 你要的數(shù)據(jù)返回成list集合再用map封裝。轉(zhuǎn)換成json數(shù)據(jù)傳到頁面就行了!
本文標(biāo)題:mysql怎么實(shí)現(xiàn)柱狀圖 如何建柱狀圖
瀏覽路徑:http://www.muchs.cn/article4/hhehie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站制作、搜索引擎優(yōu)化、軟件開發(fā)、用戶體驗(yàn)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)