jfreechart實(shí)現(xiàn)java畫圖.doc_第1頁
jfreechart實(shí)現(xiàn)java畫圖.doc_第2頁
jfreechart實(shí)現(xiàn)java畫圖.doc_第3頁
jfreechart實(shí)現(xiàn)java畫圖.doc_第4頁
jfreechart實(shí)現(xiàn)java畫圖.doc_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

package com.dsc.utils;import java.awt.Color;import java.awt.Font;import java.awt.RenderingHints;import java.io.File;import java.io.IOException;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.NumberAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.labels.StandardXYItemLabelGenerator;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.plot.XYPlot;import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;import org.jfree.chart.title.TextTitle;import org.jfree.data.xy.XYSeries;import org.jfree.data.xy.XYSeriesCollection;import org.jfree.ui.RectangleInsets;public class JfreeChartUtils_Line /* * param pictureTitle 圖表標(biāo)題 * param xTitle x軸標(biāo)題 * param yTitle y軸標(biāo)題 * param dataset 數(shù)據(jù)集 * param width 寬度 * param height 高度 * return 圖片的路徑 */ public String createLinePicture(String pictureTitle , String xTitle , String yTitle , XYSeriesCollection dataset , int width , int height) String path = ; JFreeChart chart = ChartFactory.createXYLineChart(pictureTitle, yTitle, xTitle, dataset, PlotOrientation.VERTICAL, true, true, false); chart.setBackgroundPaint(Color.LIGHT_GRAY); / 設(shè)置圖表屬性 / 取得CategoryPlot,設(shè)定Plot參數(shù) XYPlot plot = chart.getXYPlot(); /設(shè)置網(wǎng)格背景顏色 plot.setBackgroundPaint(Color.white); /設(shè)置網(wǎng)格豎線顏色 plot.setDomainGridlinePaint(Color.pink); /設(shè)置網(wǎng)格橫線顏色 plot.setRangeGridlinePaint(Color.pink); / 設(shè)置是否顯示垂直網(wǎng)格線 plot.setDomainGridlinesVisible(true); / 設(shè)置是否顯示水平網(wǎng)格線 plot.setRangeGridlinesVisible(true); XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer(); /設(shè)置曲線圖與xy軸的距離 plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D); /設(shè)置曲線是否顯示數(shù)據(jù)點(diǎn) xylineandshaperenderer.setItemLabelGenerator(new StandardXYItemLabelGenerator(); xylineandshaperenderer.setBaseShapesVisible(true); /是否顯示數(shù)據(jù)點(diǎn)的值 xylineandshaperenderer.setBaseItemLabelsVisible(true); /X軸整數(shù)顯示 NumberAxis x = (NumberAxis)plot.getDomainAxis(); x.setStandardTickUnits(NumberAxis.createIntegerTickUnits(); /Y軸整數(shù)顯示 NumberAxis y = (NumberAxis)plot.getDomainAxis(); y.setStandardTickUnits(NumberAxis.createIntegerTickUnits(); ValueAxis domainAxis = plot.getDomainAxis(); domainAxis.setTickLabelFont(new Font(黑體, Font.BOLD, 12); / 設(shè)置Y軸的標(biāo)題文字 domainAxis.setLabelFont(new Font(黑體, Font.BOLD, 18); domainAxis.setUpperMargin(0.1); ValueAxis rAxis = plot.getRangeAxis(); rAxis.setUpperMargin(0.15); rAxis.setLowerMargin(0.15); 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è)置Y軸坐標(biāo)上的文字 rAxis.setTickLabelFont(new Font(黑體, Font.BOLD, 12); / 設(shè)置Y軸的標(biāo)題文字 rAxis.setLabelFont(new Font(黑體, Font.BOLD, 18);/ 圖表底部亂碼 chart.getLegend().setItemFont(new Font(黑體, Font.BOLD, 18); / 向磁盤輸出圖片文件 path = f:/line.jpg; File file = new File(path); try ChartUtilities.saveChartAsJPEG(file, chart, width, height); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); finally return path; public static XYSeriesCollection createDataXYSet() XYSeriesCollection seriesCollection = new XYSeriesCollection(); XYSeries series1 = new XYSeries(筆記本); XYSeries series2 = new XYSeries(數(shù)碼相機(jī)); XYSeries series3 = new XYSeries(手機(jī)); series1.add(1, 70); series1.add(2, 40); series1.add(3, 30); series1.add(4, 30); series1.add(5, 20); series1.add(6, 30); series2.add(1, 10); series2.add(2, 50); series2.add(3, 40); series2.add(4, 47); series2.add(5, 35); series2.add(6, 52); series3.add(1, 44); series3.add(2, 38); series3.add(3, 25); series3.add(4, 29); series3.add(5, 23); series3.add(6, 83); seriesCollection.addSeries(series1); seriesCollection.addSeries(series2); seriesCollection.addSeries(series3); return seriesCollection; public static void main(String args) JfreeChartUtils_Line chart = new JfreeChartUtils_Line(); chart.createLinePicture(電子產(chǎn)品銷售圖, 產(chǎn)品數(shù)據(jù)(單位/萬), 統(tǒng)計(jì)月份, chart.createDataXYSet(), 700, 490); package com.dsc.utils;import java.awt.Color;import java.awt.Font;import java.awt.GradientPaint;import java.awt.RenderingHints;import java.io.File;import java.io.IOException;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.CategoryLabelPositions;import org.jfree.chart.axis.NumberAxis;import org.jfree.chart.axis.ValueAxis;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.renderer.category.BarRenderer;import org.jfree.chart.renderer.category.CategoryItemRenderer;import org.jfree.chart.title.TextTitle;import org.jfree.data.category.CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;public class JfreeChartUtils /* * 根據(jù)指定的數(shù)據(jù)集生成一張柱形圖 * param pictureTitle 圖表標(biāo)題 * param xTitle x軸標(biāo)題 * param yTitle y軸標(biāo)題 * param dataset 數(shù)據(jù)集 * param width 圖片寬度 * param height 圖片高度 * return 返回圖片的路徑 */ public String createHistogram(String pictureTitle , String xTitle , String yTitle , CategoryDataset dataset , int width , int height) /保存圖片的絕對(duì)路徑 String path = ; JFreeChart chart = ChartFactory.createBarChart(pictureTitle, yTitle, xTitle, dataset, PlotOrientation.VERTICAL, /圖表顯示方向(水平、垂直) true, /是否顯示圖例(對(duì)于簡單的柱狀圖必須是false) true, /是否生成工具 false);/是否生成URL鏈接 /設(shè)置整個(gè)圖表的背景色 chart.setBackgroundPaint(Color.LIGHT_GRAY); /獲得圖表區(qū)域?qū)ο?CategoryPlot plot = chart.getCategoryPlot(); / 設(shè)置圖表的顯示部分的背景色 plot.setBackgroundPaint(Color.PINK); / 設(shè)置垂直網(wǎng)格線顏色 plot.setDomainGridlinePaint(Color.black); / 設(shè)置是否顯示垂直網(wǎng)格線 plot.setDomainGridlinesVisible(true); / 設(shè)置水平網(wǎng)格線顏色 plot.setRangeGridlinePaint(Color.yellow); / 設(shè)置是否顯示水平網(wǎng)格線 plot.setRangeGridlinesVisible(true); /取得NumberAxis,將數(shù)據(jù)轉(zhuǎn)換為整數(shù)形式 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits(); / 設(shè)置是否在柱狀圖的狀態(tài)條上顯示邊框 BarRenderer render = (BarRenderer) plot.getRenderer(); render.setItemMargin(0.1); / 設(shè)置柱狀圖的漸變色 CategoryItemRenderer renderer = (CategoryItemRenderer) plot.getRenderer(); 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(1.57); / 設(shè)置柱狀體與圖片邊框的左右間距 domainAxis.setLowerMargin(0.01); domainAxis.setUpperMargin(0.01); / 設(shè)置柱狀體與圖片邊框的上下間距 ValueAxis rAxis = plot.getRangeAxis(); rAxis.setUpperMargin(0.15); rAxis.setLowerMargin(0.15); / 設(shè)置每一組柱狀體之間的間隔 render.setItemMargin(0.0); / 設(shè)置消除字體的鋸齒渲染(解決中文問題) chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); /設(shè)置X軸上類型子圖片的字體類型 chart.getLegend().setItemFont(new Font(黑體, Font.BOLD, 18); / 設(shè)置標(biāo)題字體 TextTitle textTitle = chart.getTitle(); textTitle.setFont(new Font(黑體, Font.PLAIN, 20); / 設(shè)置X軸坐標(biāo)上的文字 domainAxis.setTickLabelFont(new Font(黑體, Font.PLAIN, 18); / 設(shè)置X軸的標(biāo)題文字 domainAxis.setLabelFont(new Font(黑體, Font.PLAIN, 18); / 設(shè)置Y軸坐標(biāo)上的文字 rAxis.setTickLabelFont(new Font(黑體, Font.PLAIN, 18); / 設(shè)置Y軸的標(biāo)題文字 rAxis.setLabelFont(new Font(黑體, Font.PLAIN, 18); path = f:/test.png; File file = new File(path); try ChartUtilities.saveChartAsPNG(file, chart, width, height); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); return path; private CategoryDataset createCategoryDataset() String str1 = 筆記本; String str2 = 臺(tái)式機(jī); String str3 = 數(shù)碼相機(jī); String str4 = 手機(jī); String date1 = 1月; String date2 = 2月; String date3 = 3月; Str

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論