JFreeChart 线条粗细设置

一、实现前效果

 

 
JFreeChart 线条粗细设置_第1张图片
 

 

 

二、实现后效果

 
JFreeChart 线条粗细设置_第2张图片
 

 

 

三、编程代码

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.CompositeTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.urls.CategoryURLGenerator;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

import com.mln.frame.ChartAction;
import com.mln.frame.DateUtil;
import com.mln.frame.IBaseDao;

public class SunIndexChartAction extends ChartAction{
	private static Logger logger = Logger.getLogger(SunIndexChartAction.class);
	private IBaseDao<Object[]> daoSrv;
	private CategoryDataset dataset;
	private CategoryDataset datasetTwo;
	private int width;
	private int height;



    private String kpiYear; 
	@Override
	public String execute() {
		try{
			Map<String, double[]> dataMap = getMonthEnergy();
			String title="";
			if(kpiYear!=null&&!"".equals(kpiYear)){
				title= kpiYear + "年太阳能资源指标分析";
			}else{
				title= "太阳能资源指标分析";
			}
			String[] rowKeysOne = {"水平总辐射量","固定式斜面总辐射量","极轴斜面总辐射量"};
			String[] rowKeysTwo = {"实际发电量"};
		
			
			final int length = dataMap.entrySet().size();
			String[] catekeys = new String[length];
			double[][] data = new double[3][length];
			int index = 0;
			for(Map.Entry<String, double[]> entry : dataMap.entrySet()){
				catekeys[index] = entry.getKey();
				data[0][index] = entry.getValue()[0];
				data[1][index] = entry.getValue()[1];
				data[2][index] = entry.getValue()[2];
				index++;
			}
			dataset = DatasetUtilities.createCategoryDataset(rowKeysOne,catekeys,data);        
			
			String[] catekeysTwo = new String[length];
			double[][] dataTwo = new double[1][length];
			int indexTwo = 0;
			for(Map.Entry<String, double[]> entry : dataMap.entrySet()){
				catekeysTwo[indexTwo] = entry.getKey();
				dataTwo[0][indexTwo] = entry.getValue()[3];
				indexTwo++;
			}
			datasetTwo = DatasetUtilities.createCategoryDataset(rowKeysTwo,catekeysTwo,dataTwo); 
			
			StandardChartTheme standardChartTheme=new StandardChartTheme("CN");     //设置标题字体    
			standardChartTheme.setExtraLargeFont(new Font("微软雅黑",Font.BOLD,15));     //设置图例的字体   
			standardChartTheme.setRegularFont(new Font("微软雅黑",Font.PLAIN,12));     //设置轴向的字体   
			standardChartTheme.setLargeFont(new Font("微软雅黑",Font.PLAIN,12));     //应用主题样式    
 			ChartFactory.setChartTheme(standardChartTheme);
	
			JFreeChart localJFreeChart = ChartFactory.createBarChart(title, "", "MJ/m²",dataset, PlotOrientation.VERTICAL, false, true, false);
		    CategoryPlot localCategoryPlot = (CategoryPlot)localJFreeChart.getPlot();
		    CategoryDataset localCategoryDataset = datasetTwo;
		    localCategoryPlot.setDataset(1, localCategoryDataset);
		    localCategoryPlot.mapDatasetToRangeAxis(1, 1);
		    localCategoryPlot.setBackgroundPaint(new Color(238, 244, 255));
		    NumberAxis localNumberAxis = new NumberAxis("万kWh");
		    localJFreeChart.setBackgroundPaint(new Color(238, 244, 255));
		    localCategoryPlot.setRangeAxis(1, localNumberAxis);
		    
		    
		    LineAndShapeRenderer localLineAndShapeRenderer = new LineAndShapeRenderer();
		    
		    localLineAndShapeRenderer.setBaseShapesVisible(true);
		    localLineAndShapeRenderer.setDrawOutlines(true);
		    localLineAndShapeRenderer.setUseFillPaint(true);
		    localLineAndShapeRenderer.setBaseFillPaint(Color.white);//设置圆点颜色
		    localLineAndShapeRenderer.setSeriesPaint(0,Color.yellow);//设置线条颜色
		    localLineAndShapeRenderer.setSeriesStroke(0, new BasicStroke(3F));//设置线条粗细
		    localLineAndShapeRenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));//设置圆点半径
		    localLineAndShapeRenderer.setSeriesShape(0, new Ellipse2D.Double(-5.0D, -5.0D, 10.0D, 10.0D));  //设置圆点位置 
		    localLineAndShapeRenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
		    localCategoryPlot.setRenderer(1, localLineAndShapeRenderer);
		    localCategoryPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
		    
