生成柱状图:
package img.jfreechart.test; import java.io.FileOutputStream; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; public class TestBarChart { public static void main(String[] args) throws Exception { CategoryDataset dataset = getDataSet(); JFreeChart chart = ChartFactory.createBarChart3D( "这里是标题", // 北京上海广州的常见水果的产量柱状图 "这里是x坐标的标签", "这里是y坐标的标签", dataset, PlotOrientation.VERTICAL,//图表方向,水平或者垂直 true, //是否显示图例,对于简单的柱状图必须是false false,//是否生成工具 false);//是否生成url链接 FileOutputStream fos = new FileOutputStream("d:\\test.jpg"); ChartUtilities.writeChartAsJPEG(fos, chart, 400, 300); fos.close(); System.out.println("done..."); } public static CategoryDataset getDataSet() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "苹果"); dataset.addValue(100, "上海", "苹果"); dataset.addValue(100, "广州", "苹果"); dataset.addValue(200, "北京", "梨子"); dataset.addValue(200, "上海", "梨子"); dataset.addValue(200, "广州", "梨子"); dataset.addValue(300, "北京", "葡萄"); dataset.addValue(300, "上海", "葡萄"); dataset.addValue(300, "广州", "葡萄"); dataset.addValue(400, "北京", "香蕉"); dataset.addValue(400, "上海", "香蕉"); dataset.addValue(400, "广州", "香蕉"); dataset.addValue(500, "北京", "荔枝"); dataset.addValue(500, "上海", "荔枝"); dataset.addValue(500, "广州", "荔枝"); return dataset; } }
?去掉3D之后,可以生成2D的图像:
?
?
饼状图:
package img.jfreechart.test; import java.io.FileOutputStream; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; public class TestPieChart { public static void main(String[] args) throws Exception{ DefaultPieDataset dataset = getDataSet(); JFreeChart chart = ChartFactory.createPieChart( "这里是标题", dataset, true, //是否显示图例 false,//是否生成工具 false);//是否生成url链接 FileOutputStream fos = new FileOutputStream("d:\\testpie2.png"); ChartUtilities.writeChartAsPNG(fos, chart, 400, 300); fos.close(); System.out.println("done..."); } public static DefaultPieDataset getDataSet() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("北京", 100); dataset.setValue("上海", 400); dataset.setValue("重庆", 500); dataset.setValue("广州", 200); dataset.setValue("深圳", 700); dataset.setValue("南京", 800); return dataset; } }
?同样的,也有2d和3d两种形式的
参考:http://www.ibm.com/developerworks/cn/java/l-jfreechart/
?
?