jfreechart 를 기반 으로 곡선,기둥 모양 등 그림 을 생 성하 여 JSP 에 보 여 줍 니 다.
20968 단어 jfreechart생 성JSP
이 자 료 를 뒤 져 보면 예전 에 쓴 예 시 를 볼 때 jfreechart 에 관 한 간단 한 예 시 를 발 견 했 습 니 다.어쨌든 보 내 서 공유 하 세 요!
이 예제 에 서 는 JSP 와 Servlet 을 배경 과 프론트 데스크 톱 으로 보 여 줍 니 다.아래 에 소스 코드 가 있 으 면 직접 실행 할 수 있 습 니 다!
생산 라인 트 렌 드 맵:
package com.xidian.servlet;
import java.awt.Color;
import java.awt.Font;
import java.io.*;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
/**
*
* @
* @author cuisuqiang
* @version 1.0
* @since
*/
@SuppressWarnings("serial")
public class LineServlet extends HttpServlet {
@SuppressWarnings("deprecation")
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
// Mysql select
// year(accessdate),month(accessdate),day(accessdate),count(*)
// accessdate date
//
TimeSeriesCollection chartTime = new TimeSeriesCollection();
// , 1 , 2 ,
// count
TimeSeries timeSeries = new TimeSeries(" ", Day.class);
// ,
// Day day-month-year
timeSeries.add(new Day(1, 1, 2010), 50);
timeSeries.add(new Day(2, 1, 2010), 47);
timeSeries.add(new Day(3, 1, 2010), 82);
timeSeries.add(new Day(4, 1, 2010), 95);
timeSeries.add(new Day(5, 1, 2010), 104);
timeSeries.add(new Day(6, 1, 2010), 425);
chartTime.addSeries(timeSeries);
XYDataset date = chartTime;
try {
// ChartFactory
JFreeChart chart = ChartFactory.createTimeSeriesChart(
" ", //
" ", // X
" ", // Y
date, //
true, //
true, // Tooltips
false // URL
);
//
chart.setBackgroundPaint(Color.PINK);
//
chart.setBorderVisible(true);
//
XYPlot xyPlot = (XYPlot) chart.getPlot();
//
xyPlot.setBackgroundPaint(Color.lightGray);
//
xyPlot.setDomainGridlinePaint(Color.GREEN);
xyPlot.setRangeGridlinePaint(Color.GREEN);
// 、
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);
// (X,Y) render,
XYItemRenderer xyItemRenderer = xyPlot.getRenderer();
if (xyItemRenderer instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer xyLineAndShapeRenderer = (XYLineAndShapeRenderer) xyItemRenderer;
xyLineAndShapeRenderer.setShapesVisible(true); //
xyLineAndShapeRenderer.setShapesFilled(true); //
xyLineAndShapeRenderer.setSeriesFillPaint(0, Color.RED); //
xyLineAndShapeRenderer.setUseFillPaint(true);// render
}
//
//
Font xfont = new Font(" ", Font.PLAIN, 12); // X
Font yfont = new Font(" ", Font.PLAIN, 12); // Y
Font kfont = new Font(" ", Font.PLAIN, 12); //
Font titleFont = new Font(" ", Font.BOLD, 25); //
//
chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
//
chart.getLegend().setItemFont(kfont);
// X
ValueAxis domainAxis = xyPlot.getDomainAxis();
domainAxis.setLabelFont(xfont);//
domainAxis.setTickLabelFont(xfont);//
domainAxis.setTickLabelPaint(Color.BLUE); //
// Y
ValueAxis rangeAxis = xyPlot.getRangeAxis();
rangeAxis.setLabelFont(yfont);
rangeAxis.setLabelPaint(Color.BLUE); //
rangeAxis.setTickLabelFont(yfont);
//
DateAxis dateAxis = (DateAxis) xyPlot.getDomainAxis();
//
dateAxis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
//
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,
chart, 500, 300, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
생산 막대 그래프:
package com.xidian.servlet;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
/**
*
* @
* @author cuisuqiang
* @version 1.0
* @since
*/
@SuppressWarnings("serial")
public class PillarServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
//
DefaultCategoryDataset chartDate = new DefaultCategoryDataset();
// , , ,
chartDate.addValue(55, " ", "2010-01");
chartDate.addValue(65, " ", "2010-02");
chartDate.addValue(59, " ", "2010-03");
chartDate.addValue(156, " ", "2010-04");
chartDate.addValue(452, " ", "2010-05");
chartDate.addValue(359, " ", "2010-06");
try {
//
DefaultCategoryDataset data = chartDate;
// ChartFactory 3D , 3D, createBarChart
JFreeChart chart = ChartFactory.createBarChart3D(
" ", //
" ", //
" ", //
data, //
PlotOrientation.VERTICAL, // ,
// PlotOrientation.HORIZONTAL, // ,
true, //
true, //
false // URL
);
//
chart.setBackgroundPaint(Color.PINK);
//
chart.setBorderVisible(true);
Font kfont = new Font(" ", Font.PLAIN, 12); //
Font titleFont = new Font(" ", Font.BOLD, 25); //
//
chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
//
chart.getLegend().setItemFont(kfont);
//
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeCrosshairVisible(true);
categoryplot.setRangeCrosshairPaint(Color.blue);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
barrenderer.setBaseItemLabelFont(new Font(" ", Font.PLAIN, 12));
barrenderer.setSeriesItemLabelFont(1, new Font(" ", Font.PLAIN, 12));
CategoryAxis domainAxis = categoryplot.getDomainAxis();
/*------ X -----------*/
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
/*------ X ------------*/
domainAxis.setLabelFont(new Font(" ", Font.PLAIN, 12));
/*------ Y -----------*/
numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
/*------ Y ------------*/
numberaxis.setLabelFont(new Font(" ", Font.PLAIN, 12));
/*------ -----------*/
chart.getLegend().setItemFont(new Font(" ", Font.PLAIN, 12));
//
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,
chart, 500, 300, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
떡 모양 통계 도 생 성:
package com.xidian.servlet;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
/**
*
* @
* @author cuisuqiang
* @version 1.0
* @since
*/
@SuppressWarnings("serial")
public class CakeServlet extends HttpServlet {
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
//
DefaultPieDataset dataType = new DefaultPieDataset();
// ,
dataType.setValue("IE6", 156);
dataType.setValue("IE7", 230);
dataType.setValue("IE8", 45);
dataType.setValue(" ", 640);
dataType.setValue(" ", 245);
try {
DefaultPieDataset data = dataType;
// 3D
// 3D
PiePlot3D plot = new PiePlot3D(data);
JFreeChart chart = new JFreeChart(
" ", //
JFreeChart.DEFAULT_TITLE_FONT, //
plot, //
true //
);
//
chart.setBackgroundPaint(Color.PINK);
//
chart.setBorderVisible(true);
//
Font kfont = new Font(" ", Font.PLAIN, 12); //
Font titleFont = new Font(" ", Font.BOLD, 25); //
//
chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
//
chart.getLegend().setItemFont(kfont);
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,
chart, 500, 300, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
기둥 모양 분포 통계 도:
package com.xidian.servlet;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
/**
*
* @
* @author cuisuqiang
* @version 1.0
* @since
*/
@SuppressWarnings("serial")
public class ParagraphsServlet extends HttpServlet {
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
DefaultCategoryDataset dataTime = new DefaultCategoryDataset();
//
dataTime.addValue(52, "0-6", "2010-1-1");
dataTime.addValue(86, "6-12", "2010-1-1");
dataTime.addValue(126, "12-18", "2010-1-1");
dataTime.addValue(42, "18-24", "2010-1-1");
//
dataTime.addValue(452, "0-6", "2010-1-2");
dataTime.addValue(96, "6-12", "2010-1-2");
dataTime.addValue(254, "12-18", "2010-1-2");
dataTime.addValue(126, "18-24", "2010-1-2");
//
dataTime.addValue(256, "0-6", "2010-1-3");
dataTime.addValue(86, "6-12", "2010-1-3");
dataTime.addValue(365, "12-18", "2010-1-3");
dataTime.addValue(24, "18-24", "2010-1-3");
try {
DefaultCategoryDataset data = dataTime;
// ChartFactory 3D , 3D, createBarChart
JFreeChart chart = ChartFactory.createBarChart3D(
" ",
" ",
" ",
data,
PlotOrientation.VERTICAL,
true,
false,
false
);
//
chart.setBackgroundPaint(Color.PINK);
//
chart.setBorderVisible(true);
Font kfont = new Font(" ", Font.PLAIN, 12); //
Font titleFont = new Font(" ", Font.BOLD, 25); //
//
chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
//
chart.getLegend().setItemFont(kfont);
//
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeCrosshairVisible(true);
categoryplot.setRangeCrosshairPaint(Color.blue);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
barrenderer.setBaseItemLabelFont(new Font(" ", Font.PLAIN, 12));
barrenderer.setSeriesItemLabelFont(1, new Font(" ", Font.PLAIN, 12));
CategoryAxis domainAxis = categoryplot.getDomainAxis();
/*------ X -----------*/
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
/*------ X ------------*/
domainAxis.setLabelFont(new Font(" ", Font.PLAIN, 12));
/*------ Y -----------*/
numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
/*------ Y ------------*/
numberaxis.setLabelFont(new Font(" ", Font.PLAIN, 12));
/*------ -----------*/
chart.getLegend().setItemFont(new Font(" ", Font.PLAIN, 12));
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,
chart, 500, 300, null);
} catch (Exception es) {
es.printStackTrace();
}
}
}
JSP 에서 IMG 그림 을 사용 하여 Servlet 에 그림 을 보 여 달라 고 요청 합 니 다:
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" >
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<center>
<img src="LineServlet">
<img src="PillarServlet">
<img src="ParagraphsServlet">
<img src="CakeServlet">
</center>
</body>
</html>
WEB.xml 설정:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LineServlet</servlet-name>
<servlet-class>com.xidian.servlet.LineServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>PillarServlet</servlet-name>
<servlet-class>com.xidian.servlet.PillarServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ParagraphsServlet</servlet-name>
<servlet-class>
com.xidian.servlet.ParagraphsServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>CakeServlet</servlet-name>
<servlet-class>com.xidian.servlet.CakeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LineServlet</servlet-name>
<url-pattern>/LineServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PillarServlet</servlet-name>
<url-pattern>/PillarServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ParagraphsServlet</servlet-name>
<url-pattern>/ParagraphsServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CakeServlet</servlet-name>
<url-pattern>/CakeServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
난 장 판 문 제 를 해결 하기 위해 필 터 를 설 정 했 습 니 다.
package org.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
*
* @
* @author cuisuqiang
* @version 1.0
* @since
*/
public class EncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JFree Chart 중국어 난자 원스톱 해결 방법 하나.텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.