【JAVA图表】Jfreechart常用图表总结

柱状图:

  1. package chart;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.util.Map;  
  6. import java.util.Map.Entry;  
  7. import java.util.Set;  
  8.   
  9. import org.jfree.chart.ChartFactory;  
  10. import org.jfree.chart.ChartFrame;  
  11. import org.jfree.chart.JFreeChart;  
  12. import org.jfree.chart.axis.CategoryAxis;  
  13. import org.jfree.chart.axis.CategoryLabelPositions;  
  14. import org.jfree.chart.axis.NumberAxis;  
  15. import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;  
  16. import org.jfree.chart.plot.CategoryPlot;  
  17. import org.jfree.chart.plot.PlotOrientation;  
  18. import org.jfree.chart.renderer.category.BarRenderer;  
  19. import org.jfree.chart.title.TextTitle;  
  20. import org.jfree.data.category.DefaultCategoryDataset;  
  21.   
  22. /** 
  23.  * @author shenchao 
  24.  *      柱状图 
  25.  */  
  26. public class BarChart {  
  27.   
  28.     public BarChart() {  
  29.         Map<String,Integer> map = initDataSet();  
  30.         showChart(map);  
  31.     }  
  32.   
  33.     /** 
  34.      *  初始化数据集 
  35.      *      柱状图的数据集接受三个参数,第一个参数为柱子的高度,第二个为几个类别进行比较,第三个为共有几组数据 
  36.      *   
  37.      * @return 
  38.      */  
  39.     private Map<String, Integer> initDataSet() {  
  40.         return null;  
  41.     }  
  42.   
  43.     /** 
  44.      * @param map 
  45.      */  
  46.     private void showChart(Map<String, Integer> map) {  
  47.         // 创建饼图数据对象  
  48.         DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
  49.         Set<Entry<String, Integer>> set = map.entrySet();  
  50.         for (Entry<String, Integer> entry : set) {  
  51.             dataset.setValue(entry.getValue(), "评论数量",entry.getKey());  
  52.         }  
  53.           
  54.         JFreeChart chart = ChartFactory.createBarChart3D("评论次数TOP10""好友昵称",  
  55.                 "评论数量", dataset, PlotOrientation.VERTICAL, falsetruetrue);  
  56.         ChartFrame frame = new ChartFrame("评论次数TOP10", chart, true);  
  57.         // 自定义设定背景色  
  58.         // chart.setBackgroundPaint(Color.getHSBColor(23,192,223));  
  59.         chart.setBackgroundPaint(Color.WHITE);  
  60.         // 获得 plot:3dBar为CategoryPlot  
  61.         CategoryPlot categoryPlot = chart.getCategoryPlot();  
  62.         // 设定图表数据显示部分背景色  
  63.         categoryPlot.setBackgroundPaint(Color.WHITE);  
  64.         // 横坐标网格线  
  65.         categoryPlot.setDomainGridlinePaint(Color.GRAY);  
  66.         // 设置网格线可见  
  67.         categoryPlot.setDomainGridlinesVisible(true);  
  68.         // 纵坐标网格线  
  69.         categoryPlot.setRangeGridlinePaint(Color.GRAY);  
  70.         // 重要的类,负责生成各种效果  
  71.         // BarRenderer3D renderer=(BarRenderer3D) categoryPlot.getRenderer();  
  72.         // 获取纵坐标  
  73.         NumberAxis numberaxis = (NumberAxis) categoryPlot.getRangeAxis();  
  74.           
  75.         // 设置纵坐标的标题字体和大小  
  76.         numberaxis.setLabelFont(new Font("黑体", Font.CENTER_BASELINE, 16));  
  77.         // 设置丛坐标的坐标值的字体颜色  
  78.         numberaxis.setLabelPaint(Color.BLACK);  
  79.         // 设置丛坐标的坐标轴标尺颜色  
  80.         numberaxis.setTickLabelPaint(Color.BLACK);  
  81.         // 坐标轴标尺颜色  
  82.         numberaxis.setTickMarkPaint(Color.BLUE);  
  83.         // 丛坐标的默认间距值  
  84.         // numberaxis.setAutoTickUnitSelection(true);  
  85.         // 设置丛坐标间距值  
  86.         numberaxis.setAutoTickUnitSelection(true);  
  87.         // numberaxis.setTickUnit(new NumberTickUnit(150));  
  88.           
  89.         //在柱体的上面显示数据   
  90.         BarRenderer custombarrenderer3d = new BarRenderer();   
  91.         custombarrenderer3d.setBaseItemLabelPaint(Color.BLACK);//数据字体的颜色   
  92.         custombarrenderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());   
  93.         custombarrenderer3d.setBaseItemLabelsVisible(true);   
  94.         categoryPlot.setRenderer(custombarrenderer3d);   
  95.           
  96.           
  97.         // 获取横坐标  
  98.         CategoryAxis domainAxis = categoryPlot.getDomainAxis();  
  99.         // 设置横坐标的标题字体和大小  
  100.         domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 16));  
  101.         // 设置横坐标的坐标值的字体颜色  
  102.         domainAxis.setTickLabelPaint(Color.GRAY);  
  103.         // 设置横坐标的坐标值的字体  
  104.         domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 16));  
  105.         // 设置横坐标的显示  
  106.         domainAxis.setCategoryLabelPositions(CategoryLabelPositions  
  107.                 .createUpRotationLabelPositions(0.4));  
  108.         // 这句代码解决了底部汉字乱码的问题  
  109. //      chart.getLegend().setItemFont(new Font("黑体", 0, 16));  
  110.         // 设置图例标题  
  111.         Font font = new java.awt.Font("黑体", java.awt.Font.CENTER_BASELINE, 20);  
  112.         TextTitle title = new TextTitle("谁是评论你最多的人?");  
  113.         title.getBackgroundPaint();  
  114.         title.setFont(font);  
  115.         // 设置标题的字体颜色  
  116.         chart.setTitle(title);  
  117.         frame.pack();  
  118.         frame.setVisible(true);  
  119.     }  
  120.   
  121. }  

折线图:

  1. package chart;  
  2.   
  3. import java.awt.BasicStroke;  
  4. import java.awt.Color;  
  5. import java.awt.Font;  
  6. import java.util.Map;  
  7. import java.util.Map.Entry;  
  8. import java.util.Set;  
  9. import org.jfree.chart.ChartFactory;  
  10. import org.jfree.chart.ChartFrame;  
  11. import org.jfree.chart.JFreeChart;  
  12. import org.jfree.chart.axis.CategoryAxis;  
  13. import org.jfree.chart.axis.NumberAxis;  
  14. import org.jfree.chart.axis.ValueAxis;  
  15. import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;  
  16. import org.jfree.chart.plot.CategoryPlot;  
  17. import org.jfree.chart.plot.PlotOrientation;  
  18. import org.jfree.chart.renderer.category.LineAndShapeRenderer;  
  19. import org.jfree.data.category.DefaultCategoryDataset;  
  20.   
  21. public class LineChart {  
  22.   
  23.     public LineChart() {  
  24.         Map<Integer, Integer> map = initDataSet();  
  25.         showChart(map);  
  26.     }  
  27.   
  28.     /** 
  29.      *  初始化数据集 
  30.      *      柱状图的数据集接受三个参数,第一个参数为柱子的高度,第二个为几个类别进行比较,第三个为共有几组数据 
  31.      *   
  32.      * @return 
  33.      */  
  34.     private Map<Integer, Integer> initDataSet() {  
  35.         return null;  
  36.     }  
  37.   
  38.     private void showChart(Map<Integer, Integer> map) {  
  39.         // 创建饼图数据对象  
  40.         DefaultCategoryDataset dataSet = new DefaultCategoryDataset();  
  41.         Set<Entry<Integer, Integer>> set = map.entrySet();  
  42.         for (Entry<Integer, Integer> entry : set) {  
  43.             dataSet.setValue(entry.getValue(), "数量", entry.getKey());  
  44.         }  
  45.   
  46.         // 如果把createLineChart改为createLineChart3D就变为了3D效果的折线图  
  47.         JFreeChart chart = ChartFactory.createLineChart("每年说说的发布量""年份""数目",  
  48.                 dataSet, PlotOrientation.VERTICAL, // 绘制方向  
  49.                 false// 显示图例  
  50.                 true// 采用标准生成器  
  51.                 false // 是否生成超链接  
  52.                 );  
  53.         ChartFrame frame = new ChartFrame("图表标题", chart, true);  
  54.         chart.setBackgroundPaint(Color.WHITE);// 设置背景色  
  55.         // 获取绘图区对象  
  56.         CategoryPlot plot = chart.getCategoryPlot();  
  57.         plot.setBackgroundPaint(Color.WHITE); // 设置绘图区背景色  
  58.         plot.setRangeGridlinePaint(Color.GRAY); // 设置水平方向背景线颜色  
  59.         plot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为true  
  60.         plot.setDomainGridlinePaint(Color.GRAY); // 设置垂直方向背景线颜色  
  61.         plot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为false  
  62.   
  63.         CategoryAxis domainAxis = plot.getDomainAxis();  
  64.         domainAxis.setLowerMargin(0.01);// 左边距 边框距离  
  65.         domainAxis.setUpperMargin(0.06);// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。  
  66.         domainAxis.setMaximumCategoryLabelLines(2);  
  67.         domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 16));  
  68.   
  69.         ValueAxis rangeAxis = plot.getRangeAxis();  
  70.         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());// Y轴显示整数  
  71.         rangeAxis.setAutoRangeMinimumSize(1); // 最小跨度  
  72.         rangeAxis.setUpperMargin(0.18);// 上边距,防止最大的一个数据靠近了坐标轴。  
  73.         rangeAxis.setLowerBound(0); // 最小值显示0  
  74.         rangeAxis.setAutoRange(false); // 不自动分配Y轴数据  
  75.         rangeAxis.setTickMarkStroke(new BasicStroke(1.6f)); // 设置坐标标记大小  
  76.         rangeAxis.setTickMarkPaint(Color.BLACK); // 设置坐标标记颜色  
  77.         rangeAxis.setLabelFont(new Font("宋体", Font.PLAIN, 16));  
  78.   
  79.         // 获得renderer 注意这里是下嗍造型到lineandshaperenderer!!  
  80.         LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) plot  
  81.                 .getRenderer();  
  82.         lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见  
  83.   
  84.         lineandshaperenderer.setBaseLinesVisible(true);  
  85.         // 显示折点数据  
  86.         lineandshaperenderer  
  87.                 .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());  
  88.         lineandshaperenderer.setBaseItemLabelsVisible(true);  
  89.   
  90.         frame.pack();  
  91.         frame.setVisible(true);  
  92.     }  
  93. }  

饼图:

  1. package chart;  
  2.   
  3. import java.awt.BasicStroke;  
  4. import java.awt.Color;  
  5. import java.awt.Font;  
  6. import java.util.Map;  
  7. import java.util.Map.Entry;  
  8. import java.util.Set;  
  9. import org.jfree.chart.ChartFactory;  
  10. import org.jfree.chart.ChartFrame;  
  11. import org.jfree.chart.JFreeChart;  
  12. import org.jfree.chart.labels.StandardPieToolTipGenerator;  
  13. import org.jfree.chart.plot.PiePlot3D;  
  14. import org.jfree.chart.title.TextTitle;  
  15. import org.jfree.data.general.DefaultPieDataset;  
  16.   
  17. /** 
  18.  * @author shenchao 
  19.  *      饼图 
  20.  */  
  21. public class PieChart {  
  22.       
  23.     public PieChart() {  
  24.         Map<String,Integer> map = initDataSet();  
  25.         showChart(map);  
  26.     }  
  27.   
  28.       
  29.     /** 
  30.      *  饼图接受的数据集要求是键值对<br> 
  31.      *  
  32.      *      可以在这里初始化数据集 
  33.      * @return map 
  34.      */  
  35.     private Map<String,Integer> initDataSet() {  
  36.         return null;  
  37.     }  
  38.   
  39.     /** 
  40.      *  显示图表 
  41.      * @param map 
  42.      */  
  43.     private void showChart(Map<String, Integer> map) {  
  44.   
  45.         // 创建饼图数据对象  
  46.         DefaultPieDataset dfp = new DefaultPieDataset();  
  47.         Set<Entry<String, Integer>> set = map.entrySet();  
  48.         for (Entry<String, Integer> entry : set) {  
  49.             dfp.setValue(entry.getKey(), entry.getValue());  
  50.         }  
  51.         // createpieChart3D创建3D饼图  
  52.         JFreeChart chart = ChartFactory.createPieChart3D("窗口标题",  
  53.                 dfp, truetruetrue);  
  54.         // 图片背景色  
  55.         chart.setBackgroundPaint(Color.white);  
  56.         // 设置标题文字  
  57.         ChartFrame frame = new ChartFrame("窗口标题", chart, true);  
  58.         // 取得3D饼图对象  
  59.         PiePlot3D plot = (PiePlot3D) chart.getPlot();  
  60.         // 图形边框颜色  
  61.         plot.setBaseSectionOutlinePaint(Color.WHITE);  
  62.         // 图形边框粗细  
  63.         plot.setBaseSectionOutlineStroke(new BasicStroke(1.0f));  
  64.         // 指定图片的透明度(0.0-1.0)  
  65.         plot.setForegroundAlpha(0.45f);  
  66.         // 指定显示的饼图上圆形(false)还椭圆形(true)  
  67.         plot.setCircular(true);  
  68.         // 设置第一个 饼块section 的开始位置,默认是12点钟方向  
  69.         plot.setStartAngle(360);  
  70.         // 设置鼠标悬停提示  
  71.         plot.setToolTipGenerator(new StandardPieToolTipGenerator());  
  72.         // 设置饼图各部分标签字体  
  73.         plot.setLabelFont(new Font("微软雅黑", Font.ITALIC, 16));  
  74.         // 设置分饼颜色  
  75.         plot.setSectionPaint(0new Color(244194144));  
  76.         // 定义字体格式    
  77.         Font font = new java.awt.Font("黑体", java.awt.Font.CENTER_BASELINE,20);    
  78.         TextTitle title = new TextTitle("图表标题");    
  79.         title.setFont(font);    
  80.         // 设置字体,非常关键不然会出现乱码的,下方的字体    
  81.         chart.setTitle(title);    
  82.         frame.pack();   
  83.         frame.setVisible(true);  
  84.     }  
  85.   
  86. }  

散点图:

  1. package chart;  
  2.   
  3. import java.awt.BasicStroke;  
  4. import java.awt.Color;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7. import java.util.Map.Entry;  
  8.   
  9. import org.jfree.chart.ChartFactory;  
  10. import org.jfree.chart.ChartFrame;  
  11. import org.jfree.chart.JFreeChart;  
  12. import org.jfree.chart.axis.NumberAxis;  
  13. import org.jfree.chart.axis.ValueAxis;  
  14. import org.jfree.chart.plot.PlotOrientation;  
  15. import org.jfree.chart.plot.XYPlot;  
  16. import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;  
  17. import org.jfree.data.xy.DefaultXYDataset;  
  18.   
  19. /** 
  20.  * @author shenchao 散点图 
  21.  */  
  22. public class ScatterPlotChart {  
  23.   
  24.     public ScatterPlotChart() {  
  25.         Map<Integer,List<Data>> map = initDataSet();  
  26.         showChart(map);  
  27.     }  
  28.   
  29.     /** 
  30.      *  这里的Data类是数据的封装,可以安装实际需求实现,map集合的键为类别 
  31.      */  
  32.     private Map<Integer, List<Data>> initDataSet() {  
  33.         return null;  
  34.     }  
  35.   
  36.     public void showChart(Map<Integer,List<Data>> map) {  
  37.           
  38.         DefaultXYDataset xydataset = new DefaultXYDataset();  
  39.           
  40.         //根据类别建立数据集  
  41.         for (Entry<Integer, List<Data>> entry : map.entrySet()) {  
  42.             List<Data> l = entry.getValue();  
  43.               
  44.             int size = l.size();  
  45.             //散点图要求数据集为二维数组  
  46.             double[][] datas = new double[2][size];  
  47.             for (int i = 0; i < size; i++) {  
  48.                 Data data = l.get(i);  
  49.                 datas[0][i] = data.getDistance();  
  50.                 datas[1][i] = data.getIcecream();  
  51.             }  
  52.             xydataset.addSeries(entry.getKey(), datas);  
  53.         }  
  54.           
  55.         JFreeChart chart = ChartFactory.createScatterPlot("散点图""每年获取的飞行常客里程数""每周所消费的冰淇淋公升数", xydataset, PlotOrientation.VERTICAL, truefalsefalse);  
  56.         ChartFrame frame = new ChartFrame("散点图", chart, true);  
  57.         chart.setBackgroundPaint(Color.white);    
  58.         chart.setBorderPaint(Color.GREEN);    
  59.         chart.setBorderStroke(new BasicStroke(1.5f));    
  60.         XYPlot xyplot = (XYPlot) chart.getPlot();    
  61.     
  62.         xyplot.setBackgroundPaint(new Color(255253246));    
  63.         ValueAxis vaaxis = xyplot.getDomainAxis();    
  64.         vaaxis.setAxisLineStroke(new BasicStroke(1.5f));    
  65.     
  66.         ValueAxis va = xyplot.getDomainAxis(0);    
  67.         va.setAxisLineStroke(new BasicStroke(1.5f));    
  68.     
  69.         va.setAxisLineStroke(new BasicStroke(1.5f)); // 坐标轴粗细    
  70.         va.setAxisLinePaint(new Color(215215215)); // 坐标轴颜色    
  71.         xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 边框粗细    
  72.         va.setLabelPaint(new Color(101010)); // 坐标轴标题颜色    
  73.         va.setTickLabelPaint(new Color(102102102)); // 坐标轴标尺值颜色    
  74.         ValueAxis axis = xyplot.getRangeAxis();    
  75.         axis.setAxisLineStroke(new BasicStroke(1.5f));    
  76.     
  77.         XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot    
  78.                 .getRenderer();    
  79.         xylineandshaperenderer.setSeriesOutlinePaint(0, Color.WHITE);    
  80.         xylineandshaperenderer.setUseOutlinePaint(true);    
  81.         NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis();    
  82.         numberaxis.setAutoRangeIncludesZero(false);    
  83.         numberaxis.setTickMarkInsideLength(2.0F);    
  84.         numberaxis.setTickMarkOutsideLength(0.0F);    
  85.         numberaxis.setAxisLineStroke(new BasicStroke(1.5f));    
  86.           
  87.         frame.pack();  
  88.         frame.setVisible(true);  
  89.     }  
  90.   
  91. }  
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值