把圖片導出到excel中

package picture;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import ChartDirector.BarLayer;
import ChartDirector.Chart;
import ChartDirector.LegendBox;
import ChartDirector.LineLayer;
import ChartDirector.PieChart;
import ChartDirector.TextBox;
import ChartDirector.XYChart;

/**
 *==========================================================
 * Version              Author                 Date                 Description
 * 1.0                 wumengna               May 24, 2013                 創建
 * ==========================================================
 */

/** 
 * <p>Title: ChartUtils.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2013</p>
 * <p>Company: Beijing Orient View Technology Co.,Ltd.</p>
 * @author wumengna
 * 
 */
public class ChartUtils {


	/**
	 * 設置數據,獲取封裝構造出來PieChart對象 - 方便用於生成本地圖片或者html頁面圖片
	 * 
	 * @param data -
	 *            餅圖數據
	 * @param labels -
	 *            餅圖各扇形的標示標籤
	 * @param chartTitle -
	 *            餅圖標題
	 * @return
	 */
	public static PieChart getPieChart(double[] data, String[] labels,
			String chartTitle) {//背景不是很好,以後可以慢慢調整
        
		PieChart c2 = new PieChart(950, 450, Chart.goldColor(), -1, 1);
		c2.setDefaultFonts("宋體");

		// Add a title box using 15 pts Times Bold Italic font and metallic pink
		// background
		// color
		c2.addTitle(chartTitle, "微軟黑雅", 15).setBackground(0xffffff);
		// Set the center of the pie at (280, 135) and the radius to 110 pixels
		c2.setPieSize(450, 190, 110);

		// Draw the pie in 3D with 20 pixels 3D depth
		c2.set3D(20);

		// Use the side label layout method
		c2.setLabelLayout(Chart.SideLayout);

		// Set the label box background color the same as the sector color, with
		// glass
		// effect, and with 5 pixels rounded corners
		TextBox tt = c2.setLabelStyle();
		tt.setBackground(Chart.SameAsMainColor, Chart.Transparent, Chart
				.glassEffect());
		tt.setRoundedCorners(0);

		// Set the border color of the sector the same color as the fill color.
		// Set the line
		// color of the join line to black (0x0)
		c2.setLineColor(Chart.SameAsMainColor, 0x000000);

		// Set the start angle to 135 degrees may improve layout when there are
		// many small
		// sectors at the end of the data array (that is, data sorted in
		// descending order).
		// It is because this makes the small sectors position near the
		// horizontal axis,
		// where the text label has the least tendency to overlap. For data
		// sorted in
		// ascending order, a start angle of 45 degrees can be used instead.
		c2.setStartAngle(135);

		// Set the pie data and the pie labels
		c2.setData(data, labels);

		return c2;
	}
	
	
	/**
	 * 設置數據,獲取封裝構造出來XYChart對象 - 方便用於生成本地圖片或者html頁面圖片
	 * 
	 * @param data -
	 *            柱狀圖數據
	 * @param labels -
	 *            柱狀圖下標的標示標籤
	 * @param chartTitle -
	 *            柱狀圖標題
	 * @return
	 */
	public static XYChart getXYChart(double[] data, String[] labels,
			String chartTitle) {//背景不是很好,以後可以慢慢調整
        
		 // 圖表數據    
		 // Create a XYChart object of size 600 x 360 pixels
		 XYChart c = new XYChart(950, 560);

		 // Add a title to the chart using 18pts Times Bold Italic font
		  c.addTitle(chartTitle+" 柱狀圖","微軟黑雅", 15).setBackground(0xffffff);
		 // Set the plotarea at (60, 40) and of size 480 x 280 pixels. Use a vertical gradient
		 // color from light green (eeffee) to dark green (008800) as background. Set border
		 // and grid lines to white (ffffff).
		  c.setPlotArea(90, 50, 750, 330, c.linearGradientColor(60, 40, 60, 280, 0xeeffee,
				     0x008800), -1, 0xffffff, 0xffffff);

		 // Add a multi-color bar chart layer using the revenue data.
		 BarLayer layer = c.addBarLayer3(data);

		 // Set cylinder bar shape
		 layer.setBarShape(Chart.CircleShape);
		 
		 // Set the labels on the x axis.
		 c.xAxis().setLabels(labels);
		 c.xAxis().setLabelStep(1);//下標步長
		 

		 // In this example, we show the same scale using both axes
		 c.syncYAxis();

		 // Set the axis line to transparent
		 c.xAxis().setColors(Chart.Transparent);
		 c.yAxis().setColors(Chart.Transparent);
		 c.yAxis2().setColors(Chart.Transparent);

		 // Set the axis label to using 8pt Arial Bold as font
		 c.yAxis().setLabelStyle("宋體", 8);
		 c.yAxis2().setLabelStyle("宋體", 8);
		 c.xAxis().setLabelStyle("宋體", 9, Chart.TextColor, 270);

		 // Add title to the y axes
		 c.yAxis().setTitle("點擊次數(次)", "宋體", 10);
		 c.yAxis2().setTitle("點擊次數(次)", "宋體", 10);

		return c;
	}
	
