removed the distributions from scatter model to the scatterWithLegend.. making scatter.js perform as best as possible for use as an interactive layer

master-patched
Bob Monteverde 12 years ago
parent d988f100fa
commit 2741676827

@ -1,96 +0,0 @@
<!DOCTYPE html>
<meta charset="utf-8">
<link href="../src/d3.css" rel="stylesheet" type="text/css">
<style>
body {
overflow-y:scroll;
}
</style>
<body>
<svg id="test1"></svg>
<script src="../lib/d3.v2.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/models/legend.js"></script>
<script src="../src/models/scatter.js"></script>
<script src="../src/models/lineWithScatter.js"></script>
<script>
//Format A
nv.addGraph({
generate: function() {
var width = nv.utils.windowSize().width - 40,
height = nv.utils.windowSize().height - 40;
var chart = nv.models.line()
.width(width)
.height(height)
.margin({top: 20, right: 20, bottom: 20, left: 20})
d3.select('#test1')
.attr('width', width)
.attr('height', height)
.datum(sinAndCos())
.call(chart);
return chart;
},
callback: function(graph) {
window.onresize = function() {
var width = nv.utils.windowSize().width - 40,
height = nv.utils.windowSize().height - 40,
margin = graph.margin();
if (width < margin.left + margin.right + 20)
width = margin.left + margin.right + 20;
if (height < margin.top + margin.bottom + 20)
height = margin.top + margin.bottom + 20;
graph
.width(width)
.height(height);
d3.select('#test1')
.attr('width', width)
.attr('height', height)
.call(graph);
};
}
});
function sinAndCos() {
var sin = [],
cos = [];
for (var i = 0; i < 100; i++) {
sin.push({x: i, y: Math.sin(i/10)});
cos.push({x: i, y: .5 * Math.cos(i/10)});
}
return [
{
values: sin,
key: "Sine Wave",
color: "#ff7f0e"
},
{
values: cos,
key: "Cosine Wave",
color: "#2ca02c"
}
];
}
</script>

@ -1,138 +0,0 @@
<!DOCTYPE html>
<meta charset="utf-8">
<link href="../src/d3.css" rel="stylesheet" type="text/css">
<style>
body {
overflow-y:scroll;
}
text {
font: 12px sans-serif;
}
#chart1 {
height: 500px;
margin: 10px;
min-width: 100px;
min-height: 100px;
/*
Minimum height and width is a good idea to prevent negative SVG dimensions...
For example width should be =< margin.left + margin.right + 1,
of course 1 pixel for the entire chart would not be very useful, BUT should not have errors
*/
}
</style>
<body>
<div id="chart1">
</div>
<script src="../lib/d3.v2.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/tooltip.js"></script>
<script src="../src/models/legend.js"></script>
<script src="../src/models/axis.js"></script>
<script src="../src/models/scatter.js"></script>
<script src="../src/models/lineWithScatter.js"></script>
<script src="../src/models/lineWithLegend.js"></script>
<script>
var selector = '#chart1',
chart = nv.models.lineWithLegend(),
data = sinAndCos(),
xTickFormat = d3.format(',r'),
yTickFormat = d3.format(',.2f'),
xAxisLabel = null,
yAxisLabel = 'Voltage (v)',
duration = 500;
nv.addGraph({
generate: function() {
var container = d3.select(selector),
width = function() { return parseInt(container.style('width')) },
height = function() { return parseInt(container.style('height')) },
svg = container.append('svg');
chart
.width(width)
.height(height);
chart.xAxis
.tickFormat(xTickFormat);
chart.yAxis
.tickFormat(yTickFormat)
.axisLabel(yAxisLabel);
svg
.attr('width', width())
.attr('height', height())
.datum(data)
.transition().duration(duration).call(chart);
return chart;
},
callback: function(chart) {
var showTooltip = function(e) {
var offsetElement = document.getElementById(selector.substr(1)),
left = e.pos[0] + offsetElement.offsetLeft,
top = e.pos[1] + offsetElement.offsetTop,
formatY = chart.yAxis.tickFormat(), //Assumes using same format as axis, can customize to show higher precision, etc.
formatX = chart.xAxis.tickFormat();
// uses the chart's getX and getY, you may customize if x position is not the same as the value you want
// ex. daily data without weekends, x is the index, while you want the date
var content = '<h3>' + e.series.key + '</h3>' +
'<p>' +
formatY(chart.y()(e.point)) + ' at ' + formatX(chart.x()(e.point)) +
'</p>';
nv.tooltip.show([left, top], content);
};
chart.dispatch.on('tooltipShow', showTooltip);
chart.dispatch.on('tooltipHide', nv.tooltip.cleanup);
window.onresize= function() {
// now that width and height are functions, should be automatic..of course you can always override them
d3.select('#chart1 svg')
.attr('width', chart.width()()) //need to set SVG dimensions, chart is not aware of the SVG component
.attr('height', chart.height()())
.call(chart);
};
}
});
function sinAndCos() {
var sin = [],
cos = [];
for (var i = 0; i < 100; i++) {
sin.push({x: i, y: Math.sin(i/10)});
cos.push({x: i, y: .5 * Math.cos(i/10)});
}
return [
{
values: sin,
key: "Sine Wave",
color: "#ff7f0e"
},
{
values: cos,
key: "Cosine Wave",
color: "#2ca02c"
}
];
}
</script>

