生成柱线图方法说明

最近做了柱线一体图,大概是成型了,一些细节还没调通,例如右Y轴百分比的显示。还是得多研究研究,暂且这个程度保留一下。 :) 方法中大部分参数是根据业务而定,实现思路大致如此啦! :arrow:


import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.io.File;
import java.io.IOException;

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.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;

import com.windtec.framework.util.FileHelper;
/**
* 柱线一体图
* @author zhaoqingyan
*
*/
public class DrawHistogram
{
int width;// 图象宽度

int height; // 图象高度

String chartTitle;// 图表标题

String subtitle;// 副标题

String xTitle;// X轴标题

String yTitle;// Y轴标题

String[] cutline;// 图例名称

String[] category; // 统计种类

double[][] data;// 绘图数据

String firstURL = "/admin/upload/common_downloadFile.do?localfilename=";// 映射路径
String secondURL = "&destfilename=";// 映射路径

public DrawHistogram() {
this.width = 450;
this.height = 325;
}

public DrawHistogram(int width, int height, String chartTitle,
String subtitle, String xTitle, String yTitle, String[] cutline,
String[] category, double[][] data) {
this.width = width;
this.height = height;
this.chartTitle = chartTitle;
this.subtitle = subtitle;
this.xTitle = xTitle;
this.yTitle = yTitle;
this.cutline = cutline;
this.category = category;
this.data = data;
}
/**
*
* @param lineName -- 折线名称
* @param Y1Name -- 右Y轴名称
* @param fileName -- 生成图片的名称
* @param data2 -- 附加区域数据集(这里指折线),此值可以给空,例如:在折线图的数据集是柱图中各项数据的总和时,可以解析柱图数据集算出总数。
* @param startY -- 左Y轴起点
* @param endY -- 左Y轴跨度
* @param startY1 -- 右Y轴起点
* @param endY1 -- 右Y轴跨度
* @param isY1 -- 是否显示右Y轴刻度
* @return
*/
public String draw(String lineName, String Y1Name,String fileName, double[][] data2,double startY, double endY,double startY1, double endY1,boolean isY1) {

// 创建绘图数据集
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(cutline,category,data);

// 创建图表对象
JFreeChart chart = ChartFactory.createBarChart3D(chartTitle, // 图表标题
xTitle, // X轴标题
yTitle, // Y轴标题
dataset, // 绘图数据集
PlotOrientation.VERTICAL, // 柱形图绘制方向
true, // 显示图例
true, // 采用标准生成器
false // 生成链接
);
// 添加副标题
chart.addSubtitle(new TextTitle(subtitle));
chart.setBorderVisible(true);
chart.setBorderPaint(new Color(0xFF,0x66,0x00));
chart.setBackgroundPaint(new Color(0xFF,0xF3,0xDE));
// 下面开始绘制附加轴(右侧轴)

// 首先获得绘图区对象
CategoryPlot plot = chart.getCategoryPlot();
//bar实例化
BarRenderer renderer = (BarRenderer) plot.getRenderer();
//设置条形图上bar是否显示背景阴影
//renderer.setDrawBarOutline(false);
//设置bar最大宽度百分比
//renderer.setMaximumBarWidth(0.10D);
//手动设置条形图上bar设置颜色
GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.ORANGE,
0.0f, 0.0f, new Color(0, 0, 64));
GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.BLUE,
0.0f, 0.0f, new Color(64, 0, 0));
GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.green,
0.0f, 0.0f, new Color(0, 64, 0));
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
renderer.setSeriesPaint(2, gp2);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
//设置柱子上数值的字体
renderer.setItemLabelFont(new Font("宋体",Font.PLAIN,13));
renderer.setItemLabelsVisible(true);

//设置柱子上数据的颜色
renderer.setItemLabelPaint(Color.RED);

//设置bar的最小宽度,以保证能显示数值
renderer.setMinimumBarLength(0.02);

//最大宽度
//renderer.setMaximumBarWidth(0.07);


//使设置生效
renderer.setBaseItemLabelsVisible(true);

ValueAxis valueAxis = plot.getRangeAxis();
//设置Y轴起点
valueAxis.setLowerBound(startY);
valueAxis.setUpperBound(30);
NumberAxis numAxis = (NumberAxis)plot.getRangeAxis();
numAxis.setAutoTickUnitSelection(false);//数据轴的数据标签是否自动确定
//设置Y轴跨度
numAxis.setTickUnit(new NumberTickUnit(endY));

// 创建附加轴对象,并添加到绘图区

if(isY1)
{
ValueAxis axis1 = new NumberAxis(Y1Name);
axis1.setLowerBound(startY1);
axis1.setUpperBound(2);

NumberAxis numAxis1 = (NumberAxis)axis1;
numAxis1.setAutoTickUnitSelection(false);//数据轴的数据标签是否自动确定
//设置Y轴跨度
numAxis1.setTickUnit(new NumberTickUnit(endY1));

plot.setRangeAxis(1,axis1);
}else
{
ValueAxis axis1 = new NumberAxis(Y1Name);
axis1.setLowerBound(startY);
axis1.setUpperBound(30);
NumberAxis numAxis1 = (NumberAxis)axis1;;
numAxis1.setAutoTickUnitSelection(false);//数据轴的数据标签是否自动确定
//设置Y轴跨度
numAxis1.setTickUnit(new NumberTickUnit(endY));
plot.setRangeAxis(1,axis1);
}

// 创建与附加轴对应的数据集,并添加到绘图区
DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
if(data2 == null)
{
for (int m = 0; m < category.length; m++)
{

double sum = 0.0;
for (int n = 0; n < cutline.length; n++)
{
sum += data[n][m];
}
dataset1.addValue(sum, lineName, category[m]);
}

}else
{
for (int m = 0; m < category.length; m++)
{
dataset1.addValue(data2[0][m], lineName, category[m]);
}
}
plot.setDataset(1, dataset1);

// 将绘图数据集映射到附加轴上
plot.mapDatasetToRangeAxis(1, 1);

// 定义附加轴的绘图风格,这里为折线
CategoryItemRenderer renderer1 = new LineAndShapeRenderer();
renderer1.setSeriesPaint(0, Color.RED);
plot.setRenderer(1, renderer1);
plot.setNoDataMessage("无数据可供显示!");
// 绘制附加轴结束(右侧轴)

// 生成指定格式的图片,并返回图片名称
String pieName = "";
try {
pieName = FileHelper.getUploadDir() + fileName;
ChartUtilities.saveChartAsJPEG(
new File(pieName),
chart,
width,
height);
} catch (IOException e) {
System.out.println("------ 在绘制图片时抛出异常,内容如下:");
e.printStackTrace();
}

// 组织图片浏览路径
String graphURL = firstURL + fileName + secondURL + fileName;

// 返回图片浏览路径
return graphURL;

}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值