【发布时间】:2017-01-17 06:01:47
【问题描述】:
Thymeleaf 和 JFreechart 的新手。我必须在一个 html 页面上显示多个条形图。通过研究如何完成这项工作,我发现了一个使用 Thymeleaf 的示例,如下所示:
JFreeChart chart = createChart(pdSet, "Test Pie Chart using JFreeChart");
try{
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 750, 400);
response.getOutputStream().close();
} catch(IOException ex) {
ex.printStackTrace();
}
我需要能够将渲染的图表传递给模型,以便能够作为图像嵌入到 html 中。我试图将其作为字节数组传递,但没有成功。 控制器代码:
JFreeChart chart = ChartFactory.createBarChart(null, null, null, bardataset, PlotOrientation.VERTICAL, false, false, false);
File file = new File("barchart.jpg");
try {
ChartUtilities.saveChartAsJPEG(new File(filename), chart, 800, 100);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (clientList != null){
model.addAttribute("chart", chart);
}
chart.html:
<div>
<p> Bar Graph</p>
<div>
<img th:src="@{~/chart}" width="1000"/>
</div>
</div>
</div>
上面的实现来自我遇到的一个例子...... 使用 Spring-boot 1.4.3 运行它,并在 Java jdk1.8.0_111 上运行 MVC、Thymeleaf、Security 和 JPA 的相关依赖项。
任何帮助或指导将不胜感激。
【问题讨论】:
标签: java spring-boot thymeleaf jfreechart