private JFreeChart chart;
public JFreeChart getChart() {
return chart;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}
public String show() {
this.chart =createchart();
return "success";
}
//jfreechart创建方法
public JFreeChart createchart(){
try {
chart = ChartFactory.createBarChart("Bar Chart Demo 1", "Category", "Value", createDataset(), PlotOrientation.VERTICAL, true, true, false);
CategoryPlot localCategoryPlot = (CategoryPlot)chart.getPlot();
localCategoryPlot.setDomainGridlinesVisible(true);
localCategoryPlot.setRangeCrosshairVisible(true);
localCategoryPlot.setRangeCrosshairPaint(Color.blue);
NumberAxis localNumberAxis = (NumberAxis)localCategoryPlot.getRangeAxis();
localNumberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer localBarRenderer = (BarRenderer)localCategoryPlot.getRenderer();
localBarRenderer.setDrawBarOutline(false);
GradientPaint localGradientPaint1 = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, new Color(0, 0, 64));
GradientPaint localGradientPaint2 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
GradientPaint localGradientPaint3 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(64, 0, 0));
localBarRenderer.setSeriesPaint(0, localGradientPaint1);
localBarRenderer.setSeriesPaint(1, localGradientPaint2);
localBarRenderer.setSeriesPaint(2, localGradientPaint3);
localBarRenderer.setLegendItemToolTipGenerator(new StandardCategorySeriesLabelGenerator("Tooltip: {0}"));
CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis();
localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.5235987755982988D));
} catch (Exception e) {
e.printStackTrace();
}
return chart;
}
//创建实验数据源
public CategoryDataset createDataset()
{
String str1 = "First";
String str2 = "Second";
String str3 = "Third";
String str4 = "Category 1";
String str5 = "Category 2";
String str6 = "Category 3";
String str7 = "Category 4";
String str8 = "Category 5";
DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
localDefaultCategoryDataset.addValue(1.0D, str1, str4);
localDefaultCategoryDataset.addValue(4.0D, str1, str5);
localDefaultCategoryDataset.addValue(3.0D, str1, str6);
localDefaultCategoryDataset.addValue(5.0D, str1, str7);
localDefaultCategoryDataset.addValue(5.0D, str1, str8);
localDefaultCategoryDataset.addValue(5.0D, str2, str4);
localDefaultCategoryDataset.addValue(7.0D, str2, str5);
localDefaultCategoryDataset.addValue(6.0D, str2, str6);
localDefaultCategoryDataset.addValue(8.0D, str2, str7);
localDefaultCategoryDataset.addValue(4.0D, str2, str8);
localDefaultCategoryDataset.addValue(4.0D, str3, str4);
localDefaultCategoryDataset.addValue(3.0D, str3, str5);
localDefaultCategoryDataset.addValue(2.0D, str3, str6);
localDefaultCategoryDataset.addValue(3.0D, str3, str7);
localDefaultCategoryDataset.addValue(6.0D, str3, str8);
return localDefaultCategoryDataset;
}
导入包之类的我就不说了 主要的是 struts配置里要配置下
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="eventstatistics" extends="struts-default,jfreechart-default"
namespace="/eventstatistics">
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
</result-types>
<action name="eventstatistics" class="com.brilliance.xhsbd.view.EventStatisticsAction">
<result name="success" type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
</package>
</struts>
单独写了个配置文件 <include file="struts/struts-jfreechart.xml"/> 在struts.xml中引入。访问路径........./eventstatistics/eventstatistics!show.action