JFreeChart折线图的生成方法

2025-05-29 0 38

jfreechart是java平台上的一个开放的图表绘制类库。它完全使用java语言编写,是为applications, applets, servlets 以及jsp等使用所设计。jfreechart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(gantt charts)等等多种图表,并且可以产生png和jpeg格式的输出,还可以与pdf和excel关联。

折线图的实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104
package com.sprite.test;

import java.io.file;

import java.io.filenotfoundexception;

import java.io.fileoutputstream;

import java.io.ioexception;

import org.jfree.chart.chartfactory;

import org.jfree.chart.chartutilities;

import org.jfree.chart.jfreechart;

import org.jfree.chart.labels.standardcategoryitemlabelgenerator;

import org.jfree.chart.plot.categoryplot;

import org.jfree.chart.plot.plotorientation;

import org.jfree.chart.renderer.category.lineandshaperenderer;

import org.jfree.data.category.categorydataset;

import org.jfree.data.general.datasetutilities;

//jfreechart line chart(折线图)

public class testjfreechart {

/**

* 创建jfreechart line chart(折线图)

*/

public static void main(string[] args) {

// 步骤1:创建categorydataset对象(准备数据)

categorydataset dataset = createdataset();

// 步骤2:根据dataset 生成jfreechart对象,以及做相应的设置

jfreechart freechart = createchart(dataset);

// 步骤3:将jfreechart对象输出到文件,servlet输出流等

saveasfile(freechart, "e:\\\\line.jpg", 600, 400);

}

// 保存为文件

public static void saveasfile(jfreechart chart, string outputpath,

int weight, int height) {

fileoutputstream out = null;

try {

file outfile = new file(outputpath);

if (!outfile.getparentfile().exists()) {

outfile.getparentfile().mkdirs();

}

out = new fileoutputstream(outputpath);

// 保存为png

// chartutilities.writechartaspng(out, chart, 600, 400);

// 保存为jpeg

chartutilities.writechartasjpeg(out, chart, 600, 400);

out.flush();

} catch (filenotfoundexception e) {

e.printstacktrace();

} catch (ioexception e) {

e.printstacktrace();

} finally {

if (out != null) {

try {

out.close();

} catch (ioexception e) {

// do nothing

}

}

}

}

// 根据categorydataset创建jfreechart对象

public static jfreechart createchart(categorydataset categorydataset) {

// 创建jfreechart对象:chartfactory.createlinechart

jfreechart jfreechart = chartfactory.createlinechart("不同类别按小时计算拆线图", // 标题

"年分", // categoryaxislabel (category轴,横轴,x轴标签)

"数量", // valueaxislabel(value轴,纵轴,y轴的标签)

categorydataset, // dataset

plotorientation.vertical, true, // legend

false, // tooltips

false); // urls

// 使用categoryplot设置各种参数。以下设置可以省略。

categoryplot plot = (categoryplot)jfreechart.getplot();

// 背景色 透明度

plot.setbackgroundalpha(0.5f);

// 前景色 透明度

plot.setforegroundalpha(0.5f);

// 其他设置 参考 categoryplot类

lineandshaperenderer renderer = (lineandshaperenderer)plot.getrenderer();

renderer.setbaseshapesvisible(true); // series 点(即数据点)可见

renderer.setbaselinesvisible(true); // series 点(即数据点)间有连线可见

renderer.setuseseriesoffset(true); // 设置偏移量

renderer.setbaseitemlabelgenerator(new standardcategoryitemlabelgenerator());

renderer.setbaseitemlabelsvisible(true);

return jfreechart;

}

/**

* 创建categorydataset对象

*

*/

public static categorydataset createdataset() {

string[] rowkeys = {"a平台"};

string[] colkeys = {"0:00", "1:00", "2:00", "7:00", "8:00", "9:00",

"10:00", "11:00", "12:00", "13:00", "16:00", "20:00", "21:00",

"23:00"};

double[][] data = {{4, 3, 1, 1, 1, 1, 2, 2, 2, 1, 8, 2, 1, 1},};

// 或者使用类似以下代码

// defaultcategorydataset categorydataset = new

// defaultcategorydataset();

// categorydataset.addvalue(10, "rowkey", "colkey");

return datasetutilities.createcategorydataset(rowkeys, colkeys, data);

}

}

生成效果图:

JFreeChart折线图的生成方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/seven_tao/article/details/8508775

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 JFreeChart折线图的生成方法 https://www.kuaiidc.com/111369.html

相关文章

发表评论
暂无评论