论坛首页 Java企业应用论坛

用JFREECHART绘制重叠的柱状图

浏览 4217 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-07-15   最后修改:2010-07-15
以下例子是一个利用JFREECHART绘制重叠柱状图的例子,源码可以直接使用。
*****************************************************************
/**
* 重叠的柱状图
*/

import java.awt.*;
import javax.swing.JPanel;
import org.jfree.chart.*;
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.LayeredBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.SortOrder;

@SuppressWarnings("serial")
public class LayeredBarChartDemo1 extends ApplicationFrame
{

    public LayeredBarChartDemo1(String s)
    {
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    /**
     * 生成显示图形用的数据集
     * @return
     */
    private static CategoryDataset createDataset()
    {
    // 图例
        String s1 = "One";
        String s2 = "Two";
        String s3 = "Thr";
        // 横坐标分类
        String t1 = "C1";
        String t2 = "C2";
        String t3 = "C3";
        String t4 = "C4";
        String t5 = "C5";
       
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
       
        // addValue方法说明:参数1:柱状图显示的量
        //                  参数2:柱子(或图例)标注(一个点显示一个柱子时,只需要一个)
        //                  参数3:横坐标上显示的分类
        defaultcategorydataset.addValue(1D, s1, t1);
        defaultcategorydataset.addValue(3D, s1, t2);
        defaultcategorydataset.addValue(2D, s1, t3);
        defaultcategorydataset.addValue(5D, s1, t4);
        defaultcategorydataset.addValue(8D, s1, t5);
        defaultcategorydataset.addValue(5D, s2, t1);
        defaultcategorydataset.addValue(9D, s2, t2);
        defaultcategorydataset.addValue(6D, s2, t3);
        defaultcategorydataset.addValue(1D, s2, t4);
        defaultcategorydataset.addValue(5D, s2, t5);
        defaultcategorydataset.addValue(8D, s3, t1);
        defaultcategorydataset.addValue(1D, s3, t2);
        defaultcategorydataset.addValue(5D, s3, t3);
        defaultcategorydataset.addValue(2D, s3, t4);
        defaultcategorydataset.addValue(6D, s3, t5);
        return defaultcategorydataset;
    }

    /**
     * 绘制图形,设置参数
     * @param categorydataset
     * @return
     */
    private static JFreeChart createChart(CategoryDataset categorydataset)
    {
    // 生成图形--参数说明
    // 参数1:标题
    // 参数2:分类
    // 参数3:值
    // 参数4:数据集
    // 参数5:柱子的显示方向
    // 参数6:是否显示图例
    // 参数7:暂不知道
    // 参数8:是否显示URL
        JFreeChart jfreechart = ChartFactory.createBarChart("BarImage", "CCC", "Value", categorydataset, PlotOrientation.VERTICAL, true, true, true);
        // 设置背景色
        jfreechart.setBackgroundPaint(Color.lightGray);
        // 设置图形对象
        CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
        // 设置图形的背景色
        categoryplot.setBackgroundPaint(Color.yellow);
        // 设置图形上竖线的颜色
        categoryplot.setDomainGridlinePaint(Color.black);
        // 设置图形上竖线是否显示
        categoryplot.setDomainGridlinesVisible(true);
        // 设置图形上横线的颜色
        categoryplot.setRangeGridlinePaint(Color.black);
        // 获取横轴
        CategoryAxis categoryAxis = categoryplot.getDomainAxis();
        // 设置横坐标上的分类是否显示
        categoryAxis.setTickLabelsVisible(true);
        // 获取纵轴
        NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
        // 设置纵轴的刻度线
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        LayeredBarRenderer layeredbarrenderer = new LayeredBarRenderer();
        layeredbarrenderer.setDrawBarOutline(false);
        // 设置柱子的边框是否显示
        categoryplot.setRenderer(layeredbarrenderer);
        // 设置柱子的排序方式
        categoryplot.setRowRenderingOrder(SortOrder.DESCENDING);
        GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.black, 0.0F, 0.0F, new Color(0, 0, 64));
        GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
        GradientPaint gradientpaint3 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(64, 0, 0));
        // 设置柱子的颜色
        layeredbarrenderer.setSeriesPaint(0, gradientpaint1);
        layeredbarrenderer.setSeriesPaint(1, gradientpaint2);
        layeredbarrenderer.setSeriesPaint(2, gradientpaint3);
        return jfreechart;
    }

    public static JPanel createDemoPanel()
    {
        JFreeChart jfreechart = createChart(createDataset());
        return new ChartPanel(jfreechart);
    }

    public static void main(String args[])
    {
        LayeredBarChartDemo1 layeredbarchartdemo1 = new LayeredBarChartDemo1("LayeredBarChart");
        layeredbarchartdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(layeredbarchartdemo1);
        layeredbarchartdemo1.setVisible(true);
    }
}
  • 大小: 14.5 KB
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics