-
Notifications
You must be signed in to change notification settings - Fork 3
/
chart.js
89 lines (73 loc) 路 2.55 KB
/
chart.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Source : http://bl.ocks.org/andredumas/ad585d6d68cf8a894207
/**
* Construct a sample chart, accepting runtime environment loaded dependencies and data. This object is not aware of
* where it runs: browser or node/V8
*
* @param d3
* @param techan
* @param csvData CSV data string
* @returns {Function}
*/
function chart(d3, techan, csvData) {
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 250 - margin.top - margin.bottom;
var parseDate = d3.timeParse("%d-%b-%y");
var x = techan.scale.financetime()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var candlestick = techan.plot.candlestick()
.xScale(x)
.yScale(y);
var accessor = candlestick.accessor();
var xAxis = d3.axisBottom(x);
var yAxis = d3.axisLeft(y);
var data = csvData.slice(0, 200).map(function (d) {
return {
date: parseDate(d.Date),
open: +d.Open,
high: +d.High,
low: +d.Low,
close: +d.Close,
volume: +d.Volume
};
}).sort(function (a, b) {
return d3.ascending(a.date, b.date);
});
return function(g) {
var svg = g.append("svg")
.attr("version", "1.1")
.attr("xmlns", "http://www.w3.org/2000/svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain(data.map(accessor.d));
y.domain(techan.scale.plot.ohlc(data, accessor).domain());
svg.append("style")
.text(".axis path,.axis line{fill:none;stroke:#000;shape-rendering:crispEdges;}path.candle{stroke:#000000;}path.candle.body{stroke-width:0;}path.candle.up{fill:#00AA00;stroke:#00AA00;}path.candle.down{fill:#FF0000;stroke:#FF0000;}")
svg.append("g")
.datum(data)
.attr("class", "candlestick")
.call(candlestick);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
}
}
// If we're in node
if(typeof module === 'object') {
// Expose the chart
module.exports = chart;
}