Although now JS It is very common and beautiful to make reports and graphic displays , But we can't ignore jfreechart Such a thing !
These browsing materials , When looking at the examples written before, I found something about jfreechart Simple example , Anyway, send it to share !
This example uses JSP and Servlet Do backstage and front desk display , The following source code can be run directly !
Production line trend chart :
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;/** * Production line trend chart * @ explain * @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"); // stay Mysql Use in select // year(accessdate),month(accessdate),day(accessdate),count(*) // among accessdate It's a date Type of time // A collection of time series objects TimeSeriesCollection chartTime = new TimeSeriesCollection(); // Time series objects , The first 1 Parameters represent the name of the time series , The first 2 The first parameter is the time type , Here is heaven // This object is used before saving count Daily visits per day TimeSeries timeSeries = new TimeSeries(" A visit to ", Day.class); // To demonstrate , Directly assemble data // Day The assembly format of is day-month-year Number of visits 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 { // Use ChartFactory To create chart objects for time series JFreeChart chart = ChartFactory.createTimeSeriesChart( " Website visit statistics every day ", // Graphic title " date ", // X Axis description " Traffic volume ", // Y Axis description date, // data true, // Create Legend true, // Whether to generate Tooltips false // Whether to produce URL link ); // Set the background color of the whole picture chart.setBackgroundPaint(Color.PINK); // Set the picture to have a border chart.setBorderVisible(true); // Get the chart area object XYPlot xyPlot = (XYPlot) chart.getPlot(); // Set the background color of the report area xyPlot.setBackgroundPaint(Color.lightGray); // Set horizontal Ordinate grid color xyPlot.setDomainGridlinePaint(Color.GREEN); xyPlot.setRangeGridlinePaint(Color.GREEN); // Set horizontal 、 Whether the vertical coordinate intersection line is displayed xyPlot.setDomainCrosshairVisible(true); xyPlot.setRangeCrosshairVisible(true); // Get data points (X,Y) Of render, Be responsible for mapping data points XYItemRenderer xyItemRenderer = xyPlot.getRenderer(); if (xyItemRenderer instanceof XYLineAndShapeRenderer) { XYLineAndShapeRenderer xyLineAndShapeRenderer = (XYLineAndShapeRenderer) xyItemRenderer; xyLineAndShapeRenderer.setShapesVisible(true); // Data points are visible xyLineAndShapeRenderer.setShapesFilled(true); // Data points are solid points xyLineAndShapeRenderer.setSeriesFillPaint(0, Color.RED); // Data points are filled in blue xyLineAndShapeRenderer.setUseFillPaint(true);// Apply the set attributes to render On } // The following contents can be configured to solve the problem of garbled code // Configure Fonts Font xfont = new Font(" Song style ", Font.PLAIN, 12); // X Axis Font yfont = new Font(" Song style ", Font.PLAIN, 12); // Y Axis Font kfont = new Font(" Song style ", Font.PLAIN, 12); // Bottom Font titleFont = new Font(" Song style ", Font.BOLD, 25); // Picture title // Picture title chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont)); // Bottom chart.getLegend().setItemFont(kfont); // X Axis ValueAxis domainAxis = xyPlot.getDomainAxis(); domainAxis.setLabelFont(xfont);// Axis title domainAxis.setTickLabelFont(xfont);// Axis numerical domainAxis.setTickLabelPaint(Color.BLUE); // The font color // Y Axis ValueAxis rangeAxis = xyPlot.getRangeAxis(); rangeAxis.setLabelFont(yfont); rangeAxis.setLabelPaint(Color.BLUE); // The font color rangeAxis.setTickLabelFont(yfont); // Define the format of date display on the coordinate axis DateAxis dateAxis = (DateAxis) xyPlot.getDomainAxis(); // Set the date format dateAxis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd")); // Output the generated image to the client ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f, chart, 500, 300, null); } catch (Exception e) { e.printStackTrace(); } }}
Production histogram :
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;/** * Production histogram * @ explain * @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"); // Use normal data sets DefaultCategoryDataset chartDate = new DefaultCategoryDataset(); // Add test data , The first parameter is the number of visits , The last one is time , In the middle is the display, regardless chartDate.addValue(55, " Traffic volume ", "2010-01"); chartDate.addValue(65, " Traffic volume ", "2010-02"); chartDate.addValue(59, " Traffic volume ", "2010-03"); chartDate.addValue(156, " Traffic volume ", "2010-04"); chartDate.addValue(452, " Traffic volume ", "2010-05"); chartDate.addValue(359, " Traffic volume ", "2010-06"); try { // Get the data set from the database DefaultCategoryDataset data = chartDate; // Use ChartFactory establish 3D Histogram , Do not want to use 3D, Use it directly createBarChart JFreeChart chart = ChartFactory.createBarChart3D( " Monthly website traffic statistics ", // Chart title " Time ", // The display label of the directory axis " Traffic volume ", // The display label of the value axis data, // Data sets PlotOrientation.VERTICAL, // Chart direction , Here is the vertical direction // PlotOrientation.HORIZONTAL, // Chart direction , Here is the horizontal direction true, // Show legend or not true, // Whether to generate tools false // Whether to generate URL link ); // Set the background color of the whole picture chart.setBackgroundPaint(Color.PINK); // Set the picture to have a border chart.setBorderVisible(true); Font kfont = new Font(" Song style ", Font.PLAIN, 12); // Bottom Font titleFont = new Font(" Song style ", Font.BOLD, 25); // Picture title // Picture title chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont)); // Bottom chart.getLegend().setItemFont(kfont); // Get the coordinates, set the font, and solve the garbled code 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(" Song style ", Font.PLAIN, 12)); barrenderer.setSeriesItemLabelFont(1, new Font(" Song style ", Font.PLAIN, 12)); CategoryAxis domainAxis = categoryplot.getDomainAxis(); /*------ Set up X Text on axis coordinates -----------*/ domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); /*------ Set up X The title text of the axis ------------*/ domainAxis.setLabelFont(new Font(" Song style ", Font.PLAIN, 12)); /*------ Set up Y Text on axis coordinates -----------*/ numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); /*------ Set up Y The title text of the axis ------------*/ numberaxis.setLabelFont(new Font(" Song style ", Font.PLAIN, 12)); /*------ This code solves the problem of garbled Chinese characters at the bottom -----------*/ chart.getLegend().setItemFont(new Font(" Song style ", Font.PLAIN, 12)); // Generate pictures and output ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f, chart, 500, 300, null); } catch (Exception e) { e.printStackTrace(); } }}
Generate a pie chart :
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;/** * Generate a pie chart * @ explain * @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"); // Default data type DefaultPieDataset dataType = new DefaultPieDataset(); // Data parameters Content , Number dataType.setValue("IE6", 156); dataType.setValue("IE7", 230); dataType.setValue("IE8", 45); dataType.setValue(" firefox ", 640); dataType.setValue(" Google ", 245); try { DefaultPieDataset data = dataType; // Generate an ordinary pie chart and get rid of 3D that will do // production 3D The pie chart PiePlot3D plot = new PiePlot3D(data); JFreeChart chart = new JFreeChart( " The type of browser the user uses ", // Graphic title JFreeChart.DEFAULT_TITLE_FONT, // Title font plot, // Icon Title Object true // Show legend or not ); // Set the background color of the whole picture chart.setBackgroundPaint(Color.PINK); // Set the picture to have a border chart.setBorderVisible(true); // Configure Fonts Font kfont = new Font(" Song style ", Font.PLAIN, 12); // Bottom Font titleFont = new Font(" Song style ", Font.BOLD, 25); // Picture title // Picture title chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont)); // Bottom chart.getLegend().setItemFont(kfont); ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f, chart, 500, 300, null); } catch (Exception e) { e.printStackTrace(); } }}
Because the space is limited , Please continue to read the next article .
I recommend you to read more about “ jsp servlet jfreechart java report form Histogram The pie chart ” The article