⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mybarchartdemo.java

📁 开源图表组件JFreeChart的入门例程
💻 JAVA
字号:
/* ===========================================================
 * JFreeChart柱状图的示例 by:daocaorenwwt
 * ===========================================================
 */
import java.awt.Color;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.io.*;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryLabelGenerator;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
 * A simple demonstration application showing how to create a bar chart.
 */
public class MyBarChartDemo {

    public MyBarChartDemo() {
    }

    /**
     * Returns a sample dataset.
     * 
     * @return The dataset.
     */
    private CategoryDataset createDataset() {
        
        // row keys...
        String series1 = "2005年";
        String series2 = "2004年";

        // column keys...
        String category1 = "2004年";
		String category2 = "2005年";
        
        // create the dataset...
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(115648.3, series1, category1);
        dataset.addValue(116409.3, series2, category2);
        
        return dataset;
        
    }
    
    /**
     * Creates a sample chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return The chart.
     */
    private JFreeChart createChart(CategoryDataset dataset) {
        
        // create the chart...
        JFreeChart chart = ChartFactory.createBarChart(
            "月供电量棒图",         // chart title图表标题
            "年份",               // domain axis label
            "值(万KWH)",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // 横或竖显示柱状图  HORIZONTAL VERTICAL
            true,                     // 是否包含图例 即最下端的圆点与方块代表什么的小图例
            true,                     // tooltips?
            false                     // URLs?
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION  OF THE CHART...

        // set the background color for the chart...设置背景色
        chart.setBackgroundPaint(Color.white);//整个大背景的颜色

        // get a reference to the plot for further customisation...
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);//图区的背景
        plot.setDomainGridlinePaint(Color.white);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.red);//横线的颜色

        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);//false柱状图的边线比较模糊,true柱状图的边线比较清析
		renderer.setItemMargin(0.01);//与柱状图柱宽相关(百分比,值越小宽度越大)
        
        // set up gradient paints for series...给柱状图设置过度色
        GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 
            0.0f, 0.0f, new Color(0, 0, 64)
        );
        GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green, 
            0.0f, 0.0f, new Color(0, 64, 0)
        );
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);

/****以下三句用于在柱图上显示各项数字*/
renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
renderer.setItemLabelFont(new Font("黑体",Font.PLAIN,12));
renderer.setItemLabelsVisible(true);
		



        CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setLowerMargin(0.15); // 左边距
		domainAxis.setCategoryMargin(0.15); // 柱边距
		domainAxis.setUpperMargin(0.15); // 右边距
        /*domainAxis.setCategoryLabelPositions(
        CategoryLabelPositions.createUpRotationLabelPositions(0)//自定制倾斜度
        );*/
		domainAxis.setCategoryLabelPositions(
        CategoryLabelPositions.STANDARD//标准倾斜度 可供选项:STANDARD UP_90 DOWN_90 UP_45 DOWN_45
        );
        // OPTIONAL CUSTOMISATION COMPLETED.
        
        return chart;
        
    }
    
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
		MyBarChartDemo cyChart = new MyBarChartDemo();
        CategoryDataset dataset = cyChart.createDataset();
        JFreeChart chart = cyChart.createChart(dataset);
		try{
		File jpFile = new File("D:\\img\\myBarChartDemo.jpg");
		ChartUtilities.saveChartAsJPEG(jpFile,chart,300,400);
		}
		catch(Exception e){
		}
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -