Bar3D.java

package com.citi.dashboard.excel.chart;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

import com.citi.dashboard.excel.xml.ParseXML;


public class Bar3D extends ApplicationFrame implements Chart {
	private String title; 
	private String categoryAxisLabel;
	private String valueAxisLabel; 
	private List<Bar3DDataset> datasets; 
	private boolean legend; 
	private boolean tooltips; 
	private boolean urls;
	private String remark;
	private int width;
	private int height;
	
	public Bar3D() {
		super("");
	}

	@Override
	public String createGraph(Chart graphData) throws Exception {
        // create the dataset...
        final DefaultCategoryDataset dcDataset = new DefaultCategoryDataset();

        if(datasets != null && datasets.size() > 0) {
        	for(Bar3DDataset dataset : datasets){
        		dcDataset.addValue(dataset.getValue(), dataset.getRowKey(), dataset.getColumnKey());
        	}
        }

        final JFreeChart chart = ChartFactory.createBarChart3D(this.title, // chart title
            this.categoryAxisLabel, // domain axis label
            this.valueAxisLabel, // range axis label
            dcDataset, // data
            PlotOrientation.VERTICAL, // orientation
            this.legend, // include legend
            this.tooltips, // tooltips?
            this.urls // URLs?
            );

        CategoryPlot categoryplot = chart.getCategoryPlot();
        BarRenderer3D custombarrenderer3d = new BarRenderer3D();
		DecimalFormat df = new DecimalFormat("0%");
		custombarrenderer3d.setBaseItemLabelGenerator(
				new StandardCategoryItemLabelGenerator("{2}", df));
		custombarrenderer3d.setItemLabelAnchorOffset(10D);
		custombarrenderer3d.setBasePositiveItemLabelPosition(new ItemLabelPosition(
						ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
		categoryplot.setRenderer(custombarrenderer3d);

		custombarrenderer3d.setBaseItemLabelsVisible(true);
		custombarrenderer3d.setMaximumBarWidth(0.1D);

//		final CategoryAxis domainAxis = categoryplot.getDomainAxis();
//      domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
        
		NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
        //设置最高的一个 Item 与图片顶端的距离
		numberaxis.setUpperMargin(0.15D);
        //设置最低的一个 Item 与图片底端的距离
		numberaxis.setLowerMargin(0.15D);
		numberaxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
		categoryplot.setRangeAxis(numberaxis);
		
		// add the chart to a panel...
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 300));
        setContentPane(chartPanel);
		
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        if(this.width > 0 && this.height > 0)
        	ChartUtilities.writeChartAsJPEG(byteArrayOutputStream, chart, this.width, this.height);
        else
        	ChartUtilities.writeChartAsJPEG(byteArrayOutputStream, chart, 500, 300);

        String userDir = System.getProperty("user.dir");
        StringBuilder sb = new StringBuilder(userDir);
        StringBuilder imageName = new StringBuilder();
        imageName.append("excel/image/").append(this.title).append(System.currentTimeMillis()).append(".jpg");
        sb.append("/html/").append(imageName);
        String imagePath = sb.toString();
        
        FileOutputStream file = new FileOutputStream(imagePath);
        file.write(byteArrayOutputStream.toByteArray());
        file.close();
		return imageName.toString();
	}
	
	public static void main(String[] args) throws Exception{
		String xmlPath = "html/excel/xml/manual.xml";
		List<Chart> list = ParseXML.getGraphObjectFromSingleXml(xmlPath, "bar3d");
		for(Chart chart: list) {
			if(chart instanceof Bar3D) {
				chart.createGraph(null);
				Bar3D bar3d = (Bar3D)chart;
				bar3d.pack();
				RefineryUtilities.centerFrameOnScreen(bar3d);
				bar3d.setVisible(true);
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值