CLICKPIE.jsp 1.79 KB
Newer Older
Thitichaipun Wutthisak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
<%@page import="ChartDirector.*" %>
<%
//Get the selected year and month
int selectedYear = Integer.parseInt(request.getParameter("year"));
int selectedMonth = Integer.parseInt(request.getParameter("x")) + 1;

//Get the monthly revenue
double monthlyRevenue = Double.parseDouble(request.getParameter("value"));

//
//  In this demo, we just split the total revenue into 3 parts using random
//  numbers. In real life, the data probably can come from a database.
//
java.util.Random rand = new java.util.Random(selectedMonth * 2000 + selectedYear
    );
double[] data = new double[3];
data[0] = (rand.nextDouble() * 0.1 + 0.5) * monthlyRevenue;
data[1] = (rand.nextDouble() * 0.1 + 0.2) * monthlyRevenue;
data[2] = monthlyRevenue - data[0] - data[1];

//The labels for the pie chart
String[] labels = {"Services", "Hardware", "Software"};

//Create a PieChart object of size 360 x 260 pixels
PieChart c = new PieChart(360, 260);

//Set the center of the pie at (180, 140) and the radius to 100 pixels
c.setPieSize(180, 130, 100);

//Add a title to the pie chart using 13 pts Times Bold Italic font
c.addTitle("Revenue Breakdown for " + selectedMonth + "/" + selectedYear,
    "timesbi.ttf", 13);

//Draw the pie in 3D
c.set3D();

//Set the pie data and the pie labels
c.setData(data, labels);

//Create the image and save it in a temporary location
request.getSession().setAttribute("chart1", c.makeChart2(Chart.PNG));

//Create an image map for the chart
String imageMap = c.getHTMLImageMap("PIESTUB.jsp", "",
    "title='{label}:USD {value|0}K'");
%>

<html>
<body>
<h1>Simple Clickable Pie Chart</h1>
<p><a href="VIEWSOURCE.jsp?file=<%=request.getServletPath()%>">
View Source Code
</a></p>

<img src="chart1.chart?no_cache=<%=Chart.getUniqueId()%>" border="0" usemap="#map1">
<map name="map1">
<%=imageMap%>
</map>
</body>
</html>