首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

JFreeChart柱状图的完整范例

2012-10-10 
JFreeChart柱状图的完整实例??defaultDataset.addValue(101d, S1, banana)??defaultDataset.addValue

JFreeChart柱状图的完整实例
??defaultDataset.addValue(101d, "S1", "banana");
??defaultDataset.addValue(150d, "S1", "grape");
??defaultDataset.addValue(12d,"S1","pear");
??
??return defaultDataset;
??
?}
?public JFreeChart createChart(CategoryDataset dataset){
??System.out.println("===========createJfreechart========");
??//创建jfreechart
??????? JFreeChart jfreechart=ChartFactory.createBarChart("Ali's fruits","type","number",dataset,PlotOrientation.HORIZONTAL,false,true,false);?
??????? //设置jfreechart的背景
??????? System.out.println("jfreechart="+jfreechart);
??????? jfreechart.setBackgroundPaint(Color.yellow);
???????
??????? //创建plot
??????? CategoryPlot categoryPlot=(CategoryPlot)jfreechart.getPlot();
??????? System.out.println("categoryPlot="+categoryPlot);
??????? //设置场景的背景色
??????? categoryPlot.setBackgroundPaint(Color.gray);
??????? //还可以设置场景的横向网格
??????? categoryPlot.setDomainGridlinePaint(Color.red);
??????? //设置场景横向网格的可见性,如果不设置的话则不可见
??????? categoryPlot.setDomainGridlinesVisible(true);
??????? //设置场景的竖向网格
??????? categoryPlot.setRangeGridlinePaint(Color.green);
??????? //设置最长的说明标签的显示比例
??????? System.out.println(categoryPlot.getDomainAxis());
??????? categoryPlot.getDomainAxis().setMaximumCategoryLabelWidthRatio(0.8F);
???????
??????? //设置轴
??????? NumberAxis numberAxis=(NumberAxis)categoryPlot.getRangeAxis();
??????? //设置轴的单位
??????? numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
???????
??????? //添加额外的效果(表示器)
??????? BarRenderer barrender=(BarRenderer)categoryPlot.getRenderer();
??????? //设置是否显示外框
??????? barrender.setDrawBarOutline(true);
??????? //显示每个柱子的具体值
??????? //下面这两句缺一不可
??????? barrender.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
??????? barrender.setSeriesItemLabelsVisible(0, Boolean.TRUE);
??????? //添加渐变色
??????? GradientPaint gradientPaint=new GradientPaint(0.0f,0.0f,new Color(255,255,255),0.0f,0.0f,new Color(170,170,255));
??????? //给柱体上色
??????? barrender.setSeriesPaint(0,gradientPaint);
?????? return jfreechart;
?}
?public? ChartPanel createPanel(){
? JFreeChart jfreechart = createChart(createDataset());
??return new ChartPanel(jfreechart);
??
?}
?public static void main(String[] args){
??AliHorizonBarChartDemo demo=new AliHorizonBarChartDemo("ALI's bar chart");
??demo.pack();
??RefineryUtilities.centerFrameOnScreen(demo);
??demo.setVisible(true);
??demo.setDefaultCloseOperation(1);
??
?}
}

?

热点排行