`
talentkep
  • 浏览: 100451 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

jfree chart test

阅读更多
/*
 * Created on 2009-9-7
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.Peter.jfreechart;

import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.PieToolTipGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;

import com.peter.jfreechart.Util;
import java.awt.Font;
import java.awt.Color;
import java.awt.RenderingHints;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Iterator;

import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.*;
import org.jfree.data.time.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.ui.*;

/**
 * @author Peter Ke
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class TestMain {

    public static void main(String[] args) {
        tryPie();

        tryCategory();

        // tryLineChart();

        tryLineChart2();

        tryTimeChart();

        //picGeneratorTry();

        //picGenerator2();

        //JfreeChartOne one = new JfreeChartOne("CityInfoPort公司组织架构图");
        //one.pack();
        //one.setVisible(true);
    }

    static public void tryPie() {
        //      创建饼图数据对象

        DefaultPieDataset dfp = new DefaultPieDataset();

        dfp.setValue("manager", 25);
        dfp.setValue("市场人员", 35);
        dfp.setValue("开发人员", 20);
        dfp.setValue("后勤人员", 5);
        dfp.setValue("财务人员", 15);

        //Create JFreeChart object

        JFreeChart a = ChartFactory.createPieChart3D("WXIC公司组织架构图", dfp, true, true, true);

        //a = Util.configFont(a);
        //title title font
        /*
         * Font font=new Font("SimSun", Font.BOLD, 12); TextTitle txtTitle =
         * null; txtTitle = a.getTitle(); txtTitle.setFont(font); //bg color
         * a.setBackgroundPaint(new Color(11,95,141)); //label font PiePlot
         * pieplot = (PiePlot) a.getPlot(); pieplot.setLabelFont(new
         * Font("SimSun", 0, 12));//楼主这边设了什么字体 pieplot.setNoDataMessage("No data
         * available"); pieplot.setCircular(false); pieplot.setLabelGap(0.02D);
         * //item font a.getLegend().setItemFont(font);
         * //pieplot.setLabelFont(new Font("SimSun", Font.PLAIN, 12));
         */
        a = Util.configPiePlotFont(a);
        
        PiePlot pieplot = (PiePlot)a.getPlot();
//      render
        PieToolTipGenerator tp = new PieToolTipGenerator(){

            public String generateToolTip(PieDataset arg0, Comparable arg1) {
                return String.valueOf(arg0.getValue(arg1));
            }
        };
        
        StandardPieToolTipGenerator stp = new StandardPieToolTipGenerator();
        pieplot.setToolTipGenerator(stp);
        //pieplot.setLabelLinksVisible(true);
        //pieplot.setSectionOutlinesVisible(true);
        //pieplot.setSimpleLabels(true);
        //render
        setSection(pieplot);   
        setLabel(pieplot);   
        setNoDataMessage(pieplot);   
        setNullAndZeroValue(pieplot); 

        ChartFrame frame = new ChartFrame("CityInfoPort公司组织架构图 ", a, true);

        frame.pack();
        //frame.setFont(font);
        frame.setVisible(true);
    }

    static public void tryCategory() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(190, "北京", "苹果");
        dataset.addValue(120, "上海", "苹果");
        dataset.addValue(140, "广州", "苹果");
        dataset.addValue(270, "北京", "梨子");
        dataset.addValue(240, "上海", "梨子");
        dataset.addValue(210, "广州", "梨子");
        dataset.addValue(345, "北京", "葡萄");
        dataset.addValue(380, "上海", "葡萄");
        dataset.addValue(370, "广州", "葡萄");
        dataset.addValue(440, "北京", "香蕉");
        dataset.addValue(420, "上海", "香蕉");
        dataset.addValue(487, "广州", "香蕉");
        dataset.addValue(576, "北京", "荔枝");
        dataset.addValue(545, "上海", "荔枝");
        dataset.addValue(553, "广州", "荔枝");
        JFreeChart chart = ChartFactory.createBarChart3D("水果产量图", "水果", "水果", dataset, PlotOrientation.VERTICAL, true, true, true);
        chart = Util.configCategoryPlotFont(chart);

        CategoryPlot plot = chart.getCategoryPlot();
        
        //peter add item info in pic
        CategoryAxis domainAxis = plot.getDomainAxis();
        //domainAxis.setVerticalCategoryLabels(false);   
        plot.setDomainAxis(domainAxis);

        ValueAxis rangeAxis = plot.getRangeAxis();
        // 设置最高的一个   Item   与图片顶端的距离   
        rangeAxis.setUpperMargin(0.15);
        // 设置最低的一个   Item   与图片底端的距离   
        rangeAxis.setLowerMargin(0.15);
        plot.setRangeAxis(rangeAxis);

        BarRenderer3D renderer = new BarRenderer3D();
        renderer.setBaseOutlinePaint(Color.BLACK);
        // 设置   Wall   的颜色   
        renderer.setWallPaint(Color.gray);
        // 设置每种水果代表的柱的颜色   
        renderer.setSeriesPaint(0, new Color(0, 0, 255));
        renderer.setSeriesPaint(1, new Color(0, 100, 255));
        renderer.setSeriesPaint(2, Color.GREEN);
        // 设置每个地区所包含的平行柱的之间距离   
        renderer.setItemMargin(0.1);
        // 显示每个柱的数值,并修改该数值的字体属性   
        renderer.setItemLabelGenerator(new   StandardCategoryItemLabelGenerator());   
        renderer.setItemLabelsVisible(true);
        //renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator());
        plot.setRenderer(renderer);
        //end peter add item info in pic

        ChartFrame frame = new ChartFrame("水果产量图 ", chart, true);
        frame.pack();
        frame.setVisible(true);
    }

    static public void tryLineChart() {
        String series1 = "北京 Fab4";
        String series2 = "武汉 Fab12";
        String series3 = "上海 Fab8";
        String type1 = "2001";
        String type2 = "2002";
        String type3 = "2003";
        String type4 = "2004";
        String type5 = "2005";
        String type6 = "2006";
        String type7 = "2007";
        String type8 = "2008";
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
        defaultcategorydataset.addValue(1.0D, series1, type1);
        defaultcategorydataset.addValue(4D, series1, type2);
        defaultcategorydataset.addValue(3D, series1, type3);
        defaultcategorydataset.addValue(5D, series1, type4);
        defaultcategorydataset.addValue(5D, series1, type5);
        defaultcategorydataset.addValue(7D, series1, type6);
        defaultcategorydataset.addValue(7D, series1, type7);
        defaultcategorydataset.addValue(8D, series1, type8);
        defaultcategorydataset.addValue(5D, series2, type1);
        defaultcategorydataset.addValue(7D, series2, type2);
        defaultcategorydataset.addValue(6D, series2, type3);
        defaultcategorydataset.addValue(8D, series2, type4);
        defaultcategorydataset.addValue(4D, series2, type5);
        defaultcategorydataset.addValue(4D, series2, type6);
        defaultcategorydataset.addValue(2D, series2, type7);
        defaultcategorydataset.addValue(1.0D, series2, type8);
        defaultcategorydataset.addValue(4D, series3, type1);
        defaultcategorydataset.addValue(3D, series3, type2);
        defaultcategorydataset.addValue(2D, series3, type3);
        defaultcategorydataset.addValue(3D, series3, type4);
        defaultcategorydataset.addValue(6D, series3, type5);
        defaultcategorydataset.addValue(3D, series3, type6);
        defaultcategorydataset.addValue(4D, series3, type7);
        defaultcategorydataset.addValue(3D, series3, type8);
        JFreeChart jfreechart = ChartFactory
                .createLineChart("折线图 Demo 1", "Type", "Value", defaultcategorydataset, PlotOrientation.VERTICAL, true, true, false);
        CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
        categoryplot.setBackgroundPaint(Color.lightGray);
        categoryplot.setRangeGridlinePaint(Color.white);
        jfreechart = Util.configCategoryPlotFont(jfreechart);
        ChartFrame frame = new ChartFrame("折线图 ", jfreechart, true);
        frame.pack();
        frame.setVisible(true);
    }

    public static void tryLineChart2() {
        XYSeries xyseries = new XYSeries("First"); //先产生XYSeries 对象
        xyseries.add(1.0D, 1.0D);
        xyseries.add(2D, 4D);
        xyseries.add(3D, 3D);
        xyseries.add(4D, 5D);
        xyseries.add(5D, 5D);
        xyseries.add(6D, 7D);
        xyseries.add(7D, 7D);
        xyseries.add(8D, 8D);
        XYSeries xyseries1 = new XYSeries("Second");
        xyseries1.add(1.0D, 5D);
        xyseries1.add(2D, 7D);
        xyseries1.add(3D, 6D);
        xyseries1.add(4D, 8D);
        xyseries1.add(5D, 4D);
        xyseries1.add(6D, 4D);
        xyseries1.add(7D, 2D);
        xyseries1.add(8D, 1.0D);
        XYSeries xyseries2 = new XYSeries("Third");
        xyseries2.add(3D, 4D);
        xyseries2.add(4D, 3D);
        xyseries2.add(5D, 2D);
        xyseries2.add(6D, 3D);
        xyseries2.add(7D, 6D);
        xyseries2.add(8D, 3D);
        xyseries2.add(9D, 4D);
        xyseries2.add(10D, 3D);
        XYSeriesCollection xyseriescollection = new XYSeriesCollection(); //再用XYSeriesCollection添加入XYSeries 对象
        xyseriescollection.addSeries(xyseries);
        xyseriescollection.addSeries(xyseries1);
        xyseriescollection.addSeries(xyseries2);
        JFreeChart jfreechart = ChartFactory.createXYLineChart("Line Chart Demo 2", "X", "Y", xyseriescollection, PlotOrientation.VERTICAL, true, true, false);
        jfreechart = Util.configXYPlotFont(jfreechart);
        XYPlot xyplot = jfreechart.getXYPlot();
//      render
        XYItemRenderer xyItemRenderer = (XYItemRenderer) xyplot.getRenderer(0);
        xyItemRenderer.setBaseItemLabelGenerator(new XYItemLabelGenerator(){
            public String generateLabel(XYDataset dataset, int series, int item) {
                return String.valueOf(dataset.getYValue(series,item));	
            }
        });
        xyItemRenderer.setBaseItemLabelsVisible(true);
        xyItemRenderer.setBaseSeriesVisible(true);
        xyplot.setRenderer(xyItemRenderer);
        //render
        ChartFrame frame = new ChartFrame("折线图2 ", jfreechart, true);
        frame.pack();
        frame.setVisible(true);
    }

    public static void tryTimeChart() {
        TimeSeries timeseries = new TimeSeries("L&G European Index Trust", Month.class);
        timeseries.add(new Month(2, 2001), 181.8D);//这里用的是Month.class,同样还有Day.class Year.class 等等
        timeseries.add(new Month(3, 2001), 167.3D);
        timeseries.add(new Month(4, 2001), 153.8D);
        timeseries.add(new Month(5, 2001), 167.6D);
        timeseries.add(new Month(6, 2001), 158.8D);
        timeseries.add(new Month(7, 2001), 148.3D);
        timeseries.add(new Month(8, 2001), 153.9D);
        timeseries.add(new Month(9, 2001), 142.7D);
        timeseries.add(new Month(10, 2001), 123.2D);
        timeseries.add(new Month(11, 2001), 131.8D);
        timeseries.add(new Month(12, 2001), 139.6D);
        timeseries.add(new Month(1, 2002), 142.9D);
        timeseries.add(new Month(2, 2002), 138.7D);
        timeseries.add(new Month(3, 2002), 137.3D);
        timeseries.add(new Month(4, 2002), 143.9D);
        timeseries.add(new Month(5, 2002), 139.8D);
        timeseries.add(new Month(6, 2002), 137D);
        timeseries.add(new Month(7, 2002), 132.8D);
        TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust", Month.class);
        timeseries1.add(new Month(2, 2001), 129.6D);
        timeseries1.add(new Month(3, 2001), 123.2D);
        timeseries1.add(new Month(4, 2001), 117.2D);
        timeseries1.add(new Month(5, 2001), 124.1D);
        timeseries1.add(new Month(6, 2001), 122.6D);
        timeseries1.add(new Month(7, 2001), 119.2D);
        timeseries1.add(new Month(8, 2001), 116.5D);
        timeseries1.add(new Month(9, 2001), 112.7D);
        timeseries1.add(new Month(10, 2001), 101.5D);
        timeseries1.add(new Month(11, 2001), 106.1D);
        timeseries1.add(new Month(12, 2001), 110.3D);
        timeseries1.add(new Month(1, 2002), 111.7D);
        timeseries1.add(new Month(2, 2002), 111D);
        timeseries1.add(new Month(3, 2002), 109.6D);
        timeseries1.add(new Month(4, 2002), 113.2D);
        timeseries1.add(new Month(5, 2002), 111.6D);
        timeseries1.add(new Month(6, 2002), 108.8D);
        timeseries1.add(new Month(7, 2002), 101.6D);
        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        timeseriescollection.addSeries(timeseries);
        timeseriescollection.addSeries(timeseries1);
        timeseriescollection.setDomainIsPointsInTime(true); //domain轴上的刻度点代表的是时间点而不是时间段
        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", "Date", "Price Per Unit", timeseriescollection, true,
                true, false);
        jfreechart.setBackgroundPaint(Color.white);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot(); //获得 plot : XYPlot!!
        xyplot.setBackgroundPaint(Color.lightGray);
        xyplot.setDomainGridlinePaint(Color.white);
        xyplot.setRangeGridlinePaint(Color.white);
        xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
        xyplot.setDomainCrosshairVisible(true);
        xyplot.setRangeCrosshairVisible(true);
        //render
        XYItemRenderer xyItemRenderer = (XYItemRenderer) xyplot.getRenderer(0);
        xyItemRenderer.setBaseItemLabelGenerator(new XYItemLabelGenerator(){
            public String generateLabel(XYDataset dataset, int series, int item) {
                return String.valueOf(dataset.getYValue(series,item));	
            }
        });
        xyItemRenderer.setBaseItemLabelsVisible(true);
        xyItemRenderer.setBaseSeriesVisible(true);
        xyplot.setRenderer(xyItemRenderer);
        //render
        jfreechart = Util.configXYPlotFont(jfreechart);
        ChartFrame frame = new ChartFrame("时间折线图 ", jfreechart, true);
        frame.pack();
        frame.setVisible(true);
    }

    private static PieDataset getDataSet() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue(" 市场前期", new Double(10));
        dataset.setValue(" 立项", new Double(15));
        dataset.setValue(" 计划", new Double(10));
        dataset.setValue(" 需求与设计", new Double(10));
        dataset.setValue(" 执行控制", new Double(35));
        dataset.setValue(" 收尾", new Double(10));
        dataset.setValue(" 运维", new Double(10));
        return dataset;
    }

    static public void picGeneratorTry() {
        PieDataset dataset = getDataSet();
        JFreeChart chart = ChartFactory.createPieChart3D(" 项目进度分布", // chart title    
                dataset,// data    
                true,// include legend    
                true, false);
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        // 图片中显示百分比:默认方式    
        //plot.setLabelGenerator(new           StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));    
        //        图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位    
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
        //        图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例                    
        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
        //        设置背景色为白色     
        chart.setBackgroundPaint(Color.white);
        //        指定图片的透明度(0.0-1.0)     
        plot.setForegroundAlpha(1.0f);
        //        指定显示的饼图上圆形(false)还椭圆形(true)     
        plot.setCircular(true);
        // peter invoke util to format font
        chart = Util.configPiePlotFont(chart);
        //        设置图标题的字体   
        Font font = new Font(" 黑体", Font.CENTER_BASELINE, 20);
        TextTitle title = new TextTitle(" 项目状态分布");
        title.setFont(font);
        chart.setTitle(title);
        FileOutputStream fos_jpg = null;
        String path = "D:\\项目状态分布.jpg";
        try {
            fos_jpg = new FileOutputStream(path);
            ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 640, 480, null);
            fos_jpg.close();
        } catch (Exception e) {
            System.out.println("pic generated error occor: " + e.getMessage());
            e.getStackTrace();
        }
        System.out.println("pic generated in the following path: " + path);

    }

    public static void picGenerator2() {
        //Step 1 建立 Dataset 資料結構物件
        HashMap datas = new HashMap();
        datas.put("Java", new Long(721));
        datas.put(".Net", new Long(543));
        datas.put("PHP", new Long(374));
        datas.put("C++", new Long(438));
        datas.put("Others", new Long(424));

        DefaultPieDataset dataSet = new DefaultPieDataset();
        for (Iterator it = datas.keySet().iterator(); it.hasNext();) {
            String technology = (String) it.next();
            dataSet.setValue(technology, (Long) datas.get(technology));
        }

        //Step 3 建立 Plot 物件
        PiePlot plot = new PiePlot(dataSet);
        plot.setInsets(new RectangleInsets(5, 5, 5, 5));
        plot.setURLGenerator(new StandardPieURLGenerator("DetailBarChart.jsp", "section"));
        plot.setToolTipGenerator(new StandardPieToolTipGenerator());
        plot.setExplodePercent(2, 0.1);

        //Step 4 產生 JFreeChart 物件,並設定圖表底色                  
        JFreeChart chart = new JFreeChart("My First Pie Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        chart.setBackgroundPaint(Color.LIGHT_GRAY);

        //Step 5 輸出圖表,使用 ServletUtilities 將圖表輸出後由 DisplayChart Servlet 讀取
        ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

        //peter 
        FileOutputStream fos_jpg = null;
        String path = "D:\\项目状态分布.jpg";
        File file = new File("D:\\项目状态分布.jpg");
        try {
            fos_jpg = new FileOutputStream(path);
            ChartUtilities.saveChartAsPNG(file, chart, 640, 480, info);
            fos_jpg.close();
        } catch (Exception e) {
            System.out.println("pic generated error occor: " + e.getMessage());
            e.getStackTrace();
        }
        System.out.println("pic generated in the following path: " + path);
        //peter

        //String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);   
        //PrintWriter writer = new PrintWriter(out); 
        //ChartUtilities.writeImageMap(writer, filename, info,false);
        //writer.flush();          
        //String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;        

    }
    
    public static DefaultPieDataset createDataset() {   
        //设置数据   
        DefaultPieDataset pieDataset = new DefaultPieDataset();   
        pieDataset.setValue("篮球火", 2.80);   
        pieDataset.setValue("无敌珊宝妹", 3.63);   
        pieDataset.setValue("不良笑花", 2.84);   
        pieDataset.setValue("黑糖群侠传", null);   
        pieDataset.setValue("命中注定我爱你", 0);   
        return pieDataset;   
    }   
  
    public static void setSection(PiePlot pieplot) {   
        //设置扇区颜色   
        pieplot.setSectionPaint("篮球火", new Color(160, 160, 255));   
        pieplot.setSectionPaint("无敌珊宝妹", new Color(128, 128, 223));   
        pieplot.setSectionPaint("不良笑花", new Color(96, 96, 191));   
        pieplot.setSectionPaint("命中注定我爱你", new Color(64, 64, 128));   
        //设置扇区分离显示   
        pieplot.setExplodePercent("篮球火", 0.2D);   
        //设置扇区边框不可见   
        pieplot.setSectionOutlinesVisible(false);   
    }   
  
    public static void setLabel(PiePlot pieplot) {   
        //设置扇区标签显示格式:关键字:值(百分比)   
        pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(   
                "{0}:{1}({2} percent)"));   
        //设置扇区标签颜色   
        pieplot.setLabelBackgroundPaint(new Color(220, 220, 220));   
        pieplot.setLabelFont((new Font("宋体", Font.PLAIN, 12)));   
  
    }   
  
    public static void setNoDataMessage(PiePlot pieplot) {   
        //设置没有数据时显示的信息   
        pieplot.setNoDataMessage("无数据");   
        //设置没有数据时显示的信息的字体   
        pieplot.setNoDataMessageFont(new Font("宋体", Font.BOLD, 14));   
        //设置没有数据时显示的信息的颜色   
        pieplot.setNoDataMessagePaint(Color.red);   
    }   
  
    public static void setNullAndZeroValue(PiePlot piePlot) {   
        //设置是否忽略0和null值   
         piePlot.setIgnoreNullValues(true);   
         piePlot.setIgnoreZeroValues(true);   
    }
}

 vutil 代码如下:

/*
 * Created on 2009-9-7
 */
package com.peter.jfreechart;

import java.awt.Color;
import java.awt.Font;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.title.TextTitle;

/**
 * @author E000323
 */
public class Util {

    static public JFreeChart configCategoryPlotFont(JFreeChart chart){   
        // 配置字体   
        Font xfont = new Font("宋体",Font.PLAIN,12) ;// X轴   
        Font yfont = new Font("宋体",Font.PLAIN,12) ;// Y轴   
        Font kfont = new Font("宋体",Font.PLAIN,12) ;// 底部   
        Font titleFont = new Font("隶书", Font.BOLD , 25) ; // 图片标题   
        CategoryPlot plot = chart.getCategoryPlot();// 图形的绘制结构对象   
           
        // 图片标题   
        chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));   
           
        // 底部   
        chart.getLegend().setItemFont(kfont);   
           
        // X 轴   
        CategoryAxis domainAxis = plot.getDomainAxis();      
        domainAxis.setLabelFont(xfont);// 轴标题   
        domainAxis.setTickLabelFont(xfont);// 轴数值     
        domainAxis.setTickLabelPaint(Color.BLUE) ; // 字体颜色   
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的label斜显示    
           
        // Y 轴   
        ValueAxis rangeAxis = plot.getRangeAxis();      
        rangeAxis.setLabelFont(yfont);    
        rangeAxis.setLabelPaint(Color.BLUE) ; // 字体颜色   
        rangeAxis.setTickLabelFont(yfont); 
        
        //item 
        //CategoryItemRenderer itemRender = plot.getRenderer();
        //itemRender.setBaseItemLabelsVisible(true);
        //itemRender.setBaseItemLabelFont(xfont);
        
        return chart;
           
    }  

    static public JFreeChart configPiePlotFont(JFreeChart chart){   

        // 配置字体   
        Font labelFont = new Font("宋体",Font.PLAIN,12) ;// label 
        Font itemFont = new Font("宋体",Font.PLAIN,12) ;//  item
        Font titleFont = new Font("隶书", Font.BOLD , 25) ; // 图片标题   
        PiePlot pieplot = (PiePlot) chart.getPlot();// 图形的绘制结构对象   
           
        // 图片标题   
        chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));   
           
        // 底部   
        chart.getLegend().setItemFont(itemFont);   
           
        // label
        pieplot.setLabelFont(labelFont);
        
        return chart;
           
    }
    
    static public JFreeChart configXYPlotFont(JFreeChart chart){   
        // 配置字体   
        Font xfont = new Font("宋体",Font.PLAIN,12) ;// X轴   
        Font yfont = new Font("宋体",Font.PLAIN,12) ;// Y轴   
        Font kfont = new Font("宋体",Font.PLAIN,12) ;// 底部   
        Font titleFont = new Font("隶书", Font.BOLD , 25) ; // 图片标题   
        XYPlot plot = chart.getXYPlot();// 图形的绘制结构对象   
           
        // 图片标题   
        chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));   
           
        // 底部   
        chart.getLegend().setItemFont(kfont);   
           
        // X 轴   
        ValueAxis domainAxis = plot.getDomainAxis();   
        domainAxis.setLabelFont(xfont);// 轴标题   
        domainAxis.setTickLabelFont(xfont);// 轴数值     
        domainAxis.setTickLabelPaint(Color.BLUE) ; // 字体颜色   
        //domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);     
        //domainAxis.setLabelAngle(CategoryLabelPositions.UP_45) ;  // 横轴上的label斜显示
        
        // Y 轴   
        ValueAxis rangeAxis = plot.getRangeAxis();      
        rangeAxis.setLabelFont(yfont);    
        rangeAxis.setLabelPaint(Color.BLUE) ; // 字体颜色   
        rangeAxis.setTickLabelFont(yfont); 
        
        return chart;
           
    }  

}

 

分享到:
评论

相关推荐

    jfree chart docs

    例如,`org.jfree.chart`包下的`JFreeChart`类是创建图表的主要入口点,它提供了创建各种类型图表的方法。`ChartFactory`类提供了一些静态方法,可以快速生成常见的图表类型。`CategoryDataset`和`...

    jfree.jar,jfree.chart.jar包

    该资源包含jfree.jar,jfree.chart.jar包,是Java允许特殊的图形界面所需要的,如果你使用的程序需要import jfree的包那么下载这个资源就可以了,同时还包含jfree.chart在其中,因此只要和jfree有关的都可以下载该资源

    jfree chart...

    标题 "jfree chart..." 暗示我们正在讨论 JFreeChart,这是一个强大的Java库,用于创建各种图表,如折线图、柱状图、饼图、散点图等。JFreeChart 支持多种复杂的图表类型,使得在Java应用程序中进行数据可视化变得...

    jfree chart demo

    jfree chart -1.0.8-demo 是Java Swing 写的 金融 K线图 画图 通用库函数使用的demo。 这个版本比较新。

    jfree chart demo (3/4)

    jfreechart demo

    CHART , JFREE api

    在这个类中,可能会有调用JFreeChart API的方法,如`createChart()`,用于生成特定类型的图表,并根据业务需求进行定制,如设置标题、轴标签、数据集等。 `JFreeChart API.java`很可能是包含JFreeChart关键方法的...

    jsp数据报表(柱状,饼状,折线)

    import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.StandardLegend; import ...

    jfreechart根据24小时数据画折线图

    import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYDataset; import org.jfree.ui.RectangleInsets; ``` 这里导入了创建折线图所需的所有类和接口,包括用于图表创建的...

    JFree生成图表

    - 线性图(LineChart):用于表示数据随时间的变化趋势。 2. **创建图表**: - 使用`ChartFactory`类可以快速创建常见类型的图表。例如,`createPieChart()`用于创建饼状图,`createLineChart3D()`用于创建3D线性...

    jfree包jcommon、jfreechart

    <servlet-class>org.jfree.chart.servlet.DisplayChart <servlet-name>DisplayChart <url-pattern>/servlet/DisplayChart 至此jfreechart的配置就完成了,下面就可以进行jfreechart的开发了。这里值得提出...

    java使用JFreeChart制作柱形图

    例如,你可以使用 `ChartTheme` 来改变默认的主题,或者使用 `CategoryPlot` 来设置 X 轴和 Y 轴的行为。 4. **展示或保存图表**: 最后,你可以选择将图表显示在 Swing 组件(如 `JFrame`)中,或者将其保存为...

    jfreechart demo (2/4)

    jfree chart demo

    Jfree完整的jar包

    implementation 'org.jfree:jfreechart:1.0.13' } ``` 然后就可以在代码中导入JFreeChart和JCommon的类,创建和定制图表了。 总之,JFreeChart和JCommon是Java开发者进行数据可视化的重要工具,它们提供了丰富的...

    jfree

    要使用JFreeChart创建图表,首先需要创建一个数据集(Dataset),然后使用`ChartFactory.createChart()`方法根据数据集生成图表对象。例如,创建一个简单的饼图可以通过以下步骤实现: 1. 创建`DefaultPieDataset`...

    jfree生成百分比折线图,自定义纵坐标

    jfree生成百分比折线图,自定义纵坐标,只需要把main方法下的存放地址修改到自己的地址就OK了,,

    jfree jar包

    这个"jfree jar包"包含了JFreeChart库的所有必要组件,方便开发者在Java项目中集成和使用。JFreeChart支持多种图表类型,包括折线图、柱状图、饼图、散点图、甘特图、雷达图等,广泛应用于数据分析、报表生成以及...

    JfreeChart的最常用的制图代码实例(饼图,柱图,SeriesChart,折线图)附带工程源码。

    1. **饼图(Pie Chart)** 饼图用于显示各部分占整体的比例。创建饼图的关键在于`PieDataset`接口的实现,通常使用`DefaultPieDataset`类。以下是一个简单的代码示例: ```java DefaultPieDataset dataset = new ...

Global site tag (gtag.js) - Google Analytics