質問編集履歴
1
コードを記入
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,10 +2,162 @@
|
|
2
2
|
|
3
3
|
すいませんが、どなたかわかる方いたら教えて欲しいです。
|
4
4
|
|
5
|
-
|
5
|
+
<body>
|
6
6
|
|
7
|
+
<button>Update</button>
|
8
|
+
|
9
|
+
<script src="http://d3js.org/d3.v4.min.js"></script>
|
10
|
+
|
7
|
-
http://
|
11
|
+
<script src="http://techanjs.org/techan.min.js"></script>
|
12
|
+
|
13
|
+
<script>
|
8
14
|
|
9
15
|
|
10
16
|
|
17
|
+
var margin = {top: 20, right: 20, bottom: 30, left: 50},
|
18
|
+
|
19
|
+
width = 960 - margin.left - margin.right,
|
20
|
+
|
21
|
+
height = 500 - margin.top - margin.bottom;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
var parseDate = d3.timeParse("%d-%b-%y");
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
var x = techan.scale.financetime()
|
30
|
+
|
31
|
+
.range([0, width]);
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
var y = d3.scaleLinear()
|
36
|
+
|
37
|
+
.range([height, 0]);
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
var candlestick = techan.plot.candlestick()
|
42
|
+
|
43
|
+
.xScale(x)
|
44
|
+
|
11
|
-
|
45
|
+
.yScale(y);
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
var xAxis = d3.axisBottom()
|
50
|
+
|
51
|
+
.scale(x);
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
var yAxis = d3.axisLeft()
|
56
|
+
|
57
|
+
.scale(y);
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
var svg = d3.select("body").append("svg")
|
62
|
+
|
63
|
+
.attr("width", width + margin.left + margin.right)
|
64
|
+
|
65
|
+
.attr("height", height + margin.top + margin.bottom)
|
66
|
+
|
67
|
+
.append("g")
|
68
|
+
|
69
|
+
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
d3.csv("data.csv", function(error, data) {
|
74
|
+
|
75
|
+
var accessor = candlestick.accessor();
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
data = data.slice(0, 200).map(function(d) {
|
80
|
+
|
81
|
+
return {
|
82
|
+
|
83
|
+
date: parseDate(d.Date),
|
84
|
+
|
85
|
+
open: +d.Open,
|
86
|
+
|
87
|
+
high: +d.High,
|
88
|
+
|
89
|
+
low: +d.Low,
|
90
|
+
|
91
|
+
close: +d.Close,
|
92
|
+
|
93
|
+
volume: +d.Volume
|
94
|
+
|
95
|
+
};
|
96
|
+
|
97
|
+
}).sort(function(a, b) { return d3.ascending(accessor.d(a), accessor.d(b)); });
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
svg.append("g")
|
102
|
+
|
103
|
+
.attr("class", "candlestick");
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
svg.append("g")
|
108
|
+
|
109
|
+
.attr("class", "x axis")
|
110
|
+
|
111
|
+
.attr("transform", "translate(0," + height + ")");
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
svg.append("g")
|
116
|
+
|
117
|
+
.attr("class", "y axis")
|
118
|
+
|
119
|
+
.append("text")
|
120
|
+
|
121
|
+
.attr("transform", "rotate(-90)")
|
122
|
+
|
123
|
+
.attr("y", 6)
|
124
|
+
|
125
|
+
.attr("dy", ".71em")
|
126
|
+
|
127
|
+
.style("text-anchor", "end")
|
128
|
+
|
129
|
+
.text("Price ($)");
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
// Data to display initially
|
134
|
+
|
135
|
+
draw(data.slice(0, data.length-20));
|
136
|
+
|
137
|
+
// Only want this button to be active if the data has loaded
|
138
|
+
|
139
|
+
d3.select("button").on("click", function() { draw(data); }).style("display", "inline");
|
140
|
+
|
141
|
+
});
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
function draw(data) {
|
146
|
+
|
147
|
+
x.domain(data.map(candlestick.accessor().d));
|
148
|
+
|
149
|
+
y.domain(techan.scale.plot.ohlc(data, candlestick.accessor()).domain());
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
svg.selectAll("g.candlestick").datum(data).call(candlestick);
|
154
|
+
|
155
|
+
svg.selectAll("g.x.axis").call(xAxis);
|
156
|
+
|
157
|
+
svg.selectAll("g.y.axis").call(yAxis);
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
</script>
|