Update
New screenshot showing the svgHeight
exists in this
in the _data
key.
I have one component in Vue, and I want to draw two rectangles using d3. I try to set the x and y attribute of the rect element using a callback method defined in the Vue component.
But I can not access the data property set for Vue component inside this callback.
Here is my component, I am getting confused further because when I hit the debugger and does console.log(this.svgHeight)
in the Chrome DevTools console directly, it does log the svgHeight
defined in the data.
<template>
<v-container class="travel-pattern">
<v-layout>
<v-flex xs12 id='svg-container'>
</v-flex>
</v-layout>
</v-container>
</template>
<script>
/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */
export default {
name: 'travel-pattern',
data () {
return {
msg: 'Travel Pattern component',
dataset: [{h: 50, w: 100}, {h: 80, w: 200}],
svgHeight: 100,
svgWidth: 500
}
},
methods: {
getRectHeight: d => {
return d.h
},
getRectWidth: d => {
return d.w
},
getRectX: (d, i) => {
return (i * d.w) + 25
},
getRectY: function () {
// return 50
debugger
let x = this.svgHeight // here x gets undefined.
return (x)
},
getClickEvent: d => {
debugger
}
},
mounted () {
// 1. Draw two rectangles
// 2. Each rectangle can be clicked
// 3. Once clicked a pop up will appear with a text field
// 4. Entering a color in the text field will change the other rectangles color
// Create an SVG element
var svg = d3.select('#svg-container')
.append('svg')
.attr('height', this.svgHeight)
.attr('width', this.svgWidth)
// Create a rectangle for each dataset
var rectangles =
svg.selectAll('rect')
.data(this.dataset)
.enter()
.append('rect')
// Actually draw the rectangles
rectangles.attr('x', this.getRectX)
.attr('y', this.getRectY)
.attr('width', this.getRectWidth)
.attr('height', this.getRectHeight)
rectangles.on('click', this.getClickEvent)
}
}
</script>