JFreeChart_BarChart

package com.jfreechart; import java.awt.Color; import java.awt.Font; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; 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.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.IntervalMarker; 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; import org.jfree.ui.Layer; import org.jfree.ui.RectangleAnchor; import org.jfree.ui.TextAnchor; /** * * @ClassName: JFreeChart_BarChart * @author xialong * @date Jan 25, 2011 6:36:12 PM * @Description: * JFreeChart生成柱状图 * */ @SuppressWarnings("deprecation") public class JFreeChart_BarChart { public static void main(String[] arg){ try { print(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 输出图片 * @throws IOException */ public static void print() throws IOException{ FileOutputStream fos = new FileOutputStream("D://jfreechart//barChart"+System.currentTimeMillis()+".jpg"); ChartUtilities.writeChartAsJPEG(fos,//输出到那个流, 1, //图片质量,0~1 createChart(), //图表对象 900,//宽 600,//高 null//ChartRenderingInfo信息 ); fos.close(); } /** * 产生JFreeChart对象 * @return */ private static JFreeChart createChart(){ JFreeChart chart = null; //平面 // chart = ChartFactory.createBarChart("", "X轴", "Y轴", createDateSet(), // PlotOrientation.VERTICAL, true, false, false); //3D chart = ChartFactory.createBarChart3D("","X轴", "Y轴", createDateSet(), PlotOrientation.VERTICAL, true, false, false); TextTitle title = new TextTitle("测试柱状图", new Font("宋体", Font.BOLD, 20)); // 解决曲线图片标题中文乱码问题 chart.setTitle(title); //获得 plot:CategoryPlot!! CategoryPlot barplot = chart.getCategoryPlot(); //取得x轴 CategoryAxis domainAxis = barplot.getDomainAxis(); setDomainAxis(domainAxis); //取得Y轴 NumberAxis numberAxis = (NumberAxis)barplot.getRangeAxis(); setNumberAxis(numberAxis); //获得renderer 注意这里是下嗍造型到BarRenderer!! BarRenderer barrenderer = (BarRenderer)barplot.getRenderer(); //显示每个柱的数值,并修改该数值的字体属性; barrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); barrenderer.setBaseItemLabelFont(new Font("黑体",Font.BOLD,12)); barrenderer.setBaseItemLabelsVisible(true); /* // Bar的外轮廓线不画 barrenderer.setDrawBarOutline(true); //设定特定颜色 GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue,0.0F, 0.0F, new Color(0, 0, 64)); GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green,0.0F, 0.0F, new Color(0, 64, 0)); GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red,0.0F, 0.0F, new Color(64, 0, 0)); //给series1 Bar设定上面定义的颜色 barrenderer.setSeriesPaint(0, gradientpaint); //给series2 Bar 设定上面定义的颜色 barrenderer.setSeriesPaint(1, gradientpaint1); //给series3 Bar 设定上面定义的颜色 barrenderer.setSeriesPaint(2, gradientpaint2); */ //增加一个标记块 //标记区间 IntervalMarker intervalmarker = new IntervalMarker(11*0.6, 11.5D); //标记显示名称 intervalmarker.setLabel("标记"); //名称字体 intervalmarker.setLabelFont(new Font("黑体", Font.PLAIN, 11)); //名称显示位子 intervalmarker.setLabelAnchor(RectangleAnchor.TOP_LEFT); //设置标记块的位置 intervalmarker.setLabelTextAnchor(TextAnchor.CENTER_LEFT); //标记块颜色 intervalmarker.setPaint(new Color(222, 222, 255, 128)); //将标记块注册到显示区域,并设置样式 barplot.addRangeMarker(intervalmarker, Layer.BACKGROUND); return chart; } /** * 设置X轴 * @param domainAxis */ private static void setDomainAxis(CategoryAxis domainAxis){ //设置x轴标题字体 domainAxis.setLabelFont(new Font("宋体",Font.BOLD,12)); // 解决x轴坐标上中文乱码 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); //x轴坐标上倾斜45°显示 domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); } /** * 设置Y轴 * @param numberAxis */ private static void setNumberAxis(NumberAxis numberaxis){ //设置Y间隔 //numberaxis.setTickUnit(new NumberTickUnit(2)); // 解决Y轴标题中文乱码 numberaxis.setLabelFont(new Font("sans-serif", Font.PLAIN, 14)); } /** * 产生数据源 * @return */ private static DefaultCategoryDataset createDateSet(){ DefaultCategoryDataset dataset = new DefaultCategoryDataset(); int i = 0; Random rand = new Random(); while (i < 3) { for (int j = 1; j < 10; j++) { int value=rand.nextInt(10)+2; dataset.addValue(value, "series" + i, "x轴"+j); } i++; } return dataset; } }

 

输出图片如下:

JFreeChart_BarChart_第1张图片

你可能感兴趣的:(Date,String,jfreechart,Random,dataset,plot)