|
|
|
|
@ -1,95 +1,36 @@
|
|
|
|
|
package com.utils;
|
|
|
|
|
// 声明该类所在的包名为com.utils
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.ChartFactory;
|
|
|
|
|
// 导入JFreeChart库中的图表工厂类,用于创建各种类型的图表
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.JFreeChart;
|
|
|
|
|
// 导入JFreeChart类,它是JFreeChart库中表示图表的核心类
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.StandardChartTheme;
|
|
|
|
|
// 导入标准图表主题类,用于设置图表的外观主题
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.axis.DateAxis;
|
|
|
|
|
// 导入日期轴类,用于在图表中显示日期
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.axis.DateTickUnit;
|
|
|
|
|
// 导入日期刻度单位类,用于定义日期轴上的刻度间隔
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.axis.DateTickUnitType;
|
|
|
|
|
// 导入日期刻度单位类型枚举类,用于指定日期刻度的类型(如年、月、日等)
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.axis.ValueAxis;
|
|
|
|
|
// 导入值轴类,用于表示图表中的数值轴
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.block.BlockBorder;
|
|
|
|
|
// 导入块边框类,用于设置图表中块(如图例)的边框
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.labels.*;
|
|
|
|
|
// 导入JFreeChart中用于标签相关的类的包,用于设置图表的标签显示等
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.plot.*;
|
|
|
|
|
// 导入JFreeChart中用于绘图区域相关的类的包,用于处理图表的绘图区域设置
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.renderer.category.BarRenderer;
|
|
|
|
|
// 导入柱状图渲染器类,用于渲染柱状图
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
|
|
|
|
|
// 导入折线图和形状渲染器类,用于渲染折线图和数据点的形状
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.renderer.category.StackedBarRenderer;
|
|
|
|
|
// 导入堆积柱状图渲染器类,用于渲染堆积柱状图
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.renderer.category.StandardBarPainter;
|
|
|
|
|
// 导入标准柱状图绘制器类,用于绘制柱状图的外观
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
|
|
|
|
|
// 导入标准XY柱状图绘制器类,用于绘制XY图表中的柱状图外观
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.renderer.xy.XYBarRenderer;
|
|
|
|
|
// 导入XY柱状图渲染器类,用于渲染XY图表中的柱状图
|
|
|
|
|
|
|
|
|
|
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
|
|
|
|
|
// 导入XY折线图和形状渲染器类,用于渲染XY图表中的折线图和数据点的形状
|
|
|
|
|
|
|
|
|
|
import org.jfree.data.category.DefaultCategoryDataset;
|
|
|
|
|
// 导入默认的类别数据集类,用于处理柱状图、折线图等类别数据的数据集
|
|
|
|
|
|
|
|
|
|
import org.jfree.data.general.DefaultPieDataset;
|
|
|
|
|
// 导入默认的饼图数据集类,用于处理饼图的数据集
|
|
|
|
|
|
|
|
|
|
import org.jfree.data.time.Day;
|
|
|
|
|
// 导入表示一天的时间类,用于时间序列数据
|
|
|
|
|
|
|
|
|
|
import org.jfree.data.time.TimeSeries;
|
|
|
|
|
// 导入时间序列类,用于处理时间序列数据
|
|
|
|
|
|
|
|
|
|
import org.jfree.ui.RectangleInsets;
|
|
|
|
|
// 导入矩形边距类,用于设置图表中元素的边距
|
|
|
|
|
|
|
|
|
|
import org.jfree.ui.TextAnchor;
|
|
|
|
|
// 导入文本锚点类,用于指定文本的位置
|
|
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
// 导入java.awt包,包含用于处理图形和用户界面的类
|
|
|
|
|
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
// 导入十进制格式化类,用于格式化数字
|
|
|
|
|
|
|
|
|
|
import java.text.NumberFormat;
|
|
|
|
|
// 导入数字格式化类,用于格式化数字
|
|
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
// 导入解析异常类,用于处理日期等解析时可能抛出的异常
|
|
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
// 导入简单日期格式化类,用于格式化日期
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
// 导入日期类,用于表示日期和时间
|
|
|
|
|
|
|
|
|
|
import java.util.Vector;
|
|
|
|
|
// 导入Vector类,它是一个动态数组,用于存储数据系列等
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Jfreechart工具类
|
|
|
|
|
@ -99,31 +40,26 @@ import java.util.Vector;
|
|
|
|
|
* 用来对柱状图、折线图、饼图、堆积柱状图、时间序列图的样式进行渲染<br>
|
|
|
|
|
* 设置X-Y坐标轴样式
|
|
|
|
|
* <p>
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @author chenchangwen
|
|
|
|
|
* @since:2014-2-18
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public class ChartUtils {
|
|
|
|
|
// 定义一个静态字符串,用于表示无数据时的提示信息
|
|
|
|
|
private static String NO_DATA_MSG = "暂无数据";
|
|
|
|
|
// 定义一个静态字体对象,设置字体为宋体,样式为普通,大小为12
|
|
|
|
|
private static Font FONT = new Font("宋体", Font.PLAIN, 12);
|
|
|
|
|
// 定义一个静态颜色数组,包含多种颜色,用于图表的颜色设置
|
|
|
|
|
public static Color[] CHART_COLORS = { new Color(31, 129, 188),
|
|
|
|
|
new Color(92, 92, 97), new Color(144, 237, 125),
|
|
|
|
|
new Color(255, 188, 117), new Color(153, 158, 255),
|
|
|
|
|
new Color(255, 117, 153), new Color(253, 236, 109),
|
|
|
|
|
new Color(128, 133, 232), new Color(158, 90, 102),
|
|
|
|
|
new Color(255, 204, 102) };
|
|
|
|
|
new Color(255, 204, 102) };// 颜色
|
|
|
|
|
|
|
|
|
|
// 静态代码块,在类加载时执行,用于设置图表主题
|
|
|
|
|
static {
|
|
|
|
|
setChartTheme();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 空的构造函数
|
|
|
|
|
public ChartUtils() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -131,75 +67,52 @@ public class ChartUtils {
|
|
|
|
|
* 中文主题样式 解决乱码
|
|
|
|
|
*/
|
|
|
|
|
public static void setChartTheme() {
|
|
|
|
|
// 创建一个标准图表主题对象,命名为"CN"
|
|
|
|
|
// 设置中文主题样式 解决乱码
|
|
|
|
|
StandardChartTheme chartTheme = new StandardChartTheme("CN");
|
|
|
|
|
// 设置标题字体为之前定义的字体
|
|
|
|
|
// 设置标题字体
|
|
|
|
|
chartTheme.setExtraLargeFont(FONT);
|
|
|
|
|
// 设置图例的字体为之前定义的字体
|
|
|
|
|
// 设置图例的字体
|
|
|
|
|
chartTheme.setRegularFont(FONT);
|
|
|
|
|
// 设置轴向的字体为之前定义的字体
|
|
|
|
|
// 设置轴向的字体
|
|
|
|
|
chartTheme.setLargeFont(FONT);
|
|
|
|
|
chartTheme.setSmallFont(FONT);
|
|
|
|
|
// 设置标题的颜色为灰色
|
|
|
|
|
chartTheme.setTitlePaint(new Color(51, 51, 51));
|
|
|
|
|
// 设置副标题的颜色为深灰色
|
|
|
|
|
chartTheme.setSubtitlePaint(new Color(85, 85, 85));
|
|
|
|
|
|
|
|
|
|
// 设置图例的背景颜色为白色
|
|
|
|
|
chartTheme.setLegendBackgroundPaint(Color.WHITE);
|
|
|
|
|
// 设置图例项的颜色为黑色
|
|
|
|
|
chartTheme.setLegendItemPaint(Color.BLACK);
|
|
|
|
|
// 设置图表的背景颜色为白色
|
|
|
|
|
chartTheme.setLegendBackgroundPaint(Color.WHITE);// 设置标注
|
|
|
|
|
chartTheme.setLegendItemPaint(Color.BLACK);//
|
|
|
|
|
chartTheme.setChartBackgroundPaint(Color.WHITE);
|
|
|
|
|
// 绘制颜色绘制颜色.轮廓供应商
|
|
|
|
|
// paintSequence,outlinePaintSequence,strokeSequence,outlineStrokeSequence,shapeSequence
|
|
|
|
|
|
|
|
|
|
// 定义轮廓颜色数组,这里只有白色
|
|
|
|
|
Paint[] OUTLINE_PAINT_SEQUENCE = new Paint[] { Color.WHITE };
|
|
|
|
|
// 创建一个默认的绘图供应商,设置颜色、轮廓颜色等
|
|
|
|
|
// 绘制器颜色源
|
|
|
|
|
DefaultDrawingSupplier drawingSupplier = new DefaultDrawingSupplier(
|
|
|
|
|
CHART_COLORS, CHART_COLORS, OUTLINE_PAINT_SEQUENCE,
|
|
|
|
|
DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
|
|
|
|
|
DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
|
|
|
|
|
DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE);
|
|
|
|
|
// 设置图表主题的绘图供应商
|
|
|
|
|
chartTheme.setDrawingSupplier(drawingSupplier);
|
|
|
|
|
|
|
|
|
|
// 设置绘图区域的背景颜色为白色
|
|
|
|
|
chartTheme.setPlotBackgroundPaint(Color.WHITE);
|
|
|
|
|
// 设置绘图区域外边框的颜色为白色
|
|
|
|
|
chartTheme.setPlotOutlinePaint(Color.WHITE);
|
|
|
|
|
// 设置链接标签的颜色
|
|
|
|
|
chartTheme.setLabelLinkPaint(new Color(8, 55, 114));
|
|
|
|
|
// 设置标签链接的样式为三次曲线
|
|
|
|
|
chartTheme.setPlotBackgroundPaint(Color.WHITE);// 绘制区域
|
|
|
|
|
chartTheme.setPlotOutlinePaint(Color.WHITE);// 绘制区域外边框
|
|
|
|
|
chartTheme.setLabelLinkPaint(new Color(8, 55, 114));// 链接标签颜色
|
|
|
|
|
chartTheme.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
|
|
|
|
|
|
|
|
|
|
// 设置图表的边距
|
|
|
|
|
chartTheme.setAxisOffset(new RectangleInsets(5, 12, 5, 12));
|
|
|
|
|
// 设置X坐标轴垂直网格的颜色
|
|
|
|
|
chartTheme.setDomainGridlinePaint(new Color(192, 208, 224));
|
|
|
|
|
// 设置Y坐标轴水平网格的颜色
|
|
|
|
|
chartTheme.setRangeGridlinePaint(new Color(192, 192, 192));
|
|
|
|
|
chartTheme.setDomainGridlinePaint(new Color(192, 208, 224));// X坐标轴垂直网格颜色
|
|
|
|
|
chartTheme.setRangeGridlinePaint(new Color(192, 192, 192));// Y坐标轴水平网格颜色
|
|
|
|
|
|
|
|
|
|
// 设置基线的颜色为白色
|
|
|
|
|
chartTheme.setBaselinePaint(Color.WHITE);
|
|
|
|
|
// 设置十字准线的颜色为蓝色
|
|
|
|
|
chartTheme.setCrosshairPaint(Color.BLUE);
|
|
|
|
|
// 设置坐标轴标题文字的颜色
|
|
|
|
|
chartTheme.setAxisLabelPaint(new Color(51, 51, 51));
|
|
|
|
|
// 设置刻度数字的颜色
|
|
|
|
|
chartTheme.setTickLabelPaint(new Color(67, 67, 72));
|
|
|
|
|
// 设置柱状图的渲染器为标准柱状图绘制器
|
|
|
|
|
chartTheme.setBarPainter(new StandardBarPainter());
|
|
|
|
|
// 设置XYBar的渲染器为标准XYBar绘制器
|
|
|
|
|
chartTheme.setXYBarPainter(new StandardXYBarPainter());
|
|
|
|
|
|
|
|
|
|
// 设置项目标签的颜色为黑色
|
|
|
|
|
chartTheme.setCrosshairPaint(Color.BLUE);// 不确定含义
|
|
|
|
|
chartTheme.setAxisLabelPaint(new Color(51, 51, 51));// 坐标轴标题文字颜色
|
|
|
|
|
chartTheme.setTickLabelPaint(new Color(67, 67, 72));// 刻度数字
|
|
|
|
|
chartTheme.setBarPainter(new StandardBarPainter());// 设置柱状图渲染
|
|
|
|
|
chartTheme.setXYBarPainter(new StandardXYBarPainter());// XYBar 渲染
|
|
|
|
|
|
|
|
|
|
chartTheme.setItemLabelPaint(Color.black);
|
|
|
|
|
// 设置温度计的颜色为白色
|
|
|
|
|
chartTheme.setThermometerPaint(Color.white);
|
|
|
|
|
chartTheme.setThermometerPaint(Color.white);// 温度计
|
|
|
|
|
|
|
|
|
|
// 设置JFreeChart的全局图表主题
|
|
|
|
|
ChartFactory.setChartTheme(chartTheme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -207,16 +120,16 @@ public class ChartUtils {
|
|
|
|
|
* 必须设置文本抗锯齿
|
|
|
|
|
*/
|
|
|
|
|
public static void setAntiAlias(JFreeChart chart) {
|
|
|
|
|
// 设置图表的文本抗锯齿为关闭状态
|
|
|
|
|
chart.setTextAntiAlias(false);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置图例无边框,默认黑色边框
|
|
|
|
|
*/
|
|
|
|
|
public static void setLegendEmptyBorder(JFreeChart chart) {
|
|
|
|
|
// 创建一个白色边框的BlockBorder对象,并将其设置为图表图例的边框
|
|
|
|
|
chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -224,39 +137,29 @@ public class ChartUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static DefaultCategoryDataset createDefaultCategoryDataset(
|
|
|
|
|
Vector<Serie> series, String[] categories) {
|
|
|
|
|
// 创建一个默认的类别数据集对象
|
|
|
|
|
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
|
|
|
|
|
|
|
|
|
|
// 遍历数据系列
|
|
|
|
|
for (Serie serie : series) {
|
|
|
|
|
// 获取数据系列的名称
|
|
|
|
|
String name = serie.getName();
|
|
|
|
|
// 获取数据系列的数据
|
|
|
|
|
Vector<Object> data = serie.getData();
|
|
|
|
|
// 如果数据和类别都不为空,且数据数量和类别数量相等
|
|
|
|
|
if (data != null && categories != null
|
|
|
|
|
&& data.size() == categories.length) {
|
|
|
|
|
// 遍历数据
|
|
|
|
|
for (int index = 0; index < data.size(); index++) {
|
|
|
|
|
// 获取数据值,并处理为字符串
|
|
|
|
|
String value = data.get(index) == null? "" : data.get(
|
|
|
|
|
String value = data.get(index) == null ? "" : data.get(
|
|
|
|
|
index).toString();
|
|
|
|
|
// 如果数据是百分比形式
|
|
|
|
|
if (isPercent(value)) {
|
|
|
|
|
// 去掉百分比符号
|
|
|
|
|
value = value.substring(0, value.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
// 如果数据是数字
|
|
|
|
|
if (isNumber(value)) {
|
|
|
|
|
// 将数据值添加到数据集中
|
|
|
|
|
dataset.setValue(Double.parseDouble(value), name,
|
|
|
|
|
categories[index]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// 返回创建好的数据集
|
|
|
|
|
return dataset;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -264,30 +167,23 @@ public class ChartUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static DefaultPieDataset createDefaultPieDataset(
|
|
|
|
|
String[] categories, Object[] datas) {
|
|
|
|
|
// 创建一个默认的饼图数据集对象
|
|
|
|
|
DefaultPieDataset dataset = new DefaultPieDataset();
|
|
|
|
|
// 遍历类别
|
|
|
|
|
for (int i = 0; i < categories.length && categories != null; i++) {
|
|
|
|
|
// 获取数据值,并处理为字符串
|
|
|
|
|
String value = datas[i].toString();
|
|
|
|
|
// 如果数据是百分比形式
|
|
|
|
|
if (isPercent(value)) {
|
|
|
|
|
// 去掉百分比符号
|
|
|
|
|
value = value.substring(0, value.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
// 如果数据是数字
|
|
|
|
|
if (isNumber(value)) {
|
|
|
|
|
// 将数据值添加到数据集中
|
|
|
|
|
dataset.setValue(categories[i], Double.valueOf(value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 返回创建好的数据集
|
|
|
|
|
return dataset;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建时间序列数据
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param category
|
|
|
|
|
* 类别
|
|
|
|
|
* @param dateValues
|
|
|
|
|
@ -297,96 +193,73 @@ public class ChartUtils {
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static TimeSeries createTimeseries(String category,
|
|
|
|
|
Vector<Object[]> dateValues) {
|
|
|
|
|
// 创建一个时间序列对象,传入类别名称
|
|
|
|
|
Vector<Object[]> dateValues) {
|
|
|
|
|
TimeSeries timeseries = new TimeSeries(category);
|
|
|
|
|
|
|
|
|
|
// 如果日期值数组不为空
|
|
|
|
|
if (dateValues != null) {
|
|
|
|
|
// 创建一个简单日期格式化对象,格式为"yyyy-MM-dd"
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
// 遍历日期值数组
|
|
|
|
|
for (Object[] objects : dateValues) {
|
|
|
|
|
// 初始化日期对象
|
|
|
|
|
Date date = null;
|
|
|
|
|
try {
|
|
|
|
|
// 解析日期字符串为日期对象
|
|
|
|
|
date = dateFormat.parse(objects[0].toString());
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
}
|
|
|
|
|
// 获取数据值,并处理为字符串
|
|
|
|
|
String sValue = objects[1].toString();
|
|
|
|
|
// 初始化数据值为0
|
|
|
|
|
double dValue = 0;
|
|
|
|
|
// 如果日期不为空且数据是数字
|
|
|
|
|
if (date != null && isNumber(sValue)) {
|
|
|
|
|
// 将数据值转换为double类型
|
|
|
|
|
dValue = Double.parseDouble(sValue);
|
|
|
|
|
// 将数据添加到时间序列中
|
|
|
|
|
timeseries.add(new Day(date), dValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回创建好的时间序列对象
|
|
|
|
|
return timeseries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置 折线图样式
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param plot
|
|
|
|
|
* @param isShowDataLabels
|
|
|
|
|
* 是否显示数据标签 默认不显示节点形状
|
|
|
|
|
*/
|
|
|
|
|
public static void setLineRender(CategoryPlot plot, boolean isShowDataLabels) {
|
|
|
|
|
// 调用另一个设置折线图样式的方法,不显示节点形状
|
|
|
|
|
setLineRender(plot, isShowDataLabels, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置折线图样式
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param plot
|
|
|
|
|
* @param isShowDataLabels
|
|
|
|
|
* 是否显示数据标签
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
|
public static void setLineRender(CategoryPlot plot,
|
|
|
|
|
boolean isShowDataLabels, boolean isShapesVisible) {
|
|
|
|
|
// 设置无数据时的提示信息
|
|
|
|
|
boolean isShowDataLabels, boolean isShapesVisible) {
|
|
|
|
|
plot.setNoDataMessage(NO_DATA_MSG);
|
|
|
|
|
// 设置绘图区域的边距
|
|
|
|
|
plot.setInsets(new RectangleInsets(10, 10, 0, 10), false);
|
|
|
|
|
// 获取折线图的渲染器
|
|
|
|
|
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
|
|
|
|
|
.getRenderer();
|
|
|
|
|
|
|
|
|
|
// 设置线条的粗细为1.5F
|
|
|
|
|
renderer.setStroke(new BasicStroke(1.5F));
|
|
|
|
|
// 如果要显示数据标签
|
|
|
|
|
if (isShowDataLabels) {
|
|
|
|
|
// 设置基本项目标签可见
|
|
|
|
|
renderer.setBaseItemLabelsVisible(true);
|
|
|
|
|
// 设置基本项目标签生成器
|
|
|
|
|
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator(
|
|
|
|
|
StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING,
|
|
|
|
|
NumberFormat.getInstance()));
|
|
|
|
|
// 设置基本正项目标签的位置
|
|
|
|
|
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
|
|
|
|
|
ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));
|
|
|
|
|
ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));// weizhi
|
|
|
|
|
}
|
|
|
|
|
// 设置数据点绘制形状的可见性
|
|
|
|
|
renderer.setBaseShapesVisible(isShapesVisible);
|
|
|
|
|
// 设置X坐标轴样式
|
|
|
|
|
renderer.setBaseShapesVisible(isShapesVisible);// 数据点绘制形状
|
|
|
|
|
setXAixs(plot);
|
|
|
|
|
// 设置Y坐标轴样式
|
|
|
|
|
setYAixs(plot);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置时间序列图样式
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param plot
|
|
|
|
|
* @param isShowData
|
|
|
|
|
* 是否显示数据
|
|
|
|
|
@ -394,36 +267,249 @@ public class ChartUtils {
|
|
|
|
|
* 是否显示数据节点形状
|
|
|
|
|
*/
|
|
|
|
|
public static void setTimeSeriesRender(Plot plot, boolean isShowData,
|
|
|
|
|
boolean isShapesVisible) {
|
|
|
|
|
boolean isShapesVisible) {
|
|
|
|
|
|
|
|
|
|
// 将绘图区域转换为XYPlot类型
|
|
|
|
|
XYPlot xyplot = (XYPlot) plot;
|
|
|
|
|
// 设置无数据时的提示信息
|
|
|
|
|
xyplot.setNoDataMessage(NO_DATA_MSG);
|
|
|
|
|
// 设置绘图区域的边距
|
|
|
|
|
xyplot.setInsets(new RectangleInsets(10, 10, 5, 10));
|
|
|
|
|
|
|
|
|
|
// 获取XY折线图和形状渲染器
|
|
|
|
|
XYLineAndShapeRenderer xyRenderer = (XYLineAndShapeRenderer) xyplot
|
|
|
|
|
.getRenderer();
|
|
|
|
|
|
|
|
|
|
// 设置基本项目标签生成器
|
|
|
|
|
xyRenderer
|
|
|
|
|
.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
|
|
|
|
|
// 设置基本数据点形状不可见
|
|
|
|
|
xyRenderer.setBaseShapesVisible(false);
|
|
|
|
|
// 如果要显示数据
|
|
|
|
|
if (isShowData) {
|
|
|
|
|
// 设置基本项目标签可见
|
|
|
|
|
xyRenderer.setBaseItemLabelsVisible(true);
|
|
|
|
|
// 设置基本项目标签生成器
|
|
|
|
|
xyRenderer
|
|
|
|
|
.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
|
|
|
|
|
// 设置基本正项目标签的位置
|
|
|
|
|
xyRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
|
|
|
|
|
ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));
|
|
|
|
|
ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));// weizhi
|
|
|
|
|
}
|
|
|
|
|
xyRenderer.setBaseShapesVisible(isShapesVisible);// 数据点绘制形状
|
|
|
|
|
|
|
|
|
|
DateAxis domainAxis = (DateAxis) xyplot.getDomainAxis();
|
|
|
|
|
domainAxis.setAutoTickUnitSelection(false);
|
|
|
|
|
DateTickUnit dateTickUnit = new DateTickUnit(DateTickUnitType.YEAR, 1,
|
|
|
|
|
new SimpleDateFormat("yyyy-MM")); // 第二个参数是时间轴间距
|
|
|
|
|
domainAxis.setTickUnit(dateTickUnit);
|
|
|
|
|
|
|
|
|
|
StandardXYToolTipGenerator xyTooltipGenerator = new StandardXYToolTipGenerator(
|
|
|
|
|
"{1}:{2}", new SimpleDateFormat("yyyy-MM-dd"),
|
|
|
|
|
new DecimalFormat("0"));
|
|
|
|
|
xyRenderer.setBaseToolTipGenerator(xyTooltipGenerator);
|
|
|
|
|
|
|
|
|
|
setXY_XAixs(xyplot);
|
|
|
|
|
setXY_YAixs(xyplot);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置时间序列图样式 -默认不显示数据节点形状
|
|
|
|
|
*
|
|
|
|
|
* @param plot
|
|
|
|
|
* @param isShowData
|
|
|
|
|
* 是否显示数据
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public static void setTimeSeriesRender(Plot plot, boolean isShowData) {
|
|
|
|
|
setTimeSeriesRender(plot, isShowData, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置时间序列图渲染:但是存在一个问题:如果timeseries里面的日期是按照天组织, 那么柱子的宽度会非常小,和直线一样粗细
|
|
|
|
|
*
|
|
|
|
|
* @param plot
|
|
|
|
|
* @param isShowDataLabels
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public static void setTimeSeriesBarRender(Plot plot,
|
|
|
|
|
boolean isShowDataLabels) {
|
|
|
|
|
|
|
|
|
|
XYPlot xyplot = (XYPlot) plot;
|
|
|
|
|
xyplot.setNoDataMessage(NO_DATA_MSG);
|
|
|
|
|
|
|
|
|
|
XYBarRenderer xyRenderer = new XYBarRenderer(0.1D);
|
|
|
|
|
xyRenderer
|
|
|
|
|
.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
|
|
|
|
|
|
|
|
|
|
if (isShowDataLabels) {
|
|
|
|
|
xyRenderer.setBaseItemLabelsVisible(true);
|
|
|
|
|
xyRenderer
|
|
|
|
|
.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
|
|
|
|
|
}
|
|
|
|
|
// 设置数据点绘制形状的可见性
|
|
|
|
|
xyRenderer.setBaseShapesVisible(isShapesVisible);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
StandardXYToolTipGenerator xyTooltipGenerator = new StandardXYToolTipGenerator(
|
|
|
|
|
"{1}:{2}", new SimpleDateFormat("yyyy-MM-dd"),
|
|
|
|
|
new DecimalFormat("0"));
|
|
|
|
|
xyRenderer.setBaseToolTipGenerator(xyTooltipGenerator);
|
|
|
|
|
setXY_XAixs(xyplot);
|
|
|
|
|
setXY_YAixs(xyplot);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置柱状图渲染
|
|
|
|
|
*
|
|
|
|
|
* @param plot
|
|
|
|
|
* @param isShowDataLabels
|
|
|
|
|
*/
|
|
|
|
|
public static void setBarRenderer(CategoryPlot plot,
|
|
|
|
|
boolean isShowDataLabels) {
|
|
|
|
|
|
|
|
|
|
plot.setNoDataMessage(NO_DATA_MSG);
|
|
|
|
|
plot.setBackgroundAlpha(0);//设置背景透明度
|
|
|
|
|
plot.setInsets(new RectangleInsets(10, 10, 5, 10));
|
|
|
|
|
BarRenderer renderer = (BarRenderer) plot.getRenderer();
|
|
|
|
|
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
|
|
|
|
|
renderer.setMaximumBarWidth(0.075);// 设置柱子最大宽度
|
|
|
|
|
|
|
|
|
|
if (isShowDataLabels) {
|
|
|
|
|
renderer.setBaseItemLabelsVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setXAixs(plot);
|
|
|
|
|
setYAixs(plot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置堆积柱状图渲染
|
|
|
|
|
*
|
|
|
|
|
* @param plot
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public static void setStackBarRender(CategoryPlot plot) {
|
|
|
|
|
plot.setNoDataMessage(NO_DATA_MSG);
|
|
|
|
|
plot.setInsets(new RectangleInsets(10, 10, 5, 10));
|
|
|
|
|
StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
|
|
|
|
|
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
|
|
|
|
|
plot.setRenderer(renderer);
|
|
|
|
|
setXAixs(plot);
|
|
|
|
|
setYAixs(plot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置类别图表(CategoryPlot) X坐标轴线条颜色和样式
|
|
|
|
|
*
|
|
|
|
|
* @param axis
|
|
|
|
|
*/
|
|
|
|
|
public static void setXAixs(CategoryPlot plot) {
|
|
|
|
|
Color lineColor = new Color(31, 121, 170);
|
|
|
|
|
plot.getDomainAxis().setAxisLinePaint(lineColor);// X坐标轴颜色
|
|
|
|
|
plot.getDomainAxis().setTickMarkPaint(lineColor);// X坐标轴标记|竖线颜色
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置类别图表(CategoryPlot) Y坐标轴线条颜色和样式 同时防止数据无法显示
|
|
|
|
|
*
|
|
|
|
|
* @param axis
|
|
|
|
|
*/
|
|
|
|
|
public static void setYAixs(CategoryPlot plot) {
|
|
|
|
|
Color lineColor = new Color(192, 208, 224);
|
|
|
|
|
ValueAxis axis = plot.getRangeAxis();
|
|
|
|
|
axis.setAxisLinePaint(lineColor);// Y坐标轴颜色
|
|
|
|
|
axis.setTickMarkPaint(lineColor);// Y坐标轴标记|竖线颜色
|
|
|
|
|
// 隐藏Y刻度
|
|
|
|
|
axis.setAxisLineVisible(false);
|
|
|
|
|
axis.setTickMarksVisible(false);
|
|
|
|
|
// Y轴网格线条
|
|
|
|
|
plot.setRangeGridlinePaint(new Color(192, 192, 192));
|
|
|
|
|
plot.setRangeGridlineStroke(new BasicStroke(1));
|
|
|
|
|
|
|
|
|
|
plot.getRangeAxis().setUpperMargin(0.1);// 设置顶部Y坐标轴间距,防止数据无法显示
|
|
|
|
|
plot.getRangeAxis().setLowerMargin(0.1);// 设置底部Y坐标轴间距
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置XY图表(XYPlot) X坐标轴线条颜色和样式
|
|
|
|
|
*
|
|
|
|
|
* @param axis
|
|
|
|
|
*/
|
|
|
|
|
public static void setXY_XAixs(XYPlot plot) {
|
|
|
|
|
Color lineColor = new Color(31, 121, 170);
|
|
|
|
|
plot.getDomainAxis().setAxisLinePaint(lineColor);// X坐标轴颜色
|
|
|
|
|
plot.getDomainAxis().setTickMarkPaint(lineColor);// X坐标轴标记|竖线颜色
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置XY图表(XYPlot) Y坐标轴线条颜色和样式 同时防止数据无法显示
|
|
|
|
|
*
|
|
|
|
|
* @param axis
|
|
|
|
|
*/
|
|
|
|
|
public static void setXY_YAixs(XYPlot plot) {
|
|
|
|
|
Color lineColor = new Color(192, 208, 224);
|
|
|
|
|
ValueAxis axis = plot.getRangeAxis();
|
|
|
|
|
axis.setAxisLinePaint(lineColor);// X坐标轴颜色
|
|
|
|
|
axis.setTickMarkPaint(lineColor);// X坐标轴标记|竖线颜色
|
|
|
|
|
// 隐藏Y刻度
|
|
|
|
|
axis.setAxisLineVisible(false);
|
|
|
|
|
axis.setTickMarksVisible(false);
|
|
|
|
|
// Y轴网格线条
|
|
|
|
|
plot.setRangeGridlinePaint(new Color(192, 192, 192));
|
|
|
|
|
plot.setRangeGridlineStroke(new BasicStroke(1));
|
|
|
|
|
plot.setDomainGridlinesVisible(false);
|
|
|
|
|
|
|
|
|
|
plot.getRangeAxis().setUpperMargin(0.12);// 设置顶部Y坐标轴间距,防止数据无法显示
|
|
|
|
|
plot.getRangeAxis().setLowerMargin(0.12);// 设置底部Y坐标轴间距
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置饼状图渲染
|
|
|
|
|
*/
|
|
|
|
|
public static void setPieRender(Plot plot) {
|
|
|
|
|
|
|
|
|
|
plot.setNoDataMessage(NO_DATA_MSG);
|
|
|
|
|
plot.setInsets(new RectangleInsets(10, 10, 5, 10));
|
|
|
|
|
PiePlot piePlot = (PiePlot) plot;
|
|
|
|
|
piePlot.setInsets(new RectangleInsets(0, 0, 0, 0));
|
|
|
|
|
piePlot.setCircular(true);// 圆形
|
|
|
|
|
|
|
|
|
|
// piePlot.setSimpleLabels(true);// 简单标签
|
|
|
|
|
piePlot.setLabelGap(0.01);
|
|
|
|
|
// piePlot.setForegroundAlpha(0.5f);// 饼图的透明度
|
|
|
|
|
piePlot.setBackgroundAlpha(0);//背景透明度
|
|
|
|
|
piePlot.setOutlinePaint(null);//去掉边框
|
|
|
|
|
piePlot.setInteriorGap(0.05D);
|
|
|
|
|
piePlot.setLegendItemShape(new Rectangle(10, 10));// 图例形状
|
|
|
|
|
piePlot.setIgnoreNullValues(true);
|
|
|
|
|
piePlot.setLabelBackgroundPaint(null);// 去掉背景色
|
|
|
|
|
piePlot.setLabelShadowPaint(null);// 去掉阴影
|
|
|
|
|
piePlot.setLabelOutlinePaint(null);// 去掉边框
|
|
|
|
|
piePlot.setShadowPaint(null);
|
|
|
|
|
// 0:category 1:value:2 :percentage(标签显示形式1)
|
|
|
|
|
/*piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(
|
|
|
|
|
"{0}:{2}"));// 显示标签数据*/
|
|
|
|
|
//设置图形的生成格式为(张三 23 (10%))(姓名 值 百分比)(标签显示形式2)
|
|
|
|
|
piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(
|
|
|
|
|
"{0} {1} ({2})"));// 显示标签数据
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是不是一个%形式的百分比
|
|
|
|
|
*
|
|
|
|
|
* @param str
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isPercent(String str) {
|
|
|
|
|
return str != null ? str.endsWith("%")
|
|
|
|
|
&& isNumber(str.substring(0, str.length() - 1)) : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是不是一个数字
|
|
|
|
|
*
|
|
|
|
|
* @param str
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isNumber(String str) {
|
|
|
|
|
return str != null ? str
|
|
|
|
|
.matches("^[-+]?(([0-9]+)((([.]{0})([0-9]*))|(([.]{1})([0-9]+))))$")
|
|
|
|
|
: false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|