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


     1     package barchartdemo1;  
     2       
     3     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;  
     4     import java.io.FileOutputStream;  
     5     import java.io.IOException;  
     6     import org.jfree.chart.ChartFactory;  
     7     import org.jfree.chart.ChartUtilities;  
     8     import org.jfree.chart.JFreeChart;  
     9     import org.jfree.chart.axis.CategoryAxis;  
    10     import org.jfree.chart.axis.NumberAxis;  
    11     import org.jfree.chart.plot.CategoryPlot;  
    12     import org.jfree.chart.plot.PlotOrientation;  
    13     import org.jfree.data.category.CategoryDataset;  
    14     import org.jfree.data.category.DefaultCategoryDataset;  
    15       
    16     /** 
    17      * 
    18      * @author Administrator 
    19      */  
    20     public class Main {  
    21       
    22         /** 
    23          * @param args the command line arguments 
    24          */  
    25         public static void main(String[] args) throws IOException {  
    26             CategoryDataset ds = getDataSet();  
    27             JFreeChart chart = ChartFactory.createBarChart3D(  
    28                     "水果产量图", //图表标题  
    29                     "水果", //目录轴的显示标签  
    30                     "产量", //数值轴的显示标签  
    31                     ds, //数据集  
    32                     PlotOrientation.VERTICAL, //图表方向  
    33                     true, //是否显示图例,对于简单的柱状图必须为false  
    34                     false, //是否生成提示工具  
    35                     false);         //是否生成url链接  
    36       
    37             CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();  
    38       
    39             NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();  
    40       
    41             CategoryAxis domainAxis = categoryplot.getDomainAxis();  
    42       
    43             /*------设置X轴坐标上的文字-----------*/  
    44             domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
    45       
    46             /*------设置X轴的标题文字------------*/  
    47             domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
    48       
    49             /*------设置Y轴坐标上的文字-----------*/  
    50             numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
    51       
    52             /*------设置Y轴的标题文字------------*/  
    53             numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));  
    54       
    55             /*------这句代码解决了底部汉字乱码的问题-----------*/  
    56             chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));  
    57       
    58             /*******这句代码解决了标题汉字乱码的问题********/  
    59             chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));  
    60       
    61             FileOutputStream out = null;  
    62             try {  
    63                 out = new FileOutputStream("E://Items//BarChartDemo1//image//1.jpg");  
    64                 ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 400, 300, null);  
    65             } finally {  
    66                 try {  
    67                     out.close();  
    68                 } catch (Exception ex) {  
    69                     ex.printStackTrace();  
    70                 }  
    71             }  
    72         }  
    73       
    74         private static CategoryDataset getDataSet() {  
    75             DefaultCategoryDataset ds = new DefaultCategoryDataset();  
    76             ds.addValue(100, "北京", "苹果");  
    77             ds.addValue(100, "上海", "苹果");  
    78             ds.addValue(100, "广州", "苹果");  
    79             ds.addValue(200, "北京", "梨子");  
    80             ds.addValue(200, "上海", "梨子");  
    81             ds.addValue(200, "广州", "梨子");  
    82             ds.addValue(300, "北京", "葡萄");  
    83             ds.addValue(300, "上海", "葡萄");  
    84             ds.addValue(300, "广州", "葡萄");  
    85             ds.addValue(400, "北京", "橘子");  
    86             ds.addValue(400, "上海", "橘子");  
    87             ds.addValue(400, "广州", "橘子");  
    88             ds.addValue(500, "北京", "香蕉");  
    89             ds.addValue(500, "上海", "香蕉");  
    90             ds.addValue(500, "广州", "香蕉");  
    91             return ds;  
    92         }  
    93     }  
     1     package barchartdemo1;  
     2       
     3     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;  
     4     import java.io.FileOutputStream;  
     5     import java.io.IOException;  
     6     import org.jfree.chart.ChartFactory;  
     7     import org.jfree.chart.ChartUtilities;  
     8     import org.jfree.chart.JFreeChart;  
     9     import org.jfree.chart.axis.CategoryAxis;  
    10     import org.jfree.chart.axis.NumberAxis;  
    11     import org.jfree.chart.plot.CategoryPlot;  
    12     import org.jfree.chart.plot.PlotOrientation;  
    13     import org.jfree.data.category.CategoryDataset;  
    14     import org.jfree.data.category.DefaultCategoryDataset;  
    15       
    16     /** 
    17      * 
    18      * @author Administrator 
    19      */  
    20     public class Main {  
    21       
    22         /** 
    23          * @param args the command line arguments 
    24          */  
    25         public static void main(String[] args) throws IOException {  
    26             CategoryDataset ds = getDataSet();  
    27             JFreeChart chart = ChartFactory.createBarChart3D(  
    28                     "水果产量图", //图表标题  
    29                     "水果", //目录轴的显示标签  
    30                     "产量", //数值轴的显示标签  
    31                     ds, //数据集  
    32                     PlotOrientation.VERTICAL, //图表方向  
    33                     true, //是否显示图例,对于简单的柱状图必须为false  
    34                     false, //是否生成提示工具  
    35                     false);         //是否生成url链接  
    36       
    37             CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();  
    38       
    39             NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();  
    40       
    41             CategoryAxis domainAxis = categoryplot.getDomainAxis();  
    42       
    43             /*------设置X轴坐标上的文字-----------*/  
    44             domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
    45       
    46             /*------设置X轴的标题文字------------*/  
    47             domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
    48       
    49             /*------设置Y轴坐标上的文字-----------*/  
    50             numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
    51       
    52             /*------设置Y轴的标题文字------------*/  
    53             numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));  
    54       
    55             /*------这句代码解决了底部汉字乱码的问题-----------*/  
    56             chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));  
    57       
    58             /*******这句代码解决了标题汉字乱码的问题********/  
    59             chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));  
    60       
    61             FileOutputStream out = null;  
    62             try {  
    63                 out = new FileOutputStream("E://Items//BarChartDemo1//image//1.jpg");  
    64                 ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 400, 300, null);  
    65             } finally {  
    66                 try {  
    67                     out.close();  
    68                 } catch (Exception ex) {  
    69                     ex.printStackTrace();  
    70                 }  
    71             }  
    72         }  
    73       
    74         private static CategoryDataset getDataSet() {  
    75             DefaultCategoryDataset ds = new DefaultCategoryDataset();  
    76             ds.addValue(100, "北京", "苹果");  
    77             ds.addValue(100, "上海", "苹果");  
    78             ds.addValue(100, "广州", "苹果");  
    79             ds.addValue(200, "北京", "梨子");  
    80             ds.addValue(200, "上海", "梨子");  
    81             ds.addValue(200, "广州", "梨子");  
    82             ds.addValue(300, "北京", "葡萄");  
    83             ds.addValue(300, "上海", "葡萄");  
    84             ds.addValue(300, "广州", "葡萄");  
    85             ds.addValue(400, "北京", "橘子");  
    86             ds.addValue(400, "上海", "橘子");  
    87             ds.addValue(400, "广州", "橘子");  
    88             ds.addValue(500, "北京", "香蕉");  
    89             ds.addValue(500, "上海", "香蕉");  
    90             ds.addValue(500, "广州", "香蕉");  
    91             return ds;  
    92         }  
    93     }  

    图片效果:

    效果图

  • 相关阅读:
    怎么解决Chrome浏览器崩溃“STATUS_INVALID_IMAGE_HASH”的问题
    Windows下PHP如何选择Thread Safe和Non ThreadSafe版本
    Windows环境下安装Yaf框架
    创建Redis-Cluster集群常见问题-解决方案
    Linxu下PHP版本升级
    Linxu下Yii2的POST提交被拒经历
    彻底搞懂 Redis 事务
    python模块之psutil详解
    iptables学习笔记
    incaseformat 病毒事件企业解决流程
  • 原文地址:https://www.cnblogs.com/sharpest/p/6032285.html
Copyright © 2020-2023  润新知