加入jfreechart-1.0.13.jar,jcommon-1.0.16.jar
一、饼状图代码package tf; import java.awt.Color; import java.awt.Font; import java.io.FileOutputStream; import java.text.DecimalFormat; import java.text.NumberFormat; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.plot.PiePlot3D; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.PieDataset; public class PieChartTest { public static void main(String[] args) { PieDataset dataset = getDataSet(); JFreeChart chart = ChartFactory.createPieChart3D(null, dataset, true, true, false); PiePlot3D plot = (PiePlot3D) chart.getPlot(); // 图片中显示百分比:默认方式 // plot.setLabelGenerator(new // StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT)); // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位 plot.setLabelGenerator(new StandardPieSectionLabelGenerator( "{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例 plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( "{0}={1}({2})")); // 设置背景色为白色 chart.setBackgroundPaint(Color.white); // 指定图片的透明度(0.0-1.0) plot.setForegroundAlpha(1.0f); // 指定显示的饼图上圆形(false)还椭圆形(true) plot.setCircular(true); /*------设置图片标题的字体-----------*/ Font font = new Font("黑体", Font.CENTER_BASELINE, 20); TextTitle title = new TextTitle("项目状态分布"); title.setFont(font); chart.setTitle(title); /*------设置图片显示的字体-----------*/ plot.setLabelFont(new Font("黑体", Font.PLAIN, 12)); /*------设置图片底部的字体-----------*/ chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("D:\\项目状态分布.jpg"); ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 640, 480, null); fos_jpg.close(); } catch (Exception e) { } } private static PieDataset getDataSet() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("市场前期", new Double(10)); dataset.setValue("立项", new Double(15)); dataset.setValue("计划", new Double(10)); dataset.setValue("需求与设计", new Double(10)); dataset.setValue("执行控制", new Double(35)); dataset.setValue("收尾", new Double(10)); dataset.setValue("运维", new Double(10)); return dataset; } }二、柱状图代码
package tf; import java.awt.Color; import java.awt.Font; import java.awt.GradientPaint; import java.io.FileOutputStream; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.DefaultCategoryDataset; public class BarChartTest { public static void main(String[] args) { DefaultCategoryDataset dataset = getDataSet(); JFreeChart chart = ChartFactory.createBarChart(null, "网站", "数值", dataset, PlotOrientation.VERTICAL, true, true, false); /*------设置图片标题的字体-----------*/ Font font = new Font("黑体", Font.CENTER_BASELINE, 20); TextTitle title = new TextTitle("网站信息统计"); title.setFont(font); chart.setTitle(title); chart.setBackgroundPaint(Color.white); CategoryPlot plot = (CategoryPlot) chart.getPlot(); CategoryAxis domainAxis = plot.getDomainAxis(); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setDrawBarOutline(false); /*------设置X轴坐标上的文字-----------*/ domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); /*------设置X轴的标题文字------------*/ domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); /*------设置Y轴坐标上的文字-----------*/ rangeAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); /*------设置Y轴的标题文字------------*/ rangeAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12)); /*------设置底部标题文字-----------*/ chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color(0, 0, 64)); GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color(0, 64, 0)); GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(64, 0, 0)); renderer.setSeriesPaint(0, gp0); renderer.setSeriesPaint(1, gp1); renderer.setSeriesPaint(2, gp2); domainAxis.setCategoryLabelPositions(CategoryLabelPositions .createUpRotationLabelPositions(Math.PI / 6.0)); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("D:\\柱状图.jpg"); ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 640, 480, null); fos_jpg.close(); } catch (Exception e) { } } private static DefaultCategoryDataset getDataSet() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); String s = "每日PV"; String s1 = "每日IP数"; String s2 = "注册用户数"; String s3 = "A网站"; String s4 = "B网站"; String s5 = "C网站"; String s6 = "D网站"; String s7 = "E网站"; dataset.addValue(1.0D, s, s3); dataset.addValue(4D, s, s4); dataset.addValue(3D, s, s5); dataset.addValue(5D, s, s6); dataset.addValue(5D, s, s7); dataset.addValue(5D, s1, s3); dataset.addValue(7D, s1, s4); dataset.addValue(6D, s1, s5); dataset.addValue(8D, s1, s6); dataset.addValue(4D, s1, s7); dataset.addValue(4D, s2, s3); dataset.addValue(3D, s2, s4); dataset.addValue(2D, s2, s5); dataset.addValue(3D, s2, s6); dataset.addValue(6D, s2, s7); return dataset; } }