文章目录
JFreeChart 简介
是一个开源的图表绘制组件。可以通过该组件生成多种图表,如饼状图(pie chart)、柱状图(bar chart)、散点图(seatter plots)、时序图(time series)、甘特图(gantt charts),最后产生的是一个png or jpeg格式的图片。
没有任何的动画效果。日
还是python的数据可视化好
做这个需要俩个包
jfreechart-1.~~.jar
jcommon-1.~~.jar
上代码:
package com.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
/**
* @Author 911
* @create 2021/02/09 16:02
*/
public class Bar {
int width;
int height;
String chartTitle;
String subtitle;
String xTitle;
String yTitle;
String legend[];
String category[];
Integer[][] data;
String servletURL = "/DisplayChart";
public Bar(){
width = 600;
height = 325;
chartTitle = "每月平局温度";
subtitle = "--统计时间:2021年";
xTitle = "月份";
yTitle = "气温 单位:摄氏度";
legend = new String[] {"吉林长春","湖南长沙"};
category = new String[]{"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
data = new Integer[][]{
{-21,-3,12,19,22,28,30,29,23,18,5,-10},
{3,12,17,20,25,32,41,38,30,27,15,10}
};
}
public String draw(HttpSession session,String contextPath){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < legend.length; i++) {
for (int i1 = 0; i1 < category.length; i1++) {
dataset.addValue(data[i][i1],legend[i],category[i1]);
}
}
Font font = new Font("Simsun", 10, 15);
//初始化图表
JFreeChart chart = ChartFactory.createBarChart(chartTitle, xTitle, yTitle, dataset, PlotOrientation.VERTICAL, true, true, false);
chart.setTitle(new TextTitle(chartTitle,font));
if (subtitle.length()>0){
chart.addSubtitle(new TextTitle(subtitle));
}
chart.setBackgroundPaint(new Color(200,200,200));
CategoryPlot plot = chart.getCategoryPlot(); //得到绘图区的变量
plot.getRangeAxis().setLabelFont(font);
plot.getDomainAxis().setLabelFont(font);
plot.getRangeAxis().setTickLabelFont(font);
plot.getDomainAxis().setTickLabelFont(font);
plot.setBackgroundPaint(new Color(241,219,127));
plot.setRangeGridlinePaint(Color.BLACK);
plot.setRangeMinorGridlinesVisible(true);
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
renderer.setDefaultLegendTextFont(font);
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
String filename ="";
try {
filename+= ServletUtilities.saveChartAsPNG(chart,width,height,info,session);
} catch (IOException e) {
e.printStackTrace();
}
String graphURL = contextPath+servletURL+"?filename="+filename;
return graphURL;
}
}
无话可说…