public class Jfreechart {
public Jfreechart() {
this.createChart();
}
// 获得数据集 (这里的数据是为了测试我随便写的一个自动生成数据的例子)
public DefaultCategoryDataset createDataset() {
DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
// 曲线名称
String series = "分析报告"; // series指的就是报表里的那条数据线
// 因此 对数据线的相关设置就需要联系到serise
// 比如说setSeriesPaint 设置数据线的颜色
// 横轴名称(列名称)
String[] timeValue = { "政治方向", "6-2日", "6-3日", "6-4日", "6-5日", "6-6日",
"6-7日", "6-8日", "6-9日", "6-10日", "6-11日", "6-12日"};
// 随机添加数据值
for (int i = 0; i < 12; i++) {
java.util.Random r = new java.util.Random();
linedataset.addValue(i + i * 9.34 + r.nextLong() % 100, // 值
series, // 哪条数据线
timeValue[i]);
// 对应的横轴
}
return linedataset;
}
// 生成图标对象JFreeChart
/*
* 整个大的框架属于JFreeChart坐标轴里的属于 Plot 其常用子类有:CategoryPlot, MultiplePiePlot,
* PiePlot , XYPlot
*
* **
*/
public void createChart() {
try {
// 定义图标对象
JFreeChart chart = ChartFactory.createLineChart(null,// 报表题目,字符串类型
null, // 横轴
null, // 纵轴
this.createDataset(), // 获得数据集
PlotOrientation.VERTICAL, // 图标方向垂直
true, // 显示图例
false, // 不用生成工具
false // 不用生成URL地址
);
// 整个大的框架属于chart 可以设置chart的背景颜色
chart.setBackgroundPaint(new Color(170, 228, 255));
// 生成图形
CategoryPlot plot = chart.getCategoryPlot();
PiePlot piePlot = (PiePlot) chart.getPlot();
// 图像属性部分
Color color = new Color(170, 228, 255);
plot.setBackgroundPaint(color);
plot.setDomainGridlinesVisible(true); // 设置背景网格线是否可见
plot.setDomainGridlinePaint(Color.BLACK); // 设置背景网格线颜色
plot.setRangeGridlinePaint(Color.GRAY);
plot.setNoDataMessage("没有数据");// 没有数据时显示的文字说明。
//设置字体
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
piePlot.setLabelFont(labelFont);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(labelFont);// 轴标题
domainAxis.setTickLabelFont(labelFont);// 轴数值
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的
// Lable
// 45度倾斜
// 设置距离图片左端距离
domainAxis.setLowerMargin(0.0);
// 设置距离图片右端距离
domainAxis.setUpperMargin(0.0);
// 数据轴属性部分
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true); // 自动生成
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI / 2.0);
rangeAxis.setAutoRange(false);
// 数据渲染部分 主要是对折线做操作
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
.getRenderer();
renderer.setBaseItemLabelsVisible(true);
renderer.setSeriesPaint(0, Color.black); // 设置折线的颜色
renderer.setBaseShapesFilled(true);
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
/*
* 这里的StandardCategoryItemLabelGenerator()我想强调下:当时这个地*方被搅得头很晕,Standard
* **ItemLabelGenerator是通用的 因为我创建*的是CategoryPlot 所以很多设置都是Category相关
* 而XYPlot 对应的则是 : StandardXYItemLabelGenerator
*/
// 对于编程人员 这种根据一种类型方法联想到其他类型相似方法的思
// 想是必须有的吧!目前只能慢慢培养了。。
renderer.setBaseItemLabelFont(new Font("Dialog", 1, 14)); // 设置提示折点数据形状
plot.setRenderer(renderer);
// 区域渲染部分
/*double lowpress = 24.5;
double uperpress = 80; // 设定正常血糖值的范围
IntervalMarker inter = new IntervalMarker(lowpress, uperpress);
inter.setLabelOffsetType(LengthAdjustmentType.EXPAND); // 范围调整——扩张
inter.setPaint(Color.LIGHT_GRAY);// 域顏色
inter.setLabelFont(new Font("SansSerif", 41, 14));
inter.setLabelPaint(Color.RED);
inter.setLabel("正常血糖值范围"); // 设定区域说明文字
plot.addRangeMarker(inter, Layer.BACKGROUND); // 添加mark到图形
// BACKGROUND使得数据折线在区域的前端
*/
// 创建文件输出流
File fos_jpg = new File("E://bloodSugarChart.jpg ");
// 输出到哪个输出流
ChartUtilities.saveChartAsJPEG(fos_jpg, chart, // 统计图表对象
700, // 宽
500 // 高
);
} catch (IOException e) {
e.printStackTrace();
}
}
// 测试类
public static void main(String[] args) {
Jfreechart my = new Jfreechart();
}
}
Jfreechart
最新推荐文章于 2020-03-22 12:15:13 发布