利用JFreeChart生成简单柱状图(Java)

package barchartdemo1;  
      
    import <a href="http://lib.csdn.net/base/javaee" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.awt.Font;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    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.NumberAxis;  
    import org.jfree.chart.plot.CategoryPlot;  
    import org.jfree.chart.plot.PlotOrientation;  
    import org.jfree.data.category.CategoryDataset;  
    import org.jfree.data.category.DefaultCategoryDataset;  
      
    /** 
     * 
     * @author Administrator 
     */  
    public class Main {  
      
        /** 
         * @param args the command line arguments 
         */  
        public static void main(String[] args) throws IOException {  
            CategoryDataset ds = getDataSet();  
            JFreeChart chart = ChartFactory.createBarChart3D(  
                    "水果产量图", //图表标题  
                    "水果", //目录轴的显示标签  
                    "产量", //数值轴的显示标签  
                    ds, //数据集  
                    PlotOrientation.VERTICAL, //图表方向  
                    true, //是否显示图例,对于简单的柱状图必须为false  
                    false, //是否生成提示工具  
                    false);         //是否生成url链接  
      
            CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();  
      
            NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();  
      
            CategoryAxis domainAxis = categoryplot.getDomainAxis();  
      
            /*------设置X轴坐标上的文字-----------*/  
            domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
      
            /*------设置X轴的标题文字------------*/  
            domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
      
            /*------设置Y轴坐标上的文字-----------*/  
            numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
      
            /*------设置Y轴的标题文字------------*/  
            numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));  
      
            /*------这句代码解决了底部汉字乱码的问题-----------*/  
            chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));  
      
            /*******这句代码解决了标题汉字乱码的问题********/  
            chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));  
      
            FileOutputStream out = null;  
            try {  
                out = new FileOutputStream("E://Items//BarChartDemo1//image//1.jpg");  
                ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 400, 300, null);  
            } finally {  
                try {  
                    out.close();  
                } catch (Exception ex) {  
                    ex.printStackTrace();  
                }  
            }  
        }  
      
        private static CategoryDataset getDataSet() {  
            DefaultCategoryDataset ds = new DefaultCategoryDataset();  
            ds.addValue(100, "北京", "苹果");  
            ds.addValue(100, "上海", "苹果");  
            ds.addValue(100, "广州", "苹果");  
            ds.addValue(200, "北京", "梨子");  
            ds.addValue(200, "上海", "梨子");  
            ds.addValue(200, "广州", "梨子");  
            ds.addValue(300, "北京", "葡萄");  
            ds.addValue(300, "上海", "葡萄");  
            ds.addValue(300, "广州", "葡萄");  
            ds.addValue(400, "北京", "橘子");  
            ds.addValue(400, "上海", "橘子");  
            ds.addValue(400, "广州", "橘子");  
            ds.addValue(500, "北京", "香蕉");  
            ds.addValue(500, "上海", "香蕉");  
            ds.addValue(500, "广州", "香蕉");  
            return ds;  
        }  
    }
package barchartdemo1;  
      
    import <a href="http://lib.csdn.net/base/javaee" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.awt.Font;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    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.NumberAxis;  
    import org.jfree.chart.plot.CategoryPlot;  
    import org.jfree.chart.plot.PlotOrientation;  
    import org.jfree.data.category.CategoryDataset;  
    import org.jfree.data.category.DefaultCategoryDataset;  
      
    /** 
     * 
     * @author Administrator 
     */  
    public class Main {  
      
        /** 
         * @param args the command line arguments 
         */  
        public static void main(String[] args) throws IOException {  
            CategoryDataset ds = getDataSet();  
            JFreeChart chart = ChartFactory.createBarChart3D(  
                    "水果产量图", //图表标题  
                    "水果", //目录轴的显示标签  
                    "产量", //数值轴的显示标签  
                    ds, //数据集  
                    PlotOrientation.VERTICAL, //图表方向  
                    true, //是否显示图例,对于简单的柱状图必须为false  
                    false, //是否生成提示工具  
                    false);         //是否生成url链接  
      
            CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();  
      
            NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();  
      
            CategoryAxis domainAxis = categoryplot.getDomainAxis();  
      
            /*------设置X轴坐标上的文字-----------*/  
            domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
      
            /*------设置X轴的标题文字------------*/  
            domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
      
            /*------设置Y轴坐标上的文字-----------*/  
            numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
      
            /*------设置Y轴的标题文字------------*/  
            numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));  
      
            /*------这句代码解决了底部汉字乱码的问题-----------*/  
            chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));  
      
            /*******这句代码解决了标题汉字乱码的问题********/  
            chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));  
      
            FileOutputStream out = null;  
            try {  
                out = new FileOutputStream("E://Items//BarChartDemo1//image//1.jpg");  
                ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 400, 300, null);  
            } finally {  
                try {  
                    out.close();  
                } catch (Exception ex) {  
                    ex.printStackTrace();  
                }  
            }  
        }  
      
        private static CategoryDataset getDataSet() {  
            DefaultCategoryDataset ds = new DefaultCategoryDataset();  
            ds.addValue(100, "北京", "苹果");  
            ds.addValue(100, "上海", "苹果");  
            ds.addValue(100, "广州", "苹果");  
            ds.addValue(200, "北京", "梨子");  
            ds.addValue(200, "上海", "梨子");  
            ds.addValue(200, "广州", "梨子");  
            ds.addValue(300, "北京", "葡萄");  
            ds.addValue(300, "上海", "葡萄");  
            ds.addValue(300, "广州", "葡萄");  
            ds.addValue(400, "北京", "橘子");  
            ds.addValue(400, "上海", "橘子");  
            ds.addValue(400, "广州", "橘子");  
            ds.addValue(500, "北京", "香蕉");  
            ds.addValue(500, "上海", "香蕉");  
            ds.addValue(500, "广州", "香蕉");  
            return ds;  
        }  
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值