|
@@ -4,6 +4,7 @@
|
|
|
<script>
|
|
|
import DataSet from '@antv/data-set'
|
|
|
import { Chart } from '@antv/g2'
|
|
|
+const ds = new DataSet()
|
|
|
export default {
|
|
|
props: {
|
|
|
id: {
|
|
@@ -24,15 +25,14 @@ export default {
|
|
|
},
|
|
|
data() {
|
|
|
return {
|
|
|
- chart: null,
|
|
|
- ds: null
|
|
|
+ chart: null
|
|
|
}
|
|
|
},
|
|
|
watch: {
|
|
|
option: {
|
|
|
handler(newValue, oldValue) {
|
|
|
this.$nextTick(() => {
|
|
|
- this.changeChart()
|
|
|
+ this.initChart()
|
|
|
})
|
|
|
},
|
|
|
deep: true
|
|
@@ -40,87 +40,36 @@ export default {
|
|
|
data: {
|
|
|
handler(newValue, oldValue) {
|
|
|
this.$nextTick(() => {
|
|
|
- this.changeChart()
|
|
|
+ this.initChart()
|
|
|
})
|
|
|
},
|
|
|
deep: true
|
|
|
}
|
|
|
},
|
|
|
mounted() {
|
|
|
- this.initChart1()
|
|
|
+ this.initChart()
|
|
|
},
|
|
|
methods: {
|
|
|
initChart() {
|
|
|
- fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/flare.json')
|
|
|
- .then((res) => res.json())
|
|
|
- .then((data) => {
|
|
|
- const ds = new DataSet()
|
|
|
- const dv = ds.createView().source(data, {
|
|
|
- type: 'hierarchy'
|
|
|
- })
|
|
|
- dv.transform({
|
|
|
- type: 'hierarchy.partition'
|
|
|
- })
|
|
|
- const chart = new Chart({
|
|
|
- container: this.id,
|
|
|
- autoFit: true,
|
|
|
- height: 500,
|
|
|
- padding: 0
|
|
|
- })
|
|
|
- chart.data(
|
|
|
- dv.getAllNodes().map((node) => ({
|
|
|
- name: node.data.name,
|
|
|
- value: node.value,
|
|
|
- depth: node.depth,
|
|
|
- x: node.x,
|
|
|
- y: node.y
|
|
|
- }))
|
|
|
- )
|
|
|
- console.log(dv.getAllNodes().map((node) => ({
|
|
|
- name: node.data.name,
|
|
|
- value: node.value,
|
|
|
- depth: node.depth,
|
|
|
- x: node.x,
|
|
|
- y: node.y
|
|
|
- })))
|
|
|
- chart.scale({
|
|
|
- x: { nice: true },
|
|
|
- y: { nice: true }
|
|
|
- })
|
|
|
- chart.tooltip({
|
|
|
- showTitle: false,
|
|
|
- showMarkers: false
|
|
|
- })
|
|
|
- chart.axis(false)
|
|
|
- chart.legend(false)
|
|
|
- chart
|
|
|
- .polygon()
|
|
|
- .position('x*y')
|
|
|
- .color('name')
|
|
|
- chart.interaction('element-active')
|
|
|
- chart.render()
|
|
|
- })
|
|
|
- },
|
|
|
- initChart1() {
|
|
|
- const ds = new DataSet()
|
|
|
+ this.chart && this.chart.destroy()
|
|
|
const dv = ds.createView().source(this.data, {
|
|
|
type: 'hierarchy'
|
|
|
})
|
|
|
dv.transform({
|
|
|
type: 'hierarchy.partition'
|
|
|
})
|
|
|
- const chart = new Chart({
|
|
|
+ this.chart = new Chart({
|
|
|
container: this.id,
|
|
|
autoFit: true,
|
|
|
height: 400,
|
|
|
padding: 0
|
|
|
})
|
|
|
- chart.data(
|
|
|
+ this.chart.data(
|
|
|
dv.getAllNodes().map((node) => {
|
|
|
if (node) {
|
|
|
return {
|
|
|
name: node.data.name,
|
|
|
- value: node.data.count,
|
|
|
+ value: node.data.value,
|
|
|
depth: node.depth,
|
|
|
x: node.x,
|
|
|
y: node.y
|
|
@@ -128,49 +77,43 @@ export default {
|
|
|
}
|
|
|
})
|
|
|
)
|
|
|
- console.log(dv.getAllNodes().map((node) => {
|
|
|
- if (node) {
|
|
|
- return {
|
|
|
- name: node.data.name,
|
|
|
- value: node.data.count,
|
|
|
- depth: node.depth,
|
|
|
- x: node.x,
|
|
|
- y: node.y
|
|
|
- }
|
|
|
- }
|
|
|
- }))
|
|
|
- chart.scale({
|
|
|
+ this.chart.scale({
|
|
|
x: { nice: true },
|
|
|
y: { nice: true }
|
|
|
})
|
|
|
- chart.tooltip({
|
|
|
+ this.chart.tooltip({
|
|
|
showTitle: false,
|
|
|
showMarkers: false
|
|
|
})
|
|
|
- chart.axis(false)
|
|
|
- chart.legend(false)
|
|
|
- chart
|
|
|
+ this.chart.axis(false)
|
|
|
+ this.chart.legend(false)
|
|
|
+ this.chart
|
|
|
.polygon()
|
|
|
.position('x*y')
|
|
|
.color('name')
|
|
|
- chart.interaction('element-active')
|
|
|
- chart.render()
|
|
|
- },
|
|
|
- changeChart() {
|
|
|
- console.log(this.data)
|
|
|
- const dv = this.ds.createView().source(this.data, {
|
|
|
- type: 'hierarchy'
|
|
|
- })
|
|
|
- dv.transform({
|
|
|
- type: 'hierarchy.partition'
|
|
|
- })
|
|
|
- this.chart.changeData(dv.getAllNodes().map((node) => ({
|
|
|
- name: node.data.name,
|
|
|
- value: node.count,
|
|
|
- depth: node.depth,
|
|
|
- x: node.x,
|
|
|
- y: node.y
|
|
|
- })))
|
|
|
+ .tooltip('name*value', function(name, value) {
|
|
|
+ return {
|
|
|
+ name,
|
|
|
+ value
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .label(
|
|
|
+ 'name',
|
|
|
+ {
|
|
|
+ offset: 0,
|
|
|
+ style: {
|
|
|
+ textBaseline: 'middle',
|
|
|
+ fill: '#000',
|
|
|
+ shadowBlur: 10,
|
|
|
+ shadowColor: '#fff'
|
|
|
+ },
|
|
|
+ layout: {
|
|
|
+ type: 'limit-in-shape'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ )
|
|
|
+ this.chart.interaction('element-active')
|
|
|
+ this.chart.render()
|
|
|
}
|
|
|
}
|
|
|
}
|