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
| <template>
| <!-- 选项 -->
| <div class="options">
| <el-form :inline="true">
| <!-- <el-form-item>
| <el-option
| v-model="charType"
| v-for="type of charTypeOptions"
| :key="type"
| :value="type"
| :label="type"
| ></el-option>
| </el-form-item> -->
| <el-form-item>
| <el-select
| v-model="charType"
| style="width: 150px"
| placeholder="请选择图表类型"
| >
| <el-option
| v-for="item in charTypeOptions"
| :key="item.value"
| :value="item.value"
| :label="item.label"
| ></el-option>
| </el-select>
| </el-form-item>
| <el-form-item>
| <el-select
| v-model="selectHeader"
| style="width: 200px"
| @change="onSelectHeaderChange"
| value-key="id"
| placeholder="请选择展示的列"
| >
| <el-option
| v-for="item in allHeader"
| :key="item.id"
| :value="item"
| :label="item.label"
| ></el-option>
| </el-select>
| </el-form-item>
| </el-form>
| </div>
| <div ref="myChart" id="myChart" class="chart"></div>
| <el-button type="primary" @click="ok()">确定</el-button>
| </template>
| <script>
| export default {
| props: {
| tableHeader: {
| type: Array,
| required: true
| },
| tableData: {
| type: Array,
| required: true
| }
| },
| computed: {
| allHeader() {
| return this.treeToArray(this.tableHeader);
| }
| },
| data() {
| return {
| selectHeader: null,
| charType: null,
| charTypeOptions: [
| {
| label: '直方图',
| value: 'bar'
| },
| {
| label: '折线图',
| value: 'line'
| },
| {
| label: '饼图',
| value: 'pie'
| }
| ],
|
| charts: [],
| chart: {}
| };
| },
| mounted() {
| // this.setChartOptions('myChart', this.header.prop, this.header.label);
| // this.selectHeader = this.tableHeader[0]
| },
| methods: {
| getChartObj() {
| return this.chart;
| },
| ok() {
| let option = this.setChartOptions(
| 'myChart',
| this.selectHeader.prop,
| this.selectHeader.label
| );
| this.$emit('painted-chart', option);
| },
| onSelectHeaderChange() {
| this.setChartOptions(
| 'myChart',
| this.selectHeader.prop,
| this.selectHeader.label
| );
| },
| // 递归的获取obj中的prop属性
| getPropValueLoop(obj, prop) {
| if (typeof prop !== 'string') {
| return obj;
| }
| const props = prop.split('.');
| let result = obj;
| props.forEach((item) => {
| result = result[item];
| });
| return result;
| },
| addChart(ref) {},
| setChartOptions(refName, prop, label) {
| let series = this.tableData.map((item) =>
| this.getPropValueLoop(item, prop)
| );
|
| const dom = this.$refs[refName];
| const myChart = this.$echarts.init(dom); // 初始化echarts实例
| this.chart = myChart;
| const option = {
| title: {
| text: label //设置我们的标题
| },
| tooltip: {
| trigger: 'item'
| },
| legend: {
| orient: 'vertical',
| left: 'left'
| },
| xAxis: {
| type: 'category',
| data: Array.from(new Set(series)),
| axisLabel: {
| rotate: 45, // 旋转标签,避免重叠
| // 或者
| interval: 0 // 显示所有标签,可能导致重叠,根据需求调整
| }
| },
| yAxis: {
| type: 'value'
| },
| series: [
| {
| data: Array.from(new Set(series)).map((item) =>
| this.getCount(series, item)
| ),
| type: this.charType,
| smooth: true
| }
| ]
| };
| // 设置实例参数
| myChart.setOption(option);
| return option;
| },
|
| getCount(array, element) {
| let count = 0;
| array.forEach((e) => {
| if (e == element) {
| count++;
| }
| });
| return count;
| },
| treeToArray(nodes) {
| let labels = [];
|
| function traverseNodes(nodes) {
| nodes.forEach((node) => {
| labels.push(node);
|
| // 如果当前节点有子节点,递归调用遍历函数
| if (node.children && node.children.length > 0) {
| traverseNodes(node.children);
| }
| });
| }
|
| traverseNodes(nodes); // 开始递归遍历
|
| return labels; // 返回收集到的 labels 数组
| }
| }
| };
| </script>
| <style scoped>
| .options {
| display: flex;
| margin: 5px;
| margin-left: 0px;
| }
| .chart {
| width: 'auto';
| height: 500px;
| }
| </style>
|
|