2 nv.models.lineChart = function() {
4 var margin = {top: 30, right: 20, bottom: 50, left: 60},
5 color = nv.utils.defaultColor(),
13 tooltip = function(key, x, y, e, graph) {
14 return '<h3>' + key + '</h3>' +
15 '<p>' + y + ' at ' + x + '</p>'
17 noData = "No Data Available."
21 var x = d3.fisheye.scale(d3.scale.linear).distortion(0);
23 var lines = nv.models.line().xScale(x),
26 xAxis = nv.models.axis().scale(x).orient('bottom').tickPadding(5),
27 yAxis = nv.models.axis().scale(y).orient('left'),
28 legend = nv.models.legend().height(30),
29 controls = nv.models.legend().height(30).updateState(false),
30 dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
33 var showTooltip = function(e, offsetElement) {
34 var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
35 top = e.pos[1] + ( offsetElement.offsetTop || 0),
36 x = xAxis.tickFormat()(lines.x()(e.point, e.pointIndex)),
37 y = yAxis.tickFormat()(lines.y()(e.point, e.pointIndex)),
38 content = tooltip(e.series.key, x, y, e, chart);
40 nv.tooltip.show([left, top], content, null, null, offsetElement);
45 { key: 'Magnify', disabled: true }
49 function chart(selection) {
50 selection.each(function(data) {
51 var container = d3.select(this),
54 var availableWidth = (width || parseInt(container.style('width')) || 960)
55 - margin.left - margin.right,
56 availableHeight = (height || parseInt(container.style('height')) || 400)
57 - margin.top - margin.bottom;
59 chart.update = function() { container.transition().call(chart) };
60 chart.container = this; // I need a reference to the container in order to have outside code check if the chart is visible or not
61 //------------------------------------------------------------
62 // Display No Data message if there's nothing to show.
64 if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
65 container.append('text')
66 .attr('class', 'nvd3 nv-noData')
67 .attr('x', availableWidth / 2)
68 .attr('y', availableHeight / 2)
70 .style('text-anchor', 'middle')
74 container.select('.nv-noData').remove();
77 //------------------------------------------------------------
81 var wrap = container.selectAll('g.nv-wrap.nv-lineChart').data([data]);
82 var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-lineChart').append('g');
86 .attr('class', 'nvd3 nv-background')
87 .attr('width', availableWidth)
88 .attr('height', availableHeight);
91 gEnter.append('g').attr('class', 'nv-x nv-axis');
92 gEnter.append('g').attr('class', 'nv-y nv-axis');
93 gEnter.append('g').attr('class', 'nv-linesWrap');
94 gEnter.append('g').attr('class', 'nv-legendWrap');
95 gEnter.append('g').attr('class', 'nv-controlsWrap');
96 gEnter.append('g').attr('class', 'nv-controlsWrap');
99 var g = wrap.select('g');
105 legend.width(availableWidth);
107 g.select('.nv-legendWrap')
111 if ( margin.top != legend.height()) {
112 margin.top = legend.height();
113 availableHeight = (height || parseInt(container.style('height')) || 400)
114 - margin.top - margin.bottom;
117 g.select('.nv-legendWrap')
118 .attr('transform', 'translate(0,' + (-margin.top) +')')
122 controls.width(180).color(['#444']);
123 g.select('.nv-controlsWrap')
125 .attr('transform', 'translate(0,' + (-margin.top) +')')
132 .width(availableWidth)
133 .height(availableHeight)
134 .color(data.map(function(d,i) {
135 return d.color || color(d, i);
136 }).filter(function(d,i) { return !data[i].disabled }));
140 g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
143 var linesWrap = g.select('.nv-linesWrap')
144 .datum(data.filter(function(d) { return !d.disabled }))
146 d3.transition(linesWrap).call(lines);
152 .ticks( availableWidth / 100 )
153 .tickSize(-availableHeight, 0);
155 g.select('.nv-x.nv-axis')
156 .attr('transform', 'translate(0,' + y.range()[0] + ')');
157 d3.transition(g.select('.nv-x.nv-axis'))
163 .ticks( availableHeight / 36 )
164 .tickSize( -availableWidth, 0);
166 d3.transition(g.select('.nv-y.nv-axis'))
171 g.select('.nv-background').on('mousemove', updateFisheye);
172 g.select('.nv-background').on('click', function() { pauseFisheye = !pauseFisheye; });
173 //g.select('.point-paths').on('mousemove', updateFisheye);
176 function updateFisheye() {
178 //g.select('.background') .style('pointer-events', 'none');
179 g.select('.nv-point-paths').style('pointer-events', 'all');
183 g.select('.nv-background') .style('pointer-events', 'all');
184 g.select('.nv-point-paths').style('pointer-events', 'none' );
186 var mouse = d3.mouse(this);
187 linesWrap.call(lines);
188 g.select('.nv-x.nv-axis').call(xAxis);
189 x.distortion(fisheye).focus(mouse[0]);
193 controls.dispatch.on('legendClick', function(d,i) {
194 d.disabled = !d.disabled;
196 fisheye = d.disabled ? 0 : 5;
197 g.select('.nv-background') .style('pointer-events', d.disabled ? 'none' : 'all');
198 g.select('.nv-point-paths').style('pointer-events', d.disabled ? 'all' : 'none' );
200 //scatter.interactive(d.disabled);
201 //tooltips = d.disabled;
204 x.distortion(fisheye).focus(0);
206 linesWrap.call(lines);
207 g.select('.nv-x.nv-axis').call(xAxis);
209 pauseFisheye = false;
216 legend.dispatch.on('stateChange', function(newState) {
220 lines.dispatch.on('elementMouseover.tooltip', function(e) {
221 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
222 dispatch.tooltipShow(e);
224 if (tooltips) dispatch.on('tooltipShow', function(e) { showTooltip(e, that.parentNode) } ); // TODO: maybe merge with above?
226 lines.dispatch.on('elementMouseout.tooltip', function(e) {
227 dispatch.tooltipHide(e);
229 if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
237 chart.dispatch = dispatch;
238 chart.legend = legend;
242 d3.rebind(chart, lines, 'defined', 'x', 'y', 'size', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id', 'interpolate');
244 chart.options = nv.utils.optionsFunc.bind(chart);
246 chart.margin = function(_) {
247 if (!arguments.length) return margin;
252 chart.width = function(_) {
253 if (!arguments.length) return width;
258 chart.height = function(_) {
259 if (!arguments.length) return height;
264 chart.color = function(_) {
265 if (!arguments.length) return color;
266 color = nv.utils.getColor(_);
271 chart.showLegend = function(_) {
272 if (!arguments.length) return showLegend;
277 chart.tooltips = function(_) {
278 if (!arguments.length) return tooltips;
283 chart.tooltipContent = function(_) {
284 if (!arguments.length) return tooltip;
289 chart.noData = function(_) {
290 if (!arguments.length) return noData;