11
11
<!-- x轴坐标单位 -->
12
12
<p class =" absolute right-5 bottom-10 text-xs text-dimmer scale-90 select-none" >{{ xTitle }}</p >
13
13
<!-- 图表主体 -->
14
+ <LineChartLegend
15
+ :items =" legend"
16
+ @hoverin =" (item) => legendHoverin(item, false)"
17
+ @hoverout =" (item) => legendHoverout(item, false)"
18
+ v-if =" legend && mutli"
19
+ />
14
20
<div class =" relative" ref =" g2Ref" >
15
21
<LineChartTooltip ref =" tooltipRef" />
16
22
</div >
17
23
<!-- 放大效果 -->
18
24
<SLModal class =" p-10 pt-0 overflow-hidden" max-w =" -1" v-model =" isZoom" >
19
25
<p class =" text-center mt-4 mb-10 text-2xl font-semibold" >{{ title }}</p >
26
+ <LineChartLegend
27
+ :items =" legend"
28
+ @hoverin =" (item) => legendHoverin(item, true)"
29
+ @hoverout =" (item) => legendHoverout(item, true)"
30
+ v-if =" legend && mutli"
31
+ />
20
32
<div class =" relative" ref =" g2ZoomRef" >
21
33
<LineChartTooltip detail ref =" tooltipZoomRef" />
22
34
</div >
@@ -43,6 +55,7 @@ import { getTimes } from '@swanlab-vue/utils/time'
43
55
import { isApple } from ' @swanlab-vue/utils/browser'
44
56
import { message } from ' @swanlab-vue/components/message'
45
57
import LineChartTooltip from ' ../components/LinChartTooltip.vue'
58
+ import LineChartLegend from ' ../components/LineChartLegend.vue'
46
59
47
60
// ---------------------------------- 配置 ----------------------------------
48
61
const props = defineProps ({
@@ -80,7 +93,7 @@ const gridColor = rootStyle.getPropertyValue('--outline-dimmest')
80
93
// 十字准线颜色,通过js获取css变量值
81
94
const crosshairsColor = rootStyle .getPropertyValue (' --primary-dimmest' )
82
95
// 线段默认宽度
83
- const lineWidth = 2
96
+ const lineWidth = 1.5
84
97
// 线段加粗宽度
85
98
const thickerLineWidth = 3.5
86
99
@@ -134,28 +147,8 @@ const createChart = (dom, data, config = {}, zoom = false) => {
134
147
// 多数据的时候,需要设置seriesField,单数据也可以设置,但是不希望出现label
135
148
// seriesField,
136
149
colorField,
137
- legend: {
138
- // flipPage: false,
139
- pageNavigator: {
140
- marker: {
141
- style: {
142
- // 非激活,不可点击态时的填充色设置
143
- inactiveFill: ' #000' ,
144
- inactiveOpacity: 0.45 ,
145
- // 默认填充色设置
146
- fill: ' #000' ,
147
- opacity: 0.8 ,
148
- size: 8
149
- }
150
- },
151
- text: {
152
- style: {
153
- fill: ' #ccc' ,
154
- fontSize: 8
155
- }
156
- }
157
- }
158
- },
150
+ // 自己写图例
151
+ legend: false ,
159
152
// 多数据的时候颜色通过回调拿到,colors应该自带getSeries方法
160
153
color : ({ series }) => {
161
154
return colors .getSeriesColor (series, source .indexOf (series))
@@ -249,7 +242,7 @@ const createChart = (dom, data, config = {}, zoom = false) => {
249
242
}
250
243
}
251
244
},
252
- // 图例相关
245
+ // 悬浮提示相关
253
246
tooltip: {
254
247
// 在此处完成悬浮数据提示的格式化
255
248
// 如果需要自定义浮窗,可以用下面的customContent
@@ -286,7 +279,9 @@ const createChart = (dom, data, config = {}, zoom = false) => {
286
279
width: undefined ,
287
280
autoFit: true ,
288
281
// 开启一些交互
289
- interactions: [{ type: ' hover-cursor' }],
282
+ // interactions: [{ type: 'element-active' }],
283
+ // 图例相关
284
+
290
285
// 平滑曲线
291
286
smooth: false ,
292
287
animation: false ,
@@ -334,6 +329,7 @@ const createChart = (dom, data, config = {}, zoom = false) => {
334
329
*/
335
330
const format = (data ) => {
336
331
// 如果source的长度小于1,抛出错误
332
+ console .log (data)
337
333
if (source .length < 1 ) throw new Error (' source length must be greater than 1' )
338
334
// 新的数据,遍历得到
339
335
const d = []
@@ -352,7 +348,15 @@ const format = (data) => {
352
348
d .sort ((a , b ) => a[xField] - b[xField])
353
349
// console.log('d', d)
354
350
// console.log('data', data)
355
- // 如果source的长度大于1,需要设置seriesField
351
+ // 依据source生成legend,排序依据是source,数据结构:[{name, color}]
352
+ const items = []
353
+ for (const s of source) {
354
+ if (props .chart .error && props .chart .error [s]) continue
355
+ if (! data[s]) continue
356
+ items .push ({ name: s, color: colors .getSeriesColor (s, source .indexOf (s)), experiment_id: data[s].experiment_id })
357
+ }
358
+ legend .value = items
359
+ // console.log('legend', legend.value)
356
360
return { d, config: mutli ? { seriesField } : { color: colors[0 ] } }
357
361
}
358
362
@@ -627,6 +631,22 @@ const restoreByTagLinkage = (zoom, tag, color) => {
627
631
restoreByTag (plot, zoom, tag, color)
628
632
}
629
633
634
+ // ---------------------------------- 图例渲染,在这保存数据,传给legend组件 ----------------------------------
635
+
636
+ const legend = ref (null )
637
+
638
+ const legendHoverin = (item , zoom ) => {
639
+ // console.log('mouseenter', item)
640
+ const plot = zoom ? zoomChartObj : chartObj
641
+ thickenByTag (plot, zoom, item .name , item .color )
642
+ }
643
+
644
+ const legendHoverout = (item , zoom ) => {
645
+ // console.log('mouseleave', item)
646
+ const plot = zoom ? zoomChartObj : chartObj
647
+ restoreByTag (plot, zoom, item .name , item .color )
648
+ }
649
+
630
650
// ---------------------------------- 暴露api ----------------------------------
631
651
defineExpose ({
632
652
render,
@@ -639,72 +659,4 @@ defineExpose({
639
659
})
640
660
< / script>
641
661
642
- < style lang= " scss" >
643
- .lc - tooltip {
644
- @apply py- 2 px- 3 absolute bg- default border rounded;
645
- box- shadow: rgba (21 , 24 , 31 , 0.16 ) 0px 12px 24px 0px ;
646
- visibility: visible;
647
- p {
648
- @apply text- xs text- default font- semibold;
649
- }
650
- .lc - tooltip- item- no- zoom,
651
- .lc - tooltip- item- zoom {
652
- @apply flex items- center gap- 3 ;
653
- & : not (: last - child ) {
654
- @apply mb- 1.5 ;
655
- }
656
- .lc - tooltip- color {
657
- @apply w- 5 flex items- center;
658
- }
659
- .lc - tooltip- color- rect {
660
- & :: before {
661
- content: ' ' ;
662
- display: inline- block;
663
- width: 20px ;
664
- height: 6px ;
665
- border- radius: 2px ;
666
- margin- right: 5px ;
667
- background- color: currentColor;
668
- }
669
- }
670
- }
671
- .lc - tooltip- tip {
672
- @apply font- normal text- dimmest text- xs;
673
- }
674
- }
675
-
676
- .lc - tooltip- item- no- zoom {
677
- .lc - tooltip- step {
678
- @apply font- semibold;
679
- & :: after {
680
- content: ' :' ;
681
- @apply font- semibold;
682
- }
683
- }
684
- .lc - tooltip- value {
685
- @apply w- 10 text- left font- semibold;
686
- }
687
- .lc - tooltip- tag {
688
- @apply truncate;
689
- max- width: 128px ;
690
- }
691
- }
692
-
693
- .lc - tooltip- item- zoom {
694
- .lc - tooltip- step {
695
- @apply w- 7 ;
696
- }
697
- .lc - tooltip- value {
698
- @apply col- span- 1 ;
699
- @apply w- 10 text- left;
700
- }
701
- .lc - tooltip- time {
702
- @apply w- 28 ;
703
- }
704
-
705
- .lc - tooltip- tag {
706
- @apply truncate;
707
- max- width: 160px ;
708
- }
709
- }
710
- < / style>
662
+ < style lang= " scss" >< / style>
0 commit comments