Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import merge from 'lodash-es/merge'; import { ChartOptions, ChartTextColorOptions } from '../types'; export abstract class BaseChart<TOptions extends ChartOptions> { protected chart: any; protected options: TOptions; protected textColors: ChartTextColorOptions; constructor(dom: HTMLDivElement | HTMLCanvasElement, options: TOptions) { this.chart = echarts.init(dom); this.options = options; this.textColors = this.getTextColor(); } dispose(): void { this.chart && this.chart.dispose(); } rerender(options: TOptions): void { merge(this.options, options); this.render(); } protected abstract render(): void; protected getTitleOptions(): any { const result: any = {}; const textColors = this.getTextColor(); if (textColors.primary) { result.textStyle = { color: textColors.primary, }; } if (textColors.secondary) { result.subtextStyle = { color: textColors.secondary, }; } if (this.options.title) { if (this.options.title.text) { result.text = this.options.title.text; } if (this.options.title.description) { result.subtext = this.options.title.description; } } return result; } protected getLegendOptions(): any { const result: any = merge({ show: true, padding: [10, 10, 10, 10], textStyle: this.getTextStyle(this.options.legend), }); const locationMap = { left: { top: 'center', left: 0, orient: 'vertical' }, right: { top: 'center', right: 0, orient: 'vertical' }, top: { top: 0, left: 'center', orient: 'horizontal' }, 'top-left': { top: 0, left: 0, orient: 'horizontal' }, 'top-right': { top: 0, right: 0, orient: 'horizontal' }, bottom: { bottom: 0, left: 'center', orient: 'horizontal' }, 'bottom-left': { bottom: 0, left: 0, orient: 'horizontal' }, 'bottom-right': { bottom: 0, right: 0, orient: 'horizontal' }, }; if (this.options.legend && this.options.legend.location) { merge(result, locationMap[this.options.legend.location]); } else { merge(result, locationMap['bottom']); } if (this.options.legend && this.options.legend.fnLabels) { result.formatter = (key: string) => { const total = (Object.values(this.options.data) as number[]).reduce((t: number, d: number) => t + d); const value = this.options.data[key]; const percent = (value * 100) / total; if (this.options.legend?.fnLabels) { return this.options.legend .fnLabels(key, value, percent) .map((label: string | number, i: number) => `{${i}|${label}}`) .join(''); } }; if (this.options.legend.labelStyles) { const rich: any = {}; this.options.legend.labelStyles.forEach((style, i) => { rich[`${i}`] = style; }); result.textStyle = { rich, }; } } return merge({}, result, this.options.legend); } protected getToolboxOptions(): any { return merge( {}, { show: false, feature: { mark: { show: true }, dataView: { show: true, readOnly: false }, magicType: { show: true, type: ['pie', 'funnel'], }, restore: { show: true }, saveAsImage: { show: true }, }, }, this.options.toolbox, ); } protected getTextStyle(currentNodeOptions: any): any { const result: any = {}; if (this.textColors.primary) { result.color = this.textColors.primary; } if (currentNodeOptions && currentNodeOptions.color) { result.color = currentNodeOptions.color; } return result; } protected getTextColor(): ChartTextColorOptions { const result: ChartTextColorOptions = {}; if (this.options.isDark) { result.primary = '#ffffff'; result.secondary = '#cccccc'; } else { result.primary = '#000000'; result.secondary = '#666666'; } if (this.options.textColor) { result.primary = this.options.textColor; } if (this.options.mutedTextColor) { result.secondary = this.options.mutedTextColor; } return result; } protected getColor(key: string, index: number): string { if (this.options.colors) { if (typeof this.options.colors === 'string') { return this.options.colors; } else if (Array.isArray(this.options.colors)) { if (this.options.colors.length > index) { return this.options.colors[index]; } } else { return this.options.colors[key]; } } return ''; } } |