-
Notifications
You must be signed in to change notification settings - Fork 7
/
Dataset.java
164 lines (144 loc) · 4.13 KB
/
Dataset.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.biz.analytics.metrics;
import sirius.kernel.nls.NLS;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* Defines a dataset which can be passed into a chart.
* <p>
* The main purpose is to format the given data into a representation which is usable in the JavaScript implementation
* which renders the chart.
*/
public class Dataset {
/**
* Contains the id of the right Y axis.
*/
public static final String AXIS_RIGHT = "right";
/**
* Provides a default gray tone for secondary/comparison lines in a chart.
*/
public static final String COLOR_GRAY = "#888888";
private String axis;
private final String label;
private final List<Number> values = new ArrayList<>();
private boolean gray;
/**
* Creates a new dataset with the given label.
*
* @param label a label to use
*/
public Dataset(String label) {
this.label = label;
}
/**
* Specifies which axis to use.
*
* @param axisId the axis to use. Most probably this will be {@link #AXIS_RIGHT} as left is the default
* @return the dataset itself for fluent method calls
*/
public Dataset onAxis(String axisId) {
this.axis = axisId;
return this;
}
/**
* Adds a value to the dataset.
*
* @param value the value to add
* @return the dataset itself for fluent method calls
*/
public Dataset addValue(Number value) {
values.add(value);
return this;
}
/**
* Adds the given values to the dataset.
*
* @param values the values to add
* @return the dataset itself for fluent method calls
*/
public Dataset addValues(List<? extends Number> values) {
this.values.addAll(values);
return this;
}
/**
* Scales all values of the dataset with the given factor.
*
* @param factor the scaling factor to apply. This can be used to output decimal data, which isn't
* supported by {@link sirius.biz.analytics.metrics.Metrics} itself, which only stores
* integer numbers.
* @return the dataset itself for fluent method calls
*/
public Dataset scale(float factor) {
this.values.replaceAll(number -> number.floatValue() * factor);
return this;
}
/**
* Returns the label of the dataset.
*
* @return the label of the dataset
*/
public String getLabel() {
return label;
}
/**
* Returns a string representation of the data used by the JavaScript implementation to render a chart.
*
* @return a string representation of this dataset
*/
public String renderData() {
return values.stream().map(value -> {
if (value == null) {
return "null";
} else {
return NLS.toMachineString(value);
}
}).collect(Collectors.joining(","));
}
/**
* Returns the previously collected values within this dataset.
*
* @return the values of this dataset
*/
public List<Number> getValues() {
return Collections.unmodifiableList(values);
}
public String getAxis() {
return axis;
}
/**
* Returns the axis to use.
*
* @return the axis properly encoded to be directly used in JavaScript
*/
public String renderAxisName() {
if (axis == null) {
return "null";
}
return "'" + axis + "'";
}
/**
* Enforces a gray color when showing this dataset.
*
* @return the dataset itself for fluent method calls
*/
public Dataset markGray() {
this.gray = true;
return this;
}
/**
* Determines if this dataset has been marked as gray.
*
* @return <tt>true</tt> if this dataset should be rendered as gray line, <tt>false</tt> otherwise
*/
public boolean isGray() {
return gray;
}
}