jfreechart实现柱状图(rowkey coloumkey)

package my;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
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.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * jfreechart排序柱状图.
 */
public class MyBarPic extends ApplicationFrame {

    /**
     *
     * 构造函数,进行初始化操作
     *
     * @param title  the frame title.
     */
    public MyBarPic(String title) {
        super(title);
        CategoryDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
        saveAsFile(chart, "D://图//第一.jpg", 430, 200);
    }

   
/**
 * 生成数据集
 *
 * @return
 */
private static CategoryDataset createDataset() {

 /**
  *
  * 数据集数据存放对象
  *
  * @author newlife111
  *
  */
 class ThreeData  implements Comparator{
  String rowKey;//行标签
  String colKey;//列标签
  double value;//值
  
  ThreeData(){
  }
  
  ThreeData(double value, String rowKey, String colKey){
   this.value = value;
   this.rowKey = rowKey;
   this.colKey = colKey;
  }
  
  /*
   * 比较的算法
   */
  public int compare(Object obj1, Object obj2) {
   double valueTmp = ((ThreeData)obj1).value - ((ThreeData)obj2).value;
   return valueTmp > 0 ? -1 : (valueTmp < 0 ? 1 : 0);
  }
  
 }

 /*
  * 存放数据
  */
 List list = new ArrayList();
 list.add(new ThreeData(100.00d, "xx", "苹/n果"));
 list.add(new ThreeData(200.00d, "xx", "葡/n萄"));
 list.add(new ThreeData(50.00d, "xx", "雪/n梨"));
 list.add(new ThreeData(24.00d, "xx", "香/n蕉"));
 list.add(new ThreeData(65.00d, "xx", "橘/n子"));
 list.add(new ThreeData(125.00d, "xx", "柑/n橘"));
 list.add(new ThreeData(198.00d, "xx", "柚/n子"));
 list.add(new ThreeData(160.00d, "xx", "草/n莓"));
 //进行排序
 Collections.sort(list,new ThreeData());
 
 /*
  * 添加数据进数据集
  */
 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 Iterator iter = list.iterator();
 while(iter.hasNext()) {
  ThreeData threeDataTmp = (ThreeData)iter.next();
  dataset.addValue(threeDataTmp.value, threeDataTmp.rowKey, threeDataTmp.colKey);
 }
 return dataset;
}

    /**
     * 生成图片.
     *
     * @param dataset  数据集.
     *
     * @return chart 图片对象
     */
    private static JFreeChart createChart(CategoryDataset dataset) {
        JFreeChart chart = ChartFactory.createBarChart(
            "xxx",       // 图片标题
            "xxx",               // 横轴标题
            "xxx",                  // 纵轴标题
            dataset,                  // 数据集
            PlotOrientation.VERTICAL, // 图形方向
            true,                     // 是否legend
            true,                     // 图形提示
            false                     // 图形url
        );
        //设置图片背景色
        chart.setBackgroundPaint(Color.white);
        //获得数据区对象
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        //设置图形背景色
        plot.setBackgroundPaint(Color.white);
        //横轴网格显示
        plot.setDomainGridlinesVisible(true);
        //横轴网格颜色
        plot.setRangeGridlinePaint(Color.black);
       
        /*
         * 图形相关设置
         */       
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setBaseItemLabelFont(new Font("黑体", Font.BOLD, 12));
        renderer.setBaseLegendTextFont(new Font("Simsun", 0, 10));
        renderer.setBaseItemLabelsVisible(true);
        renderer.setMaximumBarWidth(0.05f);
        renderer.setDrawBarOutline(false);
        renderer.setShadowVisible(false);
       
        /*
         * 纵轴相关设置
         */
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setLabelFont(new Font("Simsun", 0, 12));

        /*
         * 图形1颜色设置
         */
        GradientPaint gp0 = new GradientPaint(0.5f, 0.5f, new Color(100,100,255),
                0.0f, 0.0f, new Color(0, 0, 64));
        renderer.setSeriesPaint(0, gp0);
       
        /*
         * 横轴相关设置
         */
        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setLabelFont(new Font("Simsun", Font.BOLD, 12));
        //Font.BOLD字符加粗可使标签显示清楚些
        domainAxis.setTickLabelFont(new Font("Simsun", Font.BOLD, 8));
        domainAxis.setLowerMargin(0.03f);
        //这里设置横轴标签可显示行数,在数据集的横轴字符串上用/n可实现标签字符垂直
        domainAxis.setMaximumCategoryLabelLines(255);
        domainAxis.setCategoryLabelPositionOffset(3);
        return chart;
    }

    /**
     * 程序入口
     *
     * @param args 
     */
    public static void main(String[] args) {
        MyBarPic demo = new MyBarPic("FirstPic");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
   
 /**
  *
  * 保存为文件
  *
  * @param chart 图片对象
  * @param outputPath 保存路径
  * @param weight 宽
  * @param height 高
  */
 public static void saveAsFile(JFreeChart chart, String outputPath,
   int weight, int height) {
  FileOutputStream out = null;
  try {
   File outFile = new File(outputPath);
   if (!outFile.getParentFile().exists()) {
    outFile.getParentFile().mkdirs();
   }
   out = new FileOutputStream(outputPath);
   //保存为JPEG
   ChartUtilities.writeChartAsJPEG(out,chart,800,600);
   out.flush();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (out != null) {
    try {
     out.close();
    } catch (IOException e) {
     
    }
   }
  }
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值