在JSP中使用Servlet的JFree图表 [英] JFree chart by using servlet in jsp

查看:101
本文介绍了在JSP中使用Servlet的JFree图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Servlet和渲染的图像绘制折线图? 我写了一个servlet并创建了一个图表.但是我不知道如何在我的jsp页面上显示它.

How can i draw a line chart by using Servlets and rendered Image? I wrote a servlet and create a chart.but i don't know how to display it on my jsp page.

我的servlet代码:

my servlet code:

public class GraphGen extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        genGraph(req, resp);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        genGraph(req, resp);
    }

    @SuppressWarnings("deprecation")
    public void genGraph(HttpServletRequest req, HttpServletResponse resp) {

        try {
            OutputStream out = resp.getOutputStream();

            // Create a simple Bar chart
            System.out.println("Setting dataset values");

            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
            dataset.setValue(30, "Girls","SCIENCE CLASS");
            dataset.setValue(30,  "Boys","SCIENCE CLASS");
            dataset.setValue(10,  "Girls","ECONOMICS CLASS");
            dataset.setValue(50, "Boys","ECONOMICS CLASS");
            dataset.setValue(5, "Girls","LANGUAGE CLASS");
            dataset.setValue(55, "Boys","LANGUAGE CLASS");

            JFreeChart chart = ChartFactory.createBarChart3D(
                "Comparison between Girls and Boys in Science," + "Economics and Language classes",
                "Students Comparisons", "No of Students",
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false);

            chart.setBackgroundPaint(Color.white);

            // Set the background colour of the chart
            chart.getTitle().setPaint(Color.blue);

            // Adjust the colour of the title
            CategoryPlot plot = chart.getCategoryPlot();

            // Get the Plot object for a bar graph
            plot.setBackgroundPaint(Color.white);
            plot.setRangeGridlinePaint(Color.red);

            CategoryItemRenderer renderer = plot.getRenderer();
            renderer.setSeriesPaint(0, Color.red);
            renderer.setSeriesPaint(1, Color.green);
            renderer.setItemURLGenerator(
                new StandardCategoryURLGenerator(
                    "index1.html",
                    "series",
                    "section"));
            renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());

            resp.setContentType("image/png");

            ChartUtilities.writeChartAsPNG(out, chart, 625, 500);

        } catch (Exception e) {
            System.err.println("Problem occurred creating chart." + e.getMessage());
        }
    }

推荐答案

chart.setBackgroundPaint(Color.white);

            // Set the background colour of the chart
            chart.getTitle().setPaint(Color.blue);

            // Adjust the colour of the title
            CategoryPlot plot = chart.getCategoryPlot();

            // Get the Plot object for a bar graph
            plot.setBackgroundPaint(Color.white);
            plot.setRangeGridlinePaint(Color.red);

            CategoryItemRenderer renderer = plot.getRenderer();
            renderer.setSeriesPaint(0, Color.red);
            renderer.setSeriesPaint(1, Color.green);
            renderer.setItemURLGenerator(
                new StandardCategoryURLGenerator(
                    "index1.html",
                    "series",
                    "section"));
            renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());

            resp.setContentType("image/png");

            ChartUtilities.writeChartAsPNG(out, chart, 625, 500);

......代替上面的代码只是使用此代码,这更容易了……

......Instead of above code just use this code this is more eassy......

JFreeChart chart = ChartFactory.createBarChart3D(
                "Comparison between Girls and Boys in Science," + "Economics and Language classes",
                "Students Comparisons", "No of Students",
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false);


            try {

                final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
                final File file1 = new File(getServletContext().getRealPath(".") + "/images/piechart/lineChart.png");

                ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
            } catch (Exception e) {
                System.out.println(e);

            }  

......在jsp页面中使用此编码....

...............use this cod in jsp page.......

<div>
    <img src="path/images/piechart/lineChart.png"
         width="600" height="400" border="0" usemap="#chart" />
 </div>

这篇关于在JSP中使用Servlet的JFree图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