注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

千鸟

本blog所有日志均系原创 转载请注明出处

 
 
 

日志

 
 

jfreechart 1.0.5 笔记2  

2007-05-16 16:19:10|  分类: J2EE |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

先给个地址:http://www.java2s.com/Code/Java/Chart/CatalogChart.htm 

里面有很多jfreechart的例子。版本大都是:jfreechart 1.0.0的,下面的代码版本是jfreechart 1.0.5

1.带数字的柱状图  2。带点的线状图  3. 两种画法。结合柱状和点状图

jfreechart 1.0.5 笔记2 - souljava - 千鸟

package com.dm.test.chart;
import java.awt.*;

import java.text.NumberFormat;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.annotations.CategoryTextAnnotation;
import org.jfree.chart.axis.CategoryAnchor;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.BarRenderer3D;

import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.*;
import java.io.*;

public class Bar3dAndD{
 
 public static void main(String[] args) {  
  
        JFreeChart chart=createChart(getDataSet2());  
   FileOutputStream fos_jpg = null;
     try {
   fos_jpg = new FileOutputStream("D:\\fruit1.jpg");
   ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
  }catch(Exception e){
   
  } finally {
   try {
    fos_jpg.close();
   } catch (Exception e) {}
  }
 }
 private static JFreeChart createChart(CategoryDataset dataset){
  JFreeChart jfreechart = ChartFactory.createBarChart3D(
                "水果产量图", // 图表标题
                "水果", // 目录轴的显示标签
                "产量", // 数值轴的显示标签
                dataset, // 数据集
                PlotOrientation.VERTICAL, // 图表方向:水平、垂直
                true,  // 是否显示图例(对于简单的柱状图必须是false)
                false,  // 是否生成工具
                false  // 是否生成URL链接
                );
        CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();       
     
       BarRenderer3D barrender=new BarRenderer3D ();
       barrender.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
       barrender.setBaseItemLabelsVisible(true);
       barrender.setItemLabelAnchorOffset(10D); //数字与柱子的距离
       barrender.setPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        categoryplot.setRenderer(barrender);     
         return jfreechart;      
 }
 private static CategoryDataset getDataSet2() {
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.addValue(Math.random()*100 , "北京", "苹果");
  dataset.addValue(Math.random()*100, "上海", "苹果");
  dataset.addValue(Math.random()*100, "广州", "苹果");
  dataset.addValue(Math.random()*200, "北京", "梨子");
  dataset.addValue(Math.random()*200, "上海", "梨子");
  dataset.addValue(Math.random()*200, "广州", "梨子");
  dataset.addValue(Math.random()*400, "北京", "葡萄");
  dataset.addValue(Math.random()*400, "上海", "葡萄");
  dataset.addValue(Math.random()*400, "广州", "葡萄");
  dataset.addValue(Math.random()*500, "北京", "香蕉");
  dataset.addValue(Math.random()*500, "上海", "香蕉");
  dataset.addValue(Math.random()*500, "广州", "香蕉");
  dataset.addValue(Math.random()*600, "北京", "荔枝");
  dataset.addValue(Math.random()*600, "上海", "荔枝");
  dataset.addValue(Math.random()*600, "广州", "荔枝");
  return dataset;
 }
 
}

 

 

jfreechart 1.0.5 笔记2 - souljava - 千鸟

package com.dm.test.chart;


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.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import java.io.*;
import org.jfree.chart.ChartUtilities;
import java.awt.Color;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

public class XySeries{
 
 public static void main(String[] args) {  
  
      final XYSeries series = new XYSeries("Random Data");
        series.add(1.0, 500.2);
        series.add(5.0, 694.1);
        series.add(4.0, 100.0);
        series.add(12.5, 734.4);
        series.add(17.3, 453.2);
        series.add(21.2, 500.2);
        series.add(21.9, null);
        series.add(25.6, 734.4);
        series.add(30.0, 453.2);
        final XYSeriesCollection data = new XYSeriesCollection(series);
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "XY Series Demo",
            "X",
            "Y",
            data,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );
  
      chart.setBackgroundPaint(Color.white);


        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
   
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);       
        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();    
       
        plot.setRenderer(renderer);
       
      
   FileOutputStream fos_jpg = null;
     try {
   fos_jpg = new FileOutputStream("D:\\fruit1.jpg");
   ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
  }catch(Exception e){
   
  } finally {
   try {
    fos_jpg.close();
   } catch (Exception e) {}
  }
 }

 
}

jfreechart 1.0.5 笔记2 - souljava - 千鸟

package org.jfree.chart.demo;

import java.awt.Color;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.DataUtilities;
import org.jfree.data.DefaultKeyedValues;
import org.jfree.data.KeyedValues;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.SortOrder;
import java.io.*;
import org.jfree.chart.ChartUtilities;

/**
 * A demo showing the creation of a pareto chart.
 *
 */
public class ParetoChartDemo  {

    /**
     * Creates a new demo instance.
     *
     * @param title  the frame title.
     */
    public ParetoChartDemo(final String title) {

       

        final DefaultKeyedValues data = new DefaultKeyedValues();
        data.addValue("C", new Integer(4843));
        data.addValue("C++", new Integer(2098));
        data.addValue("C#", new Integer(26));
        data.addValue("Java", new Integer(1901));
        data.addValue("Perl", new Integer(2507));
        data.addValue("PHP", new Integer(1689));
        data.addValue("Python", new Integer(948));
        data.addValue("Ruby", new Integer(100));
        data.addValue("SQL", new Integer(263));
        data.addValue("Unix Shell", new Integer(485));

        data.sortByValues(SortOrder.DESCENDING);
        final KeyedValues cumulative = DataUtilities.getCumulativePercentages(data);
        final CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Languages", data);

        // create the chart...
        final JFreeChart chart = ChartFactory.createBarChart(
            "Freshmeat Software Projects",  // chart title
            "Language",                     // domain axis label
            "Projects",                     // range axis label
            dataset,                        // data
            PlotOrientation.VERTICAL,
            true,                           // include legend
            true,
            false
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.addSubtitle(new TextTitle("By Programming Language"));
        chart.addSubtitle(new TextTitle("As at 5 March 2003"));

        // set the background color for the chart...
        chart.setBackgroundPaint(new Color(0xBBBBDD));

        // get a reference to the plot for further customisation...
        final CategoryPlot plot = chart.getCategoryPlot();

        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setLowerMargin(0.02);
        domainAxis.setUpperMargin(0.02);

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

        final LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();

        final CategoryDataset dataset2 = DatasetUtilities.createCategoryDataset(
            "Cumulative", cumulative
        );
        final NumberAxis axis2 = new NumberAxis("Percent");
        axis2.setNumberFormatOverride(NumberFormat.getPercentInstance());
        plot.setRangeAxis(1, axis2);
        plot.setDataset(1, dataset2);
        plot.setRenderer(1, renderer2);
        plot.mapDatasetToRangeAxis(1, 1);

        plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
        FileOutputStream fos_jpg = null;
     try {
   fos_jpg = new FileOutputStream("D:\\fruit1.jpg");
   ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
  }catch(Exception e){
   
  } finally {
   try {
    fos_jpg.close();
   } catch (Exception e) {}
  }
     

    }

    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    *
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************
   
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {

        final ParetoChartDemo demo = new ParetoChartDemo("Pareto Chart Demo");
       

    }


}

 

  评论这张
 
阅读(1247)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017