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.
252 lines
7.3 KiB
JavaScript
252 lines
7.3 KiB
JavaScript
|
|
nv.models.sparklinePlus = function() {
|
|
|
|
//============================================================
|
|
// Public Variables with Default Settings
|
|
//------------------------------------------------------------
|
|
|
|
var sparkline = nv.models.sparkline();
|
|
|
|
var margin = {top: 15, right: 40, bottom: 3, left: 40}
|
|
, width = null
|
|
, height = null
|
|
, x
|
|
, y
|
|
, color = nv.utils.defaultColor()
|
|
, index
|
|
, paused = false
|
|
, xTickFormat = d3.format(',r')
|
|
, yTickFormat = d3.format(',.2f')
|
|
, noData = "No Data Available."
|
|
;
|
|
|
|
//============================================================
|
|
|
|
|
|
function chart(selection) {
|
|
selection.each(function(data) {
|
|
var container = d3.select(this);
|
|
|
|
var availableWidth = (width || parseInt(container.style('width')) || 960)
|
|
- margin.left - margin.right,
|
|
availableHeight = (height || parseInt(container.style('height')) || 400)
|
|
- margin.top - margin.bottom;
|
|
|
|
|
|
chart.update = function() { chart(selection) };
|
|
chart.container = this;
|
|
|
|
|
|
//------------------------------------------------------------
|
|
// Display No Data message if there's nothing to show.
|
|
|
|
if (!data || !data.length) {
|
|
var noDataText = container.selectAll('.nv-noData').data([noData]);
|
|
|
|
noDataText.enter().append('text')
|
|
.attr('class', 'nvd3 nv-noData')
|
|
.attr('dy', '-.7em')
|
|
.style('text-anchor', 'middle');
|
|
|
|
noDataText
|
|
.attr('x', margin.left + availableWidth / 2)
|
|
.attr('y', margin.top + availableHeight / 2)
|
|
.text(function(d) { return d });
|
|
|
|
return chart;
|
|
} else {
|
|
container.selectAll('.nv-noData').remove();
|
|
}
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
// Setup Scales
|
|
|
|
x = sparkline.xScale();
|
|
y = sparkline.yScale();
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
|
//------------------------------------------------------------
|
|
// Setup containers and skeleton of chart
|
|
|
|
var wrap = container.selectAll('g.nv-wrap.nv-sparklineplus').data([data]);
|
|
var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-sparklineplus');
|
|
var gEnter = wrapEnter.append('g');
|
|
var g = wrap.select('g');
|
|
|
|
gEnter.append('g').attr('class', 'nv-sparklineWrap');
|
|
gEnter.append('g').attr('class', 'nv-hoverArea');
|
|
|
|
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
|
//------------------------------------------------------------
|
|
// Main Chart Component(s)
|
|
|
|
var sparklineWrap = g.select('.nv-sparklineWrap');
|
|
|
|
sparkline
|
|
.width(availableWidth)
|
|
.height(availableHeight);
|
|
|
|
sparklineWrap
|
|
.style('stroke', function(d, i){ return d.color || color(d, i) })
|
|
.call(sparkline);
|
|
|
|
//------------------------------------------------------------
|
|
|
|
|
|
|
|
gEnter.select('.nv-hoverArea').append('rect')
|
|
.on('mousemove', sparklineHover)
|
|
.on('click', function() { paused = !paused })
|
|
.on('mouseout', function() { index = null; updateValueLine(); });
|
|
|
|
g.select('.nv-hoverArea rect')
|
|
.attr('transform', function(d) { return 'translate(' + -margin.left + ',' + -margin.top + ')' })
|
|
.attr('width', availableWidth + margin.left + margin.right)
|
|
.attr('height', availableHeight + margin.top);
|
|
|
|
// if index is not set, default to last point
|
|
//index = typeof index == 'undefined' ? data.length - 1 : index;
|
|
// if index is not set, default to null
|
|
index = typeof index == 'undefined' ? null : index;
|
|
|
|
var hoverValue = g.selectAll('.nv-hoverValue').data([index]);
|
|
|
|
var hoverG = hoverValue.enter().append('g').attr('class', 'nv-hoverValue');
|
|
|
|
var hoverLine = hoverG.append('line')
|
|
.attr('x1', 0)
|
|
.attr('y1', -margin.top)
|
|
.attr('x2', 0)
|
|
.attr('y2', availableHeight);
|
|
|
|
var hoverX = hoverG.append('text').attr('class', 'nv-xValue')
|
|
.attr('x', -6)
|
|
.attr('y', -margin.top)
|
|
.attr('text-anchor', 'end')
|
|
.attr('dy', '.9em');
|
|
|
|
var hoverY = hoverG.append('text').attr('class', 'nv-yValue')
|
|
.attr('x', 6)
|
|
.attr('y', -margin.top)
|
|
.attr('text-anchor', 'start')
|
|
.attr('dy', '.9em');
|
|
|
|
updateValueLine();
|
|
|
|
|
|
function updateValueLine() { //index is currently global (within the chart), may or may not keep it that way
|
|
if (paused) return;
|
|
|
|
hoverValue.data([index])
|
|
|
|
//d3.transition(hoverValue)
|
|
hoverValue
|
|
.transition().duration(250)
|
|
.style('stroke-opacity', function(d) { return d === null ? 0 : 1 })
|
|
.style('fill-opacity', function(d) { return d === null ? 0 : 1 });
|
|
|
|
if (index == null) return;
|
|
|
|
hoverValue
|
|
.attr('transform', function(d) { return 'translate(' + x(sparkline.x()(data[d],d)) + ',0)' })
|
|
|
|
hoverValue.select('.nv-xValue')
|
|
.text(xTickFormat(sparkline.x()(data[index], index)));
|
|
|
|
hoverValue.select('.nv-yValue')
|
|
.text(yTickFormat(sparkline.y()(data[index], index)));
|
|
}
|
|
|
|
|
|
function sparklineHover() {
|
|
if (paused) return;
|
|
|
|
var pos = d3.event.offsetX - margin.left;
|
|
|
|
function getClosestIndex(data, x) {
|
|
var distance = Math.abs(sparkline.x()(data[0], 0) - x);
|
|
var closestIndex = 0;
|
|
for (var i = 0; i < data.length; i++){
|
|
if (Math.abs(sparkline.x()(data[i], i) - x) < distance) {
|
|
distance = Math.abs(sparkline.x()(data[i], i) - x);
|
|
closestIndex = i;
|
|
}
|
|
}
|
|
return closestIndex;
|
|
}
|
|
|
|
index = getClosestIndex(data, Math.round(x.invert(pos)));
|
|
|
|
updateValueLine();
|
|
}
|
|
|
|
});
|
|
|
|
return chart;
|
|
}
|
|
|
|
|
|
//============================================================
|
|
// Expose Public Variables
|
|
//------------------------------------------------------------
|
|
|
|
// expose chart's sub-components
|
|
chart.sparkline = sparkline;
|
|
|
|
d3.rebind(chart, sparkline, 'x', 'y', 'xScale', 'yScale');
|
|
|
|
chart.margin = function(_) {
|
|
if (!arguments.length) return margin;
|
|
margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
|
|
margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
|
|
margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
|
|
margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
|
|
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.xTickFormat = function(_) {
|
|
if (!arguments.length) return xTickFormat;
|
|
xTickFormat = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.yTickFormat = function(_) {
|
|
if (!arguments.length) return yTickFormat;
|
|
yTickFormat = _;
|
|
return chart;
|
|
};
|
|
|
|
chart.noData = function(_) {
|
|
if (!arguments.length) return noData;
|
|
noData = _;
|
|
return chart;
|
|
};
|
|
|
|
//============================================================
|
|
|
|
|
|
return chart;
|
|
}
|