		    LegendTitle localLegendTitle1 = new LegendTitle(localCategoryPlot.getRenderer(0));
		    localLegendTitle1.setMargin(new RectangleInsets(2.0D, 2.0D, 2.0D, 2.0D));
		    localLegendTitle1.setFrame(new BlockBorder());
		
		    LegendTitle localLegendTitle2 = new LegendTitle(localCategoryPlot.getRenderer(1));
		    localLegendTitle2.setMargin(new RectangleInsets(2.0D, 2.0D, 2.0D, 2.0D));
		    localLegendTitle2.setFrame(new BlockBorder());
		    
		    BlockContainer localBlockContainer = new BlockContainer(new BorderArrangement());
		    localBlockContainer.add(localLegendTitle1, RectangleEdge.LEFT);
		    localBlockContainer.add(localLegendTitle2, RectangleEdge.RIGHT);
		
		    CompositeTitle localCompositeTitle = new CompositeTitle(localBlockContainer);
		    localCompositeTitle.setPosition(RectangleEdge.BOTTOM);
		    localJFreeChart.addSubtitle(localCompositeTitle);
		    //ChartUtilities.applyCurrentTheme(localJFreeChart); //设置默认主题
		    
		    localJFreeChart.setBackgroundPaint(Color.white);

		    localCategoryPlot.setDomainGridlinesVisible(true);
		    localCategoryPlot.setRangeCrosshairVisible(true);
		    localCategoryPlot.setRangeCrosshairPaint(Color.blue);
		    localCategoryPlot.setDomainGridlinePaint(Color.lightGray); 
		    localCategoryPlot.setRangeGridlinePaint(Color.lightGray); 
		    BarRenderer barrenderer = (BarRenderer)localCategoryPlot.getRenderer(0);
			CategoryURLGenerator generator = new StandardCategoryURLGenerator("sunIndexStationChart.jsp", "param1", "param2");  
			barrenderer.setBaseItemURLGenerator(generator);
			
		    ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
			ChartUtilities.writeChartAsPNG(this.getResponse().getOutputStream(), localJFreeChart, width, height, info);
			String chartMap = ChartUtilities.getImageMap("SunIndexChartMap", info);

			this.setSessionAttribute("SunIndexChartMap", chartMap);
			
