Jone
1 year ago
2 changed files with 272 additions and 0 deletions
@ -0,0 +1,236 @@
|
||||
package com.hnac.hzims.operational.data.controller; |
||||
|
||||
import java.awt.*; |
||||
import java.io.FileOutputStream; |
||||
import java.math.BigDecimal; |
||||
import java.text.DecimalFormat; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import com.hnac.hzims.operational.report.vo.TicketChartCell; |
||||
import com.hnac.hzims.operational.report.vo.TicketChartVo; |
||||
import com.hnac.hzims.ticket.constants.TicketConstants; |
||||
import org.jfree.chart.ChartFactory; |
||||
import org.jfree.chart.ChartFrame; |
||||
import org.jfree.chart.ChartUtils; |
||||
import org.jfree.chart.JFreeChart; |
||||
import org.jfree.chart.axis.CategoryAxis; |
||||
import org.jfree.chart.axis.CategoryLabelPositions; |
||||
import org.jfree.chart.axis.NumberAxis; |
||||
import org.jfree.chart.axis.ValueAxis; |
||||
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.StandardBarPainter; |
||||
import org.jfree.chart.title.TextTitle; |
||||
import org.jfree.data.category.CategoryDataset; |
||||
import org.jfree.data.category.DefaultCategoryDataset; |
||||
|
||||
public class BarChartDemo { |
||||
// public static void main(String[] args) {
|
||||
// // 1. 得到数据
|
||||
// CategoryDataset dataset = getDataSetTicket();
|
||||
//// CategoryDataset dataset = getDataSet();
|
||||
//// CategoryDataset dataset = getDataSet3();
|
||||
// // 2. 构造chart
|
||||
// JFreeChart chart = ChartFactory.createBarChart(
|
||||
// "水果产量图", // 图表标题
|
||||
// "水果", // 目录轴的显示标签--横轴
|
||||
// "产量", // 数值轴的显示标签--纵轴
|
||||
// dataset, // 数据集
|
||||
// PlotOrientation.VERTICAL, // 图表方向:水平、
|
||||
// true, // 是否显示图例(对于简单的柱状图必须
|
||||
// false, // 是否生成工具
|
||||
// false // 是否生成URL链接
|
||||
// );
|
||||
// // 3. 处理chart中文显示问题
|
||||
// iSetBarChart(chart);
|
||||
//
|
||||
// // 4. chart输出图片
|
||||
//// writeChartAsImage(chart, "fruit.jpg");
|
||||
// writeChartAsImage(chart, "ticket.jpg");
|
||||
//
|
||||
//// writeChartAsImage(chart, "fruit2.jpg");
|
||||
//
|
||||
// // 5. chart 以swing形式输出
|
||||
// ChartFrame pieFrame = new ChartFrame("水果生产", chart);
|
||||
// pieFrame.pack();
|
||||
// pieFrame.setVisible(true);
|
||||
//
|
||||
// }
|
||||
|
||||
/** |
||||
* 获取一个演示用的组合数据集对象 |
||||
* |
||||
* @return |
||||
*/ |
||||
private static CategoryDataset getDataSet() { |
||||
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); |
||||
dataset.addValue(100, "北京", "苹果"); |
||||
dataset.addValue(120, "上海", "苹果"); |
||||
dataset.addValue(160, "广州", "苹果"); |
||||
dataset.addValue(210, "北京", "梨子"); |
||||
dataset.addValue(220, "上海", "梨子"); |
||||
dataset.addValue(230, "广州", "梨子"); |
||||
dataset.addValue(330, "北京", "葡萄"); |
||||
dataset.addValue(340, "上海", "葡萄"); |
||||
dataset.addValue(340, "广州", "葡萄"); |
||||
dataset.addValue(420, "北京", "香蕉"); |
||||
dataset.addValue(430, "上海", "香蕉"); |
||||
dataset.addValue(400, "广州", "香蕉"); |
||||
dataset.addValue(510, "北京", "荔枝"); |
||||
dataset.addValue(530, "上海", "荔枝"); |
||||
dataset.addValue(510, "广州", "荔枝"); |
||||
return dataset; |
||||
} |
||||
|
||||
private static CategoryDataset getDataSet3() { |
||||
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); |
||||
dataset.addValue(100, "北京", "苹果"); |
||||
dataset.addValue(210, "北京", "梨子"); |
||||
dataset.addValue(330, "北京", "葡萄"); |
||||
dataset.addValue(420, "北京", "香蕉"); |
||||
dataset.addValue(510, "北京", "荔枝"); |
||||
return dataset; |
||||
} |
||||
|
||||
|
||||
private static CategoryDataset getDataSetTicket() { |
||||
List<TicketChartVo> ticketList = new ArrayList<>(); |
||||
TicketConstants.TicketTypeNameEnum[] values = TicketConstants.TicketTypeNameEnum.values(); |
||||
TicketChartVo sum=new TicketChartVo(); |
||||
List<TicketChartCell> sas = new ArrayList<>(); |
||||
sum.setDeptName("总计"); |
||||
for (TicketConstants.TicketTypeNameEnum value : values) { |
||||
TicketChartCell ticketChartCell = new TicketChartCell(); |
||||
ticketChartCell.setTicketType(value.getType()); |
||||
ticketChartCell.setTicketName(value.getFileName()); |
||||
Integer integer = Integer.valueOf(value.getType()); |
||||
ticketChartCell.setTicketMon(30+ integer); |
||||
BigDecimal d=BigDecimal.valueOf(integer).divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_UP); |
||||
ticketChartCell.setTicketMonPerc(0.3+ d.doubleValue()); |
||||
sas.add(ticketChartCell); |
||||
} |
||||
sum.setChartCells(sas); |
||||
ticketList.add(sum); |
||||
//两票柱状图数据
|
||||
// 创建数据
|
||||
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); |
||||
// for (TicketChartVo chartVo : ticketList) {
|
||||
// for (int i = 0; i < chartVo.getChartCells().size(); i++) {
|
||||
// TicketChartCell ticketChartCell = chartVo.getChartCells().get(i);
|
||||
// dataset.addValue(ticketChartCell.getTicketMon(), ticketChartCell.getTicketName(), ticketChartCell.getTicketName());
|
||||
// }
|
||||
// }
|
||||
|
||||
dataset.addValue(33+1, "", "倒闸操作票"); |
||||
dataset.addValue(33+2,"", "开机操作票"); |
||||
dataset.addValue(33+3,"", "停机操作票"); |
||||
dataset.addValue(33+4,"", "第一种工作票"); |
||||
dataset.addValue(33+5,"", "第二种工作票"); |
||||
dataset.addValue(33+6,"", "水力机械票"); |
||||
return dataset; |
||||
} |
||||
|
||||
/** |
||||
* 解决图表汉字显示问题 |
||||
* |
||||
* @param chart |
||||
*/ |
||||
private static void processChart(JFreeChart chart) { |
||||
CategoryPlot plot = chart.getCategoryPlot(); |
||||
CategoryAxis domainAxis = plot.getDomainAxis(); |
||||
ValueAxis rAxis = plot.getRangeAxis(); |
||||
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, |
||||
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); |
||||
TextTitle textTitle = chart.getTitle(); |
||||
textTitle.setFont(new Font("宋体", Font.PLAIN, 20)); |
||||
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); |
||||
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); |
||||
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); |
||||
rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); |
||||
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); |
||||
// renderer.setItemLabelGenerator(new LabelGenerator(0.0));
|
||||
// renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12));
|
||||
// renderer.setItemLabelsVisible(true);
|
||||
} |
||||
|
||||
/** |
||||
* 输出图片 |
||||
* |
||||
* @param chart |
||||
*/ |
||||
private static void writeChartAsImage(JFreeChart chart, String fileName) { |
||||
FileOutputStream fos_jpg = null; |
||||
try { |
||||
fos_jpg = new FileOutputStream("D:\\test\\"+fileName); |
||||
ChartUtils.writeChartAsJPEG(fos_jpg, 1, chart, 400, 300, null); |
||||
// ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 400, 300, null);
|
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} finally { |
||||
try { |
||||
fos_jpg.close(); |
||||
} catch (Exception e) { |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
public static void iSetBarChart(JFreeChart chart) { |
||||
CategoryPlot categoryplot = chart.getCategoryPlot();// 图本身
|
||||
ValueAxis rangeAxis = categoryplot.getRangeAxis(); |
||||
CategoryAxis domainAxis = categoryplot.getDomainAxis(); |
||||
// 设置Y轴的提示文字样式
|
||||
rangeAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12)); |
||||
// 设置Y轴刻度线的长度
|
||||
rangeAxis.setTickMarkInsideLength(10f); |
||||
|
||||
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD); // 横轴上的
|
||||
domainAxis.setLowerMargin(0.1); |
||||
domainAxis.setUpperMargin(0.1); |
||||
|
||||
// rangeAxis.setTickMarkOutsideLength(10f);
|
||||
// 设置X轴下的标签文字
|
||||
domainAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12)); |
||||
// 设置X轴上提示文字样式
|
||||
domainAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12)); |
||||
NumberAxis vn = (NumberAxis) categoryplot.getRangeAxis(); |
||||
|
||||
// 设置Y轴的数字为百分比样式显示设置Y轴的数字为百分比样式显示
|
||||
DecimalFormat df = new DecimalFormat("0.0"); |
||||
vn.setNumberFormatOverride(df); |
||||
// 使柱状图反过来显示
|
||||
// vn.setInverted(true);
|
||||
// vn.setVerticalTickLabels(true);
|
||||
|
||||
// 自定义柱状图中柱子的样式
|
||||
CustomRenderer brender = new CustomRenderer(); |
||||
// 设置柱状图的顶端显示数字
|
||||
brender.setIncludeBaseInRange(true); |
||||
brender.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator()); |
||||
brender.setDefaultItemLabelsVisible(true); |
||||
// 设置柱子为平面图不是立体的
|
||||
brender.setBarPainter(new StandardBarPainter()); |
||||
// 设置柱状图之间的距离0.1代表10%;
|
||||
brender.setItemMargin(0.3); |
||||
// 设置柱子的阴影,false代表没有阴影
|
||||
brender.setShadowVisible(false); |
||||
|
||||
// 设置图的背景为白色
|
||||
categoryplot.setBackgroundPaint(Color.WHITE); |
||||
// 设置背景虚线的颜色
|
||||
categoryplot.setRangeGridlinePaint(Color.decode("#B6A2DE")); |
||||
// 去掉柱状图的背景边框,使边框不可见
|
||||
categoryplot.setOutlineVisible(false); |
||||
// 设置标题的字体样式
|
||||
chart.getTitle().setFont(new Font("微软雅黑", Font.PLAIN, 24)); |
||||
// 设置图表下方图例上的字体样式
|
||||
chart.getLegend().setItemFont(new Font("微软雅黑", Font.PLAIN, 12)); |
||||
|
||||
categoryplot.setRenderer(brender); |
||||
|
||||
|
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
package com.hnac.hzims.operational.data.controller; |
||||
import lombok.Data; |
||||
|
||||
import java.awt.*; |
||||
|
||||
/** |
||||
* 类描述: 重写BarRenderer类 |
||||
* |
||||
* @author Jack_Liberty |
||||
* @version 1.0 |
||||
* @date 2021-04-01 11:30 |
||||
*/ |
||||
@Data |
||||
public class CustomRenderer extends org.jfree.chart.renderer.category.BarRenderer { |
||||
|
||||
/** |
||||
* 柱子颜色 |
||||
*/ |
||||
private Paint[] colors; |
||||
|
||||
public CustomRenderer() { |
||||
/** |
||||
* 初始化柱子颜色 |
||||
*/ |
||||
String[] colorValues = {"#C0504D", "#E46C0A", "#4F81BD", "#00B050", "#7030A0", "#00BF00"}; |
||||
colors = new Paint[colorValues.length]; |
||||
for (int i = 0; i < colorValues.length; i++) { |
||||
colors[i] = Color.decode(colorValues[i]); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public Paint getItemPaint(int i, int j) { |
||||
return colors[j % colors.length]; |
||||
} |
||||
} |
Loading…
Reference in new issue