导入所需的包:
java代码:
public class Histogram extends ActionSupport
{
private JFreeChart chart;
public JFreeChart getChart(){
//图标题,横轴,纵轴,数据集,图标方向,是否显示图例,是否生成工具,是否生成ur链接
chart = ChartFactory.createBarChart3D("水果销量统计图","水果种类","销量", getfruitDataSet(),
PlotOrientation.VERTICAL , // 垂直
false,
false,
false
);
//重新设置图标标题,改变字体
chart.setTitle(new TextTitle("水果销量统计图", new Font("黑体", Font.ITALIC , 26)));
CategoryPlot plot = (CategoryPlot)chart.getPlot();
//取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
//设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
//分类标签以45度角倾斜
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 12));
//取得纵轴
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
//设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
return chart;
}
//返回一个CategoryDataset实例
private CategoryDataset getfruitDataSet()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(40000 , "" , "苹果");
dataset.addValue(30000 , "" , "橘子");
dataset.addValue(30000 , "" , "葡萄");
dataset.addValue(20000 , "" , "香蕉");
dataset.addValue(22000 , "" , "柚子");
return dataset;
}
}
strus.xml:
<action name="fruithistogram" class="jfreechartfruit.Histogram">
<result type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action>
jsp:
<img src="fruithistogram.action"/>
效果显示如下: