zoukankan      html  css  js  c++  java
  • JFreeChart柱状图单组柱子的不同颜色显示

    JFreeChart柱状图中单组柱子用不同颜色来显示的实现方法是自定义一个Renderer来继承BarRenderer类,然后重载getItemPaint(int i,int j)方法。

    实现效果如下:

    实现代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    public class CustomRenderer extends org.jfree.chart.renderer.category.BarRenderer { 
       
        /** 
         *  
         */ 
        private static final long serialVersionUID = 784630226449158436L; 
        private Paint[] colors; 
        //初始化柱子颜色 
        private String[] colorValues = { "#AFD8F8""#F6BD0F""#8BBA00""#FF8E46""#008E8E""#D64646" }; 
       
        public CustomRenderer() { 
            colors = new Paint[colorValues.length]; 
            for (int i = 0; i < colorValues.length; i++) { 
                colors[i] = Color.decode(colorValues[i]); 
            
        
       
        //每根柱子以初始化的颜色不断轮循 
        public Paint getItemPaint(int i, int j) { 
            return colors[j % colors.length]; 
        
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    public class CreateJFreeChartBarColor { 
       
        /** 
         * 创建JFreeChart Bar Chart(柱状图) 
         */ 
        public static void main(String[] args) { 
            // 步骤1:创建CategoryDataset对象(准备数据) 
            CategoryDataset dataset = createDataset(); 
            // 步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置 
            JFreeChart freeChart = createChart(dataset); 
            // 步骤3:将JFreeChart对象输出到文件,Servlet输出流等 
            saveAsFile(freeChart, "E:\bar.png"500400); 
        
       
        // 保存为文件 
        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); 
                // 保存为PNG文件 
                ChartUtilities.writeChartAsPNG(out, chart, weight, height); 
                out.flush(); 
            catch (FileNotFoundException e) { 
                e.printStackTrace(); 
            catch (IOException e) { 
                e.printStackTrace(); 
            finally
                if (out != null) { 
                    try
                        out.close(); 
                    catch (IOException e) { 
                        // do nothing 
                    
                
            
        
       
        // 根据CategoryDataset生成JFreeChart对象 
        public static JFreeChart createChart(CategoryDataset categoryDataset) { 
            JFreeChart jfreechart = ChartFactory.createBarChart("学生统计图"// 标题 
                    "学生姓名"// categoryAxisLabel (category轴,横轴,X轴的标签) 
                    "年龄"// valueAxisLabel(value轴,纵轴,Y轴的标签) 
                    categoryDataset, // dataset 
                    PlotOrientation.VERTICAL, false// legend 
                    false// tooltips 
                    false); // URLs 
       
            Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); 
       
            jfreechart.setTextAntiAlias(false); 
            jfreechart.setBackgroundPaint(Color.white); 
       
            CategoryPlot plot = jfreechart.getCategoryPlot();// 获得图表区域对象 
       
            // 设置横虚线可见 
            plot.setRangeGridlinesVisible(true); 
            // 虚线色彩 
            plot.setRangeGridlinePaint(Color.gray); 
            // 数据轴精度 
            NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
            // vn.setAutoRangeIncludesZero(true); 
            DecimalFormat df = new DecimalFormat("#0.0"); 
            vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式 
       
            // x轴设置 
            CategoryAxis domainAxis = plot.getDomainAxis(); 
            domainAxis.setLabelFont(labelFont);// 轴标题 
            domainAxis.setTickLabelFont(labelFont);// 轴数值 
            // Lable(Math.PI/3.0)度倾斜 
            // domainAxis.setCategoryLabelPositions(CategoryLabelPositions 
            // .createUpRotationLabelPositions(Math.PI / 3.0)); 
            domainAxis.setMaximumCategoryLabelWidthRatio(6.00f);// 横轴上的 Lable 
            // 是否完整显示 
       
            // 设置距离图片左端距离 
            domainAxis.setLowerMargin(0.1); 
            // 设置距离图片右端距离 
            domainAxis.setUpperMargin(0.1); 
            // 设置 columnKey 是否间隔显示 
            // domainAxis.setSkipCategoryLabelsToFit(true); 
            plot.setDomainAxis(domainAxis); 
            // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确) 
            plot.setBackgroundPaint(new Color(255255204)); 
       
            // y轴设置 
            ValueAxis rangeAxis = plot.getRangeAxis(); 
            rangeAxis.setLabelFont(labelFont); 
            rangeAxis.setTickLabelFont(labelFont); 
            // 设置最高的一个 Item 与图片顶端的距离 
            rangeAxis.setUpperMargin(0.15); 
            // 设置最低的一个 Item 与图片底端的距离 
            rangeAxis.setLowerMargin(0.15); 
            plot.setRangeAxis(rangeAxis); 
       
            // 解决中文乱码问题(关键) 
            TextTitle textTitle = jfreechart.getTitle(); 
            textTitle.setFont(new Font("黑体", Font.PLAIN, 20)); 
            domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); 
            domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); 
            vn.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); 
            vn.setLabelFont(new Font("黑体", Font.PLAIN, 12)); 
            // jfreechart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); 
       
            // 使用自定义的渲染器 
                    CustomRenderer renderer = new CustomRenderer(); 
            // 设置柱子宽度 
            renderer.setMaximumBarWidth(0.2); 
            // 设置柱子高度 
            renderer.setMinimumBarLength(0.2); 
            // 设置柱子边框颜色 
            renderer.setBaseOutlinePaint(Color.BLACK); 
            // 设置柱子边框可见 
            renderer.setDrawBarOutline(true); 
            // 设置每个地区所包含的平行柱的之间距离 
            renderer.setItemMargin(0.5); 
            jfreechart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 
            // 显示每个柱的数值,并修改该数值的字体属性 
            renderer.setIncludeBaseInRange(true); 
            renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
            renderer.setBaseItemLabelsVisible(true); 
            plot.setRenderer(renderer); 
            // 设置柱的透明度 
            plot.setForegroundAlpha(1.0f); 
       
            // 背景色 透明度 
            plot.setBackgroundAlpha(0.5f); 
       
            return jfreechart; 
        
       
        // 创建CategoryDataset对象 
        public static CategoryDataset createDataset() { 
            double[][] data = new double[][] { { 252440123333 } }; 
            String[] rowKeys = { "" }; 
            String[] columnKeys = { "张三""李四""王五""马六""陈七""赵八" }; 
            CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); 
            return dataset; 
        
       
    }
  • 相关阅读:
    kafka 官方 Quickstart
    oracle11.2 安装
    Perl参考函数/教程
    Mysql参见SHOW命令总结
    MySQL的Innodb缓存相关优化
    Oracle、Mysql和SQL Server数据库连接的URL写法
    jredis 客户端 使用
    sql基本命令-存储过程
    NoSql系列目录ElasticSearch-mongodb
    loadrunner 运行场景-Controller及Load Generators宿主主机优化
  • 原文地址:https://www.cnblogs.com/telwanggs/p/4921175.html
Copyright © 2011-2022 走看看