使用jfreechart創建柱狀圖、餅圖

柱圖:

private String createYXChart(String fileName, Map dataMap,String chartTitle,int days,HashMap labelMap,Date fromDate,Date endDate,HttpServletRequest request,int width,int height) throws Exception {
            String path = request.getSession().getServletContext().getRealPath("/images/temp/");
            Calendar cal = Calendar.getInstance();
//            String filename=cal.getTimeInMillis()+".png";
            String filename=fileName;
            String chartFileName = path + "/"+filename;
           
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
            cal.setTime(fromDate);
            for (int i = 0; i < days; i++) {
                String curDay = format.format(cal.getTime());
                if (dataMap.get(curDay)!=null){
                    Object vl=dataMap.get(curDay);
                    Long value1= Long.valueOf(vl.toString());
                    dataset.addValue(value1, "", curDay.substring(4));
                }else{
                    dataset.addValue(0, "", curDay.substring(4));
                }
                cal.add(Calendar.DAY_OF_MONTH, 1);
            }
           
            JFreeChart chart = ChartFactory.createBarChart3D(chartTitle, "the latest "+days+" days", format.format(fromDate) + "-" + format.format(endDate), dataset, PlotOrientation.VERTICAL, false, false, false);
            chart.setBackgroundPaint(new Color(0xE1E1E1)); 
            CategoryPlot plot = chart.getCategoryPlot();
            // Set Y-axis
            NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
             
            CategoryAxis domainAxis = plot.getDomainAxis();
            //Set the distance from the left picture
            domainAxis.setLowerMargin(0.01);
            domainAxis.setTickLabelFont(new Font("arial",Font.PLAIN,8));
             
            BarRenderer3D renderer = new BarRenderer3D();
            //Set column's color
            renderer.setSeriesPaint(0, new Color(0xff00));

            plot.setRenderer(renderer);
           
            File file = new File(chartFileName);  
             
            ChartUtilities.saveChartAsPNG(file,chart,width,height);
            return filename;
        }

 

餅圖:

private String createPieChart(String fileName, Map dataMap,String chartTitle,String title, Map labelMap,String fromDate,String endDate,HttpServletRequest request,int width,int height) throws Exception {
        String path = request.getSession().getServletContext().getRealPath("/images/temp/");
//        Calendar cal = Calendar.getInstance();
        String filename = fileName;
        String chartFileName = path + "/"+filename;
       
        DefaultPieDataset dataset = new DefaultPieDataset();
        //put chart data
        for(Iterator item=dataMap.keySet().iterator();item.hasNext();){
            Object obj=(Object)item.next();
            if (labelMap!=null && labelMap.get(obj)!=null){
                dataset.setValue(labelMap.get(obj).toString(),Integer.parseInt(dataMap.get(obj).toString()));
            }else{
                dataset.setValue(obj.toString(),Integer.parseInt(dataMap.get(obj).toString()));
            }
        }
       
        // Change the property of the chart
        JFreeChart chart = ChartFactory.createPieChart3D(chartTitle,dataset,false, true, false);
        PiePlot pieplot = (PiePlot)chart.getPlot();
        pieplot.setLabelFont(new Font("arial", 0, 10));
        pieplot.setNoDataMessage("None data");
        pieplot.setCircular(true);
        pieplot.setLabelGap(0.02D);
        pieplot.setBackgroundPaint(new Color(0xE1E1E1));
        pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
                "{0} - {2}",NumberFormat.getNumberInstance(),new DecimalFormat("0.00%"))
        );

        LegendTitle legendtitle = new LegendTitle(chart.getPlot());
        BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
        LabelBlock labelblock = new LabelBlock(title, new Font("arial", 1, 12));
        labelblock.setPadding(5D, 5D, 5D, 5D);
        blockcontainer.add(labelblock, RectangleEdge.TOP);
       
        BlockContainer blockcontainer1 = legendtitle.getItemContainer();
       
        blockcontainer1.setPadding(2D, 10D, 5D, 2D);
        blockcontainer.add(blockcontainer1);
        legendtitle.setWrapper(blockcontainer);
        legendtitle.setPosition(RectangleEdge.RIGHT);
       
        legendtitle.setWrapper(blockcontainer);
        legendtitle.setPosition(RectangleEdge.RIGHT);
        legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        chart.addSubtitle(legendtitle);
        if (fromDate!=null && fromDate.trim().length()>0 && endDate!=null && endDate.trim().length()>0){
            TextTitle t = new TextTitle(fromDate+"-"+endDate);
            chart.addSubtitle(0, t);
        }
        chart.setBackgroundPaint(new Color(0xE1E1E1));
        File file = new File(chartFileName);
        ChartUtilities.saveChartAsPNG(file,chart,width,height);
        return filename;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章