用JSP生成折线图并以图片的方式显示出来,不知道可不可以。现有如下代码,以便共同学习。<%@ page language="java" contentType="image/jpeg;charset=GB2312"
import="java.util.*,java.awt.*,java.awt.geom.*"
import="java.awt.image.*,com.sun.image.codec.jpeg.*"
%>
<%
  int width=600,height=500;
  BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  Graphics g=image.getGraphics();
  Graphics2D g2d=(Graphics2D) g;
  g.setColor(new Color(240,249,255));
  g.fillRect(0,0,width,height);
  g.setColor(Color.white);
  g.drawRect(10,35,width-20,height-46);
  g.setColor(Color.black);
  g.setFont(new Font("华文新魏",Font.PLAIN,22));
  g.drawString("水果销售情况图表  JSP版(折线图)  网友提供",15,25);
  int book_sales[]=new int[5];
  for(int i=0;i<book_sales.length;i++){
  book_sales[i]=1+(int)(Math.random()*100);
  }
  String book_title[]={"苹果","香蕉","荔枝","猕猴桃","葡萄"};
  Color color[]=new Color[5];
  color[0]=new Color(99,99,0);
  color[1]=new Color(255,169,66);
  color[2]=new Color(33,255,66);
  color[3]=new Color(33,0,255);
  color[4]=new Color(255,0,66);
  g.setFont(new Font("华文行楷",Font.BOLD,16));
  g.drawString("销售量",20,50);
  g.drawString("水果分类",500,465);
  g.setFont(new Font("SanSSerif",Font.PLAIN,12));
  int salesValue=0;
  for(int i=418;i>0;i-=38){
    g.setColor(Color.black);
g.drawString(""+salesValue,36,(i+27));
g.setColor(Color.lightGray);
g.drawLine(80,(i+27),520,(i+27));
salesValue+=10;
  }
  g.setColor(Color.black);
  g.drawLine(80,40,80,445);
  g.drawLine(80,445,550,445);
  int drawHigh[]=new int[book_title.length];
  int drawwidth[]=new int[book_title.length];
  for(int i=0;i<book_title.length;i++){
    drawHigh[i]=445-(int)(Math.ceil(book_sales[i]*3.8));
drawwidth[i]=110+i*80;
g.setColor(color[i]);
g.fillOval(drawwidth[i]-7,drawHigh[i]-7,14,14);
g.setColor(Color.black);
g.drawString(book_title[i],110+i*80,465);
  }
  g2d.setXORMode(Color.white);
  g2d.setStroke(new BasicStroke(4.0f));
  g2d.setPaint(Color.red);
  g2d.drawPolyline(drawwidth,drawHigh,book_title.length);
  g.dispose();
  ServletOutputStream sos=response.getOutputStream();
  JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(sos);
  encoder.encode(image);
%>