@ -60,7 +60,7 @@ nv.addGraph({
var chart = nv.models.scatterWithLegend()
.width(width)
.height(height)
.showDistX(true)
.showDistX(false)
.showDistY(true)
//.width(width)
//.height(height)
@ -119,8 +119,7 @@ nv.addGraph({
window.onresize = function() {
var width = nv.utils.windowSize().width - 40,
height = nv.utils.windowSize().height - 40,
margin = graph.margin(),
animate = graph.animate();
margin = graph.margin();
if (width < margin.left + margin.right + 20)

@ -1303,42 +1303,43 @@ nv.models.legend = function() {
return chart;
}
//TODO: consider adding axes
// -How to deal with time vs generic linear, vs any other scale?
// This is an attempt to use a scatter plot with the line plot do do point interaction
// If this performs good, will likely use as the line implementation, this way the
// point interaction using a voronoi will only be in the scatter not in the exact same code
// in both the scatter and the line
nv.models.line = function() {
//Default Settings
var margin = {top: 0, right: 0, bottom: 0, left: 0},
width = 960,
height = 500,
dotRadius = function() { return 2.5 }, //consider removing this, or making similar to scatter
color = d3.scale.category10().range(),
id = Math.floor(Math.random() * 10000), //Create semi-unique ID incase user doesn't select one
getX = function(d) { return d.x },
getY = function(d) { return d.y },
forceX = [],
forceY = [],
interactive = true,
clipEdge = false,
clipVoronoi = true,
xDomain, yDomain;
getX = function(d) { return d.x }, // accessor to get the x value from a data point
getY = function(d) { return d.y }, // accessor to get the y value from a data point
getSize = function() { return 2.5 }, // accessor to get the point radius from a data point
forceX = [], // List of numbers to Force into the X scale (ie. 0, or a max / min, etc.)
forceY = [], // List of nuumbers to Force into the Y scale
interactive = true, // If true, plots a voronoi overlay for advanced point interection
clipEdge = false, // if true, masks lines within x and y scale
clipVoronoi = true, // if true, masks each point with a circle... can turn off to slightly increase performance
xDomain, yDomain; // Used to manually set the x and y domain, good to save time if calculation has already been made
var x = d3.scale.linear(),
y = d3.scale.linear(),
dispatch = d3.dispatch('pointMouseover', 'pointMouseout'),
scatter = nv.models.scatter().size(getSize).id(id),
x0, y0,
timeoutID;
function chart(selection) {
selection.each(function(data) {
var seriesData = data.map(function(d) {
return d.values.map(function(d,i) {
return { x: getX(d,i), y: getY(d,i) }
})
}),
var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
data.map(function(d) {
return d.values.map(function(d,i) {
return { x: getX(d,i), y: getY(d,i) }
})
}),
availableWidth = width - margin.left - margin.right,
availableHeight = height - margin.top - margin.bottom;
@ -1353,140 +1354,58 @@ nv.models.line = function() {
y .domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return d.y }).concat(forceY)))
.range([availableHeight, 0]);
var wrap = d3.select(this).selectAll('g.d3line').data([data]);
var wrapEnter = wrap.enter().append('g').attr('class', 'd3line');
var defsEnter = wrapEnter.append('defs');
var gEnter = wrapEnter.append('g');
gEnter.append('g').attr('class', 'lines');
gEnter.append('g').attr('class', 'groups');
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
defsEnter.append('clipPath')
.attr('id', 'edge-clip-' + id)
.append('rect');
wrap.select('#edge-clip-' + id + ' rect')
.attr('width', availableWidth)
.attr('height', availableHeight);
gEnter
.attr('clip-path', clipEdge ? 'url(#edge-clip-' + id + ')' : null);
function updateInteractiveLayer() {
if (!interactive) {
wrap.select('#points-clip-' + id).remove();
wrap.select('.point-paths').remove();
return false;
}
gEnter.append('g').attr('class', 'point-paths');
defsEnter.append('clipPath').attr('id', 'points-clip-' + id);
var vertices = d3.merge(data.map(function(line, lineIndex) {
return line.values.map(function(point, pointIndex) {
// Adding noise to make duplicates very unlikely
// Inject series and point index for reference
// TODO: see how much time this consumes
return [x(getX(point, pointIndex)) * (Math.random() / 1e12 + 1) , y(getY(point, pointIndex)) * (Math.random() / 1e12 + 1), lineIndex, pointIndex];
})
})
);
var pointClips = wrap.select('#points-clip-' + id).selectAll('circle')
.data(vertices);
pointClips.enter().append('circle')
.attr('r', 25);
pointClips.exit().remove();
pointClips
.attr('cx', function(d) { return d[0] })
.attr('cy', function(d) { return d[1] });
wrap.select('.point-paths')
.attr('clip-path', clipVoronoi ? 'url(#points-clip-' + id + ')' : null);
//inject series and point index for reference into voronoi
// considering adding a removeZeros option, may be useful for the stacked chart and maybe others
var voronoi = d3.geom.voronoi(vertices).map(function(d,i) { return { 'data': d, 'series': vertices[i][2], 'point': vertices[i][3] } });
var pointPaths = wrap.select('.point-paths').selectAll('path')
.data(voronoi);
pointPaths.enter().append('path')
.attr('class', function(d,i) { return 'path-'+i; });
pointPaths.exit().remove();
pointPaths
.attr('d', function(d) { return 'M' + d.data.join(',') + 'Z'; })
.on('mouseover', function(d) {
var series = data[d.series],
point = series.values[d.point];
dispatch.pointMouseover({
point: point,
series:series,
pos: [x(getX(point, d.point)) + margin.left, y(getY(point, d.point)) + margin.top],
seriesIndex: d.series,
pointIndex: d.point
});
})
.on('mouseout', function(d, i) {
dispatch.pointMouseout({
point: data[d.series].values[d.point],
series: data[d.series],
seriesIndex: d.series,
pointIndex: d.point
});
});
dispatch.on('pointMouseover.point', function(d) {
wrap.select('.series-' + d.seriesIndex + ' .point-' + d.pointIndex)
.classed('hover', true);
});
dispatch.on('pointMouseout.point', function(d) {
wrap.select('.series-' + d.seriesIndex + ' .point-' + d.pointIndex)
.classed('hover', false);
});
if (clipEdge) {
defsEnter.append('clipPath')
.attr('id', 'edge-clip-' + id)
.append('rect');
wrap.select('#edge-clip-' + id + ' rect')
.attr('width', availableWidth)
.attr('height', availableHeight);
gEnter
.attr('clip-path', 'url(#edge-clip-' + id + ')');
}
var lines = wrap.select('.lines').selectAll('.line')
var groups = wrap.select('.groups').selectAll('.line')
.data(function(d) { return d }, function(d) { return d.key });
lines.enter().append('g')
groups.enter().append('g')
.style('stroke-opacity', 1e-6)
.style('fill-opacity', 1e-6);
d3.transition(lines.exit())
d3.transition(groups.exit())
.style('stroke-opacity', 1e-6)
.style('fill-opacity', 1e-6)
.remove();
lines
groups
.attr('class', function(d,i) { return 'line series-' + i })
.classed('hover', function(d) { return d.hover })
.style('fill', function(d,i){ return color[i % 10] })
.style('stroke', function(d,i){ return color[i % 10] })
d3.transition(lines)
d3.transition(groups)
.style('stroke-opacity', 1)
.style('fill-opacity', .5)
//setTimeout(interactiveLayer, 1000); //seems not to work as well as above... BUT fixes broken resize
var paths = lines.selectAll('path')
var paths = groups.selectAll('path')
.data(function(d, i) { return [d.values] });
paths.enter().append('path')
.attr('d', d3.svg.line()
.x(function(d,i) { return x0(getX(d,i)) })
.y(function(d,i) { return y0(getY(d,i)) })
);
d3.transition(lines.exit().selectAll('path'))
d3.transition(groups.exit().selectAll('path'))
.attr('d', d3.svg.line()
.x(function(d,i) { return x(getX(d,i)) })
.y(function(d,i) { return y(getY(d,i)) })
@ -1498,20 +1417,13 @@ nv.models.line = function() {
.y(function(d,i) { return y(getY(d,i)) })
);
var points = lines.selectAll('circle.point')
/*
var points = groups.selectAll('circle.point')
.data(function(d) { return d.values });
points.enter().append('circle')
.attr('cx', function(d,i) { return x0(getX(d,i)) })
.attr('cy', function(d,i) { return y0(getY(d,i)) });
/*
// I think this is redundant with below, but originally put this here for a reason
d3.transition(points.exit())
.attr('cx', function(d,i) { return x(getX(d,i)) })
.attr('cy', function(d,i) { return y(getY(d,i)) })
.remove();
*/
d3.transition(lines.exit().selectAll('circle.point'))
d3.transition(groups.exit().selectAll('circle.point'))
.attr('cx', function(d,i) { return x(getX(d,i)) })
.attr('cy', function(d,i) { return y(getY(d,i)) })
.remove();
@ -1519,11 +1431,23 @@ nv.models.line = function() {
.attr('class', function(d,i) { return 'point point-' + i })
.attr('cx', function(d,i) { return x(getX(d,i)) })
.attr('cy', function(d,i) { return y(getY(d,i)) })
.attr('r', dotRadius);
.attr('r', getSize);
*/
scatter
.width(availableWidth)
.height(availableHeight)
.xDomain(x.domain())
.yDomain(y.domain())
wrapEnter.append('g').attr('class', 'scatterWrap');
var scatterWrap = wrap.select('.scatterWrap').datum(data);
d3.transition(scatterWrap).call(scatter);
clearTimeout(timeoutID);
timeoutID = setTimeout(updateInteractiveLayer, 750);
//store old scales for use in transitions on update, to animate from old to new positions
x0 = x.copy();
@ -1535,7 +1459,9 @@ nv.models.line = function() {
}
chart.dispatch = dispatch;
chart.dispatch = scatter.dispatch;
d3.rebind(chart, scatter, 'size');
chart.x = function(_) {
if (!arguments.length) return getX;
@ -1609,15 +1535,10 @@ nv.models.line = function() {
return chart;
};
chart.dotRadius = function(_) {
if (!arguments.length) return dotRadius;
dotRadius = d3.functor(_);
return chart;
};
chart.color = function(_) {
if (!arguments.length) return color;
color = _;
scatter.color(_);
return chart;
};
@ -2242,11 +2163,8 @@ nv.models.lineWithFocus = function() {
nv.models.lineWithLegend = function() {
var margin = {top: 30, right: 20, bottom: 50, left: 60},
getWidth = function() { return 960 },
getHeight = function() { return 500 },
dotRadius = function() { return 2.5 },
getX = function(d) { return d.x },
getY = function(d) { return d.y },
width = function() { return 960 },
height = function() { return 500 },
color = d3.scale.category10().range(),
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
@ -2260,27 +2178,27 @@ nv.models.lineWithLegend = function() {
function chart(selection) {
selection.each(function(data) {
var width = getWidth(),
height = getHeight(),
availableWidth = width - margin.left - margin.right,
availableHeight = height - margin.top - margin.bottom;
var series = data.filter(function(d) { return !d.disabled })
var seriesData = data.filter(function(d) { return !d.disabled })
.map(function(d) {
return d.values.map(function(d,i) {
return { x: getX(d,i), y: getY(d,i) }
return { x: lines.x()(d,i), y: lines.y()(d,i) }
})
});
}),
availableWidth = width() - margin.left - margin.right,
availableHeight = height() - margin.top - margin.bottom;
x .domain(d3.extent(d3.merge(series), function(d) { return d.x } ))
x .domain(d3.extent(d3.merge(seriesData).map(function(d) { return d.x }).concat(lines.forceX) ))
.range([0, availableWidth]);
y .domain(d3.extent(d3.merge(series), function(d) { return d.y } ))
y .domain(d3.extent(d3.merge(seriesData).map(function(d) { return d.y }).concat(lines.forceY) ))
.range([availableHeight, 0]);
lines
.width(availableWidth)
.height(availableHeight)
.xDomain(x.domain())
.yDomain(y.domain())
.color(data.map(function(d,i) {
return d.color || color[i % 10];
}).filter(function(d,i) { return !data[i].disabled }))
@ -2344,11 +2262,11 @@ nv.models.lineWithLegend = function() {
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
legend.width(width/2 - margin.right);
legend.width(availableWidth / 2);
g.select('.legendWrap')
.datum(data)
.attr('transform', 'translate(' + (width/2 - margin.left) + ',' + (-margin.top) +')')
.attr('transform', 'translate(' + (availableWidth / 2) + ',' + (-margin.top) +')')
.call(legend);
@ -2362,7 +2280,7 @@ nv.models.lineWithLegend = function() {
xAxis
.domain(x.domain())
.range(x.range())
.ticks( width / 100 )
.ticks( availableWidth / 100 )
.tickSize(-availableHeight, 0);
g.select('.x.axis')
@ -2370,11 +2288,12 @@ nv.models.lineWithLegend = function() {
d3.transition(g.select('.x.axis'))
.call(xAxis);
yAxis
.domain(y.domain())
.range(y.range())
.ticks( height / 36 )
.tickSize(-availableWidth, 0);
.ticks( availableHeight / 36 )
.tickSize( -availableWidth, 0);
d3.transition(g.select('.y.axis'))
.call(yAxis);
@ -2384,27 +2303,14 @@ nv.models.lineWithLegend = function() {
return chart;
}
chart.dispatch = dispatch;
chart.legend = legend;
chart.xAxis = xAxis;
chart.yAxis = yAxis;
d3.rebind(chart, lines, 'interactive');
//consider rebinding x and y as well
chart.x = function(_) {
if (!arguments.length) return getX;
getX = _;
lines.x(_);
return chart;
};
d3.rebind(chart, lines, 'x', 'y', 'size', 'xDomain', 'yDomain', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id');
chart.y = function(_) {
if (!arguments.length) return getY;
getY = _;
lines.y(_);
return chart;
};
chart.margin = function(_) {
if (!arguments.length) return margin;
@ -2413,21 +2319,14 @@ nv.models.lineWithLegend = function() {
};
chart.width = function(_) {
if (!arguments.length) return getWidth;
getWidth = d3.functor(_);
if (!arguments.length) return width;
width = d3.functor(_);
return chart;
};
chart.height = function(_) {
if (!arguments.length) return getHeight;
getHeight = d3.functor(_);
return chart;
};
chart.dotRadius = function(_) {
if (!arguments.length) return dotRadius;
dotRadius = d3.functor(_);
lines.dotRadius = _;
if (!arguments.length) return height;
height = d3.functor(_);
return chart;
};
@ -2720,7 +2619,7 @@ nv.models.scatter = function() {
var x = d3.scale.linear(),
y = d3.scale.linear(),
z = d3.scale.sqrt(), //sqrt because point size is done by area, not radius
dispatch = d3.dispatch('pointMouseover', 'pointMouseout'),
dispatch = d3.dispatch('pointMouseover', 'pointMouseout'),//TODO: consider renaming to elementMouseove and elementMouseout for consistency
x0, y0, z0,
timeoutID;
@ -2775,15 +2674,18 @@ nv.models.scatter = function() {
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
defsEnter.append('clipPath')
.attr('id', 'edge-clip-' + id)
.append('rect');
wrap.select('#edge-clip-' + id + ' rect')
.attr('width', availableWidth)
.attr('height', availableHeight);
if (clipEdge) {
defsEnter.append('clipPath')
.attr('id', 'edge-clip-' + id)
.append('rect');
wrap.select('#edge-clip-' + id + ' rect')
.attr('width', availableWidth)
.attr('height', availableHeight);
gEnter
.attr('clip-path', 'url(#edge-clip-' + id + ')');
}
gEnter
.attr('clip-path', clipEdge ? 'url(#edge-clip-' + id + ')' : null);
function updateInteractiveLayer() {
@ -2794,7 +2696,6 @@ nv.models.scatter = function() {
return false;
}
defsEnter.append('clipPath').attr('id', 'points-clip-' + id);
gEnter.append('g').attr('class', 'point-paths');
var vertices = d3.merge(data.map(function(group, groupIndex) {
@ -2808,18 +2709,21 @@ nv.models.scatter = function() {
);
if (clipVoronoi) {
defsEnter.append('clipPath').attr('id', 'points-clip-' + id);
var pointClips = wrap.select('#points-clip-' + id).selectAll('circle')
.data(vertices);
pointClips.enter().append('circle')
.attr('r', 25);
pointClips.exit().remove();
pointClips
.attr('cx', function(d) { return d[0] })
.attr('cy', function(d) { return d[1] });
var pointClips = wrap.select('#points-clip-' + id).selectAll('circle')
.data(vertices);
pointClips.enter().append('circle')
.attr('r', 25);
pointClips.exit().remove();
pointClips
.attr('cx', function(d) { return d[0] })
.attr('cy', function(d) { return d[1] });
wrap.select('.point-paths')
.attr('clip-path', 'url(#points-clip-' + id + ')');
wrap.select('.point-paths')
.attr('clip-path', 'url(#points-clip-' + id + ')');
}
//inject series and point index for reference into voronoi
@ -2920,7 +2824,7 @@ nv.models.scatter = function() {
.attr('r', function(d,i) { return z(getSize(d,i)) });
// TODO: make axis distributions options... maybe even abstract out of this file
// TODO: consider abstracting Axis distributions out of this file
if (showDistX) {
var distX = groups.selectAll('line.distX')
@ -2979,24 +2883,6 @@ nv.models.scatter = function() {
chart.dispatch = dispatch;
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.x = function(_) {
if (!arguments.length) return getX;
getX = d3.functor(_);
@ -3015,6 +2901,24 @@ nv.models.scatter = function() {
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.xDomain = function(_) {
if (!arguments.length) return xDomain;
xDomain = _;
@ -3099,17 +3003,14 @@ nv.models.scatter = function() {
nv.models.scatterWithLegend = function() {
var margin = {top: 30, right: 20, bottom: 50, left: 60},
width = 960,
height = 500,
width = function() { return 960 },
height = function() { return 500 },
animate = 500,
xAxisRender = true,
yAxisRender = true,
xAxisLabelText = false,
yAxisLabelText = false,
color = d3.scale.category10().range(),
getX = function(d) { return d.x }, // or d[0]
getY = function(d) { return d.y }, // or d[1]
getSize = function(d) { return d.size }, // or d[2]
forceX = [],
forceY = [],
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
@ -3125,27 +3026,29 @@ nv.models.scatterWithLegend = function() {
function chart(selection) {
selection.each(function(data) {
var seriesData = data.filter(function(d) { return !d.disabled })
.map(function(d) { return d.values });
.map(function(d) {
return d.values.map(function(d,i) {
return { x: scatter.x()(d,i), y: scatter.y()(d,i) }
})
}),
availableWidth = width() - margin.left - margin.right,
availableHeight = height() - margin.top - margin.bottom;
x .domain(d3.extent(d3.merge(seriesData).map(getX).concat(forceX) ))
.range([0, width - margin.left - margin.right]);
x .domain(d3.extent(d3.merge(seriesData).map(function(d) { return d.x }).concat(scatter.forceX) ))
.range([0, availableWidth]);
y .domain(d3.extent(d3.merge(seriesData).map(getY).concat(forceY) ))
.range([height - margin.top - margin.bottom, 0]);
y .domain(d3.extent(d3.merge(seriesData).map(function(d) { return d.y }).concat(scatter.forceY) ))
.range([availableHeight, 0]);
scatter
.width(width - margin.left - margin.right)
.height(height - margin.top - margin.bottom)
.width(availableWidth)
.height(availableHeight)
.xDomain(x.domain())
.yDomain(y.domain())
.color(data.map(function(d,i) {
return d.color || color[i % 20];
}).filter(function(d,i) { return !data[i].disabled }))
xAxis
.ticks( width / 100 )
.tickSize(-(height - margin.top - margin.bottom), 0);
yAxis
.ticks( height / 36 )
.tickSize(-(width - margin.right - margin.left), 0);
var wrap = d3.select(this).selectAll('g.wrap').data([data]);
@ -3160,8 +3063,6 @@ nv.models.scatterWithLegend = function() {
legend.dispatch.on('legendClick', function(d,i, that) {
d.disabled = !d.disabled;
//d3.select(that).classed('disabled', d.disabled); //TODO: do this from the data, not manually
if (!data.filter(function(d) { return !d.disabled }).length) {
data.map(function(d) {
d.disabled = false;
@ -3171,7 +3072,6 @@ nv.models.scatterWithLegend = function() {
}
selection.transition(animate).call(chart)
//d3.transition(selection).call(chart);
});
/*
@ -3202,13 +3102,6 @@ nv.models.scatterWithLegend = function() {
dispatch.tooltipHide(e);
});
legend.width(width/2 - margin.right);
wrap.select('.legendWrap')
.datum(data)
.attr('transform', 'translate(' + (width/2 - margin.left) + ',' + (-legend.height()) +')')
.call(legend);
//TODO: margins should be adjusted based on what components are used: axes, axis labels, legend
margin.top = legend.height();
@ -3217,6 +3110,15 @@ nv.models.scatterWithLegend = function() {
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
legend.width(availableWidth / 2);
wrap.select('.legendWrap')
.datum(data)
.attr('transform', 'translate(' + (availableWidth / 2) + ',' + (-margin.top) +')')
.call(legend);
var scatterWrap = wrap.select('.scatterWrap')
.datum(data.filter(function(d) { return !d.disabled }));
@ -3225,12 +3127,11 @@ nv.models.scatterWithLegend = function() {
d3.transition(scatterWrap).call(scatter);
xAxis
.domain(x.domain())
.range(x.range())
.ticks( width / 100 )
.tickSize(-(height - margin.top - margin.bottom), 0);
.ticks( availableWidth / 100 )
.tickSize(-availableHeight, 0);
g.select('.x.axis')
.attr('transform', 'translate(0,' + y.range()[0] + ')');
@ -3242,8 +3143,8 @@ nv.models.scatterWithLegend = function() {
yAxis
.domain(y.domain())
.range(y.range())
.ticks( height / 36 )
.tickSize(-(width - margin.right - margin.left), 0);
.ticks( availableHeight / 36 )
.tickSize( -availableWidth, 0);
d3.transition(g.select('.y.axis'))
.call(yAxis);
@ -3255,8 +3156,12 @@ nv.models.scatterWithLegend = function() {
chart.dispatch = dispatch;
chart.legend = legend;
chart.xAxis = xAxis;
chart.yAxis = yAxis;
d3.rebind(chart, scatter, 'x', 'y', 'size', 'xDomain', 'yDomain', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id', 'showDistX', 'showDistY');
d3.rebind(chart, scatter, 'showDistX', 'showDistY');
chart.margin = function(_) {
if (!arguments.length) return margin;
@ -3266,38 +3171,16 @@ nv.models.scatterWithLegend = function() {
chart.width = function(_) {
if (!arguments.length) return width;
width = _;
width = d3.functor(_);
return chart;
};
chart.height = function(_) {
if (!arguments.length) return height;
height = _;
height = d3.functor(_);
return chart;
};
chart.forceX = function(_) {
if (!arguments.length) return forceX;
forceX = _;
scatter.forceX(_);
return chart;
};
chart.forceY = function(_) {
if (!arguments.length) return forceY;
forceY = _;
scatter.forceY(_);
return chart;
};
chart.animate = function(_) {
if (!arguments.length) return animate;
animate = _;
return chart;
};
chart.xAxis = xAxis;
chart.yAxis = yAxis;
return chart;
}

4
nv.d3.min.js vendored

File diff suppressed because one or more lines are too long

@ -183,7 +183,7 @@ svg .title {
* Lines
*/
.lines path {
.groups path {
fill: none;
stroke-width: 1.5px;
stroke-linecap: round;
@ -202,13 +202,13 @@ svg .title {
stroke-width: 6px;
}
.lines .point {
.groups .point {
transition: all 250ms linear;
-moz-transition: all 250ms linear;
-webkit-transition: all 250ms linear;
}
.lines .point.hover {
.groups .point.hover {
stroke-width: 20px;
stroke-opacity: .5;
}
@ -265,7 +265,7 @@ svg .title {
stroke-width: 0.1px;
}
/*
.d3stackedarea .lines path {
.d3stackedarea .groups path {
stroke-opacity: 0;
}
*/

@ -10,7 +10,7 @@ nv.models.line = function() {
getY = function(d) { return d.y }, // accessor to get the y value from a data point
getSize = function() { return 2.5 }, // accessor to get the point radius from a data point
forceX = [], // List of numbers to Force into the X scale (ie. 0, or a max / min, etc.)
forceY = [], // List of nuumbers to Force into the Y scale
forceY = [], // List of numbers to Force into the Y scale
interactive = true, // If true, plots a voronoi overlay for advanced point interection
clipEdge = false, // if true, masks lines within x and y scale
clipVoronoi = true, // if true, masks each point with a circle... can turn off to slightly increase performance
@ -18,13 +18,11 @@ nv.models.line = function() {
var x = d3.scale.linear(),
y = d3.scale.linear(),
dispatch = d3.dispatch('pointMouseover', 'pointMouseout'), //TODO: consider renaming to elementMouseove and elementMouseout for consistency
scatter = nv.models.scatter(),
x0, y0,
timeoutID;
function chart(selection) {
selection.each(function(data) {
var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
@ -47,13 +45,12 @@ nv.models.line = function() {
y .domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return d.y }).concat(forceY)))
.range([availableHeight, 0]);
var wrap = d3.select(this).selectAll('g.d3line').data([data]);
var wrapEnter = wrap.enter().append('g').attr('class', 'd3line');
var defsEnter = wrapEnter.append('defs');
var gEnter = wrapEnter.append('g');
gEnter.append('g').attr('class', 'lines');
gEnter.append('g').attr('class', 'groups');
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
@ -73,120 +70,33 @@ nv.models.line = function() {
function updateInteractiveLayer() {
if (!interactive) {
wrap.select('#points-clip-' + id).remove();
wrap.select('.point-paths').remove();
return false;
}
gEnter.append('g').attr('class', 'point-paths');
var vertices = d3.merge(data.map(function(line, lineIndex) {
return line.values.map(function(point, pointIndex) {
// Adding noise to make duplicates very unlikely
// Inject series and point index for reference
// TODO: see how much time this consumes
return [x(getX(point, pointIndex)) * (Math.random() / 1e12 + 1) , y(getY(point, pointIndex)) * (Math.random() / 1e12 + 1), lineIndex, pointIndex];
})
})
);
if (clipVoronoi) {
defsEnter.append('clipPath').attr('id', 'points-clip-' + id);
var pointClips = wrap.select('#points-clip-' + id).selectAll('circle')
.data(vertices);
pointClips.enter().append('circle')
.attr('r', 25);
pointClips.exit().remove();
pointClips
.attr('cx', function(d) { return d[0] })
.attr('cy', function(d) { return d[1] });
wrap.select('.point-paths')
.attr('clip-path', 'url(#points-clip-' + id + ')');
}
//inject series and point index for reference into voronoi
// considering adding a removeZeros option, may be useful for the stacked chart and maybe others
var voronoi = d3.geom.voronoi(vertices).map(function(d,i) { return { 'data': d, 'series': vertices[i][2], 'point': vertices[i][3] } });
var pointPaths = wrap.select('.point-paths').selectAll('path')
.data(voronoi);
pointPaths.enter().append('path')
.attr('class', function(d,i) { return 'path-'+i; });
pointPaths.exit().remove();
pointPaths
.attr('d', function(d) { return 'M' + d.data.join(',') + 'Z'; })
.on('mouseover', function(d) {
var series = data[d.series],
point = series.values[d.point];
dispatch.pointMouseover({
point: point,
series:series,
pos: [x(getX(point, d.point)) + margin.left, y(getY(point, d.point)) + margin.top],
seriesIndex: d.series,
pointIndex: d.point
});
})
.on('mouseout', function(d, i) {
dispatch.pointMouseout({
point: data[d.series].values[d.point],
series: data[d.series],
seriesIndex: d.series,
pointIndex: d.point
});
});
dispatch.on('pointMouseover.point', function(d) {
wrap.select('.series-' + d.seriesIndex + ' .point-' + d.pointIndex)
.classed('hover', true);
});
dispatch.on('pointMouseout.point', function(d) {
wrap.select('.series-' + d.seriesIndex + ' .point-' + d.pointIndex)
.classed('hover', false);
});
}
var lines = wrap.select('.lines').selectAll('.line')
var groups = wrap.select('.groups').selectAll('.line')
.data(function(d) { return d }, function(d) { return d.key });
lines.enter().append('g')
groups.enter().append('g')
.style('stroke-opacity', 1e-6)
.style('fill-opacity', 1e-6);
d3.transition(lines.exit())
d3.transition(groups.exit())
.style('stroke-opacity', 1e-6)
.style('fill-opacity', 1e-6)
.remove();
lines
groups
.attr('class', function(d,i) { return 'line series-' + i })
.classed('hover', function(d) { return d.hover })
.style('fill', function(d,i){ return color[i % 10] })
.style('stroke', function(d,i){ return color[i % 10] })
d3.transition(lines)
d3.transition(groups)
.style('stroke-opacity', 1)
.style('fill-opacity', .5)
//setTimeout(interactiveLayer, 1000); //seems not to work as well as above... BUT fixes broken resize
var paths = lines.selectAll('path')
var paths = groups.selectAll('path')
.data(function(d, i) { return [d.values] });
paths.enter().append('path')
.attr('d', d3.svg.line()
.x(function(d,i) { return x0(getX(d,i)) })
.y(function(d,i) { return y0(getY(d,i)) })
);
d3.transition(lines.exit().selectAll('path'))
d3.transition(groups.exit().selectAll('path'))
.attr('d', d3.svg.line()
.x(function(d,i) { return x(getX(d,i)) })
.y(function(d,i) { return y(getY(d,i)) })
@ -198,20 +108,13 @@ nv.models.line = function() {
.y(function(d,i) { return y(getY(d,i)) })
);
var points = lines.selectAll('circle.point')
/*
var points = groups.selectAll('circle.point')
.data(function(d) { return d.values });
points.enter().append('circle')
.attr('cx', function(d,i) { return x0(getX(d,i)) })
.attr('cy', function(d,i) { return y0(getY(d,i)) });
/*
// I think this is redundant with below, but originally put this here for a reason
d3.transition(points.exit())
.attr('cx', function(d,i) { return x(getX(d,i)) })
.attr('cy', function(d,i) { return y(getY(d,i)) })
.remove();
*/
d3.transition(lines.exit().selectAll('circle.point'))
d3.transition(groups.exit().selectAll('circle.point'))
.attr('cx', function(d,i) { return x(getX(d,i)) })
.attr('cy', function(d,i) { return y(getY(d,i)) })
.remove();
@ -221,9 +124,23 @@ nv.models.line = function() {
.attr('cy', function(d,i) { return y(getY(d,i)) })
.attr('r', getSize);
*/
scatter
.size(getSize)
.id(id)
.width(availableWidth)
.height(availableHeight)
.xDomain(x.domain())
.yDomain(y.domain());
wrapEnter.append('g').attr('class', 'scatterWrap');
var scatterWrap = wrap.select('.scatterWrap').datum(data);
d3.transition(scatterWrap).call(scatter);
clearTimeout(timeoutID);
timeoutID = setTimeout(updateInteractiveLayer, 750);
//store old scales for use in transitions on update, to animate from old to new positions
x0 = x.copy();
@ -235,7 +152,9 @@ nv.models.line = function() {
}
chart.dispatch = dispatch;
chart.dispatch = scatter.dispatch;
d3.rebind(chart, scatter, 'size');
chart.x = function(_) {
if (!arguments.length) return getX;
@ -249,12 +168,6 @@ nv.models.line = function() {
return chart;
};
chart.size = function(_) {
if (!arguments.length) return getSize;
getSize = d3.functor(_);
return chart;
};
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
@ -318,6 +231,7 @@ nv.models.line = function() {
chart.color = function(_) {
if (!arguments.length) return color;
color = _;
scatter.color(_);
return chart;
};

@ -1,250 +0,0 @@
// This is an attempt to use a scatter plot with the line plot do do point interaction
// If this performs good, will likely use as the line implementation, this way the
// point interaction using a voronoi will only be in the scatter not in the exact same code
// in both the scatter and the line
nv.models.line = function() {
//Default Settings
var margin = {top: 0, right: 0, bottom: 0, left: 0},
width = 960,
height = 500,
color = d3.scale.category10().range(),
id = Math.floor(Math.random() * 10000), //Create semi-unique ID incase user doesn't select one
getX = function(d) { return d.x }, // accessor to get the x value from a data point
getY = function(d) { return d.y }, // accessor to get the y value from a data point
getSize = function() { return 2.5 }, // accessor to get the point radius from a data point
forceX = [], // List of numbers to Force into the X scale (ie. 0, or a max / min, etc.)
forceY = [], // List of nuumbers to Force into the Y scale
interactive = true, // If true, plots a voronoi overlay for advanced point interection
clipEdge = false, // if true, masks lines within x and y scale
clipVoronoi = true, // if true, masks each point with a circle... can turn off to slightly increase performance
xDomain, yDomain; // Used to manually set the x and y domain, good to save time if calculation has already been made
var x = d3.scale.linear(),
y = d3.scale.linear(),
scatter = nv.models.scatter().size(getSize).id(id),
x0, y0,
timeoutID;
function chart(selection) {
selection.each(function(data) {
var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
data.map(function(d) {
return d.values.map(function(d,i) {
return { x: getX(d,i), y: getY(d,i) }
})
}),
availableWidth = width - margin.left - margin.right,
availableHeight = height - margin.top - margin.bottom;
//store old scales if they exist
x0 = x0 || x;
y0 = y0 || y;
x .domain(xDomain || d3.extent(d3.merge(seriesData).map(function(d) { return d.x }).concat(forceX)))
.range([0, availableWidth]);
y .domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return d.y }).concat(forceY)))
.range([availableHeight, 0]);
var wrap = d3.select(this).selectAll('g.d3line').data([data]);
var wrapEnter = wrap.enter().append('g').attr('class', 'd3line');
var defsEnter = wrapEnter.append('defs');
var gEnter = wrapEnter.append('g');
gEnter.append('g').attr('class', 'lines');
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
if (clipEdge) {
defsEnter.append('clipPath')
.attr('id', 'edge-clip-' + id)
.append('rect');
wrap.select('#edge-clip-' + id + ' rect')
.attr('width', availableWidth)
.attr('height', availableHeight);
gEnter
.attr('clip-path', 'url(#edge-clip-' + id + ')');
}
var lines = wrap.select('.lines').selectAll('.line')
.data(function(d) { return d }, function(d) { return d.key });
lines.enter().append('g')
.style('stroke-opacity', 1e-6)
.style('fill-opacity', 1e-6);
d3.transition(lines.exit())
.style('stroke-opacity', 1e-6)
.style('fill-opacity', 1e-6)
.remove();
lines
.attr('class', function(d,i) { return 'line series-' + i })
.classed('hover', function(d) { return d.hover })
.style('fill', function(d,i){ return color[i % 10] })
.style('stroke', function(d,i){ return color[i % 10] })
d3.transition(lines)
.style('stroke-opacity', 1)
.style('fill-opacity', .5)
var paths = lines.selectAll('path')
.data(function(d, i) { return [d.values] });
paths.enter().append('path')
.attr('d', d3.svg.line()
.x(function(d,i) { return x0(getX(d,i)) })
.y(function(d,i) { return y0(getY(d,i)) })
);
d3.transition(lines.exit().selectAll('path'))
.attr('d', d3.svg.line()
.x(function(d,i) { return x(getX(d,i)) })
.y(function(d,i) { return y(getY(d,i)) })
)
.remove(); // redundant? line is already being removed
d3.transition(paths)
.attr('d', d3.svg.line()
.x(function(d,i) { return x(getX(d,i)) })
.y(function(d,i) { return y(getY(d,i)) })
);
/*
var points = lines.selectAll('circle.point')
.data(function(d) { return d.values });
points.enter().append('circle')
.attr('cx', function(d,i) { return x0(getX(d,i)) })
.attr('cy', function(d,i) { return y0(getY(d,i)) });
d3.transition(lines.exit().selectAll('circle.point'))
.attr('cx', function(d,i) { return x(getX(d,i)) })
.attr('cy', function(d,i) { return y(getY(d,i)) })
.remove();
d3.transition(points)
.attr('class', function(d,i) { return 'point point-' + i })
.attr('cx', function(d,i) { return x(getX(d,i)) })
.attr('cy', function(d,i) { return y(getY(d,i)) })
.attr('r', getSize);
*/
scatter
.width(availableWidth)
.height(availableHeight)
.xDomain(x.domain())
.yDomain(y.domain())
wrapEnter.append('g').attr('class', 'scatterWrap');
var scatterWrap = wrap.select('.scatterWrap').datum(data);
d3.transition(scatterWrap).call(scatter);
//store old scales for use in transitions on update, to animate from old to new positions
x0 = x.copy();
y0 = y.copy();
});
return chart;
}
chart.dispatch = scatter.dispatch;
d3.rebind(chart, scatter, 'size');
chart.x = function(_) {
if (!arguments.length) return getX;
getX = _;
return chart;
};
chart.y = function(_) {
if (!arguments.length) return getY;
getY = _;
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.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.interactive = function(_) {
if (!arguments.length) return interactive;
interactive = _;
return chart;
};
chart.clipEdge = function(_) {
if (!arguments.length) return clipEdge;
clipEdge = _;
return chart;
};
chart.clipVoronoi= function(_) {
if (!arguments.length) return clipVoronoi;
clipVoronoi = _;
return chart;
};
chart.color = function(_) {
if (!arguments.length) return color;
color = _;
scatter.color(_);
return chart;
};
chart.id = function(_) {
if (!arguments.length) return id;
id = _;
return chart;
};
return chart;
}

@ -1,322 +0,0 @@
//TODO: consider adding axes
// -How to deal with time vs generic linear, vs any other scale?
nv.models.line = function() {
//Default Settings
var margin = {top: 0, right: 0, bottom: 0, left: 0}, //consider removing margin options from here... or make margin padding inside the chart (subtract margin from range)
width = 960,
height = 500,
dotRadius = function() { return 2.5 }, //consider removing this, or making similar to scatter
color = d3.scale.category10().range(),
id = Math.floor(Math.random() * 10000), //Create semi-unique ID incase user doesn't select one
getX = function(d) { return d.x },
getY = function(d) { return d.y },
interactive = true,
clipEdge = false,
clipVoronoi = true,
xDomain, yDomain;
var x = d3.scale.linear(),
y = d3.scale.linear(),
dispatch = d3.dispatch('pointMouseover', 'pointMouseout'),
x0, y0;
function chart(selection) {
selection.each(function(data) {
var seriesData = data.map(function(d) {
return d.values.map(function(d,i) {
return { x: getX(d,i), y: getY(d,i) }
})
}),
availableWidth = width - margin.left - margin.right,
availableHeight = height - margin.top - margin.bottom;
x0 = x0 || x;
y0 = y0 || y;
x .domain(xDomain || d3.extent(d3.merge(seriesData), function(d) { return d.x } ))
.range([0, availableWidth]);
y .domain(yDomain || d3.extent(d3.merge(seriesData), function(d) { return d.y } ))
.range([availableHeight, 0]);
var wrap = d3.select(this).selectAll('g.d3line').data([data]);
var wrapEnter = wrap.enter().append('g').attr('class', 'd3line');
var gEnter = wrapEnter.append('g');
gEnter.append('g').attr('class', 'lines');
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');
// destroy interactive layer during transition,
// VERY needed because of performance issues
// TODO: check if this is necessary with new interaction
g.selectAll('.point-clips *, .point-paths *').remove();
function interactiveLayer() {
if (!interactive) return false;
shiftWrap.append('g').attr('class', 'point-clips').append('clipPath').attr('id', 'voronoi-clip-path-' + id);
shiftWrap.append('g').attr('class', 'point-paths');
var vertices = d3.merge(data.map(function(line, lineIndex) {
return line.values.map(function(point, pointIndex) {
//return [x(getX(point)), y(getY(point)), lineIndex, pointIndex]; //inject series and point index for reference into voronoi
return [x(getX(point, pointIndex)) * (Math.random() / 1e12 + 1) , y(getY(point, pointIndex)) * (Math.random() / 1e12 + 1), lineIndex, pointIndex]; //temp hack to add noise untill I think of a better way so there are no duplicates
})
})
);
/*
// ***These clips are more than half the cause for the slowdown***
//var pointClips = wrap.select('.point-clips').selectAll('clipPath') // **BROWSER BUG** can't reselect camel cased elements
var pointClips = wrap.select('.point-clips').selectAll('.clip-path')
.data(vertices);
pointClips.enter().append('clipPath').attr('class', 'clip-path')
.append('circle')
.attr('r', 25);
pointClips.exit().remove();
pointClips
.attr('id', function(d, i) { return 'clip-' + id + '-' + d[2] + '-' + d[3] })
.attr('transform', function(d) { return 'translate(' + d[0] + ',' + d[1] + ')' })
*/
var pointClips = wrap.select('#voronoi-clip-path-' + id).selectAll('circle')
.data(vertices);
pointClips.enter().append('circle')
.attr('r', 25);
pointClips.exit().remove();
pointClips
.attr('cx', function(d) { return d[0] })
.attr('cy', function(d) { return d[1] });
wrap.select('.point-paths')
.attr('clip-path', 'url(#voronoi-clip-path-' + id + ')');
//inject series and point index for reference into voronoi
// considering adding a removeZeros option, may be useful for the stacked chart and maybe others
var voronoi = d3.geom.voronoi(vertices).map(function(d, i) { return { 'data': d, 'series': vertices[i][2], 'point': vertices[i][3] } });
var pointPaths = wrap.select('.point-paths').selectAll('path')
.data(voronoi);
pointPaths.enter().append('path')
.attr('class', function(d,i) { return 'path-'+i; })
.style('fill-opacity', 0);
pointPaths.exit().remove();
pointPaths
//.attr('clip-path', function(d,i) { return clipVoronoi ? 'url(#clip-' + id + '-' + d.series + '-' + d.point +')' : '' })
.attr('d', function(d) { return 'M' + d.data.join(',') + 'Z'; })
.on('mouseover', function(d) {
var series = data[d.series],
point = series.values[d.point];
dispatch.pointMouseover({
point: point,
series:series,
pos: [x(getX(point, d.point)) + margin.left, y(getY(point, d.point)) + margin.top],
seriesIndex: d.series,
pointIndex: d.point
});
})
.on('mouseout', function(d, i) {
dispatch.pointMouseout({
point: data[d.series].values[d.point],
series: data[d.series],
seriesIndex: d.series,
pointIndex: d.point
});
});
dispatch.on('pointMouseover.point', function(d) {
wrap.select('.series-' + d.seriesIndex + ' .point-' + d.pointIndex)
.classed('hover', true);
});
dispatch.on('pointMouseout.point', function(d) {
wrap.select('.series-' + d.seriesIndex + ' .point-' + d.pointIndex)
.classed('hover', false);
});
}
var lines = wrap.select('.lines').selectAll('.line')
.data(function(d) { return d }, function(d) { return d.key });
lines.enter().append('g')
.style('stroke-opacity', 1e-6)
.style('fill-opacity', 1e-6);
d3.transition(lines.exit())
.style('stroke-opacity', 1e-6)
.style('fill-opacity', 1e-6)
.remove();
lines
.attr('class', function(d,i) { return 'line series-' + i })
.classed('hover', function(d) { return d.hover })
.style('fill', function(d,i){ return color[i % 10] })
.style('stroke', function(d,i){ return color[i % 10] })
d3.transition(lines)
.style('stroke-opacity', 1)
.style('fill-opacity', .5)
//.each('end', function(d,i) { if (!i) setTimeout(interactiveLayer, 0) }); //trying to call this after transitions are over, doesn't work on resize!
//.each('end', function(d,i) { if (!i) interactiveLayer() }); //trying to call this after transitions are over, not sure if the timeout gains anything
setTimeout(interactiveLayer, 1000); //seems not to work as well as above... BUT fixes broken resize
var paths = lines.selectAll('path')
.data(function(d, i) { return [d.values] });
paths.enter().append('path')
.attr('d', d3.svg.line()
.x(function(d,i) { return x0(getX(d,i)) })
.y(function(d,i) { return y0(getY(d,i)) })
);
//d3.transition(paths.exit())
d3.transition(lines.exit().selectAll('path'))
.attr('d', d3.svg.line()
.x(function(d,i) { return x(getX(d,i)) })
.y(function(d,i) { return y(getY(d,i)) })
)
.remove();
d3.transition(paths)
.attr('d', d3.svg.line()
.x(function(d,i) { return x(getX(d,i)) })
.y(function(d,i) { return y(getY(d,i)) })
);
var points = lines.selectAll('circle.point')
.data(function(d) { return d.values });
points.enter().append('circle')
.attr('cx', function(d,i) { return x0(getX(d,i)) })
.attr('cy', function(d,i) { return y0(getY(d,i)) });
d3.transition(points.exit())
.attr('cx', function(d,i) { return x(getX(d,i)) })
.attr('cy', function(d,i) { return y(getY(d,i)) })
.remove();
d3.transition(lines.exit().selectAll('circle.point'))
.attr('cx', function(d,i) { return x(getX(d,i)) })
.attr('cy', function(d,i) { return y(getY(d,i)) })
.remove();
points.attr('class', function(d,i) { return 'point point-' + i });
d3.transition(points)
.attr('cx', function(d,i) { return x(getX(d,i)) })
.attr('cy', function(d,i) { return y(getY(d,i)) })
.attr('r', dotRadius);
x0 = x.copy();
y0 = y.copy();
});
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.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.xDomain = function(_) {
if (!arguments.length) return xDomain;
xDomain = _;
return chart;
};
chart.yDomain = function(_) {
if (!arguments.length) return yDomain;
yDomain = _;
return chart;
};
chart.interactive = function(_) {
if (!arguments.length) return interactive;
interactive = _;
return chart;
};
chart.clipEdge = function(_) {
if (!arguments.length) return clipEdge;
clipEdge = _;
return chart;
};
chart.clipVoronoi= function(_) {
if (!arguments.length) return clipVoronoi;
clipVoronoi = _;
return chart;
};
chart.dotRadius = function(_) {
if (!arguments.length) return dotRadius;
dotRadius = d3.functor(_);
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;
}

@ -5,18 +5,18 @@ nv.models.scatter = function() {
height = 500,
color = d3.scale.category10().range(),
id = Math.floor(Math.random() * 100000), //Create semi-unique ID incase user doesn't selet one
getX = function(d) { return d.x }, // or d[0]
getY = function(d) { return d.y }, // or d[1]
getSize = function(d) { return d.size }, // or d[2]
forceX = [],
forceY = [],
forceSize = [],
getX = function(d) { return d.x }, // accessor to get the x value from a data point
getY = function(d) { return d.y }, // accessor to get the y value from a data point
getSize = function(d) { return d.size }, // accessor to get the point radius from a data point
forceX = [], // List of numbers to Force into the X scale (ie. 0, or a max / min, etc.)
forceY = [], // List of numbers to Force into the Y scale
forceSize = [], // List of numbers to Force into the Size scale
showDistX = false,
showDistY = false,
interactive = true,
clipEdge = false,
clipVoronoi = true,
xDomain, yDomain, sizeDomain;
interactive = true, // If true, plots a voronoi overlay for advanced point interection
clipEdge = false, // if true, masks lines within x and y scale
clipVoronoi = true, // if true, masks each point with a circle... can turn off to slightly increase performance
xDomain, yDomain, sizeDomain; // Used to manually set the x and y domain, good to save time if calculation has already been made
var x = d3.scale.linear(),
y = d3.scale.linear(),
@ -71,7 +71,7 @@ nv.models.scatter = function() {
var gEnter = wrapEnter.append('g');
gEnter.append('g').attr('class', 'groups');
gEnter.append('g').attr('class', 'distribution');
//gEnter.append('g').attr('class', 'distribution');
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
@ -164,23 +164,27 @@ nv.models.scatter = function() {
dispatch.on('pointMouseover.point', function(d) {
wrap.select('.series-' + d.seriesIndex + ' .point-' + d.pointIndex)
.classed('hover', true);
/*
if (showDistX)
wrap.select('.series-' + d.seriesIndex + ' .distX-' + d.pointIndex)
.attr('y1', d.pos[1] - margin.top);
if (showDistY)
wrap.select('.series-' + d.seriesIndex + ' .distY-' + d.pointIndex)
.attr('x1', d.pos[0] - margin.left);
*/
});
dispatch.on('pointMouseout.point', function(d) {
wrap.select('.series-' + d.seriesIndex + ' circle.point-' + d.pointIndex)
.classed('hover', false);
/*
if (showDistX)
wrap.select('.series-' + d.seriesIndex + ' .distX-' + d.pointIndex)
.attr('y1', y.range()[0]);
if (showDistY)
wrap.select('.series-' + d.seriesIndex + ' .distY-' + d.pointIndex)
.attr('x1', x.range()[0]);
*/
});
}
@ -226,8 +230,7 @@ nv.models.scatter = function() {
.attr('r', function(d,i) { return z(getSize(d,i)) });
// TODO: consider abstracting Axis distributions out of this file
/*
if (showDistX) {
var distX = groups.selectAll('line.distX')
.data(function(d) { return d.values })
@ -267,6 +270,7 @@ nv.models.scatter = function() {
.attr('y1', function(d,i) { return y(getY(d,i)) })
.attr('y2', function(d,i) { return y(getY(d,i)) });
}
*/
clearTimeout(timeoutID);

@ -3,7 +3,6 @@ nv.models.scatterWithLegend = function() {
var margin = {top: 30, right: 20, bottom: 50, left: 60},
width = function() { return 960 },
height = function() { return 500 },
animate = 500,
xAxisRender = true,
yAxisRender = true,
xAxisLabelText = false,
@ -56,6 +55,7 @@ nv.models.scatterWithLegend = function() {
gEnter.append('g').attr('class', 'x axis');
gEnter.append('g').attr('class', 'y axis');
gEnter.append('g').attr('class', 'scatterWrap');
//gEnter.append('g').attr('class', 'distWrap');
legend.dispatch.on('legendClick', function(d,i, that) {
@ -69,7 +69,7 @@ nv.models.scatterWithLegend = function() {
});
}
selection.transition(animate).call(chart)
selection.transition().call(chart)
});
/*
@ -100,6 +100,18 @@ nv.models.scatterWithLegend = function() {
dispatch.tooltipHide(e);
});
scatter.dispatch.on('pointMouseover.dist', function(d) {
scatterWrap.select('.series-' + d.seriesIndex + ' .distX-' + d.pointIndex)
.attr('y1', d.pos[1]);
scatterWrap.select('.series-' + d.seriesIndex + ' .distY-' + d.pointIndex)
.attr('x1', d.pos[0]);
});
scatter.dispatch.on('pointMouseout.dist', function(d) {
scatterWrap.select('.series-' + d.seriesIndex + ' .distX-' + d.pointIndex)
.attr('y1', y.range()[0]);
scatterWrap.select('.series-' + d.seriesIndex + ' .distY-' + d.pointIndex)
.attr('x1', x.range()[0]);
});
//TODO: margins should be adjusted based on what components are used: axes, axis labels, legend
margin.top = legend.height();
@ -120,11 +132,57 @@ nv.models.scatterWithLegend = function() {
var scatterWrap = wrap.select('.scatterWrap')
.datum(data.filter(function(d) { return !d.disabled }));
//log(d3.transition()[0][0].duration); //get parent's duration
d3.transition(scatterWrap).call(scatter);
//need to fix the point rotate on enable/disable series
var distWrap = scatterWrap.selectAll('g.distribution')
.data(function(d) { return d })
distWrap.enter().append('g').attr('class', function(d,i) { return 'distribution series-' + i })
distWrap.style('stroke', function(d,i) { return color.filter(function(d,i) { return data[i] && !data[i].disabled })[i % 10] })
var distX = distWrap.selectAll('line.distX')
.data(function(d) { return d.values })
distX.enter().append('line')
//.attr('x1', function(d,i) { return x0(scatter.x()(d,i)) })
//.attr('x2', function(d,i) { return x0(scatter.x()(d,i)) })
//d3.transition(distX.exit())
d3.transition(distWrap.exit().selectAll('line.distX'))
.attr('x1', function(d,i) { return x(scatter.x()(d,i)) })
.attr('x2', function(d,i) { return x(scatter.x()(d,i)) })
.remove();
distX
.attr('class', function(d,i) { return 'distX distX-' + i })
.attr('y1', y.range()[0])
.attr('y2', y.range()[0] + 8);
d3.transition(distX)
.attr('x1', function(d,i) { return x(scatter.x()(d,i)) })
.attr('x2', function(d,i) { return x(scatter.x()(d,i)) })
var distY = distWrap.selectAll('line.distY')
.data(function(d) { return d.values })
distY.enter().append('line')
//.attr('y1', function(d,i) { return y0(scatter.y()(d,i)) })
//.attr('y2', function(d,i) { return y0(scatter.y()(d,i)) });
//d3.transition(distY.exit())
d3.transition(distWrap.exit().selectAll('line.distY'))
.attr('y1', function(d,i) { return y(scatter.y()(d,i)) })
.attr('y2', function(d,i) { return y(scatter.y()(d,i)) })
.remove();
distY
.attr('class', function(d,i) { return 'distY distY-' + i })
.attr('x1', x.range()[0])
.attr('x2', x.range()[0] - 8)
d3.transition(distY)
.attr('y1', function(d,i) { return y(scatter.y()(d,i)) })
.attr('y2', function(d,i) { return y(scatter.y()(d,i)) });
xAxis
.domain(x.domain())
.range(x.range())

Loading…
Cancel
Save