java swing jfreechart

 /**
     * 创建JFreeChart传入数据集
     */
    private void createPieChart(List<Object[]> listChartIn, List<Object[]> listChartOut)
    {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();


        for (Object[] str : listChartIn)
        {
            dataset.addValue((Number) str[1], "1", EbsiCode.get("IAE001", Utility.isNull(str[0]) ? "" : (String) str[0])); // 输入数据
        }
        for (Object[] str : listChartOut)
        {
            dataset.addValue((Number) str[1], "2", EbsiCode.get("IAE001", Utility.isNull(str[0]) ? "" : (String) str[0])); // 输入数据
        }


        JFreeChart chart = ChartFactory.createBarChart("", "", "", dataset, PlotOrientation.VERTICAL, false, false, false); //创建一个JFreeChart
        ChartPanel panel = EbsiChartUtil.createBarChart(dataset); // 调用公共样式和处理乱码的方法
        // 将产生的图片显示在Jpanel上
        panelPiechart.setLayout(new java.awt.BorderLayout()); 
        panelPiechart.add(panel, BorderLayout.CENTER);

    }



 public static final Color BACKGROUND_PAINT = new Color(236, 233, 216); // 背景色


    private EbsiChartUtil()
    {
    }


    /**
     * 创建一个3D的饼状图
     *
     * @param dataset 数据集
     *
     * @return jfreechart图表
     */
    public static ChartPanel createPieChart3D(DefaultPieDataset dataset)
    {
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 创建主题样式
        standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15)); // 设置字体
        ChartFactory.setChartTheme(standardChartTheme); // 应用主题样式


        JFreeChart chart = ChartFactory.createPieChart3D("", dataset, false, true, false);


        chart.setBackgroundPaint(BACKGROUND_PAINT);// 设置背景色
        PiePlot pieplot = (PiePlot) chart.getPlot();
        pieplot.setOutlinePaint(new Color(236, 233, 216)); // 设置边框颜色
        pieplot.setBackgroundAlpha(0f); // 设置透明度
        pieplot.setNoDataMessage("当前没有有效的数据");


        DecimalFormat df = new DecimalFormat("0.00%"); // 设置小数格式
        NumberFormat nf = NumberFormat.getNumberInstance(); // 获得一个NumberFormat对象
        StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0}  {2}", nf, df); // 获得StandardPieSectionLabelGenerator对象
        pieplot.setLabelGenerator(sp1); // 设置饼图显示百分比


        return new ChartPanel(chart);
    }


    public static ChartPanel createBarChart(DefaultCategoryDataset dataset)
    {
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 创建主题样式
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 10)); // 设置标题字体
        standardChartTheme.setRegularFont(new Font("微软雅黑", Font.PLAIN, 12)); // 设置图例的底部字体
        standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 10)); // 设置轴向的字体
        ChartFactory.setChartTheme(standardChartTheme);


        JFreeChart chart = ChartFactory.createBarChart("", "", "", dataset, PlotOrientation.VERTICAL, false, false, false); // 创建一个JFreeChart


        CategoryPlot pieplot = chart.getCategoryPlot();// 获取条形的对象
        pieplot.setBackgroundAlpha(0);// 设置图形的背景颜色
        pieplot.setNoDataMessage("当前没有有效的数据");
        chart.setBackgroundPaint(new Color(236, 233, 216));


        //设置条形柱上显示数量
        BarRenderer3D renderer = new BarRenderer3D();
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( // 设置柱子上显示的数字在上方显示
                ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        renderer.setItemLabelAnchorOffset(10D);// 设置柱形图上的文字偏离值
        renderer.setBaseItemLabelsVisible(true);
        renderer.setMaximumBarWidth(0.05);// 设置柱子宽度 
        renderer.setBaseOutlinePaint(Color.BLACK); // 设置柱子边框颜色
        pieplot.setRenderer(renderer);
        pieplot.setOutlinePaint(new Color(236, 233, 216)); // 设置图片边框颜色


        // X 轴      
        CategoryAxis domainAxis = pieplot.getDomainAxis();
        domainAxis.setTickLabelPaint(Color.BLUE); // 字体颜色      
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的label斜显示
        return new ChartPanel(chart);
    }
    
    /**
     * 柱子数大于10根 设置chartpanel大小   add by hello
     * @param dataset
     * @return 
     */
    public static ChartPanel createBarChartMore(DefaultCategoryDataset dataset)
    {
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 创建主题样式
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 10)); // 设置标题字体
        standardChartTheme.setRegularFont(new Font("微软雅黑", Font.PLAIN, 12)); // 设置图例的底部字体
        standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 10)); // 设置轴向的字体
        ChartFactory.setChartTheme(standardChartTheme);


        JFreeChart chart = ChartFactory.createBarChart("", "", "", dataset, PlotOrientation.VERTICAL, true, false, false); // 创建一个JFreeChart


        CategoryPlot pieplot = chart.getCategoryPlot();// 获取条形的对象
        pieplot.setBackgroundAlpha(0);// 设置图形的背景颜色
        pieplot.setNoDataMessage("当前没有有效的数据");
        chart.setBackgroundPaint(new Color(236, 233, 216));


        //设置条形柱上显示数量
        BarRenderer3D renderer = new BarRenderer3D();
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( // 设置柱子上显示的数字在上方显示
                ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        renderer.setItemLabelAnchorOffset(10D);// 设置柱形图上的文字偏离值
        renderer.setBaseItemLabelsVisible(true);
        renderer.setMaximumBarWidth(0.1);// 设置柱子宽度 
        renderer.setBaseOutlinePaint(Color.BLACK); // 设置柱子边框颜色
        pieplot.setRenderer(renderer);
        pieplot.setOutlinePaint(new Color(236, 233, 216)); // 设置图片边框颜色


        // X 轴      
        CategoryAxis domainAxis = pieplot.getDomainAxis();
        domainAxis.setTickLabelPaint(Color.BLUE); // 字体颜色      
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的label斜显示
        return new ChartPanel(chart, dataset.getColumnCount()*75, 300, dataset.getColumnCount()*75, 300, dataset.getColumnCount()*75, 300, true, true, true, true, true, true);
    }


猜你喜欢

转载自blog.csdn.net/wang666zoo/article/details/70314015