从控制器生成图表 [英] generate a chart from a controller

查看:85
本文介绍了从控制器生成图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring 3.0,在一个jsp中,我尝试显示数据和图表...

i use spring 3.0, in a jsp, i try to display data and a chart...

@Controller
@RequestMapping("/user.htm")
public class UserController {

@Autowired
private IUserService userService;

@RequestMapping(method = RequestMethod.GET)
public ModelAndView user(HttpServletRequest request,
    HttpServletResponse response) {

    ModelAndView modelAndView = new ModelAndView("user");
    modelAndView.addObject("statUser", userService.getStatUser());

    return modelAndView;
}

public void generateChart(HttpServletRequest request,
    HttpServletResponse response){
    try {
        AxisChart axisChart = userService.generateChart();
        ServletEncoderHelper.encodeJPEG13( axisChart, 1.0f, response );
    } catch (ChartDataException ex) {

    } catch (PropertyException ex) {

    } catch (IOException ex) {

    }

  }

}

在jsp中,我尝试显示图表

in the jsp i try to display the chart with

<img src="generateChart"/>

我可以看到信息...所以控制器的获取工作正常,但是图像从不显示

i can see the information... so the get of the controller work fine, but the image is never displayed

所以我不知道我是否可以使用相同的控制器,或者只需要创建一个新的控制器来创建图像...

so i don't know if i can use the same controller or need to create a new one only for creation of the image...

有什么主意吗?

推荐答案

我用Jfreechart做了一些类似的事情,最终创建了一个带有类似@RequestMapping("/chart.jpg")的映射的图表控制器,然后通过<img src="cart.jpg"/>链接到它.然后,必须在控制器中设置response.setHeader("Content-Type", "image/jpg");.

I've done something similer with Jfreechart, I ended up creating another chart controller with a mapping like @RequestMapping("/chart.jpg") and then link to it via <img src="cart.jpg"/>. In the controller then you have to set response.setHeader("Content-Type", "image/jpg");.

这是我的一个示例(它在groovy中,它是一个png,但应该有帮助)

Here a example of mine (it's in groovy and it's a png, but it should help)

@RequestMapping("/chart.png")
def chart(HttpServletRequest request, HttpServletResponse response){
    JFreeChart chart = ChartFactory.createTimeSeriesChart(...)
    //fill & layout the chart
    def pngChart = EncoderUtil.encode(chart.createBufferedImage(600, 400), "png")
    response.setContentType("image/png");
    response.setContentLength(pngChart.length);
    response.getOutputStream().write(pngChart);
    response.getOutputStream().close();
}

这篇关于从控制器生成图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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