Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vue.js + D3.jsでグラフを描く

Vue.js + D3.jsでグラフを描く

Vue.js Tokyo v-meetup #7 LTスライド

Tatsuya Miyado

May 23, 2018
Tweet

Other Decks in Technology

Transcript

  1. ·ͣ7VFΛ࢖Θͣʹॻ͍ͯΈΔ const dataset = [ 10, 20, 5, 3, 8,

    40, 20, 35]; const w = 200, h = 100, margin = 5; const svg = d3.select(‘.graph').append('svg') .attr('width', w + margin).attr('height', h + margin); const xScale = d3.scaleLinear().domain([0, dataset.length]).range([margin, w - margin]); const yScale = d3.scaleLinear().domain([d3.min(dataset), d3.max(dataset)]).range([h - margin, margin]); const xAxis = d3.axisBottom(xScale); svg.append('g') .attr('transform', `translate(0, ${h})`) .call(xAxis); const yAxis = d3.axisLeft(yScale); svg.append('g') .attr('transform', `translate(${margin}, 0)`) .call(yAxis); const d3line = d3.line() .x((d, i) => xScale(i)) .y(d => yScale(d)) .curve(d3.curveLinear); svg.append('path') .attr(‘d', d3line(dataset)) .style('stroke-width', 2) .style('stroke', 'steelblue') .style('fill', 'none');
  2. const dataset = [ 10, 20, 5, 3, 8, 40,

    20, 35]; const w = 200, h = 100, margin = 5; const svg = d3.select(‘.graph').append('svg') .attr('width', w + margin).attr('height', h + margin); const xScale = d3.scaleLinear().domain([0, dataset.length]).range([margin, w - margin]); const yScale = d3.scaleLinear().domain([d3.min(dataset), d3.max(dataset)]).range([h - margin, margin]); const xAxis = d3.axisBottom(xScale); svg.append('g') .attr('transform', `translate(0, ${h})`) .call(xAxis); const yAxis = d3.axisLeft(yScale); svg.append('g') .attr('transform', `translate(${margin}, 0)`) .call(yAxis); const d3line = d3.line() .x((d, i) => xScale(i)) .y(d => yScale(d)) .curve(d3.curveLinear); svg.append('path') .attr(‘d', d3line(dataset)) .style('stroke-width', 2) .style('stroke', 'steelblue') .style('fill', 'none'); 9:࣠ͷඳը QBUIͷඳը 47(ཁૉͷੜ੒ ೖྗσʔλΛඳըྖҬʹ Ϛοϐϯά͢Δؔ਺ͷੜ੒ ݩͷίʔυ
  3. <template> <path :d="d" /> </template> 5FNQMBUF 1BUI computed: { line

    () { return d3.line() .x(d => this.xScale(d.x)) .y(d => this.yScale(d.y)) .curve(d3.curveLinear); }, d () { return this.line(this.data); }, }, 4DSJQU 47(ͷཁૉΛ ςϯϓϨʔτʹॻ͘ ύεͷ࠲ඪͱͳΔ Eଐੑͷ஋Λੜ੒
  4. <template> <path :d="d" /> </template> 5FNQMBUF 1BUI computed: { line

    () { return d3.line() .x(d => this.xScale(d.x)) .y(d => this.yScale(d.y)) .curve(d3.curveLinear); }, d () { return this.line(this.data); }, }, 4DSJQU ఺ͱ఺ΛͲͷΑ͏ͳۂઢͰ ิؒ͢Δ͔ͷࢦఆ
  5. computed: { xAxis () { return d3.axisBottom(this.xScale) .tickSizeInner(10) // ໨੝ઢͷ௕͞ʢ಺ଆʣ

    .tickSizeOuter(15) // ໨੝ઢͷ௕͞ʢ֎ଆʣ .tickPadding(10) // ໨੝ઢͱςΩετͷؒͷڑ཭ .ticks(4); // ໨੝ઢͷ਺ }, }, mounted () { d3.select(this.$el).call(this.xAxis); }, 9:࣠ <template> <g :transform="`translate(${position.x}, ${position.y})`" /> </template> 5FNQMBUF 4DSJQU ࣠΍໨੝ΓΛ Hཁૉʹ ඳը͢Δ
  6. computed: { xAxis () { return d3.axisBottom(this.xScale) .tickSizeInner(10) // ໨੝ઢͷ௕͞ʢ಺ଆʣ

    .tickSizeOuter(15) // ໨੝ઢͷ௕͞ʢ֎ଆʣ .tickPadding(10) // ໨੝ઢͱςΩετͷؒͷڑ཭ .ticks(4); // ໨੝ઢͷ਺ }, }, mounted () { d3.select(this.$el).call(this.xAxis); }, ͜͜ 9:࣠ <template> <g :transform="`translate(${position.x}, ${position.y})`" /> </template> 5FNQMBUF 4DSJQU ࣠ͷ໨੝Γʹ ؔ͢ΔઃఆΛߦ͏
  7. Ξχϝʔγϣϯ w 5XFFO-JUFKTΛ࢖͏ w ͋Δ஋Λࢦఆͷඵ਺Ͱɺࢦఆͷ஋·ͰঃʑʹมԽͤ͞Δ ػೳΛ࣋ͭ const hoge = {

    a: 0 }; TweenLite.to(hoge, 3, { a: 100 }); AIPHFBAͷ஋Λඵ͔͚ͯ·ͰมԽͤ͞Δ