			return null;
		}catch(Exception e){
			e.printStackTrace();
			this.setRequestAttribute(this.ERRMSG, e.getMessage());
			logger.error(e.getLocalizedMessage());
			return this.ERROR;
		} 
	}
	
	//根据获取12个月内所有数据
	private Map<String, double[]> getMonthEnergy(){
		Map<String,double[]> cur = new TreeMap<String, double[]>();
		Date dateNow = new java.util.Date();
		int year;
		int month = 0;
		List<Integer[]> monthList = new ArrayList<Integer[]>();//初始化月份
		String fyear = "";
		String fmonth = "";
	    if(kpiYear!=null&&!"".equals(kpiYear)){
			year = Integer.parseInt(kpiYear);
			month =  DateUtil.getMonth(dateNow);
			//初始化当前年的12个月份
			int sMonth = 12;
			for(int i =0;i < 12;i++){
				Integer[] newObj = new Integer[2];
				newObj[0] = year;  //年
				newObj[1] = sMonth;  //月
				monthList.add(newObj);
				sMonth--;
			}
		}else{
			year =  DateUtil.getYear(dateNow);
			month =  DateUtil.getMonth(dateNow);
			//初始化最新的12个月份
			int sYear = year;
			int sMonth = month;
			for(int i =0;i < 12;i++){
				Integer[] newObj = new Integer[2];
				if(sMonth == 0){
					sYear = sYear -1;
					sMonth = 12;
				}
				newObj[0] = sYear;  //年
				newObj[1] = sMonth;  //月
				fyear=String.valueOf(sYear);
				if(sMonth<10){
					fmonth="0"+sMonth;
				}else{
					fmonth=String.valueOf(sMonth);
				}
				monthList.add(newObj);
				sMonth--;
			}
		}
		//初始化数据水平总辐射量  固定式斜面总辐射量  极轴斜面总辐射量     实际发电量
		String sql = "";
		
		if(kpiYear!=null&&!"".equals(kpiYear)){
		    sql = "SELECT substr(t.PROMONTH,0,4) pyear,substr(t.PROMONTH,5) pm,sum(t.LevTotRadia),sum(t.IncTotRadia),sum(t.PAIncTotRadia),sum(t.GenerCapac) FROM MNT_TOECINDICA_MONTH t WHERE substr(t.PROMONTH,0,4)='"+year+"' GROUP BY substr(t.PROMONTH,0,4),substr(t.PROMONTH,5) ORDER BY substr(t.PROMONTH,5) DESC";
		}else{
		    sql = "SELECT substr(t.PROMONTH,0,4) pyear,substr(t.PROMONTH,5) pm,sum(t.LevTotRadia),sum(t.IncTotRadia),sum(t.PAIncTotRadia),sum(t.GenerCapac) FROM MNT_TOECINDICA_MONTH t WHERE ((substr(t.PROMONTH,0,4)='"+fyear+"'  AND substr(t.PROMONTH,5) >='"+fmonth+"') OR (substr(t.PROMONTH,0,4) ='"+year+"' AND substr(t.PROMONTH,5) <='"+month+"')) GROUP BY substr(t.PROMONTH,0,4),substr(t.PROMONTH,5) " ;
	    }
		List<Object[]> rowList = daoSrv.findBySql(sql);
		for(Integer[] mlist: monthList){
			int y = mlist[0];
			int m = mlist[1];
			String key = y + "-" + (m < 10 ? "0" + m : "" + m);
			double[] energy= new double[4];
			for(Object[] row: rowList){
				int staYear = Integer.valueOf(row[0].toString());
				int StatMonth = Integer.valueOf(row[1].toString());
				if(m == StatMonth && y == staYear){
					energy[0] = Double.valueOf(row[2].toString());//水平总辐射量
					energy[1] = Double.valueOf(row[3].toString());//固定式斜面总辐射量
					energy[2] = Double.valueOf(row[4].toString());//极轴斜面总辐射量
					energy[3] = Double.valueOf(row[5].toString());//实际发电量
				}
				//等于当前月 
				if(m == month && y == year){
					String mo = m+"";
					String ye = y+"";
					if(Integer.valueOf(m)<10){
						mo="0"+mo;
					}
					String sql2 = "select sum(t.LevTotRadia),sum(t.IncTotRadia),sum(t.PAIncTotRadia),sum(t.GenerCapac)  from mnt_toecindica_day t where substr(t.prodate,0,6)='"+ye+mo+"'";
			        List<Object[]> genlist = daoSrv.findBySql(sql2);
			        if(genlist != null && genlist.size()>0){
			        	energy[0] =  genlist.get(0)[0] == null ? 0.0 : Double.valueOf(genlist.get(0)[0]+"");   
			            energy[1] =  genlist.get(0)[1] == null ? 0.0 : Double.valueOf(genlist.get(0)[1]+"");
			            energy[2] =  genlist.get(0)[2] == null ? 0.0 : Double.valueOf(genlist.get(0)[2]+"");
			            energy[3] =  genlist.get(0)[3] == null ? 0.0 : Double.valueOf(genlist.get(0)[3]+"");
			        }
				}
			}
			cur.put(key, energy);
		} 
		return cur;
	} 
		
	

	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public CategoryDataset getDataset() {
		return dataset;
	}
	public void setDataset(CategoryDataset dataset) {
		this.dataset = dataset;
	}
	public IBaseDao<Object[]> getDaoSrv() {
		return daoSrv;
	}
	public void setDaoSrv(IBaseDao<Object[]> daoSrv) {
		this.daoSrv = daoSrv;
	}
	public String getKpiYear() {
		return kpiYear;
	}

	public void setKpiYear(String kpiYear) {
		this.kpiYear = kpiYear;
	}

	public CategoryDataset getDatasetTwo() {
		return datasetTwo;
	}

	public void setDatasetTwo(CategoryDataset datasetTwo) {
		this.datasetTwo = datasetTwo;
	}
	
}

 

你可能感兴趣的:(jfreechart)