Kimi's NutShell

我荒废的今日,正是昨日殒身之人祈求的明日

BlogJava 新随笔 管理
  141 Posts :: 0 Stories :: 75 Comments :: 0 Trackbacks

package com.primesuccess.portal.chart;

import com.primesuccess.portal.chartAttTemplate.LineAttTemplate;
import com.primesuccess.portal.util.JSFUtils;

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

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import java.io.Writer;

import java.text.NumberFormat;

import javax.faces.context.FacesContext;

import javax.faces.context.ResponseStream;

import java.io.OutputStream;

import java.util.Hashtable;

import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.imagemap.ImageMapUtilities;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
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.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.data.category.CategoryDataset;


public class LineBean {


    CategoryDataset data;
    LineAttTemplate att;


    public LineBean(CategoryDataset data, LineAttTemplate att) {
        this.data = data;
        this.att = att;
    }


    static class LabelGenerator extends StandardCategoryItemLabelGenerator {

        public String generateItemLabel(CategoryDataset categorydataset, int i,
                                        int j) {
            return categorydataset.getRowKey(i).toString();
        }

        LabelGenerator() {
            super();
        }
    }
   
    static class StandardCategoryURLGeneratorEx extends StandardCategoryURLGenerator {
    String prefix="aa.jspx";

        public String generateURL(CategoryDataset dataset, int series,
                              int category) {
                  
                            return "#\""+" onclick=\"javascript:window.open('"+ super.generateURL(dataset,series,category)+"','newwindow','height=100,width=400')";
       
        }
//        public String getTargeURL(){
//            return
//        }

        StandardCategoryURLGeneratorEx(String targeturl) {
            super(targeturl);
        }
    }

    private JFreeChart createChart(String targetUrl) {
        JFreeChart jfreechart =
            ChartFactory.createLineChart("", att.getDomaindesc(),
                                         att.getRangedesc(), data,
                                         PlotOrientation.VERTICAL, true, true,
                                         false);
        jfreechart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
        categoryplot.setBackgroundPaint(att.getBackgroudpaint());
        categoryplot.setDomainAxisLocation(att.getDomainaxisloc());
        categoryplot.getDomainAxis().setTickLabelFont( new Font("Arial", Font.PLAIN, 8) );
        categoryplot.mapDatasetToRangeAxis(1, 1);
       
        LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
        if (targetUrl != null && !targetUrl.trim().equals("")) {
           
            lineandshaperenderer.setItemURLGenerator(new StandardCategoryURLGeneratorEx(targetUrl));
         
        }
        Hashtable hash=att.getSeriesitemlabelfont();
        for(int i=0;i<att.getSeries_count();i++){
            lineandshaperenderer.setSeriesItemLabelFont(i,(Font)hash.get(i),true);
        }
        lineandshaperenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
        lineandshaperenderer.setItemLabelGenerator(new LabelGenerator());
        lineandshaperenderer.setShapesFilled(att.isShapesfilled());
        lineandshaperenderer.setShapesVisible(att.isShapesvisible());
        categoryplot.setRenderer(lineandshaperenderer);
        categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
        jfreechart.setTitle(new TextTitle(att.getTitle(),
                                          new Font("Arial", Font.BOLD, 15)));
        jfreechart.setAntiAlias(false);
        jfreechart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
                                           RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        return jfreechart;
    }

    public String generateChart(String targetURL, PrintWriter pw, int w,
                                int h) {
        JFreeChart chart = createChart(targetURL);
        ChartRenderingInfo info =
            new ChartRenderingInfo(new StandardEntityCollection());
        String filename = new String();
        try {
            filename =
                    ServletUtilities.saveChartAsPNG(chart, w, h, info, JSFUtils.getSession());

            ImageMapUtilities.writeImageMap(pw, filename, info);
            pw.flush();
        } catch (Exception e) {
            e.printStackTrace();
            filename = "public_error_500x300.png";
        }
        return filename;
    }

    public String[] getLastURLandFilename() throws IOException {
        HttpServletResponse resp =
            (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
      
        String filenames =
            generateChart(att.getItemurl(), new PrintWriter((OutputStream)resp.getOutputStream()),
                          att.getWidth(), att.getHeight());
        return new String[] { "/servlet/DisplayChart?filename=" + filenames,
                              filenames };
    }


}

 

posted on 2006-04-30 16:45 Kimi 阅读(402) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: