-
Notifications
You must be signed in to change notification settings - Fork 272
/
burndown-chart-drawer.js
252 lines (210 loc) · 7.78 KB
/
burndown-chart-drawer.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright (c) Enalean, 2018-Present. All Rights Reserved.
*
* This file is a part of Tuleap.
*
* Tuleap is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Tuleap 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
*/
import moment from "moment";
import { sprintf } from "sprintf-js";
import { max } from "d3-array";
import { curveLinear } from "d3-shape";
import { select } from "d3-selection";
import { gettext_provider } from "./gettext-provider.js";
import { buildGraphScales } from "charts-builders/line-chart-scales-factory.js";
import { buildChartLayout } from "charts-builders/chart-layout-builder.js";
import { TooltipFactory } from "charts-builders/chart-tooltip-factory.js";
import { ColumnFactory } from "charts-builders/chart-column-factory.js";
import { TimeScaleLabelsFormatter } from "charts-builders/time-scale-labels-formatter.js";
import { getDaysToDisplay } from "charts-builders/chart-dates-service.js";
import { addTextCaption } from "charts-builders/chart-text-legend-generator.js";
import { addBadgeCaption } from "charts-builders/chart-badge-legend-generator.js";
import { addContentCaption } from "charts-builders/chart-content-legend-generator.js";
import { drawIdealLine, drawCurve } from "charts-builders/chart-lines-service.js";
import { getLastDayData, getDisplayableData } from "./chart-data-service.js";
export { createBurndownChart };
function createBurndownChart({ chart_container, chart_props, chart_legends, burndown_data }) {
const tooltip_factory = new TooltipFactory({
tooltip_margin_bottom: 30,
tooltip_padding_width: 10,
tooltip_padding_height: 5,
tooltip_arrow_size: 150,
tooltip_font_size: 12,
});
const DEFAULT_REMAINING_EFFORT = 5,
x_axis_tick_values = getDaysToDisplay(burndown_data),
displayable_data = getDisplayableData(burndown_data.points_with_date),
last_day_data = getLastDayData(burndown_data.points_with_date),
y_axis_maximum = getMaxRemainingEffort(burndown_data);
const properties = {
...chart_props,
x_axis_tick_values,
y_axis_maximum,
};
const { x_scale, y_scale } = buildGraphScales(properties);
const column_factory = new ColumnFactory({
x_scale,
y_scale,
column_width: x_scale.step(),
column_height: y_scale(0) - properties.margins.top,
});
const svg_burndown = buildChartLayout(chart_container, chart_props, {
x_scale,
y_scale,
});
insertLegend();
const label_formatter = new TimeScaleLabelsFormatter({
layout: svg_burndown,
first_date: x_axis_tick_values[0],
last_date: x_axis_tick_values[x_axis_tick_values.length - 1],
});
label_formatter.formatTicks();
if (!burndown_data.points_with_date.length) {
return;
}
drawBurndownChart();
function drawBurndownChart() {
addIdealLine();
drawDataColumns();
addCurve("remaining_effort");
setInteraction();
}
function drawDataColumns() {
const columns = svg_burndown
.selectAll(".chart-datum-column")
.data(displayable_data)
.enter()
.append("g")
.attr("class", "chart-datum-column");
columns.each(function ({ date, remaining_effort }) {
const column = select(this);
column_factory.addColumn(column, date);
column
.append("circle")
.attr("class", "chart-plot-remaining-effort chart-tooltip-target")
.attr("cx", x_scale(date))
.attr("cy", y_scale(remaining_effort))
.attr("r", 4);
});
}
function addCurve(line_name) {
drawCurve(
svg_burndown,
{
x_scale,
y_scale,
},
displayable_data,
line_name,
curveLinear
);
}
function setInteraction() {
svg_burndown.selectAll(".chart-datum-column").each(function () {
const datum_column = select(this);
datum_column.on("mouseenter", () => {
highlightColumn(datum_column);
});
datum_column.on("mouseleave", () => {
ceaseHighlight();
});
});
}
function highlightColumn(target_column) {
ceaseHighlight();
target_column.selectAll("circle").classed("highlighted", true);
target_column.select(".chart-column").classed("highlighted", true);
tooltip_factory
.addTooltip(target_column)
.addTextLine(({ date }) =>
moment(date, moment.ISO_8601).format(properties.tooltip_date_format)
)
.addTextLine(({ remaining_effort }) =>
sprintf(gettext_provider.gettext("Remaining effort: %s"), remaining_effort)
);
}
function ceaseHighlight() {
svg_burndown.selectAll("circle").classed("highlighted", false);
svg_burndown.selectAll(".chart-column").classed("highlighted", false);
TooltipFactory.removeTooltips(svg_burndown);
}
function addIdealLine() {
const { capacity } = burndown_data;
const first_ideal_line_point = capacity ? capacity : y_axis_maximum;
drawIdealLine(
svg_burndown,
{
x_scale,
y_scale,
},
{
line_start: first_ideal_line_point,
line_end: 0,
}
);
}
function getDateLegendContent() {
if (isThereARemainingEffort()) {
return sprintf(
chart_props.left_legend_title,
moment(last_day_data.date).format(chart_props.left_legend_date_format)
);
}
return sprintf(
chart_props.left_legend_title,
moment().format(chart_props.left_legend_date_format)
);
}
function isThereARemainingEffort() {
return (
Object.prototype.hasOwnProperty.call(last_day_data, "remaining_effort") &&
last_day_data.remaining_effort !== null
);
}
function getMaxRemainingEffort({ points_with_date, capacity }) {
const max_remaining_effort = max(
points_with_date,
({ remaining_effort }) => remaining_effort
);
const maximum = max([max_remaining_effort, capacity]);
if (maximum) {
return maximum;
}
return DEFAULT_REMAINING_EFFORT;
}
function insertLegend() {
const legend_y_position = chart_props.margins.top * 0.5;
const date_legend_content = getDateLegendContent();
const badge_value = isThereARemainingEffort()
? last_day_data.remaining_effort
: chart_props.legend_badge_default;
addTextCaption({
layout: svg_burndown,
content: date_legend_content,
legend_y_position,
});
addBadgeCaption({
layout: svg_burndown,
badge_value,
legend_y_position,
});
addContentCaption({
legend_y_position,
layout: svg_burndown,
chart_content_legend: chart_legends,
chart_width: chart_props.graph_width,
chart_margin_right: chart_props.margins.right,
});
}
}