通过JFreeChart来显示以下的XML文件:
<chart caption="Sales - 2012 v 2013" numberprefix="$" plotgradientcolor="" bgcolor="FFFFFF" showalternatehgridcolor="0" divlinecolor="CCCCCC" showvalues="0" showcanvasborder="0" canvasborderalpha="0" canvasbordercolor="CCCCCC" canvasborderthickness="1" yaxismaxvalue="30000" captionpadding="30" linethickness="3" yaxisvaluespadding="15" legendshadow="0" legendborderalpha="0" palettecolors="#f8bd19,#008ee4,#33bdda,#e44a00,#6baa01,#583e78" showborder="0">
<categories>
<category label="Jan" />
<category label="Feb" />
<category label="Mar" />
<category label="Apr" />
<category label="May" />
<category label="Jun" />
<category label="Jul" />
<category label="Aug" />
<category label="Sep" />
<category label="Oct" />
<category label="Nov" />
<category label="Dec" />
</categories>
<dataset seriesname="2013">
<set value="22400" />
<set value="24800" />
<set value="21800" />
<set value="21800" />
<set value="24600" />
<set value="27600" />
<set value="26800" />
<set value="27700" />
<set value="23700" />
<set value="25900" />
<set value="26800" />
<set value="24800" />
</dataset>
<dataset seriesname="2012">
<set value="10000" />
<set value="11500" />
<set value="12500" />
<set value="15000" />
<set value="16000" />
<set value="17600" />
<set value="18800" />
<set value="19700" />
<set value="21700" />
<set value="21900" />
<set value="22900" />
<set value="20800" />
</dataset>
</chart>
实现代码如下:
package com.oracle;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.DrawingSupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
//参见http://xuehu2009.iteye.com/blog/480387
public class Test3 extends ApplicationFrame {
private static List<String> month = new ArrayList<String>();
private static List<Integer> tongJi_2012 = new ArrayList<Integer>();
private static List<Integer> tongJi_2013 = new ArrayList<Integer>();
/**
* 画板的构造方法
*
* @param title
*/
public Test3(final String title) {
super(title);
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* 新建一个数据集合
*
* @return 返回一个数据集合
*/
private CategoryDataset createDataset() {
readXML();
DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
DefaultCategoryDataset dataset = null;
for (int i = 0; i < month.size(); i++) {
// 用法参见:linedataset.addValue(value, rowKey, columnKey);
linedataset.addValue(tongJi_2012.get(i), "2012", month.get(i));
linedataset.addValue(tongJi_2013.get(i), "2013", month.get(i));
linedataset.addValue(3000, "2014", month.get(i));
linedataset.addValue(2000, "2015", month.get(i));
}
dataset = linedataset;
return dataset;
}
/**
* 通过上面函数的得到的数据集合创建一个图表
*
* @param dataset
* @return
*/
private JFreeChart createChart(final CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createLineChart(
"JFreechart Test", // chart title
"Type", // 横坐标意义
"Value", // 纵坐标意义
dataset, // 数据集合
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
// final StandardLegend legend = (StandardLegend) chart.getLegend();
// legend.setDisplaySeriesShapes(true);
final Shape[] shapes = new Shape[3];
int[] xpoints;
int[] ypoints;
// right-pointing triangle
xpoints = new int[] { -3, 3, -3 };
ypoints = new int[] { -3, 0, 3 };
shapes[0] = new Polygon(xpoints, ypoints, 3);
// vertical rectangle
shapes[1] = new Rectangle2D.Double(-2, -3, 3, 6);
// left-pointing triangle
xpoints = new int[] { -3, 3, 3 };
ypoints = new int[] { 0, -3, 3 };
shapes[2] = new Polygon(xpoints, ypoints, 3);
final DrawingSupplier supplier = new DefaultDrawingSupplier(
DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE, shapes);
final CategoryPlot plot = chart.getCategoryPlot();
plot.setDrawingSupplier(supplier);
chart.setBackgroundPaint(Color.white);
// set the stroke for each series...
// 设置每个线条的样式,本例中有4条线
plot.getRenderer().setSeriesStroke(
0,
new BasicStroke(2.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND, 1.0f,
new float[] { 10.0f, 6.0f }, // 虚线的横纵长度
0.0f));
plot.getRenderer().setSeriesStroke(
1,
new BasicStroke(2.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND, 1.0f,
new float[] { 6.0f, 6.0f }, 0.0f));
plot.getRenderer().setSeriesStroke(
2,
new BasicStroke(2.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND, 1.0f,
new float[] { 2.0f, 6.0f }, 0.0f));
plot.getRenderer().setSeriesStroke(
3,
new BasicStroke(2.0f, BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_ROUND, 1.0f,
new float[] { 10.0f, 6.0f }, 0.0f));
// customise the renderer...
final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
.getRenderer();
// renderer.setDrawShapes(true);
renderer.setItemLabelsVisible(true);
// renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
// customise the range axis...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(false);
rangeAxis.setUpperMargin(0.12);
return chart;
}
/**
* 读XML文件
*/
public void readXML() {
SAXReader saxReader = new SAXReader();
try {
Document doc = saxReader.read(new File("123.xml"));
Element element = doc.getRootElement();
List list = element.elements();
for (int i = 0; i < list.size(); i++) {
Element elementz = (Element) list.get(i);
List list1 = elementz.elements();
if (elementz.getName().equals("categories")) {
for (int j = 0; j < list1.size(); j++) {
Element elementz2 = (Element) list1.get(j);
month.add(elementz2.attributeValue("label"));
}
} else if (elementz.getName().equals("dataset")) {
if (elementz.attributeValue("seriesname").equals("2013")) {
for (int j = 0; j < list1.size(); j++) {
Element elementz2 = (Element) list1.get(j);
tongJi_2013.add(Integer.parseInt(elementz2
.attributeValue("value")));
}
} else if (elementz.attributeValue("seriesname").equals(
"2012")) {
for (int j = 0; j < list1.size(); j++) {
Element elementz2 = (Element) list1.get(j);
tongJi_2012.add(Integer.parseInt(elementz2
.attributeValue("value")));
}
}
}
}
for (int i = 0; i < month.size(); i++) {
System.out.print(month.get(i) + " ");
System.out.print(tongJi_2012.get(i) + " ");
System.out.println(tongJi_2013.get(i));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
final Test3 demo = new Test3("JFreeChart Test");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
结果如下图所示: