Java statements JFreeChart

1 shows an example to create a vertical cylindrical
first introduced into the appropriate package
Here Insert Picture Description
under the package is copied to the item under the folder lib

Here Insert Picture Description
web.xml added:

  <servlet>
		<servlet-name>DisplayChart</servlet-name>
		<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>DisplayChart</servlet-name>
		<url-pattern>/DisplayChart</url-pattern>
	</servlet-mapping>

BarChart1 categories:

public static String getBarChart(HttpSession session)throws Exception{
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(510,"深圳" , "苹果");
		dataset.addValue(320,"深圳" , "香蕉");
		dataset.addValue(580,"深圳" , "橘子");
		dataset.addValue(390,"深圳" , "梨子");
		JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset, 
				PlotOrientation.VERTICAL, true, true, true);
		String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, null,session);
		return fileName;
	}

jsp code:

<%
	String fileName = BarChart1.getBarChart(session);
	System.out.println(fileName);
%>
<img src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0">

Access:
Here Insert Picture Description
get:
Here Insert Picture Description
1.1 JFreeChart created using the fruit sales reports sorted by color

<%
	String fileName = BarChart3.getBarChart(session);
	System.out.println(fileName);
%>
<img src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0">
	public static String getBarChart(HttpSession session)throws Exception{
		double [][]data=new double[][] {{1320},{720},{830},{400}};
		String []rowKeys= {"苹果","香蕉","橘子","梨子"};
		String []columnKeys= {"深圳"};
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
		JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset, 
				PlotOrientation.VERTICAL, true, true, true);
		String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, null,session);
		return fileName;
	}

Here Insert Picture Description
1.2 use JFreeChart created by color and fruit sales report by region

public static String getBarChart(HttpSession session)throws Exception{
		//里面的一个括号内的代表的是不同地区的同种水果
		double [][]data=new double[][] {{1320,956,241,567},{720,128,576,483},{830,257,496,578},{400,129,1110,1230}};
		String []rowKeys= {"苹果","香蕉","橘子","梨子"};
		String []columnKeys= {"深圳","北京","上海","南京"};
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
		JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset, 
				PlotOrientation.VERTICAL, true, true, true);
		String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, null,session);
		return fileName;
	}

Here Insert Picture Description
1.3 Custom 3D columnar reports created with JFreeChart

	public static String getBarChart(HttpSession session)throws Exception{
		//里面的一个括号内的代表的是不同地区的同种水果
		double [][]data=new double[][] {{1320,956,241,567},{720,128,576,483},{830,257,496,578},{400,129,1110,1230}};
		String []rowKeys= {"苹果","香蕉","橘子","梨子"};
		String []columnKeys= {"深圳","北京","上海","南京"};
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
		JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset, 
				PlotOrientation.VERTICAL, true, true, true);
		
		CategoryPlot plot = chart.getCategoryPlot();
		// 设置网格背景颜色
		plot.setBackgroundPaint(Color.white);
		// 设置网格竖线颜色
		plot.setDomainGridlinePaint(Color.pink);
		// 设置网格横线颜色
		plot.setRangeGridlinePaint(Color.pink);
		
		// 显示每个柱的数值,并修改该数值的字体属性
		BarRenderer3D renderer=new BarRenderer3D();
		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setBaseItemLabelsVisible(true);
		
		renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
		renderer.setItemLabelAnchorOffset(10D);  
		
		// 设置平行柱的之间距离
		renderer.setItemMargin(0.4);
		
		plot.setRenderer(renderer);
		
		String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, null,session);
		return fileName;
	}

Here Insert Picture Description

sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
2. Create a common use JFreeChart pie chart report

public static String getPieChart(HttpSession session)throws Exception{
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("种类1", 1000);
		dataset.setValue("种类2", 800);
		dataset.setValue("种类3", 400);
		dataset.setValue("种类4", 100);
		dataset.setValue("种类5", 29);
		
		JFreeChart chart = ChartFactory.createPieChart("分布图", dataset, true, true, true);
			
		String filename = ServletUtilities.saveChartAsPNG(chart, 700, 500, session);
		return filename;
	}

Here Insert Picture Description
2.1 ordinary use JFreeChart to create a pie chart report, and highlights a

	public static String getPieChart(HttpSession session)throws Exception{
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("种类1", 1000);
		dataset.setValue("种类2", 800);
		dataset.setValue("种类3", 400);
		dataset.setValue("种类4", 100);
		dataset.setValue("种类5", 29);
		
		JFreeChart chart = ChartFactory.createPieChart("分布图", dataset, true, true, true);
		
		// 副标题
		chart.addSubtitle(new TextTitle("2019年度"));
		
		PiePlot pieplot=(PiePlot)chart.getPlot();
		pieplot.setLabelFont(new Font("宋体",0,11));
		// 设置饼图是圆的(true),还是椭圆的(false);默认为true  
		pieplot.setCircular(true);
		// 没有数据的时候显示的内容
		pieplot.setNoDataMessage("无数据显示");
		StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1}.{2})", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance());  
		pieplot.setLabelGenerator(standarPieIG);  
		
		String filename = ServletUtilities.saveChartAsPNG(chart, 700, 500, session);
		return filename;
	}

Here Insert Picture Description
Highlight 2.2
plus:

pieplot.setExplodePercent("种类1",0.23); 

Here Insert Picture Description
2.3 use JFreeChart to create a 3D pie chart report

public static String getPieChart(HttpSession session)throws Exception{
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("种类1", 1000);
		dataset.setValue("种类2", 800);
		dataset.setValue("种类3", 400);
		dataset.setValue("种类4", 100);
		dataset.setValue("种类5", 29);
		//主要在这里方法换成3D
		JFreeChart chart = ChartFactory.createPieChart3D("分布图", dataset, true, true, true);
		
		// 副标题
		chart.addSubtitle(new TextTitle("2019年度"));
		
		PiePlot pieplot=(PiePlot)chart.getPlot();
		pieplot.setLabelFont(new Font("宋体",0,11));
		// 设置饼图是圆的(true),还是椭圆的(false);默认为true  
		pieplot.setCircular(true);
		// 没有数据的时候显示的内容
		pieplot.setNoDataMessage("无数据显示");
		StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1}.{2})", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance());  
		pieplot.setLabelGenerator(standarPieIG); 
		
		String filename = ServletUtilities.saveChartAsPNG(chart, 700, 500, session);
		return filename;
	}

Here Insert Picture Description
2.3.1 Creating a 3D pie chart report, set the style

		PiePlot3D pieplot3d = (PiePlot3D)chart.getPlot(); 
		
		//设置开始角度  
		pieplot3d.setStartAngle(120D);  
		//设置方向为”顺时针方向“  
		pieplot3d.setDirection(Rotation.CLOCKWISE);  
		//设置透明度,0.5F为半透明,1为不透明,0为全透明  
		pieplot3d.setForegroundAlpha(0.7F);

Here Insert Picture Description
3. using JFreeChart to create a line chart report

public static String genLineChart(HttpSession session)throws Exception{
		
		//访问量统计
		TimeSeries timeSeries = new TimeSeries("某网站访问量统计",Month.class);
		//添加数据
		timeSeries.add(new Month(1, 2019), 100);
		timeSeries.add(new Month(2, 2019), 200);
		timeSeries.add(new Month(3, 2019), 300);
		timeSeries.add(new Month(4, 2019), 400);
		timeSeries.add(new Month(5, 2019), 500);
		timeSeries.add(new Month(6, 2019), 450);
		timeSeries.add(new Month(7, 2019), 700);
		timeSeries.add(new Month(8, 2019), 800);
		timeSeries.add(new Month(9, 2019), 120);
		timeSeries.add(new Month(10, 2019), 1000);
		timeSeries.add(new Month(11, 2019), 900);
		timeSeries.add(new Month(12, 2019), 1100);
		
		//定义时间序列的集合
		TimeSeriesCollection lineDataset = new TimeSeriesCollection();
		lineDataset.addSeries(timeSeries);
		
		JFreeChart chart = ChartFactory.createTimeSeriesChart("访问量统计时间折线图", "月份", "访问量", lineDataset, true, true, true);
		String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, session);
		
		return fileName;
	}

Here Insert Picture Description
Setting Timeline range
plus:

//设置时间轴的范围。
		XYPlot plot = (XYPlot) chart.getPlot(); 
		DateAxis dateaxis = (DateAxis)plot.getDomainAxis();
		dateaxis.setDateFormatOverride(new java.text.SimpleDateFormat("M月"));
		dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH,1)); 

Here Insert Picture Description
Set whether data point curve

//设置曲线是否显示数据点
		XYLineAndShapeRenderer xylinerenderer = (XYLineAndShapeRenderer)plot.getRenderer();
		xylinerenderer.setBaseShapesVisible(true); 

Here Insert Picture Description
/ Curve display setting value of each data point

//设置曲线显示各数据点的值
		XYItemRenderer xyitem = plot.getRenderer(); 
		xyitem.setBaseItemLabelsVisible(true);
		xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); 
		xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
		xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 12)); 
		plot.setRenderer(xyitem);

Here Insert Picture Description
Contrast polyline

