asp我就知道。office 2000有一个web组件可以生成图片。。

解决方案 »

  1.   

    asp 里面你是怎么实现的呢?不妨说来听听?
      

  2.   


    干吗那么费劲???用APPLET不就妥了?难么?
      

  3.   

    APPLET怎么写,需要用JAVA里面的什么函数?多谢,小弟没写过APPLET!!!
      

  4.   

    这个问题讨论过了,我以前也试验过,很多方法,用applet/servlet/但用bean时,有一点小问题,或者你买吧,网上有,不过奇贵!10000$呀!
      

  5.   


    贵吗?给你源码,可以吧这是饼图的,柱状图更简单,自己画吧,另外别忘了搜以前的帖子.
    import java.awt.*;
    import java.io.*;
    import java.lang.*;public class PieChart extends java.applet.Applet {    String title;
        Font        font;
        FontMetrics fontMetrics;
        int titleHeight = 15;
        int columns;
        int values[];
        Color       colors[];
        String      labels[];
        float       percent[];
        float       angle[];
        int maxLabelWidth = 0;
        int         maxValueWidth = 0;
        int max = 0;
        int         strWidth=0;
        boolean     showLabel=true;   // Whether to display label or not
        boolean     showPercent=true; // Whether to display percent or not    int lx=0,ly=0;          //For Writing Label
        int cx=0,cy=0;          //Center of Circle  public synchronized void init() {    String temp;    font = new java.awt.Font("Sanserif", Font.BOLD, 12);
        fontMetrics = getFontMetrics(font);
        String bgColor=getParameter("bgcolor"); // Background color of Chart    if (bgColor==null)
           setBackground(Color.white);
        else{
            if (bgColor.equals("red")) {
                setBackground(Color.red);
            } else if (bgColor.equals("green")) {
                setBackground(Color.green);
            } else if (bgColor.equals("blue")) {
                setBackground(Color.blue);
            } else if (bgColor.equals("pink")) {
                setBackground(Color.pink);
            } else if (bgColor.equals("orange")) {
                setBackground(Color.orange);
            } else if (bgColor.equals("magenta")) {
                setBackground(Color.magenta);
            } else if (bgColor.equals("cyan")) {
                setBackground(Color.cyan);
            } else if (bgColor.equals("white")) {
                setBackground(Color.white);
            } else if (bgColor.equals("yellow")) {
                setBackground(Color.yellow);
            } else if (bgColor.equals("gray")) {
                setBackground(Color.gray);
            } else if (bgColor.equals("darkGray")) {
                setBackground(Color.darkGray);
        } else {
            setBackground(Color.white);
        }
        } title = getParameter("title"); // Title of the Pie Chart if (title == null) {
            title = "Pie Chart";
    }    temp = getParameter("columns");
        if (temp == null) {
        columns = 5;
    } else {
            columns = Integer.parseInt(temp);
    }    temp = getParameter("showlabel");
        if (temp == null) {
            showLabel = true;
    } else {
            if (temp.equalsIgnoreCase("YES"))
                showLabel = true;
            if (temp.equalsIgnoreCase("NO"))
                showLabel = false;
            else
                showLabel = true;
        }    temp = getParameter("showpercent");
        if (temp == null) {
            showPercent = true;
    } else {
            if (temp.equalsIgnoreCase("YES"))
                showPercent = true;
            if (temp.equalsIgnoreCase("NO"))
                showPercent = false;
            else
                showPercent = true;
        }    values = new int[columns];
        colors = new Color[columns];
        labels = new String[columns];
        percent= new float[columns];
        angle  = new float[columns];    float totalValue=0; for (int i=0; i < columns; i++) {        temp = getParameter("Pvalue" + (i+1));
            if (temp != null) {
    try {
                values[i] = Integer.parseInt(temp);
    } catch (NumberFormatException e) {
        values[i] = 0;
    }
        }
            totalValue +=  values[i];
            if (values[i] > max) {
    max = values[i];
        }     // parse the label for this column
            temp = getParameter("P" + "label"+ (i+1) );
            labels[i] = (temp == null) ? "" : temp;
            maxLabelWidth = Math.max(fontMetrics.stringWidth((String)(labels[i])),
         maxLabelWidth);     // parse the color attribute for this column
            temp = getParameter("P" + "color"+ (i+1) );
            if (temp != null) {
            if (temp.equals("red")) {
        colors[i] = Color.red;
            } else if (temp.equals("green")) {
        colors[i] = Color.green;
            } else if (temp.equals("blue")) {
        colors[i] = Color.blue;
            } else if (temp.equals("pink")) {
        colors[i] = Color.pink;
            } else if (temp.equals("orange")) {
        colors[i] = Color.orange;
            } else if (temp.equals("magenta")) {
        colors[i] = Color.magenta;
            } else if (temp.equals("cyan")) {
        colors[i] = Color.cyan;
            } else if (temp.equals("white")) {
        colors[i] = Color.white;
            } else if (temp.equals("yellow")) {
        colors[i] = Color.yellow;
            } else if (temp.equals("gray")) {
        colors[i] = Color.gray;
            } else if (temp.equals("darkGray")) {
        colors[i] = Color.darkGray;
    } else {
        colors[i] = Color.gray;
    }
        } else {
    colors[i] = Color.gray;
        }
        }
        float multiFactor = 100 / totalValue; for (int i=0; i < columns; i++) {
            percent[i]= values[i] * multiFactor;
            angle[i]  = (float) (percent[i] * 3.6) ;  // Calculation of Angle (360/100)
        }  }// paint method  public synchronized void paint(Graphics g) {
        int  x=0;
        int  y=0;
        int width=0,height=0;
        int ax=0,ay=0;          //For Drawing Black line from center to Peripherial
        int px=0,py=0;          //For Writing Percentage
        int radius=0;
        width=height=Math.min((getSize().width - 100),(getSize().height - 100));
        x=y=50;    if ( getSize().width > width ){
            x = (getSize().width - width ) /2 ; 
        }    cx = x + width/2;
        cy = y + height/2;
        radius = width/2;    // Draw the Title of the Chart on Top of the Applet    strWidth=fontMetrics.stringWidth(title);
        Font fnt = new java.awt.Font("Sanserif", Font.BOLD, 16);
        g.setFont(fnt);
        g.setColor(Color.red);
        g.drawString(title,((getSize().width - strWidth )/2),15);
        g.setFont(font);
        int initAngle=90;
        int sweepAngle=0;
        int incSweepAngle=0;
        int incLabelAngle= (int) (angle[0]/2);    for (int i=0; i < columns; i++) {
            sweepAngle = (int) Math.round(angle[i]);
            g.setColor((Color)colors[i]);        if (i==(columns-1)){
                sweepAngle = 360 - incSweepAngle;
                g.fillArc(x,y,width,height,initAngle,(-sweepAngle));
                g.setColor(Color.black);
                g.drawArc(x,y,width,height,initAngle,(-sweepAngle));            if (showLabel){
                    lx = (int) (cx + ( radius * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
                    ly = (int) (cy + ( radius * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
                    adjustLabel(i);
                    g.drawString((String)labels[i],lx,ly);
                }
                if (showPercent){
                    px = (int) (cx + ((radius*2/3) * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
                    py = (int) (cy + ((radius*2/3) * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
                    g.drawString(String.valueOf(Math.round(percent[i]))+"%",px,py); 
                }
                break;
            }        g.fillArc(x,y,width,height,initAngle,(-sweepAngle));
            g.setColor(Color.black);
            g.drawArc(x,y,width,height,initAngle,(-sweepAngle));
            incSweepAngle +=sweepAngle;        ax = (int) (cx + ( radius * Math.cos((incSweepAngle * 3.14f/180) - 3.14f/2)));
            ay = (int) (cy + ( radius * Math.sin((incSweepAngle * 3.14f/180) - 3.14f/2)));
            g.drawLine(cx,cy,ax,ay);        if (showLabel){
                lx = (int) (cx + ( radius * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
                ly = (int) (cy + ( radius * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
                adjustLabel(i);
                g.drawString((String)labels[i],lx,ly);
            }
            if (showPercent){
                px = (int) (cx + ((radius*2/3) * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
                py = (int) (cy + ((radius*2/3) * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
                strWidth = fontMetrics.stringWidth(Math.round(percent[i])+"%");
                g.drawString(String.valueOf(Math.round(percent[i]))+"%",(px - strWidth/2),py);
            }        incLabelAngle = incLabelAngle + (int) (angle[i]/2 + angle[i+1]/2);
            initAngle += (-sweepAngle);
        }
        g.setColor(Color.black);
        g.drawLine(cx,cy,cx,cy-radius);
      }  private void adjustLabel(int i){
        if ( (lx > cx) && (ly < cy) ){
            lx +=5;
            ly -=5;
        }    if ( (lx > cx) && (ly > cy) ){
            lx +=5;
            ly +=10;
        }    if ( (lx < cx) && (ly > cy) ){
            strWidth=fontMetrics.stringWidth(labels[i]);
            lx -= strWidth+5;
            if (lx < 0)
                lx=0;
        }    if ( (lx < cx) && (ly < cy) ){
            strWidth=fontMetrics.stringWidth(labels[i]);
            lx -= strWidth+5;
            if (lx < 0)
                lx=0;
        }
      }}
      

  6.   

    HccPro,多谢你,但是如果是线形图呢?
    等一下我把所有的分全部给你!!呵呵!!
      

  7.   

    hccPro,为什么饼图的各个部分之间只能有三根分隔线!!!!
      

  8.   

    我做過一个图表显示,能显示饼图,线装图,及柱状图完全用jdk1.01函数写成,你要吗?我的e-mail:[email protected]
      

  9.   

    当然要,可以给我吗?
    [email protected]
      

  10.   

    在applet里画不算什么.增加客户端的负担。应该用servlet画,然后动态生成jpeg或gif输出。
    给加分后,给出源代码。
    如何在servlet中实时地创建图象 
    在Java创建图象或进行图象处理,有几个包和类是需要用到的。当您的servlet有图象文件时您有两个选择。 把文件写入磁盘并提供连接。注意写在您的web服务器目录树下(不是在服务器磁盘的任何地方都行。)你可以
    用Java 2 JPEGCodec类,或Acme Labs' GIFEncoder类将Java Graphics 转换成图象文件或二进制流.
    值得一提的是在一些servlet引擎设置中,servlet的目录不能通过web server进入,只能通过servlet引擎,也就是
    说您不能通过http:// URL登录,您可以向您的servlet输出的HTML传送IMG标签,或传送HTTP重新定位来让浏览器直接下载图象。 
    有传送
    重新定位源代码。
    图象可以被保存在浏览器的cache中,当再次请求时不必重新运行servlet,因此减轻了服务器的负担。
    ). 图象不能从磁盘中删除,因此您必须写一段程序来定期清理图象目录,或进入目录
    后用手工删除。(或买一张大点的硬盘)2.直接从servlet输出图象。通过给image/gif (for GIFs)或 image/jpeg 
    (for JPEGs)设置Content-type头来实现它。然后打开HttpResponse  
    output流作为原始流而不是打印流,用write()方法直接传送字节。 以下是一个用servlet实时创建图像的例子程序略。一个线性图的例子。
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.io.IOException;
    import java.awt.image.BufferedImage;
    import java.awt.Graphics2D;
    import java.awt.Color;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import java.awt.FontMetrics;import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder; /**
     *  Draw a simple stock price graph for a one week period
     *  The stock is Sun Microsystems for a week in March, 2000.
     *
     */
    public class StockGraphProducer implements ImageProducer
    {
      private static int ImageWidth = 300;
      private static int ImageHeight = 300;  private static int VertInset = 25;
      private static int HorzInset = 25;
      private static int HatchLength = 10;  /**
       *  Request the producer create an image
       *
       *  @param stream stream to write image into
       *  @return image type
       */
      public String createImage(OutputStream stream) throws IOException
      {
        plottedPrices = new Point2D.Double[5];
        int prices[] =  {105, 100, 97, 93, 93};    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(stream);    BufferedImage bi = new BufferedImage(ImageWidth + 10, 
                                             ImageHeight, 
                                             BufferedImage.TYPE_BYTE_INDEXED);    graphics = bi.createGraphics();
        graphics.setColor(Color.white);
        graphics.fillRect(0, 0, bi.getWidth(), bi.getHeight());    graphics.setColor(Color.red);    createVerticalAxis();
        createHorizontalAxis();
        
        graphics.setColor(Color.green);
        
        plotPrices(prices);    encoder.encode(bi);    return "image/jpg";
      }  /**
       *  Create the vertical axis
       *
       *
       */
      void createVerticalAxis()
      {    vertAxis = new Line2D.Double(HorzInset, 
                                     VertInset, 
                                     HorzInset, 
                                     ImageHeight - VertInset);
        graphics.draw(vertAxis);
        // Draw the vertical hatch s
        int verticalLength = ImageHeight - (VertInset * 2);
        int interval = verticalLength/5;    Line2D.Double vertHatch1 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2, 
                                                     vertAxis.getP1().getY(), 
                                                     vertAxis.getP1().getX() + HatchLength/2, 
                                                     vertAxis.getP1().getY());    graphics.draw(vertHatch1);
        Line2D.Double vertHatch2 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2, 
                                                     vertAxis.getP1().getY() + interval, 
                                                     vertAxis.getP1().getX() + HatchLength/2, 
                                                     vertAxis.getP1().getY() + interval);    graphics.draw(vertHatch2);    Line2D.Double vertHatch3 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2, 
                                                     vertAxis.getP1().getY() + interval * 2, 
                                                     vertAxis.getP1().getX() + HatchLength/2, 
                                                     vertAxis.getP1().getY() + interval * 2);    graphics.draw(vertHatch3);
        Line2D.Double vertHatch4 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2, 
                                                     vertAxis.getP1().getY() + interval * 3 , 
                                                     vertAxis.getP1().getX() + HatchLength/2, 
                                                     vertAxis.getP1().getY() + interval * 3);    graphics.draw(vertHatch4);    Line2D.Double vertHatch5 = new Line2D.Double(vertAxis.getP1().getX() -  HatchLength/2, 
                                                     vertAxis.getP1().getY() + interval * 4, 
                                                     vertAxis.getP1().getX() + HatchLength/2, 
                                                     vertAxis.getP1().getY() + interval * 4);    graphics.draw(vertHatch5);    verticalAxisTicks = new Line2D.Double[5];
        verticalAxisTicks[0] = vertHatch1;
        verticalAxisTicks[1] = vertHatch2;
        verticalAxisTicks[2] = vertHatch3;
        verticalAxisTicks[3] = vertHatch4;
        verticalAxisTicks[4] = vertHatch5;    verticalYTop = vertHatch1.getP1().getY();
        verticalYBottom = vertHatch5.getP1().getY();
      }  /** 
       *  Create the horizontal axis
       *
       *
       */
      void createHorizontalAxis()
      {
        horAxis = new Line2D.Double(HorzInset, 
                                    ImageHeight - VertInset, 
                                    ImageWidth -  HorzInset, 
                                    ImageHeight - VertInset);
        graphics.draw(horAxis);    int horLength = ImageWidth - (HorzInset * 2);
        int horInterval = horLength/5;    assignVerticalRange(90, 110, 5);    // Draw the horizontal hatches
        
        Line2D.Double horHatch1 = new Line2D.Double(horAxis.getP1().getX() + horInterval, 
                                                    horAxis.getP1().getY() - HatchLength/ 2, 
                                                    horAxis.getP1().getX() + horInterval, 
                                                    horAxis.getP1().getY() + HatchLength/2);    graphics.draw(horHatch1);    decorateVerticalLine(graphics, horHatch1, "M");    Line2D.Double horHatch2 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 2, 
                                                    horAxis.getP1().getY() - HatchLength/ 2, 
                                                    horAxis.getP1().getX() + horInterval * 2, 
                                                    horAxis.getP1().getY() + HatchLength/2);    graphics.draw(horHatch2);    Line2D.Double horHatch3 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 3, 
                                                    horAxis.getP1().getY() - HatchLength/ 2, 
                                                    horAxis.getP1().getX() + horInterval * 3, 
                                                    horAxis.getP1().getY() + HatchLength/2);    graphics.draw(horHatch3);    Line2D.Double horHatch4 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 4, 
                                                    horAxis.getP1().getY() - HatchLength/ 2, 
                                                    horAxis.getP1().getX() + horInterval * 4, 
                                                    horAxis.getP1().getY() + HatchLength/2);    graphics.draw(horHatch4);    Line2D.Double horHatch5 = new Line2D.Double(horAxis.getP1().getX() + horInterval * 5, 
                                                    horAxis.getP1().getY() - HatchLength/ 2, 
                                                    horAxis.getP1().getX() + horInterval * 5, 
                                                    horAxis.getP1().getY() + HatchLength/2);    horizontalAxisTicks =  new double[5];
        horizontalAxisTicks[0] = horHatch1.getX1();
        horizontalAxisTicks[1] = horHatch2.getX1();
        horizontalAxisTicks[2] = horHatch3.getX1();
        horizontalAxisTicks[3] = horHatch4.getX1();
        horizontalAxisTicks[4] = horHatch5.getX1();    graphics.draw(horHatch5);    // Add text to hatches
        decorateVerticalLine(graphics, horHatch1, "M");
        decorateVerticalLine(graphics, horHatch2, "T");
        decorateVerticalLine(graphics, horHatch3, "W");
        decorateVerticalLine(graphics, horHatch4, "T");
        decorateVerticalLine(graphics, horHatch5, "F");  }  /**
       *  Plot the five closing stock prices
       *
       *
       */
      void plotPrices(int[] prices)
      {
        //***************************************************************
        // calculatePriceRatio will determine the percentage of the
        // Y axis the price is, then multiply by the Y axis length.
        //
        //***************************************************************
        double yAxisLength = verticalYBottom - verticalYTop;    double mondayPrice = calculatePriceRatio(prices[0]) * yAxisLength + VertInset;
        double tuesdayPrice = calculatePriceRatio(prices[1]) * yAxisLength + VertInset;
        double wednsdayPrice = calculatePriceRatio(prices[2]) * yAxisLength + VertInset;
        double thursdayPrice = calculatePriceRatio(prices[3]) * yAxisLength + VertInset;
        double fridayPrice = calculatePriceRatio(prices[4]) * yAxisLength + VertInset;    Point2D.Double day1 = new Point2D.Double(horizontalAxisTicks[0], mondayPrice);
        Point2D.Double day2 = new Point2D.Double(horizontalAxisTicks[1], tuesdayPrice);
        Point2D.Double day3 = new Point2D.Double(horizontalAxisTicks[2], wednsdayPrice);
        Point2D.Double day4 = new Point2D.Double(horizontalAxisTicks[3], thursdayPrice);
        Point2D.Double day5 = new Point2D.Double(horizontalAxisTicks[4], fridayPrice);    Line2D.Double line1 = new Line2D.Double(day1, day2);
        Line2D.Double line2 = new Line2D.Double(day2, day3);
        Line2D.Double line3 = new Line2D.Double(day3, day4);
        Line2D.Double line4 = new Line2D.Double(day4, day5);
        
        graphics.draw(line1);
        graphics.draw(line2);
        graphics.draw(line3);
        graphics.draw(line4);  }  /**
       *  Determine the location of the price in the range of price data
       *
       */
      double calculatePriceRatio(int price)
      {
        double totalDataRange = highVerticalRange - lowVerticalRange;
        double pointDelta = highVerticalRange - price;
        double ratio = pointDelta/totalDataRange;    return ratio;
      }  /**
       *  Assignes the range for the vertical axis
       *
       */
      void assignVerticalRange( int low, int high, int increment )
      {
        lowVerticalRange = low;
        highVerticalRange = high;    int current = low;
        int hatchCount = verticalAxisTicks.length - 1;    //***************************************************************
        // Label each vertical tick starting with the low value and
        // increasing by increment value
        //***************************************************************
        while ( current <= high ) 
        {
          decorateHorizontalLine(graphics, 
                                 verticalAxisTicks[hatchCount], 
                                 new Integer(current).toString() );
          current += increment;
          hatchCount--;
        }
      }  /**
       *  Adds decorating text to the enpoint of a horizontal line
       *
       */
      void decorateHorizontalLine(Graphics2D graphics, Line2D.Double line, String text)
      {
        double endX = line.getX1();
        double endY = line.getY1();
        double baseX = endX;
        double baseY = endY;    //***************************************************************
        // The text should be slightly to the left of the line
        // and centered
        //***************************************************************
        FontMetrics metrics = graphics.getFontMetrics();
        baseX -= metrics.stringWidth(text);
        baseY += metrics.getAscent()/2;
        graphics.drawString(text, 
                            new Float(baseX).floatValue(), 
                            new Float(baseY).floatValue());
      }  /**
       *  Adds decorating text to the enpoint of a vertical line
       *
       */
      void decorateVerticalLine (Graphics2D graphics, Line2D.Double line, String text)
      {
        double endX = line.getX2();
        double endY = line.getY2();
        double baseX = endX;
        double baseY = endY;    //***************************************************************
        // Center the text over the line
        //***************************************************************
        FontMetrics metrics = graphics.getFontMetrics();
        baseX -= metrics.stringWidth(text)/2;
        baseY += metrics.getAscent();
        
        graphics.drawString(text, 
                            new Float(baseX).floatValue(), 
                            new Float(baseY).floatValue());  }
         
      /**
       *  Test Entrypoint
       *
       */
      public static void main(String[] args)
      {
      
        try
        {
           FileOutputStream f = new FileOutputStream("stockgraph.jpg");
           StockGraphProducer producer = new StockGraphProducer();
           producer.createImage(f);
           f.close(); 
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }  private Line2D.Double vertAxis;
      private Line2D.Double horAxis;
      private double[] horizontalAxisTicks;
      private int highVerticalRange;
      private int lowVerticalRange;
      private Graphics2D graphics;
      private Line2D.Double[] verticalAxisTicks;
      private Point2D.Double[] plottedPrices;
      private double verticalYTop;
      private double verticalYBottom;}
      

  11.   

    按上面的stockgraphproducer例子做即可,足够详细了。源代码解释还是你自己研究吧,很简单,我刚做了一个。
      

  12.   

    robinhoodx,为什么我运行起来报错了
    不能解析符号 ImageProducerwhy?
      

  13.   

    http://www.javaworld.com/jw-05-2000/servlets/Servlets.zip 
    download  full source code.use it as http://......../ImageProducer?StockgraphProducer  in a web page.It is full solution now!!!
    for more to  see www.jspin.com             of course a lot english
      

  14.   

    delete the pieproducer.java which is not support in java2 any more.
    only keep imageproducer.java and stockgraphproducer.java
    use jbuilder compile
    or javac, first compile imageprodcer, then the other. 
      

  15.   

    I ever tried it on jbuilder5, it works perfectly. 
    but after analyzing the example program, you'd better write your own program according your application.