You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
286 lines
8.2 KiB
JavaScript
286 lines
8.2 KiB
JavaScript
//TODO: eitehr drastically clean up or deprecate this model
|
|
nv.models.ohlcBar = function() {
|
|
var margin = {top: 0, right: 0, bottom: 0, left: 0},
|
|
width = 960,
|
|
height = 500,
|
|
id = Math.floor(Math.random() * 10000), //Create semi-unique ID in case user doesn't select one
|
|
getX = function(d) { return d.x },
|
|
getY = function(d) { return d.y },
|
|
getOpen = function(d) { return d.open },
|
|
getClose = function(d) { return d.close },
|
|
getHigh = function(d) { return d.high },
|
|
getLow = function(d) { return d.low },
|
|
forceX = [],
|
|
forceY = [],
|
|
clipEdge = true,
|
|
color = d3.scale.category20().range(),
|
|
xDomain, yDomain;
|
|
|
|
var x = d3.scale.linear(),
|
|
y = d3.scale.linear(),
|
|
xAxis = d3.svg.axis().scale(x).orient('bottom'),
|
|
yAxis = d3.svg.axis().scale(y).orient('left'),
|
|
dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout');
|
|
|
|
|
|
function chart(selection) {
|
|
selection.each(function(data) {
|
|
var availableWidth = width - margin.left - margin.right,
|
|
availableHeight = height - margin.top - margin.bottom;
|
|
|
|
|
|
x .domain(xDomain || d3.extent(data[0].values.map(getX).concat(forceX) ))
|
|
.range([0, availableWidth]);
|
|
|
|
y .domain(yDomain || d3.extent(data[0].values.map(getY).concat(forceY) ))
|
|
.range([availableHeight, 0]);
|
|
|
|
// If scale's domain don't have a range, slightly adjust to make one... so a chart can show a single data point
|
|
if (x.domain()[0] === x.domain()[1] || y.domain()[0] === y.domain()[1]) singlePoint = true;
|
|
if (x.domain()[0] === x.domain()[1])
|
|
x.domain()[0] ?
|
|
x.domain([x.domain()[0] - x.domain()[0] * 0.01, x.domain()[1] + x.domain()[1] * 0.01])
|
|
: x.domain([-1,1]);
|
|
|
|
if (y.domain()[0] === y.domain()[1])
|
|
y.domain()[0] ?
|
|
y.domain([y.domain()[0] + y.domain()[0] * 0.01, y.domain()[1] - y.domain()[1] * 0.01])
|
|
: y.domain([-1,1]);
|
|
|
|
|
|
var parent = d3.select(this)
|
|
.on('click', function(d,i) {
|
|
dispatch.chartClick({
|
|
data: d,
|
|
index: i,
|
|
pos: d3.event,
|
|
id: id
|
|
});
|
|
});
|
|
|
|
|
|
var wrap = d3.select(this).selectAll('g.wrap.historicalStock').data([data[0].values]);
|
|
var wrapEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 historicalStock');
|
|
var gEnter = wrapEnter.append('g');
|
|
|
|
gEnter.append('g').attr('class', 'ticks');
|
|
|
|
|
|
wrap.attr('width', width)
|
|
.attr('height', height);
|
|
|
|
var g = wrap.select('g')
|
|
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
|
|
|
|
|
wrapEnter.append('defs').append('clipPath')
|
|
.attr('id', 'chart-clip-path-' + id)
|
|
.append('rect');
|
|
wrap.select('#chart-clip-path-' + id + ' rect')
|
|
.attr('width', availableWidth)
|
|
.attr('height', availableHeight);
|
|
|
|
gEnter
|
|
.attr('clip-path', clipEdge ? 'url(#chart-clip-path-' + id + ')' : '');
|
|
|
|
var shiftWrap = gEnter.append('g').attr('class', 'shiftWrap');
|
|
|
|
|
|
|
|
var ticks = wrap.select('.ticks').selectAll('.tick')
|
|
.data(function(d) { return d });
|
|
|
|
ticks.exit().remove();
|
|
|
|
|
|
var ticksEnter = ticks.enter().append('path')
|
|
.attr('class', function(d,i) { return getOpen(d,i) > getClose(d,i) ? 'tick negative' : 'tick positive'})
|
|
//.attr('fill', function(d,i) { return color[0]; })
|
|
//.attr('stroke', function(d,i) { return color[0]; })
|
|
//.attr('x', 0 )
|
|
//.attr('y', function(d,i) { return y(Math.max(0, getY(d,i))) })
|
|
//.attr('height', function(d,i) { return Math.abs(y(getY(d,i)) - y(0)) })
|
|
.on('mouseover', function(d,i) {
|
|
d3.select(this).classed('hover', true);
|
|
dispatch.elementMouseover({
|
|
point: d,
|
|
series: data[0],
|
|
pos: [x(getX(d,i)), y(getY(d,i))], // TODO: Figure out why the value appears to be shifted
|
|
pointIndex: i,
|
|
seriesIndex: 0,
|
|
e: d3.event
|
|
});
|
|
|
|
})
|
|
.on('mouseout', function(d,i) {
|
|
d3.select(this).classed('hover', false);
|
|
dispatch.elementMouseout({
|
|
point: d,
|
|
series: data[0],
|
|
pointIndex: i,
|
|
seriesIndex: 0,
|
|
e: d3.event
|
|
});
|
|
})
|
|
.on('click', function(d,i) {
|
|
dispatch.elementClick({
|
|
//label: d[label],
|
|
value: getY(d,i),
|
|
data: d,
|
|
index: i,
|
|
pos: [x(getX(d,i)), y(getY(d,i))],
|
|
e: d3.event,
|
|
id: id
|
|
});
|
|
d3.event.stopPropagation();
|
|
})
|
|
.on('dblclick', function(d,i) {
|
|
dispatch.elementDblClick({
|
|
//label: d[label],
|
|
value: getY(d,i),
|
|
data: d,
|
|
index: i,
|
|
pos: [x(getX(d,i)), y(getY(d,i))],
|
|
e: d3.event,
|
|
id: id
|
|
});
|
|
d3.event.stopPropagation();
|
|
});
|
|
|
|
ticks
|
|
.attr('class', function(d,i) { return getOpen(d,i) > getClose(d,i) ? 'tick negative' : 'tick positive'})
|
|
.attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',' + y(getHigh(d,i)) + ')'; })
|
|
.attr('d', function(d,i) {
|
|
var w = (availableWidth / data[0].values.length) * .9;
|
|
//nv.log(this, getOpen(d,i), getClose(d,i), y(getOpen(d,i)), y(getClose(d,i)));
|
|
return 'm0,0l0,' + (y(getOpen(d,i)) - y(getHigh(d,i))) + 'l' + (-w/2) + ',0l' + (w/2) + ',0l0,' + (y(getLow(d,i)) - y(getOpen(d,i))) +'l0,'+ (y(getClose(d,i)) - y(getLow(d,i))) +'l' + (w/2) + ',0l' + (-w/2) + ',0z';
|
|
})
|
|
//.attr('width', (availableWidth / data[0].values.length) * .9 )
|
|
|
|
|
|
//d3.transition(ticks)
|
|
//.attr('y', function(d,i) { return y(Math.max(0, getY(d,i))) })
|
|
//.attr('height', function(d,i) { return Math.abs(y(getY(d,i)) - y(0)) });
|
|
//.order(); // not sure if this makes any sense for this model
|
|
|
|
});
|
|
|
|
return chart;
|
|
}
|
|
|
|
|
|
chart.dispatch = dispatch;
|
|
|
|
chart.x = function(_) {
|
|
if (!arguments.length) return getX;
|
|
getX = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.y = function(_) {
|
|
if (!arguments.length) return getY;
|
|
getY = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.open = function(_) {
|
|
if (!arguments.length) return getOpen;
|
|
getOpen = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.close = function(_) {
|
|
if (!arguments.length) return getClose;
|
|
getClose = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.high = function(_) {
|
|
if (!arguments.length) return getHigh;
|
|
getHigh = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.low = function(_) {
|
|
if (!arguments.length) return getLow;
|
|
getLow = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.margin = function(_) {
|
|
if (!arguments.length) return margin;
|
|
margin = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.width = function(_) {
|
|
if (!arguments.length) return width;
|
|
width = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.height = function(_) {
|
|
if (!arguments.length) return height;
|
|
height = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.xScale = function(_) {
|
|
if (!arguments.length) return x;
|
|
x = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.yScale = function(_) {
|
|
if (!arguments.length) return y;
|
|
y = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.xDomain = function(_) {
|
|
if (!arguments.length) return xDomain;
|
|
xDomain = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.yDomain = function(_) {
|
|
if (!arguments.length) return yDomain;
|
|
yDomain = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.forceX = function(_) {
|
|
if (!arguments.length) return forceX;
|
|
forceX = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.forceY = function(_) {
|
|
if (!arguments.length) return forceY;
|
|
forceY = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.clipEdge = function(_) {
|
|
if (!arguments.length) return clipEdge;
|
|
clipEdge = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.color = function(_) {
|
|
if (!arguments.length) return color;
|
|
color = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.id = function(_) {
|
|
if (!arguments.length) return id;
|
|
id = _;
|
|
return chart;
|
|
};
|
|
|
|
|
|
|
|
return chart;
|
|
}
|