# Candlestick Chart

Original D3 demo at https://beta.observablehq.com/@mbostock/d3-candlestick-chart

11/2011/2712/412/1112/1812/251/11/81/151/221/292/52/122/192/263/53/123/193/264/24/94/164/234/305/7 $155$160$165$170$175$180$185$190
<template>
  <d3-cartesian class="demo" :margin="margin" :width="860" :height="450" :x="x" :y="y">
    <template #default="props">
      <d3-grid-lines orientation="Horizontal" v-bind="props"/>
      <d3-candle-sticks :data="data" v-bind="props"/>
    </template>
    <template #south="props">
      <d3-axis orientation="Bottom" :config="configX" v-bind="props"/>
    </template>
    <template #west="props">
      <d3-axis orientation="Left" :config="configY" v-bind="props"/>
    </template>
  </d3-cartesian>
</template>

<script>
import * as d3 from 'd3'
const parseDate = d3.timeParse("%Y-%m-%d")

export default {
  data () {
    return {
      margin: { top: 20, right: 20, bottom: 30, left: 40 },
      x: { type: 'Band', domain: [], config: scale => scale.padding(0.2) },
      y: { type: 'Linear', domain: [] },
      configX: null,
      configY: null,
      data: []
    }
  },
  created () {
    d3.csv('/data/appl-stock-candle.csv', d => {
      return {
        date: parseDate(d["Date"]),
        high: +d["High"],
        low: +d["Low"],
        open: +d["Open"],
        close: +d["Close"]
      }
    }).then(data => {
      data = data.slice(-120)
      this.x.domain = d3.timeDay.range(data[0].date, +data[data.length - 1].date + 1)
        .filter(d => d.getDay() !== 0 && d.getDay() !== 6)
      this.configX = axis => {
        axis.tickFormat(d3.timeFormat("%-m/%-d")),
        axis.tickValues(d3.timeMonday.every(1).range(data[0].date, data[data.length - 1].date))
      }
      this.y.domain = [d3.min(data, d => d.low), d3.max(data, d => d.high)]
      this.configY = axis => {
        axis.tickFormat(d3.format("$~f"))
        axis.tickValues(d3.scaleLinear().domain(this.y.domain).ticks())
      }
      this.data = data
    })
  }
}
</script>

<style lang="scss" scoped>
.demo /deep/ {
  .axis path {
    display: none;
  }
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65