public static String genLineChart(HttpSession session)throws Exception{
		
		//访问量统计
		TimeSeries timeSeries = new TimeSeries("A网站访问量统计",Month.class);
		//添加数据
		timeSeries.add(new Month(1, 2019), 100);
		timeSeries.add(new Month(2, 2019), 200);
		timeSeries.add(new Month(3, 2019), 300);
		timeSeries.add(new Month(4, 2019), 400);
		timeSeries.add(new Month(5, 2019), 500);
		timeSeries.add(new Month(6, 2019), 450);
		timeSeries.add(new Month(7, 2019), 700);
		timeSeries.add(new Month(8, 2019), 800);
		timeSeries.add(new Month(9, 2019), 120);
		timeSeries.add(new Month(10, 2019), 1000);
		timeSeries.add(new Month(11, 2019), 900);
		timeSeries.add(new Month(12, 2019), 1100);
		
		//访问量统计
		TimeSeries timeSeries1 = new TimeSeries("B网站访问量统计",Month.class);
		//添加数据
		timeSeries1.add(new Month(1, 2019), 130);
		timeSeries1.add(new Month(2, 2019), 150);
		timeSeries1.add(new Month(3, 2019), 40);
		timeSeries1.add(new Month(4, 2019), 350);
		timeSeries1.add(new Month(5, 2019), 500);
		timeSeries1.add(new Month(6, 2019), 450);
		timeSeries1.add(new Month(7, 2019), 600);
		timeSeries1.add(new Month(8, 2019), 800);
		timeSeries1.add(new Month(9, 2019), 120);
		timeSeries1.add(new Month(10, 2019), 900);
		timeSeries1.add(new Month(11, 2019), 1000);
		timeSeries1.add(new Month(12, 2019), 750);
		
		//定义时间序列的集合
		TimeSeriesCollection lineDataset = new TimeSeriesCollection();
		lineDataset.addSeries(timeSeries);
		lineDataset.addSeries(timeSeries1);
		
		JFreeChart chart = ChartFactory.createTimeSeriesChart("访问量统计时间折线图", "月份", "访问量", lineDataset, true, true, true);
		
		
		//设置时间轴的范围。
		XYPlot plot = (XYPlot) chart.getPlot(); 
		DateAxis dateaxis = (DateAxis)plot.getDomainAxis();
		dateaxis.setDateFormatOverride(new java.text.SimpleDateFormat("M月"));
		dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH,1)); 
		
		//设置曲线是否显示数据点
		XYLineAndShapeRenderer xylinerenderer = (XYLineAndShapeRenderer)plot.getRenderer();
		xylinerenderer.setBaseShapesVisible(true); 
		
		//设置曲线显示各数据点的值
		XYItemRenderer xyitem = plot.getRenderer(); 
		xyitem.setBaseItemLabelsVisible(true);
		xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); 
		xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
		xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 12)); 
		plot.setRenderer(xyitem);
		
		String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, session);
		
		return fileName;
	}

Here Insert Picture Description
JFreeChart and Struts2 integration
官方API:https://struts.apache.org/plugins/jfreechart/

web.xml configuration:

<filter>
       <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
	      <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
	      <url-pattern>/*</url-pattern>
  </filter-mapping>

struts.xml

<package name="jfeechart" extends="jfreechart-default">
		<action name="barChart" class="com.java1234.struts2JFreeChart.BarCharAction">
			<result name="success" type="chart">
				<param name="value">chart</param>
				<param name="type">png</param>
			    <param name="width">700</param>
			    <param name="height">500</param> 
	  		</result>
		</action>		
	</package>

action

public class BarCharAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	private JFreeChart chart;
	
	public JFreeChart getChart() {
		return chart;
	}

	@Override
	public String execute() throws Exception {
		double [][]data=new double[][]{{1320,1110,1123,321},{720,210,1423,1321},{830,1310,123,521},{400,1110,623,321}};
		String []rowKeys={"苹果","香蕉","橘子","梨子"};
		String []columnKeys={"深圳","北京","上海","南京"};
		CategoryDataset dataset=DatasetUtilities.createCategoryDataset(rowKeys,columnKeys ,data);
		chart=ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset,
				PlotOrientation.VERTICAL, true, true, true);
		
		CategoryPlot plot=chart.getCategoryPlot();
		// 设置网格背景颜色
		plot.setBackgroundPaint(Color.white);
		// 设置网格竖线颜色
		plot.setDomainGridlinePaint(Color.pink);
		// 设置网格横线颜色
		plot.setRangeGridlinePaint(Color.pink);
		
		// 显示每个柱的数值,并修改该数值的字体属性
		BarRenderer3D renderer=new BarRenderer3D();
		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setBaseItemLabelsVisible(true);
		
		renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
		renderer.setItemLabelAnchorOffset(10D);  
		
		// 设置平行柱的之间距离
		renderer.setItemMargin(0.4);
		
		plot.setRenderer(renderer);
		
		return SUCCESS;
	}	
}

Picture request:

<img src="barChart"/>

Request:
Here Insert Picture Description
Results:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_34721292/article/details/91445397