Vue.js

2016-12-09 22 views
5

के लिए D3 को कैसे कार्यान्वित करें प्रतिक्रिया के साथ डी 3 के विभिन्न कार्यान्वयन हैं। अधिक दिलचस्प लोगों में से एक प्रतिक्रिया-अशुद्ध-डोम प्रोजेक्ट का उपयोग करता है। इस दृष्टिकोण के लाभ यह है कि प्रतिक्रिया डी 3 द्वारा बनाए गए डीओएम तत्वों और आइसोमोर्फिक चार्ट बनाने की क्षमता के बारे में जानता है।Vue.js

निम्नलिखित को देखें:

क्या यह एक ही लाभ के साथ Vue.js में डी 3 को लागू करने के लिए ले जाएगा?

क्या प्रतिक्रिया-गलत-डोम के समान कुछ बनाने की आवश्यकता है या क्या आपके पास पहले से कुछ ऐसा है जिसका उपयोग इस के लिए किया जा सकता है?

इस दृष्टिकोण को वू के वास्तुकला पर विचार करने (या नहीं) कैसे समझता है?

उत्तर

7

संस्करण 4 के बाद से, डी 3 अत्यधिक मॉड्यूलरकृत है और कम्प्यूटेशनल भागों को छोटे librairies, जैसे d3-force में अलग किया गया है। मुझे पसंद है कि एक दृष्टिकोण Vue.js को डोम हेरफेर और घटनाओं को संभालने देना है, और computations के लिए d3.js का उपयोग करना है। आपका विज़ुअलाइजेशन घटक आपके अन्य घटकों के समान है, और Vue.js से परिचित किसी के लिए समझना आसान है लेकिन d3.js. नहीं।

एचटीएमएल:

<div id="app"> 
    <svg xmlns="http://www.w3.org/2000/svg" :width="width+'px'" :height="height+'px'" @mousemove="drag($event)" @mouseup="drop()" v-if="bounds.minX"> 
    <line v-for="link in graph.links" :x1="coords[link.source.index].x" :y1="coords[link.source.index].y" :x2="coords[link.target.index].x" :y2="coords[link.target.index].y" stroke="black" stroke-width="2"/> 
    <circle v-for="(node, i) in graph.nodes" :cx="coords[i].x" :cy="coords[i].y" :r="20" :fill="colors[Math.ceil(Math.sqrt(node.index))]" stroke="white" stroke-width="1" @mousedown="currentMove = {x: $event.screenX, y: $event.screenY, node: node}"/> 
    </svg> 
</div> 

जावास्क्रिप्ट:

new Vue({ 
    el: '#app', 
    data: { 
    graph: { 
     nodes: d3.range(100).map(i => ({ index: i, x: null, y: null })), 
     links: d3.range(99).map(i => ({ source: Math.floor(Math.sqrt(i)), target: i + 1 })) 
    }, 
    width: Math.max(document.documentElement.clientWidth, window.innerWidth || 0), 
    height: Math.max(document.documentElement.clientHeight, window.innerHeight || 0) - 40, 
    padding: 20, 
    colors: ['#2196F3', '#E91E63', '#7E57C2', '#009688', '#00BCD4', '#EF6C00', '#4CAF50', '#FF9800', '#F44336', '#CDDC39', '#9C27B0'], 
    simulation: null, 
    currentMove: null 
    }, 
    computed: { 
    bounds() { 
     return { 
     minX: Math.min(...this.graph.nodes.map(n => n.x)), 
     maxX: Math.max(...this.graph.nodes.map(n => n.x)), 
     minY: Math.min(...this.graph.nodes.map(n => n.y)), 
     maxY: Math.max(...this.graph.nodes.map(n => n.y)) 
     } 
    }, 
    coords() { 
     return this.graph.nodes.map(node => { 
     return { 
      x: this.padding + (node.x - this.bounds.minX) * (this.width - 2*this.padding)/(this.bounds.maxX - this.bounds.minX), 
      y: this.padding + (node.y - this.bounds.minY) * (this.height - 2*this.padding)/(this.bounds.maxY - this.bounds.minY) 
     } 
     }) 
    } 
    }, 
    created(){ 
    this.simulation = d3.forceSimulation(this.graph.nodes) 
     .force('charge', d3.forceManyBody().strength(d => -100)) 
     .force('link', d3.forceLink(this.graph.links)) 
     .force('x', d3.forceX()) 
     .force('y', d3.forceY()) 
    }, 
    methods: { 
    drag(e) { 
     if (this.currentMove) { 
     this.currentMove.node.fx = this.currentMove.node.x - (this.currentMove.x - e.screenX) * (this.bounds.maxX - this.bounds.minX)/(this.width - 2 * this.padding) 
     this.currentMove.node.fy = this.currentMove.node.y -(this.currentMove.y - e.screenY) * (this.bounds.maxY - this.bounds.minY)/(this.height - 2 * this.padding) 
     this.currentMove.x = e.screenX 
     this.currentMove.y = e.screenY 
     } 
    }, 
    drop(){ 
     delete this.currentMove.node.fx 
     delete this.currentMove.node.fy  
     this.currentMove = null 
     this.simulation.alpha(1) 
     this.simulation.restart() 
    } 
    } 
}) 

मुख्य दोष यह है मैं देख रहा हूँ है अगर आप एक बड़े d3 है

मैं एक बल ग्राफ कार्यान्वयन को दिखाने के लिए एक codepen बनाया। जेएस कोडबेस आप अपने Vue.js एप्लिकेशन में पुन: उपयोग करना चाहते हैं क्योंकि आपको इसे फिर से लिखना होगा। आपको शुद्ध d3.js वाक्यविन्यास में बहुत सारे उदाहरण भी लिखे जाएंगे और आपको उन्हें अनुकूलित करना होगा।

 संबंधित मुद्दे