<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    一杯清茶

    統(tǒng)計(jì)

    留言簿

    Oracle SQL/PLSQL

    PowerDesigner教程系列

    Struts2.0

    web開發(fā)

    三人行

    從事RCP開發(fā)的同行

    工作流和權(quán)限設(shè)置

    閱讀排行榜

    評論排行榜

    jfreechart 生成折線圖,餅圖,柱狀圖,堆棧柱狀圖

    轉(zhuǎn)載:http://hi.baidu.com/ahli/blog/item/3aec09f78df33626730eec11.html

    關(guān)鍵字: 報(bào)表 最近公司使用jfreechart來生成統(tǒng)計(jì)圖,使用了折線圖,餅圖等,因?yàn)橛玫?.0.8a版的jfreechart,與1.0.0版本的地方有很多不同,而且圖片生成清晰度比老版的高,如下是我做的demo.

    1.所需包
    (1) jfreechart-1.0.8a.jar
    (2) jcommon-1.0.12.jar

    2.運(yùn)行環(huán)境
    JDK 1.5

    3.源代碼

    import java.awt.Color; 
    import java.awt.Font; 
    import java.io.File; 
    import java.io.FileOutputStream; 
    import java.text.DecimalFormat; 
    import java.text.NumberFormat; 

    import org.jfree.chart.ChartFactory; 
    import org.jfree.chart.ChartUtilities; 
    import org.jfree.chart.JFreeChart; 
    import org.jfree.chart.axis.CategoryAxis; 
    import org.jfree.chart.axis.CategoryLabelPositions; 
    import org.jfree.chart.axis.NumberAxis; 
    import org.jfree.chart.axis.ValueAxis; 
    import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 
    import org.jfree.chart.labels.StandardPieSectionLabelGenerator; 
    import org.jfree.chart.plot.CategoryPlot; 
    import org.jfree.chart.plot.PiePlot3D; 
    import org.jfree.chart.plot.PlotOrientation; 
    import org.jfree.chart.renderer.category.BarRenderer; 
    import org.jfree.chart.renderer.category.LineAndShapeRenderer; 
    import org.jfree.chart.renderer.category.StackedBarRenderer; 
    import org.jfree.chart.title.TextTitle; 
    import org.jfree.data.category.CategoryDataset; 
    import org.jfree.data.general.DatasetUtilities; 
    import org.jfree.data.general.DefaultPieDataset; 
    import org.jfree.data.general.PieDataset; 

    /** 
    * 實(shí)際取色的時(shí)候一定要16位的,這樣比較準(zhǔn)確 

    @author new 
    */
     
    public class CreateChartServiceImpl 

    private static final String CHART_PATH = "E:/test/"

    public static void main(String[] args) 

    // TODO Auto-generated method stub 
    CreateChartServiceImpl pm = new CreateChartServiceImpl(); 
    // 生成餅狀圖 
    pm.makePieChart(); 
    // 生成單組柱狀圖 
    pm.makeBarChart(); 
    // 生成多組柱狀圖 
    pm.makeBarGroupChart(); 
    // 生成堆積柱狀圖 
    pm.makeStackedBarChart(); 
    // 生成折線圖 
    pm.makeLineAndShapeChart(); 
    }
     

    /** 
    * 生成折線圖 
    */
     
    public void makeLineAndShapeChart() 

    double[][] data = new double[][] 

    672766223540126 }
    325521210340106 }
    332256523240526 } }

    String[] rowKeys 
    = 
    "蘋果""梨子""葡萄" }
    String[] columnKeys 
    = 
    "北京""上海""廣州""成都""深圳" }
    CategoryDataset dataset 
    = getBarData(data, rowKeys, columnKeys); 
    createTimeXYChar(
    "折線圖""x軸""y軸", dataset, "lineAndShap.png"); 
    }
     

    /** 
    * 生成分組的柱狀圖 
    */
     
    public void makeBarGroupChart() 

    double[][] data = new double[][] 

    672766223540126 }
    325521210340106 }
    332256523240526 } }

    String[] rowKeys 
    = 
    "蘋果""梨子""葡萄" }
    String[] columnKeys 
    = 
    "北京""上海""廣州""成都""深圳" }
    CategoryDataset dataset 
    = getBarData(data, rowKeys, columnKeys); 
    createBarChart(dataset, 
    "x坐標(biāo)""y坐標(biāo)""柱狀圖""barGroup.png"); 
    }
     

    /** 
    * 生成柱狀圖 
    */
     
    public void makeBarChart() 

    double[][] data = new double[][] 

    672766223540126 } }

    String[] rowKeys 
    = 
    "蘋果" }
    String[] columnKeys 
    = 
    "北京""上海""廣州""成都""深圳" }
    CategoryDataset dataset 
    = getBarData(data, rowKeys, columnKeys); 
    createBarChart(dataset, 
    "x坐標(biāo)""y坐標(biāo)""柱狀圖""bar.png"); 
    }
     

    /** 
    * 生成堆棧柱狀圖 
    */
     
    public void makeStackedBarChart() 

    double[][] data = new double[][] 

    0.210.660.230.400.26 }
    0.250.210.100.400.16 } }

    String[] rowKeys 
    = 
    "蘋果""梨子" }
    String[] columnKeys 
    = 
    "北京""上海""廣州""成都""深圳" }
    CategoryDataset dataset 
    = getBarData(data, rowKeys, columnKeys); 
    createStackedBarChart(dataset, 
    "x坐標(biāo)""y坐標(biāo)""柱狀圖""stsckedBar.png"); 
    }
     

    /** 
    * 生成餅狀圖 
    */
     
    public void makePieChart() 

    double[] data = 
    991 }
    String[] keys 
    = 
    "失敗率""成功率" }

    createValidityComparePimChar(getDataPieSetByUtil(data, keys), 
    "餅狀圖"
    "pie2.png", keys); 
    }
     

    // 柱狀圖,折線圖 數(shù)據(jù)集 
    public CategoryDataset getBarData(double[][] data, String[] rowKeys, 
    String[] columnKeys) 

    return DatasetUtilities 
    .createCategoryDataset(rowKeys, columnKeys, data); 

    }
     

    // 餅狀圖 數(shù)據(jù)集 
    public PieDataset getDataPieSetByUtil(double[] data, 
    String[] datadescription) 


    if (data != null && datadescription != null

    if (data.length == datadescription.length) 

    DefaultPieDataset dataset 
    = new DefaultPieDataset(); 
    for (int i = 0; i < data.length; i++

    dataset.setValue(datadescription[i], data[i]); 
    }
     
    return dataset; 
    }
     

    }
     

    return null
    }
     

    /** 
    * 柱狀圖 

    *
    @param dataset 數(shù)據(jù)集 
    @param xName x軸的說明(如種類,時(shí)間等) 
    @param yName y軸的說明(如速度,時(shí)間等) 
    @param chartTitle 圖標(biāo)題 
    @param charName 生成圖片的名字 
    @return 
    */
     
    public String createBarChart(CategoryDataset dataset, String xName, 
    String yName, String chartTitle, String charName) 

    JFreeChart chart 
    = ChartFactory.createBarChart(chartTitle, // 圖表標(biāo)題 
    xName, // 目錄軸的顯示標(biāo)簽 
    yName, // 數(shù)值軸的顯示標(biāo)簽 
    dataset, // 數(shù)據(jù)集 
    PlotOrientation.VERTICAL, // 圖表方向:水平、垂直 
    true// 是否顯示圖例(對于簡單的柱狀圖必須是false) 
    false// 是否生成工具 
    false // 是否生成URL鏈接 
    ); 
    Font labelFont 
    = new Font("SansSerif", Font.TRUETYPE_FONT, 12); 
    /* 
    * VALUE_TEXT_ANTIALIAS_OFF表示將文字的抗鋸齒關(guān)閉, 
    * 使用的關(guān)閉抗鋸齒后,字體盡量選擇12到14號的宋體字,這樣文字最清晰好看 
    */
     
    // chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 
    chart.setTextAntiAlias(false); 
    chart.setBackgroundPaint(Color.white); 
    // create plot 
    CategoryPlot plot = chart.getCategoryPlot(); 
    // 設(shè)置橫虛線可見 
    plot.setRangeGridlinesVisible(true); 
    // 虛線色彩 
    plot.setRangeGridlinePaint(Color.gray); 

    // 數(shù)據(jù)軸精度 
    NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
    // vn.setAutoRangeIncludesZero(true); 
    DecimalFormat df = new DecimalFormat("#0.00"); 
    vn.setNumberFormatOverride(df); 
    // 數(shù)據(jù)軸數(shù)據(jù)標(biāo)簽的顯示格式 
    // x軸設(shè)置 
    CategoryAxis domainAxis = plot.getDomainAxis(); 
    domainAxis.setLabelFont(labelFont);
    // 軸標(biāo)題 
    domainAxis.setTickLabelFont(labelFont);// 軸數(shù)值 

    // Lable(Math.PI/3.0)度傾斜 
    // domainAxis.setCategoryLabelPositions(CategoryLabelPositions 
    // .createUpRotationLabelPositions(Math.PI / 3.0)); 

    domainAxis.setMaximumCategoryLabelWidthRatio(
    0.6f);// 橫軸上的 Lable 是否完整顯示 

    // 設(shè)置距離圖片左端距離 
    domainAxis.setLowerMargin(0.1); 
    // 設(shè)置距離圖片右端距離 
    domainAxis.setUpperMargin(0.1); 
    // 設(shè)置 columnKey 是否間隔顯示 
    // domainAxis.setSkipCategoryLabelsToFit(true); 

    plot.setDomainAxis(domainAxis); 
    // 設(shè)置柱圖背景色(注意,系統(tǒng)取色的時(shí)候要使用16位的模式來查看顏色編碼,這樣比較準(zhǔn)確) 
    plot.setBackgroundPaint(new Color(255255204)); 

    // y軸設(shè)置 
    ValueAxis rangeAxis = plot.getRangeAxis(); 
    rangeAxis.setLabelFont(labelFont); 
    rangeAxis.setTickLabelFont(labelFont); 
    // 設(shè)置最高的一個(gè) Item 與圖片頂端的距離 
    rangeAxis.setUpperMargin(0.15); 
    // 設(shè)置最低的一個(gè) Item 與圖片底端的距離 
    rangeAxis.setLowerMargin(0.15); 
    plot.setRangeAxis(rangeAxis); 

    BarRenderer renderer 
    = new BarRenderer(); 
    // 設(shè)置柱子寬度 
    renderer.setMaximumBarWidth(0.05); 
    // 設(shè)置柱子高度 
    renderer.setMinimumBarLength(0.2); 
    // 設(shè)置柱子邊框顏色 
    renderer.setBaseOutlinePaint(Color.BLACK); 
    // 設(shè)置柱子邊框可見 
    renderer.setDrawBarOutline(true); 

    // // 設(shè)置柱的顏色 
    renderer.setSeriesPaint(0new Color(204255255)); 
    renderer.setSeriesPaint(
    1new Color(153204255)); 
    renderer.setSeriesPaint(
    2new Color(51204204)); 

    // 設(shè)置每個(gè)地區(qū)所包含的平行柱的之間距離 
    renderer.setItemMargin(0.0); 

    // 顯示每個(gè)柱的數(shù)值,并修改該數(shù)值的字體屬性 
    renderer.setIncludeBaseInRange(true); 
    renderer 
    .setBaseItemLabelGenerator(
    new StandardCategoryItemLabelGenerator()); 
    renderer.setBaseItemLabelsVisible(
    true); 

    plot.setRenderer(renderer); 
    // 設(shè)置柱的透明度 
    plot.setForegroundAlpha(1.0f); 

    FileOutputStream fos_jpg 
    = null
    try 

    isChartPathExist(CHART_PATH); 
    String chartName 
    = CHART_PATH + charName; 
    fos_jpg 
    = new FileOutputStream(chartName); 
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 
    500500true10); 
    return chartName; 
    }
     
    catch (Exception e) 

    e.printStackTrace(); 
    return null
    }
     
    finally 

    try 

    fos_jpg.close(); 
    }
     
    catch (Exception e) 

    e.printStackTrace(); 
    }
     
    }
     
    }
     

    /** 
    * 橫向圖 

    @param dataset 數(shù)據(jù)集 
    @param xName x軸的說明(如種類,時(shí)間等) 
    @param yName y軸的說明(如速度,時(shí)間等) 
    @param chartTitle 圖標(biāo)題 
    @param charName 生成圖片的名字 
    @return 
    */
     
    public String createHorizontalBarChart(CategoryDataset dataset, 
    String xName, String yName, String chartTitle, String charName) 

    JFreeChart chart 
    = ChartFactory.createBarChart(chartTitle, // 圖表標(biāo)題 
    xName, // 目錄軸的顯示標(biāo)簽 
    yName, // 數(shù)值軸的顯示標(biāo)簽 
    dataset, // 數(shù)據(jù)集 
    PlotOrientation.VERTICAL, // 圖表方向:水平、垂直 
    true// 是否顯示圖例(對于簡單的柱狀圖必須是false) 
    false// 是否生成工具 
    false // 是否生成URL鏈接 
    ); 

    CategoryPlot plot 
    = chart.getCategoryPlot(); 
    // 數(shù)據(jù)軸精度 
    NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
    //設(shè)置刻度必須從0開始 
    // vn.setAutoRangeIncludesZero(true); 
    DecimalFormat df = new DecimalFormat("#0.00"); 
    vn.setNumberFormatOverride(df); 
    // 數(shù)據(jù)軸數(shù)據(jù)標(biāo)簽的顯示格式 

    CategoryAxis domainAxis 
    = plot.getDomainAxis(); 

    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
    // 橫軸上的 
    // Lable 
    Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); 

    domainAxis.setLabelFont(labelFont);
    // 軸標(biāo)題 
    domainAxis.setTickLabelFont(labelFont);// 軸數(shù)值 

    domainAxis.setMaximumCategoryLabelWidthRatio(
    0.8f);// 橫軸上的 Lable 是否完整顯示 
    // domainAxis.setVerticalCategoryLabels(false); 
    plot.setDomainAxis(domainAxis); 

    ValueAxis rangeAxis 
    = plot.getRangeAxis(); 
    // 設(shè)置最高的一個(gè) Item 與圖片頂端的距離 
    rangeAxis.setUpperMargin(0.15); 
    // 設(shè)置最低的一個(gè) Item 與圖片底端的距離 
    rangeAxis.setLowerMargin(0.15); 
    plot.setRangeAxis(rangeAxis); 
    BarRenderer renderer 
    = new BarRenderer(); 
    // 設(shè)置柱子寬度 
    renderer.setMaximumBarWidth(0.03); 
    // 設(shè)置柱子高度 
    renderer.setMinimumBarLength(30); 

    renderer.setBaseOutlinePaint(Color.BLACK); 

    // 設(shè)置柱的顏色 
    renderer.setSeriesPaint(0, Color.GREEN); 
    renderer.setSeriesPaint(
    1new Color(00255)); 
    // 設(shè)置每個(gè)地區(qū)所包含的平行柱的之間距離 
    renderer.setItemMargin(0.5); 
    // 顯示每個(gè)柱的數(shù)值,并修改該數(shù)值的字體屬性 
    renderer 
    .setBaseItemLabelGenerator(
    new StandardCategoryItemLabelGenerator()); 
    // 設(shè)置柱的數(shù)值可見 
    renderer.setBaseItemLabelsVisible(true); 

    plot.setRenderer(renderer); 
    // 設(shè)置柱的透明度 
    plot.setForegroundAlpha(0.6f); 

    FileOutputStream fos_jpg 
    = null
    try 

    isChartPathExist(CHART_PATH); 
    String chartName 
    = CHART_PATH + charName; 
    fos_jpg 
    = new FileOutputStream(chartName); 
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 
    500500true10); 
    return chartName; 
    }
     
    catch (Exception e) 

    e.printStackTrace(); 
    return null
    }
     
    finally 

    try 

    fos_jpg.close(); 
    }
     
    catch (Exception e) 

    e.printStackTrace(); 
    }
     
    }
     
    }
     

    /** 
    * 餅狀圖 

    @param dataset 數(shù)據(jù)集 
    @param chartTitle 圖標(biāo)題 
    @param charName 生成圖的名字 
    @param pieKeys 分餅的名字集 
    @return 
    */
     
    public String createValidityComparePimChar(PieDataset dataset, 
    String chartTitle, String charName, String[] pieKeys) 

    JFreeChart chart 
    = ChartFactory.createPieChart3D(chartTitle, // chart 
    // title 
    dataset,// data 
    true,// include legend 
    truefalse); 

    // 使下說明標(biāo)簽字體清晰,去鋸齒類似于 
    // chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);的效果 
    chart.setTextAntiAlias(false); 
    // 圖片背景色 
    chart.setBackgroundPaint(Color.white); 
    // 設(shè)置圖標(biāo)題的字體重新設(shè)置title 
    Font font = new Font("隸書", Font.BOLD, 25); 
    TextTitle title 
    = new TextTitle(chartTitle); 
    title.setFont(font); 
    chart.setTitle(title); 

    PiePlot3D plot 
    = (PiePlot3D) chart.getPlot(); 
    // 圖片中顯示百分比:默認(rèn)方式 

    // 指定餅圖輪廓線的顏色 
    // plot.setBaseSectionOutlinePaint(Color.BLACK); 
    // plot.setBaseSectionPaint(Color.BLACK); 

    // 設(shè)置無數(shù)據(jù)時(shí)的信息 
    plot.setNoDataMessage("無對應(yīng)的數(shù)據(jù),請重新查詢。"); 

    // 設(shè)置無數(shù)據(jù)時(shí)的信息顯示顏色 
    plot.setNoDataMessagePaint(Color.red); 

    // 圖片中顯示百分比:自定義方式,{0} 表示選項(xiàng), {1} 表示數(shù)值, {2} 表示所占比例 ,小數(shù)點(diǎn)后兩位 
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator( 
    "{0}={1}({2})", NumberFormat.getNumberInstance(), 
    new DecimalFormat("0.00%"))); 
    // 圖例顯示百分比:自定義方式, {0} 表示選項(xiàng), {1} 表示數(shù)值, {2} 表示所占比例 
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( 
    "{0}={1}({2})")); 

    plot.setLabelFont(
    new Font("SansSerif", Font.TRUETYPE_FONT, 12)); 

    // 指定圖片的透明度(0.0-1.0) 
    plot.setForegroundAlpha(0.65f); 
    // 指定顯示的餅圖上圓形(false)還橢圓形(true) 
    plot.setCircular(falsetrue); 

    // 設(shè)置第一個(gè) 餅塊section 的開始位置,默認(rèn)是12點(diǎn)鐘方向 
    plot.setStartAngle(90); 

    // // 設(shè)置分餅顏色 
    plot.setSectionPaint(pieKeys[0], new Color(244194144)); 
    plot.setSectionPaint(pieKeys[
    1], new Color(144233144)); 

    FileOutputStream fos_jpg 
    = null
    try 

    // 文件夾不存在則創(chuàng)建 
    isChartPathExist(CHART_PATH); 
    String chartName 
    = CHART_PATH + charName; 
    fos_jpg 
    = new FileOutputStream(chartName); 
    // 高寬的設(shè)置影響橢圓餅圖的形狀 
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500230); 

    return chartName; 
    }
     
    catch (Exception e) 

    e.printStackTrace(); 
    return null
    }
     
    finally 

    try 

    fos_jpg.close(); 
    System.out.println(
    "create pie-chart."); 
    }
     
    catch (Exception e) 

    e.printStackTrace(); 
    }
     
    }
     

    }
     

    /** 
    * 判斷文件夾是否存在,如果不存在則新建 
    @param chartPath 
    */
     
    private void isChartPathExist(String chartPath) 

    File file 
    = new File(chartPath); 
    if (!file.exists()) 

    file.mkdirs(); 
    // log.info("CHART_PATH="+CHART_PATH+"create."); 
    }
     
    }
     

    /** 
    * 折線圖 

    @param chartTitle 
    @param x 
    @param y 
    @param xyDataset 
    @param charName 
    @return 
    */
     
    public String createTimeXYChar(String chartTitle, String x, String y, 
    CategoryDataset xyDataset, String charName) 


    JFreeChart chart 
    = ChartFactory.createLineChart(chartTitle, x, y, 
    xyDataset, PlotOrientation.VERTICAL, 
    truetruefalse); 

    chart.setTextAntiAlias(
    false); 
    chart.setBackgroundPaint(Color.WHITE); 
    // 設(shè)置圖標(biāo)題的字體重新設(shè)置title 
    Font font = new Font("隸書", Font.BOLD, 25); 
    TextTitle title 
    = new TextTitle(chartTitle); 
    title.setFont(font); 
    chart.setTitle(title); 
    // 設(shè)置面板字體 
    Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); 

    chart.setBackgroundPaint(Color.WHITE); 

    CategoryPlot categoryplot 
    = (CategoryPlot) chart.getPlot(); 
    // x軸 // 分類軸網(wǎng)格是否可見 
    categoryplot.setDomainGridlinesVisible(true); 
    // y軸 //數(shù)據(jù)軸網(wǎng)格是否可見 
    categoryplot.setRangeGridlinesVisible(true); 

    categoryplot.setRangeGridlinePaint(Color.WHITE);
    // 虛線色彩 

    categoryplot.setDomainGridlinePaint(Color.WHITE);
    // 虛線色彩 

    categoryplot.setBackgroundPaint(Color.lightGray); 

    // 設(shè)置軸和面板之間的距離 
    // categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); 

    CategoryAxis domainAxis 
    = categoryplot.getDomainAxis(); 

    domainAxis.setLabelFont(labelFont);
    // 軸標(biāo)題 
    domainAxis.setTickLabelFont(labelFont);// 軸數(shù)值 

    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
    // 橫軸上的 
    // Lable 
    // 45度傾斜 
    // 設(shè)置距離圖片左端距離 
    domainAxis.setLowerMargin(0.0); 
    // 設(shè)置距離圖片右端距離 
    domainAxis.setUpperMargin(0.0); 

    NumberAxis numberaxis 
    = (NumberAxis) categoryplot.getRangeAxis(); 
    numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
    numberaxis.setAutoRangeIncludesZero(
    true); 

    // 獲得renderer 注意這里是下嗍造型到lineandshaperenderer!! 
    LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot 
    .getRenderer(); 

    lineandshaperenderer.setBaseShapesVisible(
    true); // series 點(diǎn)(即數(shù)據(jù)點(diǎn))可見 
    lineandshaperenderer.setBaseLinesVisible(true); // series 點(diǎn)(即數(shù)據(jù)點(diǎn))間有連線可見 

    // 顯示折點(diǎn)數(shù)據(jù) 
    // lineandshaperenderer.setBaseItemLabelGenerator(new 
    // StandardCategoryItemLabelGenerator()); 
    // lineandshaperenderer.setBaseItemLabelsVisible(true); 

    FileOutputStream fos_jpg 
    = null
    try 

    isChartPathExist(CHART_PATH); 
    String chartName 
    = CHART_PATH + charName; 
    fos_jpg 
    = new FileOutputStream(chartName); 

    // 將報(bào)表保存為png文件 
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500510); 

    return chartName; 
    }
     
    catch (Exception e) 

    e.printStackTrace(); 
    return null
    }
     
    finally 

    try 

    fos_jpg.close(); 
    System.out.println(
    "create time-createTimeXYChar."); 
    }
     
    catch (Exception e) 

    e.printStackTrace(); 
    }
     
    }
     
    }
     

    /** 
    * 堆棧柱狀圖 

    @param dataset 
    @param xName 
    @param yName 
    @param chartTitle 
    @param charName 
    @return 
    */
     
    public String createStackedBarChart(CategoryDataset dataset, String xName, 
    String yName, String chartTitle, String charName) 

    // 1:得到 CategoryDataset 

    // 2:JFreeChart對象 
    JFreeChart chart = ChartFactory.createStackedBarChart(chartTitle, // 圖表標(biāo)題 
    xName, // 目錄軸的顯示標(biāo)簽 
    yName, // 數(shù)值軸的顯示標(biāo)簽 
    dataset, // 數(shù)據(jù)集 
    PlotOrientation.VERTICAL, // 圖表方向:水平、垂直 
    true// 是否顯示圖例(對于簡單的柱狀圖必須是false) 
    false// 是否生成工具 
    false // 是否生成URL鏈接 
    ); 
    // 圖例字體清晰 
    chart.setTextAntiAlias(false); 

    chart.setBackgroundPaint(Color.WHITE); 

    // 2 .2 主標(biāo)題對象 主標(biāo)題對象是 TextTitle 類型 
    chart 
    .setTitle(
    new TextTitle(chartTitle, new Font("隸書", Font.BOLD, 
    25))); 
    // 2 .2.1:設(shè)置中文 
    // x,y軸坐標(biāo)字體 
    Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); 

    // 2 .3 Plot 對象 Plot 對象是圖形的繪制結(jié)構(gòu)對象 
    CategoryPlot plot = chart.getCategoryPlot(); 

    // 設(shè)置橫虛線可見 
    plot.setRangeGridlinesVisible(true); 
    // 虛線色彩 
    plot.setRangeGridlinePaint(Color.gray); 

    // 數(shù)據(jù)軸精度 
    NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
    // 設(shè)置最大值是1 
    vn.setUpperBound(1); 
    // 設(shè)置數(shù)據(jù)軸坐標(biāo)從0開始 
    // vn.setAutoRangeIncludesZero(true); 
    // 數(shù)據(jù)顯示格式是百分比 
    DecimalFormat df = new DecimalFormat("0.00%"); 
    vn.setNumberFormatOverride(df); 
    // 數(shù)據(jù)軸數(shù)據(jù)標(biāo)簽的顯示格式 
    // DomainAxis (區(qū)域軸,相當(dāng)于 x 軸), RangeAxis (范圍軸,相當(dāng)于 y 軸) 
    CategoryAxis domainAxis = plot.getDomainAxis(); 

    domainAxis.setLabelFont(labelFont);
    // 軸標(biāo)題 
    domainAxis.setTickLabelFont(labelFont);// 軸數(shù)值 

    // x軸坐標(biāo)太長,建議設(shè)置傾斜,如下兩種方式選其一,兩種效果相同 
    // 傾斜(1)橫軸上的 Lable 45度傾斜 
    // domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
    // 傾斜(2)Lable(Math.PI 3.0)度傾斜 
    // domainAxis.setCategoryLabelPositions(CategoryLabelPositions 
    // .createUpRotationLabelPositions(Math.PI / 3.0)); 

    domainAxis.setMaximumCategoryLabelWidthRatio(
    0.6f);// 橫軸上的 Lable 是否完整顯示 

    plot.setDomainAxis(domainAxis); 

    // y軸設(shè)置 
    ValueAxis rangeAxis = plot.getRangeAxis(); 
    rangeAxis.setLabelFont(labelFont); 
    rangeAxis.setTickLabelFont(labelFont); 
    // 設(shè)置最高的一個(gè) Item 與圖片頂端的距離 
    rangeAxis.setUpperMargin(0.15); 
    // 設(shè)置最低的一個(gè) Item 與圖片底端的距離 
    rangeAxis.setLowerMargin(0.15); 
    plot.setRangeAxis(rangeAxis); 

    // Renderer 對象是圖形的繪制單元 
    StackedBarRenderer renderer = new StackedBarRenderer(); 
    // 設(shè)置柱子寬度 
    renderer.setMaximumBarWidth(0.05); 
    // 設(shè)置柱子高度 
    renderer.setMinimumBarLength(0.1); 
    //設(shè)置柱的邊框顏色 
    renderer.setBaseOutlinePaint(Color.BLACK); 
    //設(shè)置柱的邊框可見 
    renderer.setDrawBarOutline(true); 

    // // 設(shè)置柱的顏色(可設(shè)定也可默認(rèn)) 
    renderer.setSeriesPaint(0new Color(204255204)); 
    renderer.setSeriesPaint(
    1new Color(255204153)); 

    // 設(shè)置每個(gè)地區(qū)所包含的平行柱的之間距離 
    renderer.setItemMargin(0.4); 

    plot.setRenderer(renderer); 
    // 設(shè)置柱的透明度(如果是3D的必須設(shè)置才能達(dá)到立體效果,如果是2D的設(shè)置則使顏色變淡) 
    // plot.setForegroundAlpha(0.65f); 

    FileOutputStream fos_jpg 
    = null
    try 

    isChartPathExist(CHART_PATH); 
    String chartName 
    = CHART_PATH + charName; 
    fos_jpg 
    = new FileOutputStream(chartName); 
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 
    500500true10); 
    return chartName; 
    }
     
    catch (Exception e) 

    e.printStackTrace(); 
    return null
    }
     
    finally 

    try 

    fos_jpg.close(); 
    }
     
    catch (Exception e) 

    e.printStackTrace(); 
    }
     
    }
     
    }
     

    posted on 2009-05-26 10:59 一杯清茶 閱讀(5162) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国色精品卡一卡2卡3卡4卡免费 | 国产午夜亚洲精品不卡电影| www.黄色免费网站| 亚洲一区二区三区亚瑟| 黄页网站在线观看免费高清| 亚洲成a人片77777群色| 亚洲欧洲AV无码专区| 无码人妻精品一二三区免费| 亚洲a级片在线观看| 欧美好看的免费电影在线观看| 亚洲一区二区三区在线观看蜜桃| av无码国产在线看免费网站| 久久精品国产亚洲av麻| 可以免费观看的毛片| 久久亚洲一区二区| 日韩在线永久免费播放| 亚洲国产超清无码专区| 无码国产精品一区二区免费| 亚洲偷自精品三十六区| 成人免费在线观看网站| 色偷偷尼玛图亚洲综合| 1000部拍拍拍18免费网站| 亚洲女人初试黑人巨高清| 老司机在线免费视频| 亚洲综合在线一区二区三区| 永久久久免费浮力影院| 亚洲AV女人18毛片水真多| 无码欧精品亚洲日韩一区夜夜嗨| 一级黄色片免费观看| 国产v片免费播放| 亚洲精品精华液一区二区| 免费欧洲毛片A级视频无风险| 日本黄页网址在线看免费不卡| 在线精品亚洲一区二区三区| 亚洲免费观看视频| 亚洲高清中文字幕综合网| 成人免费a级毛片| 特级毛片A级毛片免费播放| 亚洲综合色自拍一区| 一区二区免费视频| 亚洲avav天堂av在线网爱情|