PdfUtils.java 3.16 KB
Newer Older
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
/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.reporting.pdf;

import org.jdom2.Element;
import org.jdom2.Namespace;

/**
 * Utility class for creating PDF diagrams
 * 
 * @author Matthias Betz
 *
 */
public class PdfUtils {

Matthias Betz's avatar
Matthias Betz committed
32
33
34
35
	private static final String HEIGHT_STRING = "height";

	private static final String WIDTH_STRING = "width";

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	private static final Namespace svgNs = Namespace.getNamespace("svg", "http://www.w3.org/2000/svg");

	private static final int WIDTH = 450;

	private PdfUtils() {
		// only static use
	}

	public static Element createBar(int val1, int val2) {
		int max = val1 + val2;
		double val1Percentage = val1 / (double) max;
		int val1Length = (int) (val1Percentage * WIDTH);
		int val2Length = WIDTH - val1Length;
		String val1LengthString = "" + val1Length;
		String val2LengthString = "" + val2Length;

		Element svgElement = new Element("svg", svgNs);
Matthias Betz's avatar
Matthias Betz committed
53
54
		svgElement.setAttribute(WIDTH_STRING, "450px");
		svgElement.setAttribute(HEIGHT_STRING, "30px");
55
56
57
58
59
60
61
62
63
64

		if (val1 > 0) {
			Element gVal1Style = new Element("g", svgNs);
			svgElement.addContent(gVal1Style);
			gVal1Style.setAttribute("style", "fill:red");

			Element val1Rect = new Element("rect", svgNs);
			gVal1Style.addContent(val1Rect);
			val1Rect.setAttribute("x", "0");
			val1Rect.setAttribute("y", "0");
Matthias Betz's avatar
Matthias Betz committed
65
66
			val1Rect.setAttribute(WIDTH_STRING, val1LengthString);
			val1Rect.setAttribute(HEIGHT_STRING, "30");
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
			Element val1Text = new Element("text", svgNs);
			svgElement.addContent(val1Text);
			val1Text.setAttribute("x", "5");
			val1Text.setAttribute("y", "20");
			val1Text.setAttribute("font-size", "18pt");
			val1Text.addContent("" + val1);
		}

		if (val2 > 0) {
			Element gVal2Style = new Element("g", svgNs);
			svgElement.addContent(gVal2Style);
			gVal2Style.setAttribute("style", "fill:green");
			Element val2Rect = new Element("rect", svgNs);
			gVal2Style.addContent(val2Rect);
			val2Rect.setAttribute("x", val1LengthString);
			val2Rect.setAttribute("y", "0");
Matthias Betz's avatar
Matthias Betz committed
83
84
			val2Rect.setAttribute(WIDTH_STRING, val2LengthString);
			val2Rect.setAttribute(HEIGHT_STRING, "30");
85
86
			Element val2Text = new Element("text", svgNs);
			svgElement.addContent(val2Text);
87
			String val2String = "" + val2;
Matthias Betz's avatar
Matthias Betz committed
88
			val2Text.setAttribute("x", "" + (WIDTH - val2String.length() * 13));
89
90
			val2Text.setAttribute("y", "20");
			val2Text.setAttribute("font-size", "18pt");
91
92
			
			val2Text.addContent(val2String);
93
94
95
96
97
98
		}

		return svgElement;
	}

}