解决方案 »

  1.   

    你可以下载jfreechart的源码,在package org.jfree.chart.demo;内都是它生成各种图表的示例,非常全面;
    其中有一个折线图如下:package org.jfree.chart.demo;import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.StandardLegend;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.labels.ItemLabelPosition;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.LineAndShapeRenderer;
    import org.jfree.data.CategoryDataset;
    import org.jfree.data.DefaultCategoryDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;/**
     * A simple demonstration application showing how to create a line chart using data from a
     * {@link CategoryDataset}.
     *
     * @author David Gilbert
     */
    public class LineChartDemo1 extends ApplicationFrame {    /**
         * Creates a new demo.
         *
         * @param title  the frame title.
         */
        public LineChartDemo1(String title) {        super(title);        CategoryDataset dataset = createDataset();
            JFreeChart chart = createChart(dataset);
            ChartPanel chartPanel = new ChartPanel(chart);
            chartPanel.setPreferredSize(new Dimension(500, 270));
            setContentPane(chartPanel);    }    /**
         * Creates a sample dataset.
         * 
         * @return The dataset.
         */
        private CategoryDataset createDataset() {
            
            // row keys...
            String series1 = "First";
            String series2 = "Second";
            String series3 = "Third";        // column keys...
            String type1 = "Type 1";
            String type2 = "Type 2";
            String type3 = "Type 3";
            String type4 = "Type 4";
            String type5 = "Type 5";
            String type6 = "Type 6";
            String type7 = "Type 7";
            String type8 = "Type 8";        // create the dataset...
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();        dataset.addValue(1.0, series1, type1);
            dataset.addValue(4.0, series1, type2);
            dataset.addValue(3.0, series1, type3);
            dataset.addValue(5.0, series1, type4);
            dataset.addValue(5.0, series1, type5);
            dataset.addValue(7.0, series1, type6);
            dataset.addValue(7.0, series1, type7);
            dataset.addValue(8.0, series1, type8);        dataset.addValue(5.0, series2, type1);
            dataset.addValue(7.0, series2, type2);
            dataset.addValue(6.0, series2, type3);
            dataset.addValue(8.0, series2, type4);
            dataset.addValue(4.0, series2, type5);
            dataset.addValue(4.0, series2, type6);
            dataset.addValue(2.0, series2, type7);
            dataset.addValue(1.0, series2, type8);        dataset.addValue(4.0, series3, type1);
            dataset.addValue(3.0, series3, type2);
            dataset.addValue(2.0, series3, type3);
            dataset.addValue(3.0, series3, type4);
            dataset.addValue(6.0, series3, type5);
            dataset.addValue(3.0, series3, type6);
            dataset.addValue(4.0, series3, type7);
            dataset.addValue(3.0, series3, type8);        return dataset;
                    
        }
        
        /**
         * Creates a sample chart.
         * 
         * @param dataset  a dataset.
         * 
         * @return The chart.
         */
        private JFreeChart createChart(CategoryDataset dataset) {
            
            // create the chart...
            JFreeChart chart = ChartFactory.createLineChart(
                "Line Chart Demo 1",       // chart title
                "Type",                    // domain axis label
                "Value",                   // range axis label
                dataset,                   // data
                PlotOrientation.VERTICAL,  // orientation
                true,                      // include legend
                true,                      // tooltips
                false                      // urls
            );        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
            StandardLegend legend = (StandardLegend) chart.getLegend();
            legend.setDisplaySeriesShapes(true);
            legend.setShapeScaleX(1.5);
            legend.setShapeScaleY(1.5);
            legend.setDisplaySeriesLines(true);        chart.setBackgroundPaint(Color.white);        CategoryPlot plot = chart.getCategoryPlot();
            plot.setBackgroundPaint(Color.lightGray);
            plot.setRangeGridlinePaint(Color.white);        // customise the range axis...
            NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            rangeAxis.setAutoRangeIncludesZero(true);
            rangeAxis.setUpperMargin(0.20);
            rangeAxis.setLabelAngle(Math.PI / 2.0);        // ****************************************************************************
            // * 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.             *
            // ****************************************************************************
            
            // customise the renderer...
            LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
            renderer.setDrawShapes(true);        renderer.setSeriesStroke(
                0, new BasicStroke(2.0f,
                                   BasicStroke.CAP_ROUND,
                                   BasicStroke.JOIN_ROUND,
                                   1.0f,
                                   new float[] {10.0f, 6.0f},
                                   0.0f)
            );
            renderer.setSeriesStroke(
                1, new BasicStroke(2.0f,
                                   BasicStroke.CAP_ROUND,
                                   BasicStroke.JOIN_ROUND,
                                   1.0f,
                                   new float[] {6.0f, 6.0f},
                                   0.0f)
            );
            renderer.setSeriesStroke(
                2, new BasicStroke(2.0f,
                                   BasicStroke.CAP_ROUND,
                                   BasicStroke.JOIN_ROUND,
                                   1.0f,
                                   new float[] {2.0f, 6.0f},
                                   0.0f)
            );        renderer.setItemLabelsVisible(true);
            renderer.setPositiveItemLabelPosition(new ItemLabelPosition());
            renderer.setNegativeItemLabelPosition(new ItemLabelPosition());
            // OPTIONAL CUSTOMISATION COMPLETED.
            
            return chart;
        }
        
        /**
         * Starting point for the demonstration application.
         *
         * @param args  ignored.
         */
        public static void main(String[] args) {        LineChartDemo1 demo = new LineChartDemo1("Line Chart Demo");
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);    }}