forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoChart.java
More file actions
73 lines (60 loc) · 2.21 KB
/
DoChart.java
File metadata and controls
73 lines (60 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.zetcode.web;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
@WebServlet(name = "DoChart", urlPatterns = {"/showChart"})
public class DoChart extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
JFreeChart chart = getChart();
int width = 500;
int height = 350;
ChartUtils.writeChartAsPNG(os, chart, width, height);
}
// public JFreeChart getChart() {
//
// var dataset = new DefaultPieDataset();
//
// dataset.setValue("Croatia", 22);
// dataset.setValue("Bohemia", 34);
// dataset.setValue("Bulgaria", 18);
// dataset.setValue("Spain", 5);
// dataset.setValue("Others", 21);
//
// JFreeChart chart = ChartFactory.createPieChart("Popular destinations",
// dataset, true, false, false);
//
// chart.setBorderVisible(false);
//
// return chart;
// }
public JFreeChart getChart() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(46, "Gold medals", "USA");
dataset.setValue(38, "Gold medals", "China");
dataset.setValue(29, "Gold medals", "UK");
dataset.setValue(22, "Gold medals", "Russia");
dataset.setValue(13, "Gold medals", "South Korea");
dataset.setValue(11, "Gold medals", "Germany");
JFreeChart barChart = ChartFactory.createBarChart(
"Olympic gold medals in London",
"",
"Gold medals",
dataset,
PlotOrientation.VERTICAL,
false, true, false);
return barChart;
}
}