-
Notifications
You must be signed in to change notification settings - Fork 61
/
Bullet.ts
167 lines (147 loc) · 5.82 KB
/
Bullet.ts
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
import { HTMLWidget, publish, Utility } from "@hpcc-js/common";
import { select as d3Select } from "d3-selection";
import { bullet as d3Bullet } from "d3v4-bullet";
import "../src/Bullet.css";
export class Bullet extends HTMLWidget {
@publish(null, "set", "Title Column", function () { return this.columns(); }, { optional: true })
titleColumn: publish<this, string>;
@publish(null, "set", "Subtitle Column", function () { return this.columns(); }, { optional: true })
subtitleColumn: publish<this, string>;
@publish(null, "set", "Ranges Column", function () { return this.columns(); }, { optional: true })
rangesColumn: publish<this, string>;
@publish(null, "set", "Measures Column", function () { return this.columns(); }, { optional: true })
measuresColumn: publish<this, string>;
@publish(null, "set", "Markers Column", function () { return this.columns(); }, { optional: true })
markersColumn: publish<this, string>;
private _hiddenColumns = {};
constructor() {
super();
Utility.SimpleSelectionMixin.call(this, true);
}
bulletData() {
const context = this;
this._hiddenColumns = {};
const columns = this.columns();
return this.data().map(function (row) {
return {
title: valueOf(row, this.titleColumn()),
subtitle: valueOf(row, this.subtitleColumn()),
ranges: valueOf(row, this.rangesColumn(), "range"),
measures: valueOf(row, this.measuresColumn(), "measure"),
markers: valueOf(row, this.markersColumn(), "marker"),
origRow: row
};
}, this);
function valueOf(row, column, columnType?) {
const colIdx = columns.indexOf(column);
if (colIdx >= 0) {
if (row[colIdx] instanceof Array) {
return row[colIdx];
}
return [row[colIdx]];
}
if (columnType) {
context._hiddenColumns[columnType] = true;
}
return [0];
}
}
enter(domNode, element) {
super.enter(domNode, element);
d3Select(domNode.parentNode).style("overflow", "auto");
this._selection.widgetElement(element);
}
update(_domNode, element) {
super.update(_domNode, element);
const context = this;
element.selectAll(".axis")
.style("display", "none")
;
element.selectAll(".range,.measure,.marker")
.style("display", null)
;
const margin = { left: 2, top: 8, right: 2, bottom: 8 };
const width = this.width() - margin.left - margin.right;
const height = 40 - margin.top - margin.bottom;
const svg = element.selectAll("svg").data(this.bulletData());
const svgUpdate = svg.enter().append("svg")
.attr("class", "bullet")
.call(this._selection.enter.bind(this._selection))
.on("click", function (d) {
context.click(context.rowToObj(d.origRow), context.titleColumn(), context._selection.selected(this));
})
.on("dblclick", function (d) {
context.dblclick(context.rowToObj(d.origRow), context.titleColumn(), context._selection.selected(this));
})
.each(function () {
const element2 = d3Select(this);
const bulletBar = element2.append("g")
.attr("class", (d, i) => "bulletBar series series-" + context.cssTag(context.titleColumn()))
;
const bulletTitle = bulletBar.append("g")
.attr("class", "bulletTitle")
;
bulletTitle.append("text")
.attr("class", "title")
;
bulletTitle.append("text")
.attr("class", "subtitle")
.attr("dy", "1em")
;
})
.merge(svg)
;
// Title ---
const title = svgUpdate.select(".bulletTitle")
.style("text-anchor", "end")
.attr("transform", "translate(-6," + height / 2 + ")")
;
title.select(".title")
.text(function (d) { return d.title; })
;
title.select(".subtitle")
.text(function (d) { return d.subtitle; })
;
let titleWidth = 0;
title.each(function () {
const bbox = this.getBBox();
if (bbox.width > titleWidth) {
titleWidth = bbox.width;
}
});
titleWidth; // Gap between title and bullet bar.
// Bullet Chart ---
const chart = d3Bullet()
.width(width - titleWidth - 6)
.height(height)
;
svgUpdate
.attr("width", width)
.attr("height", height + margin.top + margin.bottom)
.style("margin-left", `${margin.left}px`)
;
svgUpdate.select(".bulletBar")
.attr("transform", "translate(" + (titleWidth + 6) + "," + margin.top + ")")
.call(chart)
;
svg.exit().remove();
if (Object.keys(this._hiddenColumns).length > 0) {
element.selectAll(`.${Object.keys(this._hiddenColumns).join(",.")}`)
.style("display", "none")
;
}
}
exit(domNode, element) {
super.exit(domNode, element);
}
// Events ---
click(row, column, selected) {
console.log("Click: " + JSON.stringify(row) + ", " + column + "," + selected);
}
dblclick(row, column, selected) {
console.log("Double click: " + JSON.stringify(row) + ", " + column + "," + selected);
}
// SimpleSelectionMixin
_selection;
}
Bullet.prototype._class += " chart_Bullet";