	public static XYChart getXYzheXianChart (double[] data, String[] labels, String titleChart, String xtitle, String ytitle) {
		XYChart c = new XYChart(950, 400);
		c.addTitle(titleChart, "微軟黑雅", 15).setBackground(0xffffff);
		c.setDefaultFonts("宋體");
		c.setPlotArea(90, 40, 800, 250);
		
		LineLayer lineLayer = c.addLineLayer();
		lineLayer.addDataSet(data, 0xcf4040, "Peak").setDataSymbol(Chart.SquareSymbol, 1);
//		lineLayer.addCustomAggregateLabel(data, 0xcf4040, "Peak");
		c.xAxis().setLabels(labels);
//		c.xAxis().setLabelStep(2);
		c.xAxis().setLabelStyle("宋體", 9, Chart.TextColor, 270);
		
		//設置x軸顯示的標題
		c.xAxis().setTitle(xtitle, "宋體");
		c.yAxis().setTitle(ytitle, "宋體");
		
		
		return c;
	}
	
	public static XYChart getXYzheXianChart2 (ChartEntity entity) {
		XYChart c = new XYChart(950, 400, Chart.BackgroundColor, Chart.Bottom, 1);
		c.setRoundedFrame();
		c.setDefaultFonts("宋體");
		
		TextBox title = c.addTitle(entity.getStyleTitle(), "隸書", 10);
		title.setMargin2(0, 0, 6, 6);
		c.addLine(10, title.getHeight(), c.getWidth() -11, title.getHeight(), Chart.LineColor);
		
		LegendBox legendBox = c.addLegend(c.getWidth() /2, title.getHeight(), false, "宋體", 10);
		legendBox.setAlignment(Chart.TopCenter);
		legendBox.setBackground(Chart.Transparent);
		c.setPlotArea(70, 75, 460, 240, -1, -1, Chart.Transparent,0x000000, -1);
		c.xAxis().setLabels(entity.getLabels());
		c.xAxis().setTitle(entity.getXtitle(),"隸書", 8);
		c.syncYAxis();
		
		c.yAxis().setTickDensity(30);
		
		c.xAxis().setColors(Chart.Transparent);
		c.yAxis().setColors(Chart.Transparent);
		
		c.xAxis().setLabelStyle("隸書", 8);
		c.yAxis().setLabelStyle("隸書", 8);
		
		c.yAxis().setTitle(entity.getYtitle(), "隸書", 8);
		
		if(entity.getData1().size() > 0){
			for(int i=0;i<entity.getData1().size();i++) {
				LineLayer layer = c.addLineLayer2();
				layer.addDataSet(entity.getData1().get(i), getRandColorCode(),entity.getName()[i]).setDataSymbol(Chart.SquareSymbol, 8);
				layer.setDataLabelFormat(String.valueOf(entity.getData1().get(i)[i]));
				layer.setLineWidth(2);
				layer.setGapColor(c.dashLineColor(getRandColorCode()));
			}
		}
		c.layoutLegend();
		c.packPlotArea(15, legendBox.getTopY()+legendBox.getHeight(), c.getWidth()-16, c.getHeight()-25);
		return c;
	}
	
	private static int getRandColorCode() {
		String r, g, b;
		Random random = new Random();
		r = Integer.toHexString(random.nextInt(256)).toUpperCase();
		g = Integer.toHexString(random.nextInt(256)).toUpperCase();
		b = Integer.toHexString(random.nextInt(256)).toUpperCase();

		r = r.length() == 1 ? "0" + r : r;
		g = g.length() == 1 ? "0" + g : g;
		b = b.length() == 1 ? "0" + b : b;
		return Integer.valueOf(r + g + b, 16);
	}
	
	//=====================以下方法用於測試=========================
	private static void testPieChart(){
		double[] data = new double[] { 0.1, 0.3, 0.6 };
		String[] labels = new String[] { "111", "222", "333" };
		String chartTitle = "餅圖測試";

		PieChart chart = getPieChart(data, labels, chartTitle);
		chart.makeChart("e:/jxl/test_aaa.png");
	}
	
	private static void testBarChart(){
		double[] data = new double[] { 0.1, 0.3, 0.6 ,0.8};
		String[] labels = new String[] { "111", "222", "333" ,"444"};
		String chartTitle = "餅圖測試";

		XYChart chart = getXYChart(data, labels, chartTitle);
		chart.makeChart("e:/jxl/test_bbb.png");
	}
	
	private static void testZheXianChart(){
		double[] data = new double[] { 0.1, 0.3, 0.0 ,0.4,0.7,1};
		String[] labels = new String[] { "蘋果", "香蕉", "西瓜" ,"榴蓮","蜜桃","火龍果"};
		String chartTitle = "折線圖測試";
		
		XYChart chart = getXYzheXianChart(data, labels, chartTitle, "水果名稱", "數量");
		chart.makeChart("e:/jxl/test_ccc.png");
	}
	
	private static void testdoublezhexiantu(){
		double[] data1 ={1,4,6,8,10,3};
		double[] data2 ={5,2,7,8,11,4};
		List<double[]> list = new ArrayList<double[]>();
		list.add(data1);
		list.add(data2);
		
		String[] label = {"蘋果", "香蕉", "西瓜" ,"榴蓮","蜜桃","火龍果"};
		String[] name = {"北京", "上海"};
		
		ChartEntity entity = new ChartEntity();
		entity.setStyleTitle("雙摺線圖測試");
		entity.setYtitle("數量");
		entity.setXtitle("水果種類");
		entity.setData1(list);
		entity.setLabels(label);
		entity.setName(name);
		
		XYChart chart = getXYzheXianChart2(entity);
		chart.makeChart("e:/jxl/test_ddd.png");
	}
	
	public static void main(String[] args) {
		//餅圖測試
//		testPieChart();
		
		//柱狀圖測試
//		testBarChart();
		
		testZheXianChart();
		
//		testdoublezhexiantu();
	}

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章