JMSL Chart Programmer’s Guide
Generating a Chart with a JSP
This example consists of a JSP working with a bean (pieChart) that contains a JspBean. The HTML image tag is the value of the imageTag property from pieChart. The following is the JSP.
View code file
 
<%@page contentType="text/html"%>
<jsp:useBean id="pieChart" scope="page" class="com.imsl.demo.jsp.SampleChartBean"/>
<html>
<head><title>Pie Chart</title></head>
<body>
<h1>Pie Chart</h1>
<jsp:setProperty name="pieChart" property="*"/>
<% pieChart.createChart(request); %>
<jsp:getProperty name="pieChart" property="imageTag"/>
</body>
</html>
In the above JSP, line 1 flags that an HTML page is being generated. Line 2 creates the object pieChart, which is an instance of SampleChartBean. This object has page scope, so it is discarded after the page has been generated. Line 8 sets attributes in pieChart using parameters in the request. Attributes are set in pieChart using its setter methods, following the usual pattern for Java beans. Line 9 calls the createChart method to create the chart tree and store it in the session. This is Java code enclosed in the special delimiters. Line 10 inserts the HTML image tag obtained by calling pieChart.getImageTag().
The class SampleChartBean uses a JspBean. It creates a pie chart and registers it with the session obtained from the request. The getImageTag() method returns an HTML tag which refers to the ChartServlet servlet that returns the chart image to the browser.
View code file
 
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
 
package com.imsl.demo.jsp;
 
import com.imsl.chart.*;
import java.awt.Color;
 
public class SampleChartBean {
 
private JspBean bean;
 
public SampleChartBean() {
bean = new JspBean();
bean.setSize(300, 300);
}
 
public void createChart(javax.servlet.http.HttpServletRequest request) {
Chart chart = new Chart();
bean.registerChart(chart, request);
 
double y[] = {35., 20., 30., 40.};
Pie pie = new Pie(chart, y);
pie.setLabelType(Pie.LABEL_TYPE_TITLE);
pie.setFillOutlineColor(Color.blue);
 
PieSlice[] slice = pie.getPieSlice();
 
slice[0].setFillColor(Color.red);
slice[0].setTitle("Red");
 
slice[1].setFillColor(Color.blue);
slice[1].setTitle("Blue");
slice[1].setFillOutlineColor(Color.yellow);
 
slice[2].setFillColor(Color.black);
slice[2].setTitle("Black");
slice[2].setExplode(0.3);
 
slice[3].setFillColor(Color.yellow);
slice[3].setTitle("Yellow");
}
 
public String getImageTag() {
return bean.getImageTag();
}
}