首页 > 其他分享 >jfreechart常用设置demo(不断更新)

jfreechart常用设置demo(不断更新)

时间:2023-09-21 12:37:16浏览次数:35  
标签:jfree demo 不断更新 chart new dataset jfreechart import org


     今天要用到jfreechart的时候到网上看自己的demo才发现,我原来的demo不知什么时候不小心删掉了!我郁闷!不过还好,在我的备份盘中又找到了一份备份资料。现在再重新更新上来!

     背景图片为2006110821482378503.jpg放入d盘。

72729572

一、柱图demo

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Image;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.CategoryAnnotation;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.LabelBlock;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
import org.jfree.ui.VerticalAlignment;

/**
 * 生成一个带有多组数据的柱图(最基础图形)
 * 
 */
public class BarChartDemo1 extends ApplicationFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * 生成柱图
     *
     * @param title  大标题.
     */
    public BarChartDemo1(String title) {
        super(title);
        CategoryDataset dataset = createDataset();
        CategoryDataset dataset1 = createDataset1();
        JFreeChart chart = createChart(title,dataset,dataset1);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * 返回条形图所需要的数据(CategoryDataset基础数据类型).
     *
     * @return The dataset.
     */
    private static CategoryDataset createDataset() {

        // 行数据名称
        String series1 = "名称";
        String series2 = "Second";
        String series3 = "Third";

        // 栏数据名称
        String category1 = "栏数据";
        String category2 = "Category 2";
        String category3 = "Category 3";
        String category4 = "Category 4";
        String category5 = "Category 5";

        // 详细数据
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(1.0, series1, " ");
        dataset.addValue(4.0, series1, "");
        dataset.addValue(3.0, series1, category3);
        dataset.addValue(5.0, series1, category4);
        dataset.addValue(-5.0, series1, category5);

        dataset.addValue(5.0, series2, category1);
        dataset.addValue(7.0, series2, category1);
        dataset.addValue(6.0, series2, category3);
        dataset.addValue(8.0, series2, category4);
        dataset.addValue(4.0, series2, category5);

        dataset.addValue(4.0, series3, category1);
        dataset.addValue(3.0, series3, category1);
        dataset.addValue(2.0, series3, category3);
        dataset.addValue(3.0, series3, category4);
        dataset.addValue(6.0, series3, category5);

        return dataset;

    }
    /**
     * 返回条形图所需要的数据(CategoryDataset基础数据类型).
     *
     * @return The dataset.
     */
    private static CategoryDataset createDataset1() {

        // 行数据名称
        String series1 = "名称1";
        String series2 = "Second1";
        String series3 = "Third1";

        // 栏数据名称
        String category1 = "栏数据";
        String category2 = "Category 2";
        String category3 = "Category 3";
        String category4 = "Category 4";
        String category5 = "Category 5";

        // 详细数据
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(11.0, series1, category1);
        dataset.addValue(14.0, series1, category1);
        dataset.addValue(13.0, series1, category3);
        dataset.addValue(15.0, series1, category4);
        dataset.addValue(-15.0, series1, category5);

        dataset.addValue(15.0, series2, category1);
        dataset.addValue(17.0, series2, category2);
        dataset.addValue(16.0, series2, category3);
        dataset.addValue(18.0, series2, category4);
        dataset.addValue(14.0, series2, category5);

        dataset.addValue(14.0, series3, category1);
        dataset.addValue(13.0, series3, category1);
        dataset.addValue(12.0, series3, category3);
        dataset.addValue(13.0, series3, category4);
        dataset.addValue(16.0, series3, category5);

        return dataset;

    }

    /**
     * 生成一个柱图图片
     *
     * @param dataset  the dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(String title,CategoryDataset dataset,CategoryDataset dataset1) {

        // create the chart...
        JFreeChart chart = ChartFactory.createBarChart(
        		title,       // chart title
            "Category",               // domain axis label
            "Value",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // 生成图片方向
            true,                     // 是否显示图例
            true,                     // 是否显示注释
            false                     // URLs?
        );
        //总图片设置
        //设置背景颜色的图表
        GradientPaint gp = new GradientPaint(0.0f, 0.0f, Color.blue,
        		500.0f, 270.0f, new Color(0, 0, 64));
        gp = null;
        chart.setBackgroundPaint(gp);
        //设置图片背景图片
        if(gp == null){
        Image image = null;
		try {
			image = ImageIO.read(new  java.io.File("D:\\2006110821482378503.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        chart.setBackgroundImage(image);
        }
        //设置图片的边框是否显示
        chart.setBorderVisible(true);
        //设置背景图片透明度(0.0~1.0)
        chart.setBackgroundImageAlpha(1.0f);
        //是否字体去锯齿
        chart.setAntiAlias(true);
        //主标题字体设置
        TextTitle text = chart.getTitle();
        Font font = new Font("黑体",Font.BOLD,20);
		text.setFont(font);
		chart.setTitle(text);
		chart.addSubtitle(new TextTitle( "Source: http://thomas0988.iteye.com/",	new Font("Dialog", Font.ITALIC, 10))); 
        
        
		//主图片主样式实例化
        CategoryPlot plot=chart.getCategoryPlot();
        //设置图片的背景颜色
        plot.setBackgroundPaint(Color.lightGray);
        //设置图片的透明度(0.0~1.0)
        plot.setBackgroundAlpha(1.0f);
        //设置x轴刻度线颜色
        plot.setDomainGridlinePaint(Color.white);
        plot.setDomainGridlinesVisible(true);
        //设置y轴刻度线颜色
        plot.setRangeGridlinePaint(Color.white);
        plot.setRangeGridlinesVisible(true);
        //设置柱子的透明度
        plot.setForegroundAlpha(0.9f);
        //设置x轴显示位置
        plot.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
        //设置y轴显示位置
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
        //设置横纵坐标与axises之间的间距
        plot.setAxisOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
        
        
        //建构单个特殊间隔标记
        IntervalMarker target = new IntervalMarker(4.5, 7.5);
        //目标范围名称
		target.setLabel("Target Range 目标范围"); 
		//设置字体
		target.setLabelFont(new Font("SansSerif", Font.ITALIC, 11)); 
		//区域名称在边界位置
		target.setLabelAnchor(RectangleAnchor.CENTER); 
		//区域名称的起始位置
		target.setLabelTextAnchor(TextAnchor.CENTER_RIGHT);
		//区域名称的颜色
		target.setPaint(new Color(222, 222, 255, 128)); 
		//区域名称的透明度
		target.setAlpha(1.0f);
		//使区域标记有效
		plot.addRangeMarker(target, Layer.FOREGROUND); 
		
		
        //bar实例化
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        //设置条形图上bar是否显示背景阴影
        renderer.setDrawBarOutline(false);
        //设置bar最大宽度百分比
        renderer.setMaximumBarWidth(0.10D);
        //设置bar显示所有value 
        //renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        //renderer.setBaseItemLabelsVisible(true);
        
        //实例化栏数据第几个bar显示value
       
        renderer.setSeriesItemLabelGenerator(0,new StandardCategoryItemLabelGenerator("{2}辆", NumberFormat.getNumberInstance()) );
        renderer.setSeriesItemLabelFont(0, new Font("黑体",Font.BOLD,20), true);
        //使该设置有效
        renderer.setSeriesItemLabelsVisible(0,true);
        // 手动设置条形图上bar设置颜色
        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.red,
                0.0f, 0.0f, new Color(64, 0, 0));
        GradientPaint gp2 = 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.setSeriesPaint(2, gp2);
        
        //bar间的宽度
        renderer.setItemMargin(0);
        //bar阴影
        renderer.setShadowVisible(false);
        //使设置生效
        renderer.setBaseItemLabelsVisible(true);
        //生成url连接(没有试验成功, 好像只有在jsp页面才ok,在jsp里面有个简单demo,做数据挖掘研究用,还没有改善)
        renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("http://www.yahoo.com.cn"));
        
        Font font1 = new Font("SansSerif", Font.BOLD, 12);   
        // 添加在线用户注释   
       // XYTextAnnotation userAnn = new XYTextAnnotation(" 峰值时间:",10d,10d);   
      //  userAnn.setFont(font1);   
       // userAnn.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);   
       // plot.addAnnotation((CategoryAnnotation) userAnn,true);  
        //设置y轴区域颜色
        plot.addRangeMarker(createIntervalMarker(0, 2, Color.GREEN, "0~2"), Layer.BACKGROUND); 

        plot.addRangeMarker(createIntervalMarker(2, 4, Color.BLACK, "2~4"), Layer.BACKGROUND); 

        plot.addRangeMarker(createIntervalMarker(4, 6, Color.PINK, "4~6"), Layer.FOREGROUND); 

        plot.addRangeMarker(createIntervalMarker(6, 8, Color.BLUE, "6~8"), Layer.BACKGROUND); 

        plot.addRangeMarker(createIntervalMarker(8, 10, Color.YELLOW, "8~10"), Layer.BACKGROUND); 

        
        
        
        //x坐标轴实例化
        CategoryAxis xaxis = plot.getDomainAxis();
        //坐标轴标尺值字体
		xaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
		xaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,20));
		//设置栏目名称的旋转角度
		xaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
		//设置栏数据距离百分比(0.0~1.0)
		xaxis.setCategoryMargin(0.1); 
		//设置距离图片左端距离,参数为图片的百分比
		xaxis.setLowerMargin(0.05);
	    //设置距离图片右端距离
		xaxis.setUpperMargin(0.05);

		
        //y坐标轴实例化
        ValueAxis yaxis = plot.getRangeAxis();
        //坐标轴标尺值字体
		yaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
		yaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,15));
		// 设置的范围轴,以显示只有整数
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        //装载设定
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        //将纵坐标间距设置为3 
        rangeAxis.setTickUnit(new NumberTickUnit(3));
        
        //设置最高的一个柱与图片顶端的距离
        rangeAxis.setUpperMargin(0.2);
        //设置最低的一个柱与图片底端的距离
        rangeAxis.setLowerMargin(0.05);
        //设置竖坐标标签的旋转角度
        rangeAxis.setLabelAngle(0.05);
        
        
        //设置第二y轴坐标名
        NumberAxis numberAxis = new NumberAxis("y轴值");
        //将第二纵坐标间距设置为5
        numberAxis.setTickUnit(new NumberTickUnit(5));
        //装载第二y轴坐标
        plot.setRangeAxis(1, numberAxis);
        //设置第二y轴坐标位置
        plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
        //装载第二组数据
        plot.setDataset(1, dataset1);
        //是第二组数据生成线图
        plot.setRenderer(1, new LineAndShapeRenderer());
        //第二个数据映射到第一个数据域x轴
        plot.mapDatasetToDomainAxis(1,0);
        //第二个数据映射到第二个数据域y轴
        plot.mapDatasetToRangeAxis(1,1);
        
        
        
        //修改图例的字体 
        LegendTitle legend = chart.getLegend(); 
        legend.setItemFont(new Font("宋体", Font.BOLD, 14));
        //设置图例中图示的位置(最短距离)
        legend.setPadding(10D,10D,10D,10D);
        //设置图例的背景颜色
        legend.setBackgroundPaint(Color.CYAN);
        //设置图例在图片中的位置(上下左右)
        legend.setPosition(RectangleEdge.RIGHT);
        //设置图例在图片中的位置(左中右)
        legend.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //设置图例在图片中的位置(上中下)
        legend.setVerticalAlignment(VerticalAlignment.TOP);
        //手动设置图例是否可见
        legend.setVisible(false);
       
        //重新生成图例(契机)
        //实例化图例
        LegendTitle legendtitle = new LegendTitle(chart.getPlot());
        //设置图片背景颜色
        legendtitle.setBackgroundPaint(Color.CYAN);
        //实例化图例块
        BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
        //图例的边界宽度
        blockcontainer.setBorder(1.0D, 1.0D, 1.0D, 1.0D);
        //自定义图例标题(标签)
        LabelBlock labelblock = new LabelBlock("test菜单:", new Font("宋体", 1, 12));
        //自定义边界距离
        labelblock.setPadding(5D, 5D, 5D, 5D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(labelblock, RectangleEdge.TOP);
        //自定义图例标题(标签)
        LabelBlock labelblock1 = new LabelBlock("by thomas 2008.12.19");
        //自定义边界距离
        labelblock1.setPadding(8D, 20D, 2D, 5D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(labelblock1, RectangleEdge.BOTTOM);
        //获得原图例的实例项目集
        BlockContainer blockcontainer1 = legendtitle.getItemContainer();
        //自定义边界距离
        blockcontainer1.setPadding(2D, 10D, 5D, 2D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(blockcontainer1);
        //画出图例边界
        legendtitle.setWrapper(blockcontainer);
        //设置图例在图片中的位置(上下左右)
        legendtitle.setPosition(RectangleEdge.RIGHT);
        //设置图例在图片中的位置(左中右)
        legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        
        //使图例生成有效
        chart.addSubtitle(legendtitle);
        //让上面所有的主要样式设置都生效
        plot.setRenderer(renderer);
        
        return chart;

    }
    
    /**
     * <b>建构间隔标记。针对y轴<b>
     * @param low 区域起始位置
     * @param height 区域结束位置
     * @param color 区域颜色
     * @param label 区域名称(统一都在y轴的左中位置)
     * @return intervalmarker 返回统一建构间隔标记
     */
    private static IntervalMarker createIntervalMarker(double low, double height, Color color, String label) { 
    	IntervalMarker intervalmarker = new IntervalMarker(low, height); 

        intervalmarker.setPaint(color); 

        intervalmarker.setLabel(label); 

        intervalmarker.setLabelFont(new Font("宋体", Font.BOLD, 8)); 

        intervalmarker.setLabelAnchor(RectangleAnchor.LEFT); 

        intervalmarker.setLabelTextAnchor(TextAnchor.CENTER_LEFT); 

        return intervalmarker; 

    } 
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        BarChartDemo1 demo = new BarChartDemo1("Bar 图 Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

 

 

二、饼图demo

/**
 * 生成一个2D饼图
 * 
 * */

package org.jfree.chart.demo;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Image;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.LabelBlock;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.VerticalAlignment;

/**
 * 生成一个2D饼图(基础图形)
 */
public class PieChartDemo1 extends ApplicationFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * Default constructor.
     *
     * @param title  the frame title.
     */
    public PieChartDemo1(String title) {
        super(title);
        setContentPane(createDemoPanel(title));
    }

    /**
     * Creates a sample dataset.
     *
     * @return A sample dataset.
     */
    private static PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("One", new Double(43.2));
        dataset.setValue("Two", new Double(10.0));
        dataset.setValue("Three", new Double(27.5));
        dataset.setValue("Four", new Double(17.5));
        dataset.setValue("Five", new Double(11.0));
        dataset.setValue("Six", new Double(19.4));
        return dataset;
    }

    /**
     * Creates a chart.
     *
     * @param dataset  the dataset.
     *
     * @return A chart.
     */
    private static JFreeChart createChart(String title,PieDataset dataset) {

        JFreeChart chart = ChartFactory.createPieChart(
        		title,  // chart title
            dataset,             // data
            true,               // include legend
            true,
            false
        );
        //总图片设置
        //设置背景颜色的图表
        GradientPaint gp = new GradientPaint(0.0f, 0.0f, Color.blue,
        		500.0f, 270.0f, new Color(0, 0, 64));
        gp = null;
        chart.setBackgroundPaint(gp);
        //设置图片背景图片
        if(gp == null){
        Image image = null;
		try {
			image = ImageIO.read(new  java.io.File("D:\\2006110821482378503.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        chart.setBackgroundImage(image);
        }
        //设置图片的边框是否显示
        chart.setBorderVisible(false);
        //设置背景图片透明度(0.0~1.0)
        chart.setBackgroundImageAlpha(1.0f);
        //是否字体去锯齿
        chart.setAntiAlias(true);
        
        
        
		//手动设置饼图样式
        PiePlot plot = (PiePlot) chart.getPlot();
        //设置图片的背景颜色
        plot.setBackgroundPaint(Color.lightGray);
        //设置图片的透明度(0.0~1.0)
        plot.setBackgroundAlpha(0.6f);
        //把Lable 为"One" 的那一块”挖“出来30%
        plot.setExplodePercent("One",0.3D); 
        plot.setExplodePercent("Six",0.3D);
        //设置无数据时的信息    
        plot.setNoDataMessage("无对应的数据,请重新查询。");    
   
        // 设置无数据时的信息显示颜色    
        plot.setNoDataMessagePaint(Color.red);    
   
        // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位    
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(    
                "{0}={1}辆({2})", NumberFormat.getNumberInstance(),    
                new DecimalFormat("0.00%")));    
        // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例 
        plot.setLabelFont(new Font("宋体", Font.BOLD, 14));
        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(    
                "{0}={1}({2})"));    
   
        plot.setLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12));    
   
        // 指定图片的透明度(0.0-1.0)    
        plot.setForegroundAlpha(1.0f);    
        // 指定显示的饼图上圆形(false)还椭圆形(true)    
        plot.setCircular(true, false);    
   
        // 设置第一个 饼块section 的开始位置,默认是12点钟方向    
        plot.setStartAngle(90);    
   
        // 设置分饼颜色    
        plot.setSectionPaint("Five", new Color(244, 194, 144));    
        plot.setSectionPaint("Six", new Color(144, 233, 144));    
        
       
        
        //修改图例的字体 
        LegendTitle legend = chart.getLegend(); 
        legend.setItemFont(new Font("宋体", Font.BOLD, 14));
        //设置图例中图示的位置(最短距离)
        legend.setPadding(10D,10D,10D,10D);
        //设置图例的背景颜色
        legend.setBackgroundPaint(Color.CYAN);
        //设置图例在图片中的位置(上下左右)
        legend.setPosition(RectangleEdge.RIGHT);
        //设置图例在图片中的位置(左中右)
        legend.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //设置图例在图片中的位置(上中下)
        legend.setVerticalAlignment(VerticalAlignment.TOP);
        //手动设置图例是否可见
        legend.setVisible(false);
       
        //重新生成图例(契机)
        //实例化图例
        LegendTitle legendtitle = new LegendTitle(chart.getPlot());
        //设置图片背景颜色
        legendtitle.setBackgroundPaint(Color.CYAN);
        //实例化图例块
        BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
        //图例的边界宽度
        blockcontainer.setBorder(1.0D, 1.0D, 1.0D, 1.0D);
        //自定义图例标题(标签)
        LabelBlock labelblock = new LabelBlock("test菜单:", new Font("宋体", 1, 12));
        //自定义边界距离
        labelblock.setPadding(5D, 5D, 5D, 5D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(labelblock, RectangleEdge.TOP);
        //自定义图例标题(标签)
        LabelBlock labelblock1 = new LabelBlock("by thomas 2008.12.19");
        //自定义边界距离
        labelblock1.setPadding(8D, 20D, 2D, 5D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(labelblock1, RectangleEdge.BOTTOM);
        //获得原图例的实例项目集
        BlockContainer blockcontainer1 = legendtitle.getItemContainer();
        //自定义边界距离
        blockcontainer1.setPadding(2D, 10D, 5D, 2D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(blockcontainer1);
        //画出图例边界
        legendtitle.setWrapper(blockcontainer);
        //设置图例在图片中的位置(上下左右)
        legendtitle.setPosition(RectangleEdge.RIGHT);
        //设置图例在图片中的位置(左中右)
        legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        
        chart.addSubtitle(legendtitle);
        
        
        return chart;

    }

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public static JPanel createDemoPanel(String title) {
    	JFreeChart chart = createChart(title,createDataset());
        return new ChartPanel(chart);
    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {

        // ******************************************************************
        //  More than 150 demo applications are included with the JFreeChart
        //  Developer Guide...for more information, see:
        //
        //  >   http://www.object-refinery.com/jfreechart/guide.html
        //
        // ******************************************************************

        PieChartDemo1 demo = new PieChartDemo1("Pie Chart Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

 

 

三、线图demo

* 生成一个线形图
 * */

package org.jfree.chart.demo;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.awt.geom.GeneralPath;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.LabelBlock;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
import org.jfree.ui.VerticalAlignment;
import org.jfree.util.ShapeUtilities;

/**
 * 生成一个带有多组数据的线形图(最基础图形)
 * 
 */
public class LineChartDemo1 extends ApplicationFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * 生成条形图
     *
     * @param title  大标题.
     */
    public LineChartDemo1(String title) {
        super(title);
        CategoryDataset dataset = createDataset();
        JFreeChart chart = createChart(title,dataset);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * 返回条形图所需要的数据(CategoryDataset基础数据类型).
     *
     * @return The dataset.
     */
    private static CategoryDataset createDataset() {

        // 行数据名称
        String series1 = "First";
        String series2 = "Second";
        String series3 = "Third";

        // 栏数据名称
        String category1 = "Category 1";
        String category2 = "Category 2";
        String category3 = "Category 3";
        String category4 = "Category 4";
        String category5 = "Category 5";

        // 详细数据
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(1.0, series1, category1);
        dataset.addValue(4.0, series1, category2);
        dataset.addValue(3.0, series1, category3);
        dataset.addValue(5.0, series1, category4);
        dataset.addValue(5.0, series1, category5);

        dataset.addValue(5.0, series2, category1);
        dataset.addValue(7.0, series2, category2);
        dataset.addValue(6.0, series2, category3);
        dataset.addValue(8.0, series2, category4);
        dataset.addValue(4.0, series2, category5);

        dataset.addValue(4.0, series3, category1);
        dataset.addValue(3.0, series3, category2);
        dataset.addValue(2.0, series3, category3);
        dataset.addValue(3.0, series3, category4);
        dataset.addValue(6.0, series3, category5);

        dataset.addValue(0.40, series1, category1);
        dataset.addValue(0.51, series1, category2);
        dataset.addValue(0.3, series1, category3);
        dataset.addValue(0.5, series1, category4);
        dataset.addValue(0.5, series1, category5);

        
        return dataset;

    }

    /**
     * 生成一个线形图图片
     *
     * @param dataset  the dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(String title,CategoryDataset dataset) {

        // create the chart...
        JFreeChart chart = ChartFactory.createLineChart(
        		title,       // chart title
            "Category",               // domain axis label
            "Value",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            true,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );
        //总图片设置
        //设置背景颜色的图表
        GradientPaint gp = new GradientPaint(0.0f, 0.0f, Color.blue,
        		500.0f, 270.0f, new Color(0, 0, 64));
        gp = null;
        chart.setBackgroundPaint(gp);
        //设置图片背景图片
        if(gp == null){
        Image image = null;
		try {
			image = ImageIO.read(new  java.io.File("D:\\2006110821482378503.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        chart.setBackgroundImage(image);
        }
        //设置图片的边框是否显示
        chart.setBorderVisible(true);
        //设置背景图片透明度(0.0~1.0)
        chart.setBackgroundImageAlpha(1.0f);
        //是否字体去锯齿
        chart.setAntiAlias(true);
        //主标题字体设置
        TextTitle text = chart.getTitle();
        Font font = new Font("黑体",Font.BOLD,20);
		text.setFont(font);
		chart.setTitle(text);
		chart.addSubtitle(new TextTitle( "Source: http://thomas0988.iteye.com/",	new Font("Dialog", Font.ITALIC, 10))); 
        
        
        
		//主图片主样式实例化
        CategoryPlot plot=chart.getCategoryPlot();
        //设置图片的背景颜色
        plot.setBackgroundPaint(Color.lightGray);
        //设置图片的透明度(0.0~1.0)
        plot.setBackgroundAlpha(1.0f);
        //设置x轴刻度线颜色
        plot.setDomainGridlinePaint(Color.white);
        plot.setDomainGridlinesVisible(true);
        //设置y轴刻度线颜色
        plot.setRangeGridlinePaint(Color.white);
        plot.setRangeGridlinesVisible(true);
        //设置柱子的透明度
        plot.setForegroundAlpha(0.9f);
        //设置x轴显示位置
        plot.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
        //设置y轴显示位置
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
        plot.addRangeMarker(createIntervalMarker(0, 2, Color.GREEN, "0~2"), Layer.BACKGROUND); 

        plot.addRangeMarker(createIntervalMarker(2, 4, Color.BLACK, "2~4"), Layer.BACKGROUND); 

        plot.addRangeMarker(createIntervalMarker(4, 6, Color.PINK, "4~6"), Layer.FOREGROUND); 

        plot.addRangeMarker(createIntervalMarker(6, 8, Color.BLUE, "6~8"), Layer.BACKGROUND); 

        plot.addRangeMarker(createIntervalMarker(8, 10, Color.YELLOW, "8~10"), Layer.BACKGROUND); 
        //Line实例化,获得LineAndShapeRenderer类的实例,目的是设置线形的绘制属性
        LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer();
        
        //手动设置条形图上bar设置颜色
        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.red,
                0.0f, 0.0f, new Color(64, 0, 0));
        GradientPaint gp2 = 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.setSeriesPaint(2, gp2);
        renderer.setBaseOutlinePaint(Color.BLACK);
        
      
        //设置第一条线有数据点
        renderer.setSeriesShapesVisible(0, true); 
        //renderer.setDrawOutlines(true); 
        //可以重新设置数据点颜色默认为白色方形
        renderer.setUseFillPaint(true); 
        //数据点填充颜色
        renderer.setFillPaint(Color.yellow); 
        //设置数据点为圆形(x为上下偏离,y为左右偏离,w为圆形左右距离,h为圆形上下距离)
        //renderer.setSeriesShape(0,new Ellipse2D.Double(-3.0,-3.0,6.0, 6.0));
        //设置数据点为菱形(x为边,y为交)
        //renderer.setSeriesShape(0,ShapeUtilities.createDiagonalCross(3.0f, 1.0f));
        //设置数据点为菱形(x为双对边距离)
        //renderer.setSeriesShape(0,ShapeUtilities.createDiamond(6.0f));
        //设置数据点为倒三角形(x为三角距离)
        //renderer.setSeriesShape(0,ShapeUtilities.createDownTriangle(3.0f));
        //设置数据点为正方形(x为边,y为角)
        //renderer.setSeriesShape(0,ShapeUtilities.createRegularCross(3.0f, 1.0f));
        //设置数据点为上三角形(x为三角距离)
        renderer.setSeriesShape(0,ShapeUtilities.createUpTriangle(3.0f));
        
        //设置每个数据点的连接线是否显示
        renderer.setSeriesLinesVisible(0, true);
        
        
        
        
        /// 设置柱形图上的文字偏离值
        renderer.setItemLabelAnchorOffset(5D);
        /// 设置柱形图上的文字
        renderer.setBaseItemLabelFont(new Font("arial", Font.PLAIN, 10), true);
        ///显示每个柱的数值,并修改该数值的字体属性
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        //设置数值在柱图的方位
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        //设置数值生效
        renderer.setBaseItemLabelsVisible(true);
        
        //设置bar生效
        plot.setRenderer(renderer);
        
        
        
        
        
        
        //x坐标轴实例化
        CategoryAxis xaxis = plot.getDomainAxis();
        //坐标轴标尺值字体
		xaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
		xaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,20));
		//设置栏目名称的旋转角度
		xaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
		//设置距离图片左端距离,参数为图片的百分比
		xaxis.setLowerMargin(0.05);
	    //设置距离图片右端距离
		xaxis.setUpperMargin(0.05);
		

		
        //y坐标轴实例化
        ValueAxis yaxis = plot.getRangeAxis();
        //坐标轴标尺值字体
		yaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
		yaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,15));
		
		// 设置的范围轴,以显示只有整数
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        //将纵坐标间距设置为3 
        rangeAxis.setTickUnit(new NumberTickUnit(3));
        //设置最高的一个柱与图片顶端的距离
        rangeAxis.setUpperMargin(0.2);
        //设置最低的一个柱与图片底端的距离
        rangeAxis.setLowerMargin(0.05);
        //设置竖坐标标签的旋转角度
        rangeAxis.setLabelAngle(0.05);
        //设置数值最小范围
        rangeAxis.setLowerBound(0.3);
        //设置数值最大范围
        rangeAxis.setUpperBound(5);
        
        
       
        
        //修改图例的字体 
        LegendTitle legend = chart.getLegend(); 
        legend.setItemFont(new Font("宋体", Font.BOLD, 14));
        //设置图例中图示的位置(最短距离)
        legend.setPadding(10D,10D,10D,10D);
        //设置图例的背景颜色
        legend.setBackgroundPaint(Color.CYAN);
        //设置图例在图片中的位置(上下左右)
        legend.setPosition(RectangleEdge.RIGHT);
        //设置图例在图片中的位置(左中右)
        legend.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //设置图例在图片中的位置(上中下)
        legend.setVerticalAlignment(VerticalAlignment.TOP);
        //手动设置图例是否可见
        legend.setVisible(false);
       
        //重新生成图例(契机)
        //实例化图例
        LegendTitle legendtitle = new LegendTitle(chart.getPlot());
        //设置图片背景颜色
        legendtitle.setBackgroundPaint(Color.CYAN);
        //实例化图例块
        BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
        //图例的边界宽度
        blockcontainer.setBorder(1.0D, 1.0D, 1.0D, 1.0D);
        //自定义图例标题(标签)
        LabelBlock labelblock = new LabelBlock("test菜单:", new Font("宋体", 1, 12));
        //自定义边界距离
        labelblock.setPadding(5D, 5D, 5D, 5D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(labelblock, RectangleEdge.TOP);
        //自定义图例标题(标签)
        LabelBlock labelblock1 = new LabelBlock("by thomas 2009.01.04");
        //自定义边界距离
        labelblock1.setPadding(8D, 20D, 2D, 5D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(labelblock1, RectangleEdge.BOTTOM);
        //获得原图例的实例项目集
        BlockContainer blockcontainer1 = legendtitle.getItemContainer();
        //自定义边界距离
        blockcontainer1.setPadding(2D, 10D, 5D, 2D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(blockcontainer1);
        //画出图例边界
        legendtitle.setWrapper(blockcontainer);
        //设置图例在图片中的位置(上下左右)
        legendtitle.setPosition(RectangleEdge.RIGHT);
        //设置图例在图片中的位置(左中右)
        legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        
        chart.addSubtitle(legendtitle);
        
        
        return chart;

    }
    private static IntervalMarker createIntervalMarker(double low, double height, Color color, String label) { 
    	IntervalMarker intervalmarker = new IntervalMarker(low, height); 

        intervalmarker.setPaint(color); 

        intervalmarker.setLabel(label); 

        intervalmarker.setLabelFont(new Font("宋体", Font.BOLD, 8)); 

        intervalmarker.setLabelAnchor(RectangleAnchor.LEFT); 

        intervalmarker.setLabelTextAnchor(TextAnchor.CENTER_LEFT); 

        return intervalmarker; 

    } 
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        LineChartDemo1 demo = new LineChartDemo1("Line Chart Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

 

四、雷达图demo

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.LabelBlock;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.chart.renderer.DefaultPolarItemRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.VerticalAlignment;

/**
 * 生成一个带有多组数据的条形图(最基础图形)
 * 
 */
public class PolarChartDemo1 extends ApplicationFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * 生成条形图
     *
     * @param title  大标题.
     */
    public PolarChartDemo1(String title) {
        super(title);
        XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new Dimension(800, 600));
        setContentPane(chartPanel);
    }

    /**
     * 返回条形图所需要的数据(CategoryDataset基础数据类型).
     *
     * @return The dataset.
     */
    private static XYDataset createDataset() {

         // 详细数据
    	XYSeriesCollection xySeriesCollection = new XYSeriesCollection();   
    	  
    	XYSeries   xyseries0   =   new   XYSeries("one");   
        xyseries0.add(0.0D,   91D);   
        xyseries0.add(90D,   100D);   
        xyseries0.add(180D,   78D);   
        xyseries0.add(270D,   84D);
        xySeriesCollection.addSeries(xyseries0);
       
        XYSeries   xyseries1   =   new   XYSeries("two");   
        xyseries1.add(90D,   11.199999999999999D);   
        xyseries1.add(180D,   21.399999999999999D);   
        xyseries1.add(230D,   17.300000000000001D); 
        xyseries1.add(355D,   10.9D); 
        
        xySeriesCollection.addSeries(xyseries1);
        XYSeries   xyseries2   =   new   XYSeries("three");   
        xyseries2.add(90D,   90.199999999999999D);   
        xyseries2.add(180D,   90.399999999999999D);   
        xyseries2.add(250D,   90.300000000000001D);   
        xyseries2.add(355D,   100D);   
        xySeriesCollection.addSeries(xyseries2);
            
        return xySeriesCollection;  
    
    }

    /**
     * 生成一个条形图图片
     *
     * @param dataset  the dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(XYDataset dataset) {

        // create the chart...
        JFreeChart chart = ChartFactory.createPolarChart(
            "Bar Chart Demo 1",       // chart title
            dataset,                  // data
            true,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        //总图片设置
        //设置背景颜色的图表
        GradientPaint gp = new GradientPaint(0.0f, 0.0f, Color.blue,
        		500.0f, 270.0f, new Color(0, 0, 64));
        gp = null;
        chart.setBackgroundPaint(gp);
        //设置图片背景图片
        if(gp == null){
        Image image = null;
		try {
			image = ImageIO.read(new  java.io.File("D:\\2006110821482378503.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        chart.setBackgroundImage(image);
        }
        //设置图片的边框是否显示
        chart.setBorderVisible(false);
        //设置背景图片透明度(0.0~1.0)
        chart.setBackgroundImageAlpha(1.0f);
        //是否字体去锯齿
        chart.setAntiAlias(true);
        
        
        
		//手动设置饼图样式
        PolarPlot plot = (PolarPlot) chart.getPlot();
        //设置图片的背景颜色
        plot.setBackgroundPaint(Color.lightGray);
        //设置图片的透明度(0.0~1.0)
        plot.setBackgroundAlpha(0.6f);
        //设置无数据时的信息    
        plot.setNoDataMessage("无对应的数据,请重新查询。");    
   
        // 设置无数据时的信息显示颜色    
        plot.setNoDataMessagePaint(Color.red);    
   
        // 指定图片的透明度(0.0-1.0)    
        plot.setForegroundAlpha(1.0f);   
        
        plot.setBackgroundPaint(Color.lightGray);
       /* plot.addCornerTextItem("Corner       Item       1 ");
        plot.addCornerTextItem("Corner       Item       2 ");
        plot.setBackgroundImageAlignment(plot.MINIMUM_HEIGHT_TO_DRAW);
        plot.setAngleTickUnit(new NumberTickUnit(45));*/
        // plot.setAngleGridlinesVisible(false);

        // 设置雷达颜色
        GradientPaint gradientpaint3 = new GradientPaint(0.0F, 0.0F,
          Color.black, 0.0F, 0.0F, Color.black);
        plot.setRadiusGridlinePaint(gradientpaint3);//

        // 两个四边形颜色
        GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.red,
          0.0F, 0.0F, Color.red); //
        GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F,
                Color.blue, 0.0F, 0.0F, Color.blue); // 
             // 设置两个四边形颜色
        DefaultPolarItemRenderer renderer = new DefaultPolarItemRenderer();
        renderer.setSeriesPaint(0, gradientpaint1);
        renderer.setSeriesPaint(1, gradientpaint2);
        renderer.setSeriesPaint(2, gradientpaint3);
        plot.setRenderer(renderer);

        NumberAxis numberaxis = (NumberAxis) plot.getAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        numberaxis.setAutoRange(false);
        numberaxis.setVisible(false);
        numberaxis.setTickUnit(new NumberTickUnit(5));// 设置雷达网格数量


        
       
        
        //修改图例的字体 
        LegendTitle legend = chart.getLegend(); 
        legend.setItemFont(new Font("宋体", Font.BOLD, 14));
        //设置图例中图示的位置(最短距离)
        legend.setPadding(10D,10D,10D,10D);
        //设置图例的背景颜色
        legend.setBackgroundPaint(Color.CYAN);
        //设置图例在图片中的位置(上下左右)
        legend.setPosition(RectangleEdge.RIGHT);
        //设置图例在图片中的位置(左中右)
        legend.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //设置图例在图片中的位置(上中下)
        legend.setVerticalAlignment(VerticalAlignment.TOP);
        //手动设置图例是否可见
        legend.setVisible(false);
       
        //重新生成图例(契机)
        //实例化图例
        LegendTitle legendtitle = new LegendTitle(chart.getPlot());
        //设置图片背景颜色
        legendtitle.setBackgroundPaint(Color.CYAN);
        //实例化图例块
        BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
        //图例的边界宽度
        blockcontainer.setBorder(1.0D, 1.0D, 1.0D, 1.0D);
        //自定义图例标题(标签)
        LabelBlock labelblock = new LabelBlock("test菜单:", new Font("宋体", 1, 12));
        //自定义边界距离
        labelblock.setPadding(5D, 5D, 5D, 5D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(labelblock, RectangleEdge.TOP);
        //自定义图例标题(标签)
        LabelBlock labelblock1 = new LabelBlock("by thomas 2008.12.19");
        //自定义边界距离
        labelblock1.setPadding(8D, 20D, 2D, 5D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(labelblock1, RectangleEdge.BOTTOM);
        //获得原图例的实例项目集
        BlockContainer blockcontainer1 = legendtitle.getItemContainer();
        //自定义边界距离
        blockcontainer1.setPadding(2D, 10D, 5D, 2D);
        //自定义图例标签的位置并使之装入自定义图例框
        blockcontainer.add(blockcontainer1);
        //画出图例边界
        legendtitle.setWrapper(blockcontainer);
        //设置图例在图片中的位置(上下左右)
        legendtitle.setPosition(RectangleEdge.RIGHT);
        //设置图例在图片中的位置(左中右)
        legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        
        chart.addSubtitle(legendtitle);
        return chart;

    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        PolarChartDemo1 demo = new PolarChartDemo1("Bar Chart Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

 

 

五、蜘蛛图demo

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Image;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.CategoryAnnotation;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.LabelBlock;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
import org.jfree.ui.VerticalAlignment;

/**
 * 生成一个带有多组数据的柱图(最基础图形)
 * 
 */
public class PolarChartDemo2 extends ApplicationFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * 生成柱图
     *
     * @param title  大标题.
     */
    public PolarChartDemo2(String title) {
        super(title);
        CategoryDataset dataset = createDataset();
        JFreeChart chart = createChart(title,dataset);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * 返回条形图所需要的数据(CategoryDataset基础数据类型).
     *
     * @return The dataset.
     */
    private static CategoryDataset createDataset() {

    	 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
         String group1 = "apple "; 
          
         dataset.addValue(5, group1, "w1"); 
         dataset.addValue(6, group1, "w2"); 
         dataset.addValue(4, group1, "w3"); 
         dataset.addValue(2, group1, "w4"); 
         dataset.addValue(5, group1, "w5"); 
         dataset.addValue(5, group1, "w6"); 
         dataset.addValue(5, group1, "w7"); 
         dataset.addValue(8, group1, "w8");

      

        return dataset;

    }
    

    /**
     * 生成一个柱图图片
     *
     * @param dataset  the dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(String title,CategoryDataset dataset) {
    	
    	SpiderWebPlot spiderwebplot = new SpiderWebPlot(dataset); 
    	JFreeChart chart = new JFreeChart("Test", TextTitle.DEFAULT_FONT,spiderwebplot, false); 
        LegendTitle legendtitle = new LegendTitle(spiderwebplot); 
        legendtitle.setPosition(RectangleEdge.BOTTOM); 
        chart.addSubtitle(legendtitle); 
        
        return chart;

    }
    
    /**
     * <b>建构间隔标记。针对y轴<b>
     * @param low 区域起始位置
     * @param height 区域结束位置
     * @param color 区域颜色
     * @param label 区域名称(统一都在y轴的左中位置)
     * @return intervalmarker 返回统一建构间隔标记
     */
    private static IntervalMarker createIntervalMarker(double low, double height, Color color, String label) { 
    	IntervalMarker intervalmarker = new IntervalMarker(low, height); 

        intervalmarker.setPaint(color); 

        intervalmarker.setLabel(label); 

        intervalmarker.setLabelFont(new Font("宋体", Font.BOLD, 8)); 

        intervalmarker.setLabelAnchor(RectangleAnchor.LEFT); 

        intervalmarker.setLabelTextAnchor(TextAnchor.CENTER_LEFT); 

        return intervalmarker; 

    } 
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        PolarChartDemo2 demo = new PolarChartDemo2("Bar 图 Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

 

标签:jfree,demo,不断更新,chart,new,dataset,jfreechart,import,org
From: https://blog.51cto.com/u_16255870/7552806

相关文章

  • RK3568开发笔记(十一):开发版buildroot固件移植一个ffmpeg播放rtsp的播放器Demo
    前言  目标开发任务还有个功能,就是播放rtsp摄像头,当然为了更好的坐这个个,我们必须支持rtsp播放失败之后重新尝试,比如5s重新尝试打开一次,从而保障联网后重新打开,然后达成这个功能。 Demo   补充  得益于方案上的buildroot已经移植了ffmpeg4.1.3。  ......
  • Java学习一路-Demo01-Java数据类型
    packagecom.chao.base;publicclassDemo01{/*Java数据类型两大类:基本数据类型和引用类型1.基本数据类型共8种:(1)数值类型分为:整数类型(4种):a.byte(字节型),占1个字节,取值范围(-128~127)b.short(短整型),占2个字节,取值范围......
  • Java学习之路-Demo02
    packagecom.chao.base;publicclassDemo02{publicstaticvoidmain(String[]args){//整数拓展:进制二进制0b十进制八进制0十六进制0xinti=10;inti1=010;//八进制0inti2=0x10;//十六进制0x0~9A~F16System......
  • Java学习之路-Demo03-类型转换
    packagecom.chao.base;publicclassDemo03{publicstaticvoidmain(String[]args){inti=128;byteb=(byte)i;//内存溢出doubled=i;//强制转换(类型)变量名高-->低例:int转byte,int转short,int转char等//自动转换......
  • 【微信小程序】【Demo】Picker模糊查询组件
    微信小程序Picker模糊查询组件 pickerSelect.jsComponent({options:{//在组件定义时的选项中启用多slot支持multipleSlots:true},/***组件的属性列表*/properties:{//父组件传递过来的数据列表items:{type:Array,......
  • RK3568开发笔记(十):开发板buildroot固件移植开发的应用Demo,启动全屏显示
    前言  上一篇,移植应用前的通讯接口工作和全屏工作都已经完成了。本篇移植开发的商业应用。<br>交叉编译好应用  (略),参照《RK3568开发笔记(八):开发板烧写buildroot固件(支持hdmi屏),搭建Qt交叉编译开发环境,编译一个Demo,目标板运行Demo测试》<br>解决全屏标题栏占用问题  交叉......
  • RK3568开发笔记(十):开发板buildroot固件移植开发的应用Demo,启动全屏显示
    前言  上一篇,移植应用前的通讯接口工作和全屏工作都已经完成了。本篇移植开发的商业应用。 交叉编译好应用  (略),参照《RK3568开发笔记(八):开发板烧写buildroot固件(支持hdmi屏),搭建Qt交叉编译开发环境,编译一个Demo,目标板运行Demo测试》 解决全屏标题栏占用问题......
  • jfreechart中文无法显示的问题
    jfreechart是一个开源免费的Java图表工具。一个简单的入门例子:packageplot;importorg.jfree.chart.ChartFactory;importorg.jfree.chart.ChartFrame;importorg.jfree.chart.JFreeChart;importorg.jfree.chart.plot.PlotOrientation;importorg.jfree.data.category.Def......
  • GPT之路(七) LangChain AI编成框架入门的第一个demo
    环境:Python3.11.4,LangChain0.0.2701.Langchain简介1.1 PythonLangchain官方文档大型语言模型(LLM)正在成为一种具有变革性的技术,使开发人员能够构建以前无法实现的应用程序。然而,仅仅依靠LLM还不足以创建一个真正强大的应用程序。它还需要其他计算资源或知识来源。Langch......
  • paddlespeech asr脚本demo
     概述paddlespeech是百度飞桨平台的开源工具包,主要用于语音和音频的分析处理,其中包含多个可选模型,提供语音识别、语音合成、说话人验证、关键词识别、音频分类和语音翻译等功能。本文介绍利用ps中的asr功能实现批量处理音频文件的demo。环境centos7.9Python3.10.3paddl......