关于使用jfreechart和cewolf的使用关键
2004-12-02 16:43
447 查看
1.生成一个不同类型的数据集
Charts and datasets
To create a chart using JFreeChart, you must create a Dataset, which you then use to create a JFreeChart. A Dataset contains the data that displays in the chart. JFreeChart features many different Dataset objects, which you can use to create assorted types of charts. Once you create a Dataset, you next create the actual chart. JFreeChart uses an object appropriately named JFreeChart to represent charts. You create JFreeChart objects from Dataset objects with the ChartFactory class. In the following examples, we will create pie, XY, and bar charts along with their corresponding Dataset objects.
Pie chart
A pie chart is created from a PieDataset. The following example creates a PieDataset using the DefaultPieDataset class, adds two values via the setValue() method, and then creates a pie chart with the ChartFactory's createPieChart() method. This example will create a pie chart with the title "Sample Pie Chart," a legend, and two slices: JavaWorld with 75 percent of the pie, and Other with the other 25 percent:
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("JavaWorld", new Integer(75));
pieDataset.setValue("Other", new Integer(25));
JFreeChart chart = ChartFactory.createPieChart
("Sample Pie Chart", // Title
pieDataset, // Dataset
true // Show legend
);
XY chart
An XYDataset can create area, line, and step XY charts. The following example creates an XYDataset from a series of data containing three XY points. Next, ChartFactory's createAreaXYChart() method creates an area XY chart. In addition to parameters for title, dataset, and legend, createAreaXYChart() takes in the labels for the X and Y axes:
XYSeries series = new XYSeries("Average Size");
series.add(20.0, 10.0);
series.add(40.0, 20.0);
series.add(70.0, 50.0);
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createAreaXYChart
("Sample XY Chart", // Title
"Height", // X-Axis label
"Weight", // Y-Axis label
xyDataset, // Dataset
true // Show legend
);
Bar chart
A CategoryDataset can create numerous different charts, including horizontal and vertical bar charts. The following example creates a CatagoryDataset with two series of data and two categories, and then creates a 3D vertical bar chart from this dataset. This example creates a chart that compares the sales growth in two quarters over two years:
String[] seriesNames = new String[] {"2001", "2002"};
String[] categoryNames = new String[] {"First Quater",
"Second Quater"};
Number[][] categoryData = new Integer[][] {{new Integer(20),
new Integer(35)},
{new Integer(40),
new Integer(60)}
};
CategoryDataset categoryDataset = new DefaultCategoryDataset
(seriesNames,
categoryNames,
categoryData);
JFreeChart chart = ChartFactory.createVerticalBarChart3D
("Sample Category Chart", // Title
"Quarters", // X-Axis label
"Sales", // Y-Axis label
categoryDataset, // Dataset
true // Show legend
);
使用cewolf标签库进行图形开发,只需要实现produceDataset
http://cewolf.sourceforge.net/
DatasetProducer xyData = new DatasetProducer() {
public Object produceDataset(Map params) {
XYSeries xys = new XYSeries("Example XY Dataset");
double last = 0.0;
for (int i = -50; i <= 50; i++) {
double y = last + ((Math.random() * 100) - 50.0);
xys.add((double) i, y);
last = y;
}
return new XYSeriesCollection(xys);
}
public String getProducerId() {
return "XYDataProducer";
}
public boolean hasExpired(Map params, Date since) {
return false;
}
};
实现鼠标的点击和提示
LinkGenerator xyLG = new XYItemLinkGenerator() {
public String generateLink(Object data, int series, int item) {
return "#Series " + series;
}
};
pageContext.setAttribute("xyLinkGenerator", xyLG);
CategoryToolTipGenerator catTG = new CategoryToolTipGenerator() {
public String generateToolTip(CategoryDataset dataset, int series, int index) {
return String.valueOf(dataset.getValue(series, index));
}
};
pageContext.setAttribute("categoryToolTipGenerator", catTG);
PieToolTipGenerator pieTG = new PieToolTipGenerator() {
public String generateToolTip(PieDataset dataset, Comparable section, int index) {
return String.valueOf(index);
}
};
Charts and datasets
To create a chart using JFreeChart, you must create a Dataset, which you then use to create a JFreeChart. A Dataset contains the data that displays in the chart. JFreeChart features many different Dataset objects, which you can use to create assorted types of charts. Once you create a Dataset, you next create the actual chart. JFreeChart uses an object appropriately named JFreeChart to represent charts. You create JFreeChart objects from Dataset objects with the ChartFactory class. In the following examples, we will create pie, XY, and bar charts along with their corresponding Dataset objects.
Pie chart
A pie chart is created from a PieDataset. The following example creates a PieDataset using the DefaultPieDataset class, adds two values via the setValue() method, and then creates a pie chart with the ChartFactory's createPieChart() method. This example will create a pie chart with the title "Sample Pie Chart," a legend, and two slices: JavaWorld with 75 percent of the pie, and Other with the other 25 percent:
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("JavaWorld", new Integer(75));
pieDataset.setValue("Other", new Integer(25));
JFreeChart chart = ChartFactory.createPieChart
("Sample Pie Chart", // Title
pieDataset, // Dataset
true // Show legend
);
XY chart
An XYDataset can create area, line, and step XY charts. The following example creates an XYDataset from a series of data containing three XY points. Next, ChartFactory's createAreaXYChart() method creates an area XY chart. In addition to parameters for title, dataset, and legend, createAreaXYChart() takes in the labels for the X and Y axes:
XYSeries series = new XYSeries("Average Size");
series.add(20.0, 10.0);
series.add(40.0, 20.0);
series.add(70.0, 50.0);
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createAreaXYChart
("Sample XY Chart", // Title
"Height", // X-Axis label
"Weight", // Y-Axis label
xyDataset, // Dataset
true // Show legend
);
Bar chart
A CategoryDataset can create numerous different charts, including horizontal and vertical bar charts. The following example creates a CatagoryDataset with two series of data and two categories, and then creates a 3D vertical bar chart from this dataset. This example creates a chart that compares the sales growth in two quarters over two years:
String[] seriesNames = new String[] {"2001", "2002"};
String[] categoryNames = new String[] {"First Quater",
"Second Quater"};
Number[][] categoryData = new Integer[][] {{new Integer(20),
new Integer(35)},
{new Integer(40),
new Integer(60)}
};
CategoryDataset categoryDataset = new DefaultCategoryDataset
(seriesNames,
categoryNames,
categoryData);
JFreeChart chart = ChartFactory.createVerticalBarChart3D
("Sample Category Chart", // Title
"Quarters", // X-Axis label
"Sales", // Y-Axis label
categoryDataset, // Dataset
true // Show legend
);
使用cewolf标签库进行图形开发,只需要实现produceDataset
http://cewolf.sourceforge.net/
DatasetProducer xyData = new DatasetProducer() {
public Object produceDataset(Map params) {
XYSeries xys = new XYSeries("Example XY Dataset");
double last = 0.0;
for (int i = -50; i <= 50; i++) {
double y = last + ((Math.random() * 100) - 50.0);
xys.add((double) i, y);
last = y;
}
return new XYSeriesCollection(xys);
}
public String getProducerId() {
return "XYDataProducer";
}
public boolean hasExpired(Map params, Date since) {
return false;
}
};
实现鼠标的点击和提示
LinkGenerator xyLG = new XYItemLinkGenerator() {
public String generateLink(Object data, int series, int item) {
return "#Series " + series;
}
};
pageContext.setAttribute("xyLinkGenerator", xyLG);
CategoryToolTipGenerator catTG = new CategoryToolTipGenerator() {
public String generateToolTip(CategoryDataset dataset, int series, int index) {
return String.valueOf(dataset.getValue(series, index));
}
};
pageContext.setAttribute("categoryToolTipGenerator", catTG);
PieToolTipGenerator pieTG = new PieToolTipGenerator() {
public String generateToolTip(PieDataset dataset, Comparable section, int index) {
return String.valueOf(index);
}
};
相关文章推荐
- VC与Delphi之间动态链接库互相调用
- 用Ole Automation实现Delphi和AutoCad之间的数据交换
- c#接简单数据库操作类
- 如何用VB获得机器的MAC地址
- Java测试规范(引用)
- Java面试题目
- 一个非常不错的业务规则管理器
- delphi中的时间操作技术(1)
- delphi中的时间操作技术(2)
- VB五子棋问题,请高手赐教
- IP地址的隐藏(delphi实现 )
- 解析IP地址为主机域名
- 将某一主机域名解析为IP地址
- 有关TrueDBGrid的问题向各位大虾请教
- 紧急求教
- 使用C#开发COM+组件
- 看到一篇好文章与大家分享!!!!!
- Netscaler数据索引String Map与Pattern Set的区别
- 2014年软考数据库系统工程师知识点讲解(1)