jfreechart集成

1:首先引入相关类库:
struts2-jfreechart-plugin-2.2.3.jar,
jfreechart-1.0.13-swt.jar,
jfreechart-1.0.8a.jar;
jfreechart-1.0.13-experimental.jar;
jcommon-1.0.12.jar
2:创建action
package com.jpower.struts.action;

import java.awt.Color;

import org.apache.commons.lang.math.RandomUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import com.opensymphony.xwork2.ActionSupport;

public class ChartAction extends ActionSupport{
private JFreeChart chart;
    public String execute() throws Exception {
            XYSeries dataSeries = new XYSeries(new Integer(1)); // pass a key for this serie
            for (int i = 0; i <= 100; i++) {
                    dataSeries.add(i, RandomUtils.nextInt());
            }
            XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);

            ValueAxis xAxis = new NumberAxis("Raw Marks");
            ValueAxis yAxis = new NumberAxis("Moderated Marks");

            // set my chart variable
            chart =
            new JFreeChart( "Moderation Function", JFreeChart.DEFAULT_TITLE_FONT,
            new XYPlot( xyDataset, xAxis, yAxis, new StandardXYItemRenderer(StandardXYItemRenderer.LINES)),

                        false);
            chart.setBackgroundPaint(java.awt.Color.white);

            return SUCCESS;
    }

    public JFreeChart getChart() {
            return chart;
    }
  
}
3:配置struts.xml:
<package name="chart" namespace="/chart" extends=" jfreechart-default">
<action name="chart" class="com.jpower.struts.action.ChartAction">
<result name="success" type="chart">
  <param name="value">chart</param>
  <param name="type">png</param>
   <param name="width">640</param>
   <param name="height">480</param>
</result>
</action>

</package>
4:直接访问:http://localhost:8080/chart/chart即可看到曲线图


注意内容:
如何让曲线图在某个位置显示:
可以在某个页面定义一个<img href="chart/chart"></img>
只需在在地址栏输入该页面地址即可

你可能感兴趣的:(jfreechart)