본문 바로가기

JFreeChart/JFreeChart in JSP

JSP 에서 JFreeChart 사용하기


예제 구성파일

package chart;
import org.jfree.chart.*;
import org.jfree.data.category.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.*;
import org.jfree.data.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.plot.*;
import java.awt.*;

public class BarChartBean {

 public static void main(String arg[]){
  BarChartBean bcb = new BarChartBean();
  JFreeChart chart = bcb.getBarChart();
  ChartFrame frame1=new ChartFrame("Bar Chart",chart);
  frame1.setSize(400,350); 
  frame1.setVisible(true);
 }

 public JFreeChart getBarChart() {
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.setValue(2, "Marks", "Rahul");
  dataset.setValue(7, "Marks", "Vinod");
  dataset.setValue(4, "Marks", "Deepak");
  dataset.setValue(9, "Marks", "Prashant");
  dataset.setValue(6, "Marks", "Chandan");
  JFreeChart chart = ChartFactory.createBarChart (
   "BarChart using JFreeChart","Student", "Marks", dataset,
   PlotOrientation.VERTICAL, false,true, false);
  chart.setBackgroundPaint(Color.yellow);
  chart.getTitle().setPaint(Color.blue);
  CategoryPlot p = chart.getCategoryPlot();
  p.setRangeGridlinePaint(Color.red);
  return chart;
 }
}


<%@ page contentType="image/jpeg"%>
<%@ page import = "org.jfree.chart.*" %>
<%@ page import ="chart.*"%>
<%
 ServletOutputStream sos = response.getOutputStream();
 BarChartBean bcb = new BarChartBean();
 JFreeChart chart = bcb.getBarChart();
 ChartUtilities.writeChartAsPNG(sos, chart, 400, 400);
%>


<%@ page contentType="text/html;charset=KSC5601"%>
<html>
<head><title>JFreeChart with JSP example</title></head>
<body>
<center>
JFreeChart를 JSP 에서 사용하는 예제
<hr width="50%">
<img src="chart.jsp">
</center>
</body>
</html>