UNPKG

8.9 kBJavaScriptView Raw
1var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5 return c > 3 && r && Object.defineProperty(target, key, r), r;
6};
7var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
8 if (kind === "m") throw new TypeError("Private method is not writable");
9 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
10 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
11 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12};
13var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
14 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
15 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
16 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
17};
18var _DuoyunBarChartElement_stackSequences;
19import { customElement, property, adoptedStyle } from '@mantou/gem/lib/decorators';
20import { html, svg } from '@mantou/gem/lib/element';
21import { css, createCSSSheet } from '@mantou/gem/lib/utils';
22import { theme } from '../lib/theme';
23import { DuoyunChartBaseElement } from './base/chart';
24import { ChartTooltip } from './chart-tooltip';
25const style = createCSSSheet(css `
26 .serie:hover .rect {
27 fill: ${theme.hoverBackgroundColor};
28 opacity: 0.2;
29 }
30 .bar:hover {
31 filter: brightness(1.05);
32 }
33`);
34/**
35 * @customElement dy-bar-chart
36 */
37let DuoyunBarChartElement = class DuoyunBarChartElement extends DuoyunChartBaseElement {
38 constructor() {
39 super(...arguments);
40 this.gutter = 0;
41 this.stack = false;
42 this.xAxi = {
43 formatter: (v) => { var _a; return ((_a = this.series) === null || _a === void 0 ? void 0 : _a[Number(v) - 1]) || ''; },
44 };
45 _DuoyunBarChartElement_stackSequences.set(this, void 0);
46 this.onMouseMove = (index, evt, sort, seqIndex = -1) => {
47 var _a, _b, _c, _d, _e;
48 let values = (_a = this.sequences) === null || _a === void 0 ? void 0 : _a.map(({ values, label }, i) => {
49 var _a, _b;
50 return {
51 label,
52 value: ((_b = (_a = this.yAxi) === null || _a === void 0 ? void 0 : _a.formatter) === null || _b === void 0 ? void 0 : _b.call(_a, values[index], 0)) || String(values[index]),
53 color: this.colors[i],
54 originValue: values[index],
55 highlight: seqIndex === i,
56 };
57 });
58 if (this.stack) {
59 values = values === null || values === void 0 ? void 0 : values.reverse();
60 }
61 if ((_b = this.tooltip) === null || _b === void 0 ? void 0 : _b.filter) {
62 values = values === null || values === void 0 ? void 0 : values.filter(this.tooltip.filter);
63 }
64 if (sort) {
65 values = values === null || values === void 0 ? void 0 : values.sort((a, b) => {
66 const an = Number(a.originValue);
67 const bn = Number(b.originValue);
68 if (isNaN(an))
69 return 1;
70 if (isNaN(bn))
71 return -1;
72 return an > bn ? -1 : 1;
73 });
74 }
75 ChartTooltip.open(evt.x, evt.y, {
76 render: (_c = this.tooltip) === null || _c === void 0 ? void 0 : _c.render,
77 xValue: index,
78 title: ((_e = (_d = this.tooltip) === null || _d === void 0 ? void 0 : _d.titleFormatter) === null || _e === void 0 ? void 0 : _e.call(_d, this.series[index])) || String(this.series[index]),
79 values: values,
80 });
81 };
82 this.onMouseOut = () => {
83 ChartTooltip.close();
84 };
85 this.willMount = () => {
86 this.memo(() => {
87 var _a, _b;
88 if (!this.contentRect.width)
89 return;
90 if (!((_a = this.sequences) === null || _a === void 0 ? void 0 : _a.length))
91 return;
92 if (!((_b = this.series) === null || _b === void 0 ? void 0 : _b.length))
93 return;
94 const seqs = this.sequences.map((e) => e.values);
95 __classPrivateFieldSet(this, _DuoyunBarChartElement_stackSequences, seqs.map((_, index) => this.mergeNumberValues(seqs.slice(0, index + 1))), "f");
96 const xMin = 0;
97 const xMax = this.series.length;
98 let yMin = Infinity;
99 let yMax = -Infinity;
100 (this.stack ? __classPrivateFieldGet(this, _DuoyunBarChartElement_stackSequences, "f") : seqs).forEach((values) => {
101 values.forEach((y) => {
102 yMin = Math.min(yMin, y !== null && y !== void 0 ? y : Infinity);
103 yMax = Math.max(yMax, y !== null && y !== void 0 ? y : -Infinity);
104 });
105 });
106 this.xStep = xMax;
107 this.initXAxi(xMin, xMax);
108 this.initYAxi(yMin, yMax);
109 this.initViewBox();
110 }, () => [this.sequences, this.contentRect.width]);
111 };
112 this.render = () => {
113 if (this.loading)
114 return this.renderLoading();
115 if (this.noData)
116 return this.renderNotData();
117 if (!this.contentRect.width || !this.sequences || !this.series)
118 return html ``;
119 return html `
120 ${svg `
121 <svg aria-hidden="true" part=${DuoyunChartBaseElement.chart} xmlns="http://www.w3.org/2000/svg" viewBox=${this.viewBox.join(' ')}>
122 ${this.renderXAxi({ centerLabel: true })}
123 ${this.renderYAxi()}
124 ${this.series.map((_value, i, _, width = 1) => svg `
125 <g class="serie" @click=${() => this.indexclick(i)} @pointerout=${this.onMouseOut}>
126 <rect
127 @pointermove=${(evt) => this.onMouseMove(i, evt, false)}
128 class="rect"
129 fill="transparent"
130 x=${(i + 0.5 - width / 2) / this.xAxiUnit}
131 y=${0}
132 width=${width / this.xAxiUnit}
133 height=${this.stageHeight}
134 />
135 ${this.sequences.map(({ values }, index, sequences, value = values[i], total = 0.6, height = (value || 0) - this.yAxiMin, seqWidth = this.stack ? total : (total - this.gutter * (sequences.length - 1)) / sequences.length, offsetX = this.stack ? 0 : index * (this.gutter + seqWidth), offsetY = this.stack
136 ? (__classPrivateFieldGet(this, _DuoyunBarChartElement_stackSequences, "f")[index - 1] ? __classPrivateFieldGet(this, _DuoyunBarChartElement_stackSequences, "f")[index - 1][i] : 0) - this.yAxiMin
137 : 0) => svg `
138 <rect
139 @pointermove=${(evt) => this.onMouseMove(i, evt, false, index)}
140 class="bar"
141 fill=${this.colors[index]}
142 x=${(i + (1 - total) / 2 + offsetX) / this.xAxiUnit}
143 y=${this.stageHeight - (height + offsetY) / this.yAxiUnit}
144 width=${seqWidth / this.xAxiUnit}
145 height=${height / this.yAxiUnit}
146 />
147 `)}
148 </g>
149 `)}
150 ${this.renderMarkLines()}
151 </svg>
152 `}
153 `;
154 };
155 }
156};
157_DuoyunBarChartElement_stackSequences = new WeakMap();
158__decorate([
159 property
160], DuoyunBarChartElement.prototype, "series", void 0);
161__decorate([
162 property
163], DuoyunBarChartElement.prototype, "gutter", void 0);
164__decorate([
165 property
166], DuoyunBarChartElement.prototype, "stack", void 0);
167__decorate([
168 property
169], DuoyunBarChartElement.prototype, "sequences", void 0);
170DuoyunBarChartElement = __decorate([
171 customElement('dy-bar-chart'),
172 adoptedStyle(style)
173], DuoyunBarChartElement);
174export { DuoyunBarChartElement };
175//# sourceMappingURL=bar-chart.js.map
\No newline at end of file