UNPKG

15.2 kBJavaScriptView Raw
1import { a as __extends, _ as __assign } from './tslib.es6-f952ba6f.js';
2import { U as Utils, T as TooltipMeasureFormat } from './Utils-38a0872e.js';
3import { select, pie, arc, pointer, easeExp, interpolate } from 'd3';
4import { L as Legend } from './Legend-a9a3b412.js';
5import { C as ChartComponentData } from './Grid-170eaa9c.js';
6import { C as ChartVisualizationComponent } from './ChartVisualizationComponent-295c909a.js';
7import { C as ContextMenu } from './ContextMenu-e6ce08ae.js';
8import { T as Tooltip } from './Tooltip-f293336f.js';
9import { G as GroupedBarChartData } from './GroupedBarChartData-57ae441e.js';
10import { S as Slider } from './Slider-e643fd4a.js';
11
12var PieChartData = /** @class */ (function (_super) {
13 __extends(PieChartData, _super);
14 function PieChartData() {
15 var _this = _super.call(this) || this;
16 _this.visibleValuesSum = 0;
17 return _this;
18 }
19 PieChartData.prototype.mergeDataToDisplayStateAndTimeArrays = function (data, timestamp, aggregateExpressionOptions) {
20 if (aggregateExpressionOptions === void 0) { aggregateExpressionOptions = null; }
21 ChartComponentData.prototype.mergeDataToDisplayStateAndTimeArrays.call(this, data, aggregateExpressionOptions);
22 this.timestamp = Utils.getValueOrDefault({ '': timestamp }, '', this.allTimestampsArray[0]);
23 _super.prototype.setValuesAtTimestamp.call(this);
24 this.setAllTimestampsArray();
25 };
26 PieChartData.prototype.updateFlatValueArray = function (timestamp) {
27 var _this = this;
28 this.visibleValuesSum = 0;
29 var values = [];
30 Object.keys(this.valuesAtTimestamp).forEach(function (aggKey) {
31 Object.keys(_this.valuesAtTimestamp[aggKey].splitBys).forEach(function (splitBy, splitByI) {
32 var value = 0;
33 if (_this.getSplitByVisible(aggKey, splitBy) && _this.valuesAtTimestamp[aggKey].splitBys[splitBy].measurements != undefined)
34 value = _this.valuesAtTimestamp[aggKey].splitBys[splitBy].measurements[_this.getVisibleMeasure(aggKey, splitBy)];
35 values.push({
36 aggKey: aggKey,
37 splitBy: splitBy,
38 val: value,
39 splitByI: splitByI
40 });
41 _this.visibleValuesSum += Math.abs(value);
42 });
43 });
44 this.flatValueArray = values;
45 };
46 return PieChartData;
47}(GroupedBarChartData));
48
49var PieChart = /** @class */ (function (_super) {
50 __extends(PieChart, _super);
51 function PieChart(renderTarget) {
52 var _this = _super.call(this, renderTarget) || this;
53 _this.chartComponentData = new PieChartData();
54 _this.chartMargins = {
55 top: 20,
56 bottom: 28,
57 left: 0,
58 right: 0
59 };
60 return _this;
61 }
62 PieChart.prototype.PieChart = function () { };
63 PieChart.prototype.render = function (data, options, aggregateExpressionOptions) {
64 var _this = this;
65 _super.prototype.render.call(this, data, options, aggregateExpressionOptions);
66 this.chartComponentData.mergeDataToDisplayStateAndTimeArrays(this.data, this.chartOptions.timestamp, this.aggregateExpressionOptions);
67 var timestamp = (options && options.timestamp != undefined) ? options.timestamp : this.chartComponentData.allTimestampsArray[0];
68 var targetElement = select(this.renderTarget)
69 .classed("tsi-pieChart", true);
70 if (this.svgSelection == null) {
71 this.svgSelection = targetElement.append("svg")
72 .attr("class", "tsi-pieChartSVG tsi-chartSVG")
73 .attr('title', this.getString('Pie chart'));
74 var g = this.svgSelection.append("g");
75 var tooltip = new Tooltip(select(this.renderTarget));
76 select(this.renderTarget).append('div').classed('tsi-sliderWrapper', true);
77 this.draw = function (isFromResize, event) {
78 if (isFromResize === void 0) { isFromResize = false; }
79 // Determine the number of timestamps present, add margin for slider
80 if (_this.chartComponentData.allTimestampsArray.length > 1)
81 _this.chartMargins.bottom = 68;
82 if (_this.chartOptions.legend == "compact") {
83 _this.chartMargins.top = 68;
84 }
85 else {
86 _this.chartMargins.top = 20;
87 }
88 _this.width = _this.getWidth();
89 var height = +targetElement.node().getBoundingClientRect().height;
90 if (!isFromResize) {
91 _this.chartWidth = _this.getChartWidth();
92 }
93 var chartHeight = height;
94 var usableHeight = height - _this.chartMargins.bottom - _this.chartMargins.top;
95 var outerRadius = (Math.min(usableHeight, _this.chartWidth) - 10) / 2;
96 var innerRadius = _this.chartOptions.arcWidthRatio &&
97 (_this.chartOptions.arcWidthRatio < 1 && _this.chartOptions.arcWidthRatio > 0) ?
98 outerRadius - (outerRadius * _this.chartOptions.arcWidthRatio) :
99 0;
100 _this.svgSelection
101 .attr("width", _this.chartWidth)
102 .attr("height", chartHeight);
103 _this.svgSelection.select("g").attr("transform", "translate(" + (_this.chartWidth / 2) + "," + (chartHeight / 2) + ")");
104 var timestamp = (_this.chartOptions.timestamp != undefined) ? _this.chartOptions.timestamp : _this.chartComponentData.allTimestampsArray[0];
105 _this.chartComponentData.updateFlatValueArray(timestamp);
106 _super.prototype.themify.call(_this, targetElement, _this.chartOptions.theme);
107 if (!_this.chartOptions.hideChartControlPanel && _this.chartControlsPanel === null) {
108 _this.chartControlsPanel = Utils.createControlPanel(_this.renderTarget, _this.CONTROLSWIDTH, _this.chartMargins.top, _this.chartOptions);
109 }
110 else if (_this.chartOptions.hideChartControlPanel && _this.chartControlsPanel !== null) {
111 _this.removeControlPanel();
112 }
113 if (_this.ellipsisItemsExist() && !_this.chartOptions.hideChartControlPanel) {
114 _this.drawEllipsisMenu();
115 _this.chartControlsPanel.style("top", Math.max((_this.chartMargins.top - 24), 0) + 'px');
116 }
117 else {
118 _this.removeControlPanel();
119 }
120 var labelMouseover = function (aggKey, splitBy) {
121 if (splitBy === void 0) { splitBy = null; }
122 //filter out the selected timeseries/splitby
123 var selectedFilter = function (d, j) {
124 return !(d.data.aggKey == aggKey && (splitBy == null || d.data.splitBy == splitBy));
125 };
126 _this.svgSelection.selectAll(".tsi-pie-path")
127 .filter(selectedFilter)
128 .attr("stroke-opacity", .3)
129 .attr("fill-opacity", .3);
130 };
131 var labelMouseout = function (aggregateKey, splitBy) {
132 _this.svgSelection.selectAll(".tsi-pie-path")
133 .attr("stroke-opacity", 1)
134 .attr("fill-opacity", 1);
135 };
136 function drawTooltip(d, mousePosition) {
137 var xPos = mousePosition[0];
138 var yPos = mousePosition[1];
139 tooltip.render(self.chartOptions.theme);
140 var color = Utils.colorSplitBy(self.chartComponentData.displayState, d.data.splitByI, d.data.aggKey, self.chartOptions.keepSplitByColor);
141 tooltip.draw(d, self.chartComponentData, xPos, yPos, __assign(__assign({}, self.chartMargins), { top: 0, bottom: 0 }), function (text) {
142 self.tooltipFormat(self.convertToTimeValueFormat(d.data), text, TooltipMeasureFormat.SingleValue);
143 }, null, 20, 20, color);
144 }
145 _this.legendObject.draw(_this.chartOptions.legend, _this.chartComponentData, labelMouseover, _this.svgSelection, _this.chartOptions, labelMouseout, null, event);
146 var pie$1 = pie()
147 .sort(null)
148 .value(function (d) {
149 return Math.abs(d.val);
150 });
151 var path = arc()
152 .outerRadius(outerRadius)
153 .innerRadius(innerRadius);
154 var arc$1 = g.selectAll(".tsi-pie-arc")
155 .data(pie$1(_this.chartComponentData.flatValueArray));
156 var arcEntered = arc$1
157 .enter().append("g")
158 .merge(arc$1)
159 .attr("class", "tsi-pie-arc");
160 var self = _this;
161 var drawArc = arc()
162 .innerRadius(innerRadius)
163 .outerRadius(outerRadius);
164 function arcTween(a) {
165 var i = interpolate(this._current, a);
166 this._current = i(0);
167 return function (t) {
168 return drawArc(i(t));
169 };
170 }
171 var self = _this;
172 function pathMouseout(event, d) {
173 if (self.contextMenu && self.contextMenu.contextMenuVisible)
174 return;
175 tooltip.hide();
176 labelMouseout(d.data.aggKey, d.data.splitBy);
177 self.legendObject.legendElement.selectAll('.tsi-splitByLabel').classed("inFocus", false);
178 }
179 function pathMouseInteraction(event, d) {
180 if (this.contextMenu && this.contextMenu.contextMenuVisible)
181 return;
182 pathMouseout(event, d);
183 labelMouseover(d.data.aggKey, d.data.splitBy);
184 self.legendObject.legendElement.selectAll('.tsi-splitByLabel').filter(function (filteredSplitBy) {
185 return (select(this.parentNode).datum() == d.data.aggKey) && (filteredSplitBy == d.data.splitBy);
186 }).classed("inFocus", true);
187 drawTooltip(d, pointer(event, self.svgSelection.node()));
188 }
189 var mouseOutArcOnContextMenuClick = function () {
190 arcEntered.selectAll("path").each(pathMouseout);
191 };
192 arcEntered.each(function () {
193 var pathElem = select(this).selectAll(".tsi-pie-path").data(function (d) { return [d]; });
194 var pathEntered = pathElem.enter()
195 .append("path")
196 .attr("class", "tsi-pie-path")
197 .attr("d", drawArc)
198 .on("mouseover", pathMouseInteraction)
199 .on("mousemove", pathMouseInteraction)
200 .on("mouseout", pathMouseout)
201 .on("contextmenu", function (event, d) {
202 if (self.chartComponentData.displayState[d.data.aggKey].contextMenuActions &&
203 self.chartComponentData.displayState[d.data.aggKey].contextMenuActions.length) {
204 var mousePosition = pointer(event, targetElement.node());
205 event.preventDefault();
206 self.contextMenu.draw(self.chartComponentData, self.renderTarget, self.chartOptions, mousePosition, d.data.aggKey, d.data.splitBy, mouseOutArcOnContextMenuClick, new Date(self.chartComponentData.timestamp));
207 }
208 })
209 .each(function (d) { this._current = d; })
210 .merge(pathElem)
211 .transition()
212 .duration(self.TRANSDURATION)
213 .ease(easeExp)
214 .attrTween("d", arcTween)
215 .attr("fill", function (d) {
216 return Utils.colorSplitBy(self.chartComponentData.displayState, d.data.splitByI, d.data.aggKey, self.chartOptions.keepSplitByColor);
217 })
218 .attr("class", "tsi-pie-path");
219 });
220 arc$1.exit().remove();
221 /******************** Temporal Slider ************************/
222 if (_this.chartComponentData.allTimestampsArray.length > 1) {
223 select(_this.renderTarget).select('.tsi-sliderWrapper').classed('tsi-hidden', false);
224 slider.render(_this.chartComponentData.allTimestampsArray.map(function (ts) {
225 var action = function () {
226 _this.chartOptions.timestamp = ts;
227 _this.render(_this.chartComponentData.data, _this.chartOptions, _this.aggregateExpressionOptions);
228 };
229 return { label: Utils.timeFormat(_this.chartComponentData.usesSeconds, _this.chartComponentData.usesMillis, _this.chartOptions.offset, _this.chartOptions.is24HourTime, null, null, _this.chartOptions.dateLocale)(new Date(ts)), action: action };
230 }), _this.chartOptions, _this.chartWidth, Utils.timeFormat(_this.chartComponentData.usesSeconds, _this.chartComponentData.usesMillis, _this.chartOptions.offset, _this.chartOptions.is24HourTime, null, null, _this.chartOptions.dateLocale)(new Date(_this.chartComponentData.timestamp)));
231 }
232 else {
233 slider.remove();
234 select(_this.renderTarget).select('.tsi-sliderWrapper').classed('tsi-hidden', true);
235 }
236 };
237 this.legendObject = new Legend(this.draw, this.renderTarget, this.CONTROLSWIDTH);
238 this.contextMenu = new ContextMenu(this.draw, this.renderTarget);
239 // temporal slider
240 var slider = new Slider(select(this.renderTarget).select('.tsi-sliderWrapper').node());
241 window.addEventListener("resize", function () {
242 if (!_this.chartOptions.suppressResizeListener)
243 _this.draw();
244 });
245 }
246 this.draw();
247 this.gatedShowGrid();
248 select("html").on("click." + Utils.guid(), function (event) {
249 if (_this.ellipsisContainer && event.target != _this.ellipsisContainer.select(".tsi-ellipsisButton").node()) {
250 _this.ellipsisMenu.setMenuVisibility(false);
251 }
252 });
253 this.legendPostRenderProcess(this.chartOptions.legend, this.svgSelection, true);
254 };
255 return PieChart;
256}(ChartVisualizationComponent));
257
258export { PieChart as P };