您的位置:首页 > 编程语言 > Java开发

把图片导出到excel中

2015-07-16 10:11 525 查看
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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java