Added cumulativeLineChartDaily chart and example, modified cumulativeLine.js to pass in the point index to getX and getY functions, which is used because the dates are not linear in daily

master-patched
Bob Monteverde 12 years ago
parent 83d56ef755
commit ff9ca0d293

File diff suppressed because one or more lines are too long

@ -1469,8 +1469,8 @@ nv.models.lineWithLegend = function() {
nv.models.cumulativeLine = function() {
var margin = {top: 30, right: 20, bottom: 30, left: 60},
width = 960,
height = 500,
getWidth = function() { return 960 },
getHeight = function() { return 500 },
color = d3.scale.category10().range(),
dotRadius = function() { return 2.5 },
getX = function(d) { return d.x },
@ -1510,22 +1510,28 @@ nv.models.cumulativeLine = 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 = indexify(index.i, data);
var seriesData = series
.filter(function(d) { return !d.disabled })
.map(function(d) { return d.values });
x .domain(d3.extent(d3.merge(seriesData), getX ))
x .domain(d3.extent(d3.merge(seriesData), function(d) { return d.x } ))
.range([0, width - margin.left - margin.right]);
dx .domain([0, data[0].values.length - 1]) //Assumes all series have same length
.range([0, width - margin.left - margin.right])
.clamp(true);
y .domain(d3.extent(d3.merge(seriesData), getY ))
y .domain(d3.extent(d3.merge(seriesData), function(d) { return d.y } ))
.range([height - margin.top - margin.bottom, 0]);
lines
.width(width - margin.left - margin.right)
.height(height - margin.top - margin.bottom)
@ -1659,12 +1665,12 @@ nv.models.cumulativeLine = function() {
/* Normalize the data according to an index point. */
function indexify(idx, data) {
return data.map(function(line, i) {
var v = getY(line.values[idx]);
var v = getY(line.values[idx], idx);
return {
key: line.key,
values: line.values.map(function(point) {
return {'x': getX(point), 'y': (getY(point) - v) / (1 + v) };
values: line.values.map(function(point, pointIndex) {
return {'x': getX(point, pointIndex), 'y': (getY(point, pointIndex) - v) / (1 + v) };
}),
disabled: line.disabled,
hover: line.hover
@ -1687,14 +1693,14 @@ nv.models.cumulativeLine = function() {
chart.x = function(_) {
if (!arguments.length) return getX;
getX = _;
lines.x(_);
//lines.x(_);
return chart;
};
chart.y = function(_) {
if (!arguments.length) return getY;
getY = _;
lines.y(_);
//lines.y(_);
return chart;
};
@ -1704,6 +1710,7 @@ nv.models.cumulativeLine = function() {
return chart;
};
/*
chart.width = function(_) {
if (!arguments.length) return width;
width = _;
@ -1715,6 +1722,19 @@ nv.models.cumulativeLine = function() {
height = _;
return chart;
};
*/
chart.width = function(_) {
if (!arguments.length) return getWidth;
getWidth = 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;

4
nv.d3.min.js vendored

File diff suppressed because one or more lines are too long

@ -0,0 +1,166 @@
// This is an attempt to make an extremely easy to use chart that is ready to go,
// basically the chart models with the extra glue... Queuing, tooltips, automatic resize, etc.
// I may make these more specific, like 'time series line with month end data points', etc.
// or may make yet another layer of abstraction... common settings.
nv.charts.cumulativeLineChartDaily = function() {
var selector = null,
data = [],
duration = 500,
tooltip = function(key, x, y, e, graph) {
return '<h3>' + key + '</h3>' +
'<p>' + y + ' at ' + x + '</p>'
};
var graph = nv.models.cumulativeLine()
.x(function(d,i) { return i }),
showTooltip = function(e) {
var offset = $(selector).offset(),
left = e.pos[0] + offset.left,
top = e.pos[1] + offset.top,
formatX = graph.xAxis.tickFormat(),
formatY = graph.yAxis.tickFormat(),
x = formatX(graph.x()(e, e.pointIndex)),
//x = formatX(graph.x()(e.point)),
y = formatY(graph.y()(e.point)),
content = tooltip(e.series.key, x, y, e, graph);
nvtooltip.show([left, top], content);
};
//setting component defaults
//graph.xAxis.tickFormat(d3.format(',r'));
graph.xAxis.tickFormat(function(d) {
//return d3.time.format('%x')(new Date(d))
return d3.time.format('%x')(new Date(data[0].values[d].x))
});
//graph.yAxis.tickFormat(d3.format(',.2f'));
graph.yAxis.tickFormat(d3.format(',.2%'));
//TODO: consider a method more similar to how the models are built
function chart() {
if (!selector || !data.length) return chart; //do nothing if you have nothing to work with
d3.select(selector).select('svg')
.datum(data)
.transition().duration(duration).call(graph); //consider using transition chaining like in the models
return chart;
}
// This should always only be called once, then update should be used after,
// in which case should consider the 'd3 way' and merge this with update,
// but simply do this on enter... should try anoter example that way
chart.build = function() {
if (!selector || !data.length) return chart; //do nothing if you have nothing to work with
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');
graph
.width(width)
.height(height);
svg
.attr('width', width())
.attr('height', height())
.datum(data)
.transition().duration(duration).call(graph);
return graph;
},
callback: function(graph) {
graph.dispatch.on('tooltipShow', showTooltip);
graph.dispatch.on('tooltipHide', nvtooltip.cleanup);
//TODO: create resize queue and have nv core handle resize instead of binding all to window resize
$(window).resize(function() {
// now that width and height are functions, should be automatic..of course you can always override them
d3.select(selector + ' svg')
.attr('width', graph.width()()) //need to set SVG dimensions, chart is not aware of the SVG component
.attr('height', graph.height()())
.call(graph);
});
}
});
return chart;
};
/*
// moved to chart()
chart.update = function() {
if (!selector || !data.length) return chart; //do nothing if you have nothing to work with
d3.select(selector).select('svg')
.datum(data)
.transition().duration(duration).call(graph);
return chart;
};
*/
chart.data = function(_) {
if (!arguments.length) return data;
data = _;
return chart;
};
chart.selector = function(_) {
if (!arguments.length) return selector;
selector = _;
return chart;
};
chart.duration = function(_) {
if (!arguments.length) return duration;
duration = _;
return chart;
};
chart.tooltip = function(_) {
if (!arguments.length) return tooltip;
tooltip = _;
return chart;
};
chart.xTickFormat = function(_) {
if (!arguments.length) return graph.xAxis.tickFormat();
graph.xAxis.tickFormat(typeof _ === 'function' ? _ : d3.format(_));
return chart;
};
chart.yTickFormat = function(_) {
if (!arguments.length) return graph.yAxis.tickFormat();
graph.yAxis.tickFormat(typeof _ === 'function' ? _ : d3.format(_));
return chart;
};
chart.xAxisLabel = function(_) {
if (!arguments.length) return graph.xAxis.axisLabel();
graph.xAxis.axisLabel(_);
return chart;
};
chart.yAxisLabel = function(_) {
if (!arguments.length) return graph.yAxis.axisLabel();
graph.yAxis.axisLabel(_);
return chart;
};
d3.rebind(chart, graph, 'x', 'y');
chart.graph = graph; // Give direct access for getter/setters, and dispatchers
return chart;
};

@ -33,6 +33,7 @@ nv.charts.lineChartDaily = function() {
//graph.xAxis.tickFormat(d3.format(',r'));
graph.xAxis.tickFormat(function(d) {
//return d3.time.format('%x')(new Date(d))
//log(d, data[0].values[d]);
return d3.time.format('%x')(new Date(data[0].values[d].x))
});

@ -1,8 +1,8 @@
nv.models.cumulativeLine = function() {
var margin = {top: 30, right: 20, bottom: 30, left: 60},
width = 960,
height = 500,
getWidth = function() { return 960 },
getHeight = function() { return 500 },
color = d3.scale.category10().range(),
dotRadius = function() { return 2.5 },
getX = function(d) { return d.x },
@ -42,22 +42,28 @@ nv.models.cumulativeLine = 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 = indexify(index.i, data);
var seriesData = series
.filter(function(d) { return !d.disabled })
.map(function(d) { return d.values });
x .domain(d3.extent(d3.merge(seriesData), getX ))
x .domain(d3.extent(d3.merge(seriesData), function(d) { return d.x } ))
.range([0, width - margin.left - margin.right]);
dx .domain([0, data[0].values.length - 1]) //Assumes all series have same length
.range([0, width - margin.left - margin.right])
.clamp(true);
y .domain(d3.extent(d3.merge(seriesData), getY ))
y .domain(d3.extent(d3.merge(seriesData), function(d) { return d.y } ))
.range([height - margin.top - margin.bottom, 0]);
lines
.width(width - margin.left - margin.right)
.height(height - margin.top - margin.bottom)
@ -191,12 +197,12 @@ nv.models.cumulativeLine = function() {
/* Normalize the data according to an index point. */
function indexify(idx, data) {
return data.map(function(line, i) {
var v = getY(line.values[idx]);
var v = getY(line.values[idx], idx);
return {
key: line.key,
values: line.values.map(function(point) {
return {'x': getX(point), 'y': (getY(point) - v) / (1 + v) };
values: line.values.map(function(point, pointIndex) {
return {'x': getX(point, pointIndex), 'y': (getY(point, pointIndex) - v) / (1 + v) };
}),
disabled: line.disabled,
hover: line.hover
@ -219,14 +225,14 @@ nv.models.cumulativeLine = function() {
chart.x = function(_) {
if (!arguments.length) return getX;
getX = _;
lines.x(_);
//lines.x(_);
return chart;
};
chart.y = function(_) {
if (!arguments.length) return getY;
getY = _;
lines.y(_);
//lines.y(_);
return chart;
};
@ -236,6 +242,7 @@ nv.models.cumulativeLine = function() {
return chart;
};
/*
chart.width = function(_) {
if (!arguments.length) return width;
width = _;
@ -247,6 +254,19 @@ nv.models.cumulativeLine = function() {
height = _;
return chart;
};
*/
chart.width = function(_) {
if (!arguments.length) return getWidth;
getWidth = 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;

Loading…
Cancel
Save