|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果你学习的是市场营销,是销售,也许参加大课堂的学习会更合适,因为你的工作能力中有个基础就是搭建自己的人脉,chart
比来因为项目必要,入手下手进修JFreeChart和iText,在网上找了一下相干的材料不是良多,出格是JFreeChart在官方文档上没有像iText那样细致的申明及例子,并且官方的demo只要一个jar文件,内里也对照乱,不晓得从甚么中央动手,在网上找了几个例子,入手下手循规蹈矩的学吧!
第一个例子是个台湾人写的,良多的名词都是用的台湾说法,读过侯捷师长教师翻译的书的伴侣应当有很深的体味!进修任何一个开源的项目第一个义务就是配情况,
JFreeChart首:http://www.jfree.org/jfreechart/
JFreeChartAPI:http://www.jfree.org/jfreechart/javadoc/
今朝的版本:jfreechart-1.0.0详细的安装和classpath的设置就未几说了
下面就是该例子要天生的柱状图!代码以下:
packageHelloJChart;importjava.awt.Dimension;
importjavax.swing.JFrame;
importorg.jfree.chart.ChartFactory;importorg.jfree.chart.ChartPanel;importorg.jfree.chart.JFreeChart;importorg.jfree.chart.plot.PlotOrientation;importorg.jfree.data.category.CategoryDataset;importorg.jfree.data.category.DefaultCategoryDataset;
publicclassHelloBarChartextendsJFrame{publicHelloBarChart(){CategoryDatasetdataset=createDataset();JFreeChartchart=createChart(dataset);chart=customizeChart(chart);ChartPanelchartPanel=newChartPanel(chart);chartPanel.setPreferredSize(newDimension(500,270));getContentPane().add(chartPanel);
pack();setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
publicstaticvoidmain(String[]args){newHelloBarChart();}
privateCategoryDatasetcreateDataset(){//rowkeys...Stringseries1="First";Stringseries2="Second";Stringseries3="Third";
//columnkeys...Stringcategory1="Category1";Stringcategory2="Category2";Stringcategory3="Category3";Stringcategory4="Category4";Stringcategory5="Category5";
//createthedataset...DefaultCategoryDatasetdataset=newDefaultCategoryDataset();
dataset.addValue(1.5,series1,category1);dataset.addValue(4.2,series1,category2);dataset.addValue(3.0,series1,category3);dataset.addValue(5.0,series1,category4);dataset.addValue(5.0,series1,category5);
dataset.addValue(5.5,series2,category1);dataset.addValue(7.8,series2,category2);dataset.addValue(6.0,series2,category3);dataset.addValue(8.0,series2,category4);dataset.addValue(4.0,series2,category5);
dataset.addValue(4.0,series3,category1);dataset.addValue(3.0,series3,category2);dataset.addValue(2.0,series3,category3);dataset.addValue(3.0,series3,category4);dataset.addValue(6.0,series3,category5);
returndataset;}
privateJFreeChartcreateChart(finalCategoryDatasetdataset){JFreeChartchart=ChartFactory.createBarChart("HelloBarChart",//charttitle"Category",//domainaxislabel"Value",//rangeaxislabeldataset,//dataPlotOrientation.VERTICAL,//orientationtrue,//includelegendtrue,//tooltips?false//URLs?);returnchart;}
privateJFreeChartcustomizeChart(finalJFreeChartchart){returnchart;}}
要创建一JFreeChart的D形次要有三步E
创建一碛匈Y料的DataSet用DataSet造JFreeChartJFreeChart作一些自的O@示JFreeChart
第一步:创建DataSet
BarChart利用的DataSet接口org.jfree.data.CategoryDataset的DataSet。有煞N体例斫CategoryDataSet
利用CategoryDataSet的子类org.jfree.data.DefaultCategoryDataset,再用addValue()把Y料到场DataSet中创建包括抵档亩S列,再利用org.jfree.data.DatasetUtilities的createCategoryDataset()
利用DefaultCategoryDataSet
DefaultCategoryDataSetclass:
publicvoidaddValue(doublevalue,java.lang.ComparablerowKey,java.lang.ComparablecolumnKey)publicvoidaddValue(java.lang.Numbervalue,java.lang.ComparablerowKey,java.lang.ComparablecolumnKey)
value-thevaluerowKey-therowkeycolumnKey-thecolumnkey
参照后面的createDataset办法!
利用org.jfree.data.DatasetUtilities
org.jfree.data.DatasetUtilitiesclass:
publicstaticCategoryDatasetcreateCategoryDataset(StringrowKeyPrefix,StringcolumnKeyPrefix,java.lang.Number[][]data)publicstaticCategoryDatasetcreateCategoryDataset(String[]rowKeys,String[]columnKeys,double[][]data)publicstaticCategoryDatasetcreateCategoryDataset(StringrowKey,KeyedValuesrowData)
rowKeyPrefix-therowkeyprefix.columnKeyPrefix-thecolumnkeyprefix.rowKeys-therowkeys.columnKeys-thecolumnkeys.data-thedata.
privateCategoryDatasetcreateDataset(){double[][]data=newdouble[][]{{1.0,43.0,35.0,58.0,54.0,77.0,71.0,89.0},{54.0,75.0,63.0,83.0,43.0,46.0,27.0,13.0},{41.0,33.0,22.0,34.0,62.0,32.0,42.0,34.0}};returnDatasetUtilities.createCategoryDataset("Series","Factor",data);}
第二步:造JFreeChart
要用DataSet造出一JFreeCharte,我K不间接w化出一JFreeChartw,而是利用ChartFactoryee面的办法。
ChartFactoryclass:
publicstaticJFreeChartcreateBarChart(java.lang.Stringtitle,java.lang.StringcategoryAxisLabel,java.lang.StringvalueAxisLabel,CategoryDatasetdata,PlotOrientationorientation,booleanlegend,booleantooltips,booleanurls)
title-thecharttitle.categoryAxisLabel-thelabelforthecategoryaxis.valueAxisLabel-thelabelforthevalueaxisdata-thedatasetforthechart.orientation-theplotorientation(PlotOrientation.HORIZONTALorPlotOrientation.VERTICAL).legend-aflagspecifyingwhetherornotalegendisrequired.tooltips-configurecharttogeneratetooltips?urls-configurecharttogenerateURLs?
privateJFreeChartcreateChart(finalCategoryDatasetdataset){JFreeChartchart=ChartFactory.createBarChart("HelloBarChart",//标题
"Category",//行称号
"Value",//列称号
dataset,//数据
PlotOrientation.VERTICAL,//横向,纵向
true,//图例
true,//柱状申明false//URLs?);returnchart;}
第三步:修JFreeChart
@例中K]有JFreeChart作修。
privateJFreeChartcustomizeChart(finalJFreeChartchart){returnchart;}
第四步:@示JFreeChart
ChartPanel是一^承JPanel的e,把JFreeChart在贸淌街酗@示出怼N只需把JFreeChart成鹘oChartPanel的建子。造出ChartPanel的w後,O定巨细,再成一样平常的Panel放进ContentPane中就能够了。
ChartPanelclass:
publicChartPanel(JFreeChartchart)publicChartPanel(JFreeChartchart,booleanuseBuffer)publicChartPanel(JFreeChartchart,booleanproperties,booleansave,booleanprint,booleanzoom,booleantooltips)publicChartPanel(JFreeChartchart,intwidth,intheight,intminimumDrawWidth,intminimumDrawHeight,intmaximumDrawWidth,intmaximumDrawHeight,booleanuseBuffer,booleanproperties,booleansave,booleanprint,booleanzoom,booleantooltips)
chart-thechart.useBuffer-aflagcontrollingwhetherornotanoff-screenbufferisused.properties-aflagindicatingwhetherornotthechartpropertyeditorshouldbeavailableviathepopupmenu.save-aflagindicatingwhetherornotsaveoptionsshouldbeavailableviathepopupmenu.print-aflagindicatingwhetherornottheprintoptionshouldbeavailableviathepopupmenu.zoom-aflagindicatingwhetherornotzoomoptionsshouldbeaddedtothepopupmenu.tooltips-aflagindicatingwhetherornottooltipsshouldbeenabledforthechart.width-thepreferredwidthofthepanel.height-thepreferredheightofthepanel.minimumDrawWidth-theminimumdrawingwidth.minimumDrawHeight-theminimumdrawingheight.maximumDrawWidth-themaximumdrawingwidth.maximumDrawHeight-themaximumdrawingheight.
publicHelloBarChart(){CategoryDatasetdataset=createDataset();JFreeChartchart=createChart(dataset);chart=customizeChart(chart);ChartPanelchartPanel=newChartPanel(chart);chartPanel.setPreferredSize(newDimension(500,270));getContentPane().add(chartPanel);pack();setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
至此第一个例子已进修终了,基础懂得了JFreeChart的一些基础手艺!上面再进修一个柱状图的例子
首先java功能强大的背后是其复杂性,就拿web来说,当今流行的框架有很多,什么struts,spring,jQuery等等,而这无疑增加了java的复杂性。 |
|