📄 chartmanagement.java
字号:
}
}
else {
throw new Exception("数据集DBResult是空的");
}
return dataset;
}
/**
* 根据数据源生成图形
* @param ds Dataset 数据源
* @param flag 生成类型
* @return BufferedImage
* @throws Exception
*/
private BufferedImage outputChart(Dataset ds, int flag) throws Exception {
OutputStream out = null;
if (flag == ChartManagement.RESPONSE) {
//设置头信息
( (HttpServletResponse) response).setContentType(CONTENT_TYPE);
( (HttpServletResponse) response).setHeader("Content-Disposition",
" filename=image.png");
out = ( (HttpServletResponse) response).getOutputStream();
}
else if (flag == ChartManagement.FILE) {
out = new BufferedOutputStream(new
FileOutputStream( (String)this.descFile));
}
try {
JFreeChart chart = null;
//生成Pie
if (chart_type.equalsIgnoreCase(ChartManagement.PIE)) {
//如果是3DPie
if (is3d) {
//如果有aplha效果
if (isalpha) {
PiePlot3D plot = new PiePlot3D( (PieDataset)
ds);
plot.setForegroundAlpha(0.5f);
//如果有背景图片
if (this.backGroundColor != null)
plot.setBackgroundPaint(this.backGroundColor);
chart = new JFreeChart(title, plot);
}
//如果没有alpha效果
else {
chart = ChartFactory.createPieChart3D(title,
(PieDataset) ds, true, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
}
//如果是2DPie
else {
if (isalpha) {
PiePlot plot = new PiePlot( (PieDataset)
ds);
plot.setForegroundAlpha(0.5f);
//如果有背景图片
if (this.backGroundColor != null)
plot.setBackgroundPaint(this.backGroundColor);
chart = new JFreeChart(title, plot);
}
else {
chart = ChartFactory.createPieChart(title, (PieDataset) ds, true, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
}
}
//Ring 2D
else if (chart_type.equalsIgnoreCase(ChartManagement.RING)) {
if (isalpha) {
RingPlot plot = new RingPlot( (PieDataset)
ds);
plot.setForegroundAlpha(0.5f);
//如果有背景图片
if (this.backGroundColor != null)
plot.setBackgroundPaint(this.backGroundColor);
chart = new JFreeChart(title, plot);
}
else {
chart = ChartFactory.createRingChart(title, (PieDataset) ds, true, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
}
//生成Bar
else if (chart_type.equalsIgnoreCase(ChartManagement.BAR)) {
//如果是3D bar
if (is3d) {
chart = ChartFactory.createBarChart3D(title,
categoryTag, valueTag,
(CategoryDataset) ds,
PlotOrientation.VERTICAL,
this.result.ColumnCount > 2 ? true : false, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
else {
chart = ChartFactory.createBarChart(title,
categoryTag, valueTag,
(CategoryDataset) ds,
PlotOrientation.VERTICAL,
this.result.ColumnCount > 2 ? true : false, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
}
//生成Line
else if (chart_type.equalsIgnoreCase(ChartManagement.LINE)) {
chart = ChartFactory.createLineChart(title,
categoryTag, valueTag,
(CategoryDataset) ds,
PlotOrientation.VERTICAL,
this.result.ColumnCount > 2 ? true : false, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
//生成XYLine
else if (chart_type.equalsIgnoreCase(ChartManagement.XYLINE)) {
chart = ChartFactory.createXYLineChart(title,
categoryTag, valueTag,
(XYDataset) ds,
PlotOrientation.VERTICAL,
true, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
//XYAREA
else if (chart_type.equalsIgnoreCase(ChartManagement.XYAREA)) {
chart = ChartFactory.createXYAreaChart(title,
categoryTag, valueTag,
(XYDataset) ds,
PlotOrientation.VERTICAL,
true, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
//生成StackBar
else if (chart_type.equalsIgnoreCase(ChartManagement.STACKBAR)) {
if (is3d) {
chart = ChartFactory.createStackedBarChart(title,
categoryTag, valueTag,
(CategoryDataset) ds,
PlotOrientation.VERTICAL, this.result.ColumnCount > 2 ? true : false, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
else {
chart = ChartFactory.createStackedBarChart3D(title,
categoryTag, valueTag,
(CategoryDataset) ds,
PlotOrientation.VERTICAL, this.result.ColumnCount > 2 ? true : false, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
}
//生成Area
else if (chart_type.equalsIgnoreCase(ChartManagement.AREA)) {
chart = ChartFactory.createAreaChart(title,
categoryTag, valueTag,
(CategoryDataset) ds,
PlotOrientation.VERTICAL,
this.result.ColumnCount > 2 ? true : false, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
//生成StackArea
else if (chart_type.equalsIgnoreCase(ChartManagement.STACKAREA)) {
chart = ChartFactory.createStackedAreaChart(title,
categoryTag, valueTag,
(CategoryDataset) ds,
PlotOrientation.VERTICAL, this.result.ColumnCount > 2 ? true : false, false, false);
if (this.backGroundColor != null)
chart.setBackgroundPaint(this.backGroundColor);
}
//设置副标题
if (this.subTitle != null && this.subTitle.length() > 0 && chart != null) {
chart.addSubtitle(new TextTitle(this.subTitle));
}
SetChartExtProperty(chart);
//以PNG方式生成
if (flag == ChartManagement.FILE || flag == ChartManagement.RESPONSE) {
ChartUtilities.writeChartAsPNG(out, chart, width, height, null);
return null;
}
else {
return chart.createBufferedImage(width, height);
}
}
finally {
if (out != null) {
out.flush();
out.close();
}
}
}
/**
* 获得其他的数据信息,用于扩展
* @return Dataset
*/
protected Dataset getExtInputData() {
return null;
}
/**
* 设置更多的图形属性,用于扩展
* @param chart JFreeChart
*/
protected void SetChartExtProperty(JFreeChart chart) {
}
public void setIs3d(boolean is3d) {
this.is3d = is3d;
}
public void setTitle(String title) {
this.title = title;
}
public void setValueTag(String valueTag) {
this.valueTag = valueTag;
}
public void setCategoryTag(String categoryTag) {
this.categoryTag = categoryTag;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public void setChartType(String ChartType) {
this.chart_type = ChartType;
}
public void setResult(DBResult result) {
this.result = result;
}
public void setBackGroundColor(Color backGroundColor) {
this.backGroundColor = backGroundColor;
}
public void setIsalpha(boolean isalpha) {
this.isalpha = isalpha;
}
public void setDescFile(String descFile) {
this.descFile = descFile;
}
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}
public void setLegend(String[] Legend) {
this.Legend = Legend;
}
public Color getBackGroundColor() {
return backGroundColor;
}
public String getDescFile() {
return descFile;
}
public String getSubTitle() {
return subTitle;
}
public String[] getLegend() {
return Legend;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -