Add pie chart to your Jsp application with JspBar3dChart

Home arrow Java Tutorials arrow Developer Tools arrow Add pie chart to your Jsp application with JspBar3dChart
Add pie chart to your Jsp application with JspBar3dChart Print E-mail
Contributed by Teddy   
Friday, 29 September 2006
JspBar3dChart is a powerful and flexible open source Java web charting library. JspBar3dChart allows you to easily incorporate advanced charting capabilities into Java web applications. This article introduces you to the JspBar3dChart library and explains how to create pie charts with it.

The following examples are based on JspBar3dChart version 1.0. To compile and run the code included with this column, you must have chart.jar in your classpath.

PieDataSerie

To create a pie chart using JspBar3dChart, you must create a PieDataSerie. A PieDataSerie contains the data that displays in the chart.

Constructor Summary of PieDataSerie

PieDataSerie(double[] dates, FillStyle[] fstyles)

A pie chart is created from a PiePlotter. The following example creates a chart using the Chart class and add a pie chart with the Chart constructor. Then adds value via the addSerie() method,

Constructor Summary of Chart

 Chart(Title t, Plotter p, Axis X, Axis Y)

Set the chart size using  setSize() method

// size of the chart image    
int w=680;
int h=450;
chart.setSize(w,h);

Create a new Legend by Legend class,and add it to chart by  chart.legend=l

Legend l=new Legend();
chart.legend=l;

Set chart file tyoe by saveToFile();

chart.saveToFile(outb,"jpeg"); 

Full source code of example


<%@ page import = "net.comrom.chart.*" %>
<%@ page import = "net.comrom.chart.gc.*" %>
<%
String encode="jpeg";
//String uri=request.getRequestURI();
//String myurl="http://"+request.getHeader("Host")+uri.substring(0,uri.length()-12); //12 ist the length of my page name
response.setContentType("image/jpeg");
response.setDateHeader ("Expires",0);
// get output stream
java.io.OutputStream outb=response.getOutputStream();
 // size of the chart image    
int w=680;
int h=450;
double[] d1={45.14,21.25,12.52,17.20,3.89};
// style of the pie
FillStyle[] s1={new FillStyle(GraphicsProvider.getColor("0xF9C2FC")),new FillStyle(GraphicsProvider.getColor("0x5EFE39")),new FillStyle(GraphicsProvider.getColor("0xFAFE84")),new FillStyle(GraphicsProvider.getColor("0xFCB456")),new FillStyle(GraphicsProvider.getColor("0x6CBDFD"))};
PieDataSerie data1= new PieDataSerie(d1,s1);
data1.textDistanceToCenter=1.2;
data1.valueFont=GraphicsProvider.getFont("Arial",ChartFont.BOLD,12);
// legend
Legend l=new Legend();
l.background=new FillStyle(GraphicsProvider.getColor(ChartColor.WHITE));
l.border=new LineStyle(1,GraphicsProvider.getColor(ChartColor.BLACK),LineStyle.LINE_NORMAL);
l.addItem("productA",new FillStyle(GraphicsProvider.getColor("0xF9C2FC")));
l.addItem("productB",new FillStyle(GraphicsProvider.getColor("0x5EFE39")));
l.addItem("productC",new FillStyle(GraphicsProvider.getColor("0xFAFE84")));
l.addItem("productD",new FillStyle(GraphicsProvider.getColor("0xFCB456")));
l.addItem("other",new FillStyle(GraphicsProvider.getColor("0x6CBDFD")));
// create title
Title title=new Title("profit structure 2001");
title.font=GraphicsProvider.getFont("Arial",ChartFont.BOLD,16);
// plotter
PiePlotter plot=new PiePlotter();
plot.radiusModifier=0.6;
plot.labelLine=new LineStyle(1,GraphicsProvider.getColor("0x2B2B2B"),LineStyle.LINE_NORMAL);
// create chart
Chart chart=new Chart(title,plot,null,null);
// chart background
chart.back=new FillStyle(GraphicsProvider.getColor(ChartColor.WHITE));
//chart.back.gradientType=FillStyle.GRADIENT_VERTICAL;         
// add legend
chart.legend=l;
// add data
chart.addSerie(data1);
chart.doubleBuffering=false;
//chart.backImage=GraphicsProvider.getImage("back16.gif");;
chart.setSize(w,h);
chart.saveToFile(outb,"jpeg");  
outb.close();
%>

Last Updated ( Friday, 29 September 2006 )

  home              contact us

 

©2006-2010 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground