Skip to content

Commit 9025539

Browse files
committed
feat: add tag-cloud component
1 parent 5d44c0c commit 9025539

File tree

5 files changed

+142
-2
lines changed

5 files changed

+142
-2
lines changed

src/components/Charts/TagCloud.vue

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<template>
2+
<v-chart :width="width" :height="height" :padding="[0]" :data="data" :scale="scale">
3+
<v-tooltip :show-title="false" />
4+
<v-coord type="rect" direction="TL" />
5+
<v-point position="x*y" color="category" shape="cloud" tooltip="value*category" />
6+
</v-chart>
7+
</template>
8+
9+
<script>
10+
import { registerShape } from 'viser-vue'
11+
const DataSet = require('@antv/data-set')
12+
13+
const imgUrl = 'https://gw.alipayobjects.com/zos/rmsportal/gWyeGLCdFFRavBGIDzWk.png'
14+
15+
const scale = [
16+
{ dataKey: 'x', nice: false },
17+
{ dataKey: 'y', nice: false }
18+
]
19+
20+
registerShape('point', 'cloud', {
21+
draw (cfg, container) {
22+
return container.addShape('text', {
23+
attrs: {
24+
fillOpacity: cfg.opacity,
25+
fontSize: cfg.origin._origin.size,
26+
rotate: cfg.origin._origin.rotate,
27+
text: cfg.origin._origin.text,
28+
textAlign: 'center',
29+
fontFamily: cfg.origin._origin.font,
30+
fill: cfg.color,
31+
textBaseline: 'Alphabetic',
32+
...cfg.style,
33+
x: cfg.x,
34+
y: cfg.y
35+
}
36+
})
37+
}
38+
})
39+
40+
export default {
41+
name: 'TagCloud',
42+
props: {
43+
tagList: {
44+
type: Array,
45+
required: true
46+
},
47+
height: {
48+
type: Number,
49+
default: 400
50+
},
51+
width: {
52+
type: Number,
53+
default: 640
54+
}
55+
},
56+
data () {
57+
return {
58+
data: [],
59+
scale
60+
}
61+
},
62+
watch: {
63+
tagList: function (val) {
64+
if (val.length > 0) {
65+
this.initTagCloud(val)
66+
}
67+
}
68+
},
69+
mounted () {
70+
if (this.tagList.length > 0) {
71+
this.initTagCloud(this.tagList)
72+
}
73+
},
74+
methods: {
75+
initTagCloud (dataSource) {
76+
const { height, width } = this
77+
78+
const dv = new DataSet.View().source(dataSource)
79+
const range = dv.range('value')
80+
const min = range[0]
81+
const max = range[1]
82+
const imageMask = new Image()
83+
imageMask.crossOrigin = ''
84+
imageMask.src = imgUrl
85+
imageMask.onload = () => {
86+
dv.transform({
87+
type: 'tag-cloud',
88+
fields: ['name', 'value'],
89+
size: [width, height],
90+
imageMask,
91+
font: 'Verdana',
92+
padding: 0,
93+
timeInterval: 5000, // max execute time
94+
rotate () {
95+
let random = ~~(Math.random() * 4) % 4
96+
if (random === 2) {
97+
random = 0
98+
}
99+
return random * 90 // 0, 90, 270
100+
},
101+
fontSize (d) {
102+
if (d.value) {
103+
return ((d.value - min) / (max - min)) * (32 - 8) + 8
104+
}
105+
return 0
106+
}
107+
})
108+
this.data = dv.rows
109+
}
110+
}
111+
}
112+
}
113+
</script>

src/components/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import MiniProgress from '@/components/Charts/MiniProgress'
99
import Radar from '@/components/Charts/Radar'
1010
import RankList from '@/components/Charts/RankList'
1111
import TransferBar from '@/components/Charts/TransferBar'
12+
import TagCloud from '@/components/Charts/TagCloud'
1213

1314
// pro components
1415
import AvatarList from '@/components/AvatarList'
@@ -35,6 +36,7 @@ export {
3536
MiniBar,
3637
MiniProgress,
3738
Radar,
39+
TagCloud,
3840
RankList,
3941
TransferBar,
4042
Trend,

src/mock/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ if (process.env.NODE_ENV !== 'production' || process.env.VUE_APP_PREVIEW === 'tr
99
require('./services/user')
1010
require('./services/manage')
1111
require('./services/other')
12+
require('./services/tagCloud')
1213

1314
Mock.setup({
1415
timeout: 800 // setter delay time

src/mock/services/tagCloud.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/views/Home.vue

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@
134134
<description-list-item term="描述">这段描述很长很长很长很长很长很长很长很长很长很长很长很长很长很长...</description-list-item>
135135
</description-list>
136136
</a-card>
137+
138+
<a-divider> TagCloud </a-divider>
139+
<a-card style="margin-bottom: 3rem">
140+
<tag-cloud :tag-list="tagCloudData"></tag-cloud>
141+
</a-card>
137142
</div>
138143
</template>
139144

@@ -146,7 +151,7 @@ import CountDown from '@/components/CountDown/CountDown'
146151
import Ellipsis from '@/components/Ellipsis'
147152
import NumberInfo from '@/components/NumberInfo'
148153
import TagSelect from '@/components/TagSelect'
149-
import { DescriptionList } from '@/components/'
154+
import { DescriptionList, TagCloud } from '@/components/'
150155
151156
const AvatarListItem = AvatarList.AvatarItem
152157
const TagSelectOption = TagSelect.Option
@@ -164,14 +169,19 @@ export default {
164169
AvatarListItem,
165170
TagSelect,
166171
TagSelectOption,
172+
TagCloud,
167173
DescriptionList,
168174
DescriptionListItem
169175
},
170176
data () {
171177
return {
172-
targetTime: new Date().getTime() + 3900000
178+
targetTime: new Date().getTime() + 3900000,
179+
tagCloudData: []
173180
}
174181
},
182+
created () {
183+
this.getTagCloudData()
184+
},
175185
methods: {
176186
onEndHandle () {
177187
this.$message.success('CountDown callback!!!')
@@ -181,6 +191,11 @@ export default {
181191
message: 'Notification Title',
182192
description: 'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
183193
})
194+
},
195+
getTagCloudData () {
196+
this.$http.get('/data/antv/tag-cloud').then(res => {
197+
this.tagCloudData = res.result
198+
})
184199
}
185200
}
186201
}

0 commit comments

Comments
 (0)