关闭

jfreechart应用篇

标签: jfreechartdatasetimagestringurlsession
2584人阅读 评论(5) 收藏 举报
分类:

前段时间有个朋友要做柱图,出了点问题,GOOGLE了半天,原来网上很多人都有这个问题,而且发现竟然只有饼图和柱图(还是有点问题的),就花了一早上,把以前做的几个项目里的几种常用的图的用法拿出来列一下,希望对那些想用jfreechart出图的人有点帮助。

(附:以下为1.0.0-rc)

一、饼图: 

 

      DefaultPieDataset dataset=new DefaultPieDataset();
        //开始填充数据
        dataset.setValue("Java", new Double(43.200000000000003D));
        dataset.setValue("Visual Basic", new Double(10D));
        dataset.setValue("C/C++", new Double(17.5D));
        dataset.setValue("PHP", new Double(32.5D));
        dataset.setValue("Perl", new Double(12.5D));
       
        JFreeChart chart=ChartFactory.createPieChart("2D饼型图",dataset,true,true,true);
        chart.setTitle("统计图");//设置图片标题
        ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
       

         String imgaeFileName=ServletUtilities.saveChartAsJPEG(chart,300,200,info,session);//设置生成图片,包括图片的大小,长度是300,宽是200

        session.setAttribute("image_name1",imageFileName;
        String URL= request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;//Display为org.jfree.chart.servlet.DisplayChart
        session.setAttribute("image_url1",URL);

 

显示代码:(以下显示代码不注明了,是一样的,只是session中的属性名不同而已,大家注意区别)

<img src="<%=session.getAttribute("image_url1")%>" width=300 height=200 border=0 usemap="#<%=session.getAttribute("image_name1") %>">

二、3D饼图

 

        DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
        defaultpiedataset.setValue("Java", new Double(43.200000000000003D));
        defaultpiedataset.setValue("Visual Basic", new Double(10D));
        defaultpiedataset.setValue("C/C++", new Double(17.5D));
        defaultpiedataset.setValue("PHP", new Double(32.5D));
        defaultpiedataset.setValue("Perl", new Double(12.5D));
       
        JFreeChart jfreechart = ChartFactory.createPieChart3D("3D 饼图", defaultpiedataset, true, false, false);
        PiePlot3D pieplot3d = (PiePlot3D)jfreechart.getPlot();
          
       String imageFileName2= ServletUtilities.saveChartAsJPEG(jfreechart,300,200,session);
        session.setAttribute("image_name2",imageFileName2);
         String URL=request.getContextPath()+"/servlet/DisplayChart?filename="+imageFileName2;

         session.setAttribute("image_url2",URL);

 

 

三、柱图

        DefaultCategoryDataset dataset=new DefaultCategoryDataset();//设置数据源
        
        dataset.setValue(new Double(43.200000000000003D),"图解","Java" );
        dataset.setValue( new Double(10D),"图解","Visual Basic");
        dataset.setValue(new Double(17.5D), "图解","C/C++");
        dataset.setValue(new Double(32.5D), "图解","PHP");
        dataset.setValue(new Double(12.5D), "图解","Perl");
         //添加数据,参数包括名称和数值
        
        
 
         JFreeChart chart1=ChartFactory.createBarChart3D("柱图","大人物","地盘",                dataset,PlotOrientation.VERTICAL,false,false,false);

         CategoryPlot plot=chart1.getCategoryPlot();
      
         //BarRenderer3D renderer1=new BarRenderer3D();
 
 final BarRenderer renderer1 = (BarRenderer) plot.getRenderer();
    
     renderer1.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());      

         renderer1.setItemLabelsVisible(true);//显示每个柱子上的数据
       
         renderer1.setItemMargin(0.1);//设置每个柱子之间的距离
         plot.setRenderer(renderer1);//让上面对柱子的设置生效
        String imageFileName=ServletUtilities.saveChartAsJPEG(chart1,300,200,session);
         //生成图片,包括图片的大小

         session.setAttribute("image_name3",imageFileName);
         String URL=request.getContextPath()+"/servlet/DisplayChart?filename="+imageFileName;

         session.setAttribute("image_url3",URL);

四、线图

 

DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
        defaultcategorydataset.addValue(212D, "Classes", "JDK 1.0");
        defaultcategorydataset.addValue(504D, "Classes", "JDK 1.1");
        defaultcategorydataset.addValue(1520D, "Classes", "SDK 1.2");
        defaultcategorydataset.addValue(1842D, "Classes", "SDK 1.3");
        defaultcategorydataset.addValue(2991D, "Classes", "SDK 1.4");
       
        JFreeChart jfreechart = ChartFactory.createLineChart("Java Standard Class Library", "线图", "Class Count", defaultcategorydataset, PlotOrientation.VERTICAL, false, true, false);
        jfreechart.addSubtitle(new TextTitle("Number of Classes By Release"));
        TextTitle texttitle = new TextTitle("线图");
        texttitle.setFont(new Font("SansSerif", 0, 10));
        texttitle.setPosition(RectangleEdge.BOTTOM);
        texttitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        jfreechart.addSubtitle(texttitle);
        jfreechart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
        categoryplot.setBackgroundPaint(Color.lightGray);
        categoryplot.setRangeGridlinePaint(Color.white);
        NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
        lineandshaperenderer.setShapesVisible(true);
        lineandshaperenderer.setDrawOutlines(true);
        lineandshaperenderer.setUseFillPaint(true);
        lineandshaperenderer.setFillPaint(Color.white);
       
        String imageFileName4 = ServletUtilities.saveChartAsJPEG(jfreechart,500,500,session);
       
       session.setAttribute("image_name4",imageFileName4);
         String URL=request.getContextPath()+"/servlet/DisplayChart?filename="+imageFileName4;

         session.setAttribute("image_url4",URL);

 五、时序图

 

TimeSeries timeseries = new TimeSeries("Random Data");
        Day day = new Day(1, 1, 1990);
        double d = 100D;
        for(int j = 0; j < 4000; j++)
            try
            {
                d = (d + Math.random()) - 0.5D;
                timeseries.add(day, new Double(d));
                day = (Day)day.next();
            }
            catch(SeriesException seriesexception)
            {
                System.err.println("Error adding to series");
            }
            XYDataset xydataset = new TimeSeriesCollection(timeseries);
            JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("时序图", "Day", "Value", xydataset, false, false, false);
        XYPlot xyplot = (XYPlot)jfreechart.getPlot();
        XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer();
        xylineandshaperenderer.setDefaultShapesVisible(false);
        xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(0.5F, 1, 1, 5F, new float[] {
            5F, 10F
        }, 0.0F));
        xyplot.setRenderer(xylineandshaperenderer);
       
        String imageFileName5 = ServletUtilities.saveChartAsJPEG(jfreechart,500,500,session);
       
       session.setAttribute("image_name5",imageFileName5);
         String URL=request.getContextPath()+"/servlet/DisplayChart?filename="+imageFileName5;

         session.setAttribute("image_url5",URL);

 

当然jfreechart能做的图还是很多的,不过我总觉得在Swing等环境下,效果还要好点,至少可以实现动态图,比如说实时股票图等,WEB页面要做也是applet,实在是个缺憾.

还有其他很多图,我就不一一类举了,我日常就用这五种图最多,大家如果还有需要的,可以和我联系,我发给你。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20809次
    • 积分:316
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:7篇
    • 译文:0篇
    • 评论:14条
    文章分类
    文章存档
    最新评论