饼状图
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset. setValue("优秀", 25) ;
dataset. setValue("良好", 40);
dataset. setValue(" 中等", 20);
dataset . setValue("及格", 10);
dataset. setValue("不及格", 5);
//标题
JFreeChart chart = ChartFactory.createPieChart3D("成绩分布", dataset);
// 内容
StandardChartTheme theme = new StandardChartTheme("CN");
theme.setExtraLargeFont(new Font("隶书", Font.BOLD, 25));
theme.setRegularFont(new Font("隶书", Font.BOLD, 25));
ChartFactory.setChartTheme(theme);
//
ChartUtilities . writeChartAsJPEG(response. getOutputStream(), chart, 600, 400);
}
柱状图
设置样式
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DefaultCategoryDataset defaultDataset = new DefaultCategoryDataset();
defaultDataset.addValue(1000,"2017","北");
defaultDataset.addValue(2000,"2018","北");
defaultDataset.addValue(3000,"2019","北");
defaultDataset.addValue(2000,"2017","上");
defaultDataset.addValue(3000,"2018","上");
defaultDataset.addValue(4000,"2019","上");
defaultDataset.addValue(3000,"2017","广");
defaultDataset.addValue(4000,"2018","广");
defaultDataset.addValue(5000,"2019","广");
JFreeChart chart = ChartFactory.createBarChart3D("城市房价对比", "城市", "房价/平方米", defaultDataset);
chart.getTitle().setFont(new Font("隶书", Font.BOLD, 25));
chart.getLegend().setItemFont(new Font("微软雅黑", Font.BOLD, 10));
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis axis = plot.getDomainAxis();
//横坐标
axis.setLabelFont(new Font("宋体",Font.BOLD,15));
axis.setTickLabelFont(new Font("隶书", Font.BOLD, 25));
//纵坐标
ValueAxis valueAxis = plot.getRangeAxis();
valueAxis.setLabelFont(new Font("宋体",Font.BOLD,20));
ChartUtilities.writeChartAsJPEG(response. getOutputStream(), chart, 600, 400);
}
折线图,点图
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
XYSeries xySeries = new XYSeries("0~100随机数");
XYSeries xySeries1 = new XYSeries("-100~0随机数");
XYSeries xySeries2 = new XYSeries("随机数");
XYSeries xySeries3 = new XYSeries("随机数pro");
for (int i = 0; i < 50; i++) {
int num = (int) (Math.random()*100+1);
System.out.println(num);
xySeries.add(i + 1, num);
System.out.println();
}
for (int i = 0; i < 50; i++) {
int num = (int) (Math.random()*-100-1);
System.out.println(num);
xySeries1.add(i+1, num);
System.out.println();
}
for (int i = -50; i < 0; i++) {
int num = (int) (Math.random()*-100-1);
System.out.println(num);
xySeries2.add(i, num);
System.out.println();
}
for (int i = -50; i < 0; i++) {
int num = (int) (Math.random()*100+1);
System.out.println(num);
xySeries3.add(i, num);
System.out.println();
}
xySeriesCollection.addSeries(xySeries);
xySeriesCollection.addSeries(xySeries1);
xySeriesCollection.addSeries(xySeries2);
xySeriesCollection.addSeries(xySeries3);
JFreeChart jFreeChart = ChartFactory.createXYLineChart("随机数分布", "次数", "大小", xySeriesCollection);
jFreeChart.getTitle().setFont(new Font("隶书", Font.BOLD, 25));
jFreeChart.getLegend().setItemFont(new Font("隶书", Font.BOLD, 25));
XYPlot plot = jFreeChart.getXYPlot();
plot.setRangeGridlinePaint(Color.black);
plot.setBackgroundPaint(Color.white);
plot.getDomainAxis().setLabelFont(new Font("隶书", Font.BOLD, 25));
plot.getDomainAxis().setTickLabelFont(new Font("隶书", Font.BOLD, 25));
plot.getRangeAxis().setLabelFont(new Font("隶书", Font.BOLD, 25));
plot.getRangeAxis().setTickLabelFont(new Font("隶书", Font.BOLD, 25));
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesStroke(0, new BasicStroke());
renderer.setBaseShapesVisible(true);
//空心实心
renderer.setUseFillPaint(true);
//显示线
renderer.setBaseLinesVisible(false);
renderer.setSeriesShape(0, new Ellipse2D.Double(-5, -5, 10, 10));
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), jFreeChart, 600, 400);
}
导图片
FileOutputStream outputStream = null;
outputStream = new FileOutputStream("C:\\Users\\hp\\Workspaces\\MyEclipse CI\\Framechart\\WebRoot\\img\\1.jpg");
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart, 400, 300);