Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic Chart In Jsp Part-2
12-13-2011, 03:36 AM
Post: #1
Thumbs Up Dynamic Chart In Jsp Part-2
Today you are going to know how to create dynamic chart from an jsp page.
First create the database and tables like below if you are using mysql otherwise use your own database you need to change only connection drivers only


mysql>create database s2sgateway;
mysql>use s2sgateway;
mysql> create table chart(name varchar(20),LeavePercentage integer);
insert into chart values('amar',10),('jani',20);
insert into chart values('safiq',10),('s2s',20);


Now write the below code in the jsp page after executing the code chart is saved in the location given by you for the saveChartAsJPEG location
ChartUtilities.saveChartAsJPEG(new File("c:\\s2s worksspace\\s2sgateway\\WebContent\\chart.jpg"), chart, 500, 450);
then use img src to define the exact location to show the chart in jsp page copy the code below

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.jfree.chart.ChartFactory" %>
<%@ page import="org.jfree.chart.ChartUtilities"%>
<%@ page import="org.jfree.chart.JFreeChart" %>
<%@ page import="org.jfree.chart.plot.PlotOrientation"%>
<%@ page import="org.jfree.data.*" %>
<%@ page import="org.jfree.data.jdbc.JDBCCategoryDataset"%>
<%@ page import="org.jfree.chart.renderer.category.CategoryItemRenderer"%>
<%@ page import="org.jfree.chart.plot.CategoryPlot"%>
<%@ page import="org.jfree.chart.plot.PlotOrientation"%>
<%@ page import="java.awt.Color"%>
<%
String query="SELECT * from chart";
JDBCCategoryDataset dataset=new JDBCCategoryDataset("jdbc:mysql://localhost:3306/s2sgateway",
"com.mysql.jdbc.Driver","root","root");

dataset.executeQuery( query);
JFreeChart chart = ChartFactory .createBarChart3D(
"Chart Creation",
"Student Names",
"Days Present",
dataset,
PlotOrientation.VERTICAL,true, true, false);

CategoryPlot plot = chart.getCategoryPlot();
CategoryItemRenderer renderer = plot.getRenderer();

renderer.setSeriesPaint(0, Color.green);
try
{
ChartUtilities.saveChartAsJPEG(new File("c:\\s2s worksspace\\s2sgateway\\WebContent\\chart.jpg"), chart, 500, 450);
}
catch (IOException e)
{
System.out.println("Problem in creating chart.");
}
%>
<img src="c:\\s2s worksspace\\s2sgateway\\WebContent\\chart.jpg" width=500 height=450>


You can change your own color at
renderer.setSeriesPaint(0, Color.green); and change the
database table and values to your own Any doubt ask us.

Happy Coding
Find all posts by this user
Add Thank You Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies Views: Last Post
Tongue How to save the Dynamic Piechart in jsp rajasri 0 217 12-14-2011 05:18 PM
Last Post: rajasri
Its My Birthday! Dynamic Chart In Jsp/Create chart in jsp Part-1 rajasri 0 279 12-13-2011 03:40 AM
Last Post: rajasri
Tongue Dynamic PieChart in Jsp rajasri 0 363 12-13-2011 01:49 AM
Last Post: rajasri

Forum Jump:


User(s) browsing this thread: 1 Guest(s)


WE will share important jobs and updates on facebook so like us to get updates