struts2零配置下的JFreeChart

jsp页面:

<img  src="chart!createpie.action">

action:

@Results( { @Result(name = "MyChart", type="chart") })  
public class ChartAction extends ActionSupport{
	/**
	 * 
	 */
	JFreeChart chart;

	public JFreeChart getChart() {
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}

	public String createpie()  
	{  
	//生成JFreeChart对象  
		chart = new MyJFreeChart().generateLineChart();
	return "MyChart";  
	}
	
}

MyJFreeChart:(两个纵轴)

import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class MyJFreeChart {
	// 筛孔尺寸
	public static final int COUNT = 15;
	static double[] pass = { 4.0, 7.9, 10.0, 12.0, 15.0, 18.8, 20.6, 25.0,
			30.3, 35.0, 48.7, 50.5, 59.3, 74.2, 88.0 };
	static double[] pass2 = { 88.0, 74.2, 59.3, 50.5, 48.7, 35.0, 25.0, 20.6,
			30.3, 18.8, 15.0, 12.0, 10.0, 7.9, 4.0 };
	static double[] size = { 0.01, 0.15, 0.6, 2.36, 9.5, 16, 26.5, 37.5 };
	// double[] size = {0,0.075, 0.15, 0.3, 0.6, 1.18, 2.36, 4.75, 9.5, 13.2, 16, 19, 26.5, 31.5, 37.5};

	private static XYSeriesCollection getDataSet1() {
		//XYSeries series1 = new XYSeries("one");
		XYSeries series1 = new XYSeries("I类");
		XYSeriesCollection dataset1 = new XYSeriesCollection();

		for (int i = 0; i < 8; i++) { // 15组试验取奇数位描点
			series1.add(size[i], pass[i * 2]);
		}
		dataset1.addSeries(series1);
		return dataset1;
	}

	private static XYSeriesCollection getDataSet2() {
		XYSeries series2 = new XYSeries("two");
		XYSeriesCollection dataset2 = new XYSeriesCollection();
		for (int i = 0; i < 8; i++) {
			series2.add(size[i], pass2[i * 2]); // 通过率 + 筛余 = 100
		}
		dataset2.addSeries(series2);
		return dataset2;
	}

	public JFreeChart generateLineChart() {
		String filename = null;
		XYSeriesCollection dataset = getDataSet1();
		XYSeriesCollection dataset2 = getDataSet2();
		JFreeChart chart = ChartFactory.createXYLineChart("粗集料筛分试验", // 图表标题
				"粒径(mm)", // 目录轴的显示标签
				"通过百分率(%)", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向:水平、垂直
				false, // 是否显示图例(对于简单的柱状图必须是false)
				true, // 是否生成工具
				false // 是否生成URL链接
				);

		/*----------设置消除字体的锯齿渲染(解决中文问题)--------------*/
		chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

		/*------------配置图表属性--------------*/
		// 1,设置整个图表背景颜色
		chart.setBackgroundPaint(Color.pink);

		/*------------设定Plot参数-------------*/
		XYPlot plot = chart.getXYPlot();
		// 2,设置详细图表的显示细节部分的背景颜色
		plot.setBackgroundPaint(Color.LIGHT_GRAY);

		/*----- 配置字体(解决中文乱码的通用方法)-----*/
		Font xfont = new Font("宋体", Font.PLAIN, 12); // X轴
		Font cfont = new Font("宋体", Font.PLAIN, 12);
		Font yfont = new Font("宋体", Font.PLAIN, 13); // Y轴
		Font kfont = new Font("宋体", Font.PLAIN, 12); // 底部
		Font titleFont = new Font("宋体", Font.BOLD, 16); // 图片标题
		plot.getDomainAxis().setLabelFont(xfont);
		plot.getDomainAxis().setTickLabelFont(cfont);
		plot.getDomainAxis().setLowerMargin(0);
		plot.getRangeAxis().setLabelFont(yfont);

		//chart.getLegend().setItemFont(kfont);   // 没有显示图列
		chart.getTitle().setFont(titleFont);

		// 设置左边纵坐标
		ValueAxis left = plot.getRangeAxis();
		left.setAutoRange(false);
		left.setRange(0, 100);
		NumberAxis vValueAxis = (NumberAxis) plot.getRangeAxis();
		NumberTickUnit nt = new NumberTickUnit(20d); // )数据轴的数据标签,定义刻度
		vValueAxis.setTickUnit(nt);
		/*-----第二个纵轴的设置-----*/
  		NumberAxis axis2 = new NumberAxis("累计筛余(%)");
		axis2.setAxisLinePaint(Color.BLUE);
		axis2.setLabelPaint(Color.BLUE);
		axis2.setTickLabelPaint(Color.BLUE);
		axis2.setAutoTickUnitSelection(false);
		axis2.setTickUnit(nt);
		axis2.setAutoRange(false);// 自动 调整范围
		axis2.setRange(0, 100);
		axis2.setInverted(true);// 降序排序
		plot.setRangeAxis(1, axis2);
		//plot.setDataset(1, dataset2);//右边纵轴的数据设置为null,即不显示数据折线 : 通过率 + 累计筛分 = 100
		plot.setDataset(1, null);
		plot.mapDatasetToRangeAxis(1, 1);

		// -- 修改第2条曲线显示效果
		XYLineAndShapeRenderer render2 = new XYLineAndShapeRenderer();
		//render2.setSeriesPaint(0, Color.LIGHT_GRAY);
		render2.setSeriesPaint(0, Color.gray);
		plot.setRenderer(1, render2);

		// 3,设置垂直网格线颜色
		plot.setDomainGridlinePaint(Color.black);
		// 4,设置是否显示垂直网格线
		plot.setDomainGridlinesVisible(true);
		// 5,设置水平网格线颜色
		plot.setRangeGridlinePaint(Color.blue);
		// 6,设置是否显示水平网格线
		plot.setRangeGridlinesVisible(true);

		return chart;
	}
}
struts.xml

<struts>  
    
    <constant name="struts.devMode" value="true" />  
    <!--  -->
    <constant name="struts.convention.result.path" value="/WEB-INF/page" /> 
    <constant name="struts.convention.package.locators" value="action" />
    <constant name="struts.convention.package.locators.basePackage" value="com.pengtu" />
    <constant name="struts.convention.action.name.separator" value="-" /> 
      <constant name="struts.enable.SlashesInActionNames" value="true"/> 	
    <constant name="struts.convention.action.disableScanning" value="false"/> 
	<constant name="struts.convention.default.parent.package"  value="convention-jfreechart" />  
	
	 <package name="convention-jfreechart" extends="convention-default">  
        <result-types>  
            <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult">  
                <param name="height">300</param>  
                <param name="width">400</param>  
            </result-type>  
        </result-types>  
</package> 
</struts>  


启发来自:http://javeye.iteye.com/blog/557658

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值