nvd3-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitmodules +3 -0
- data/README.md +101 -0
- data/lib/nvd3-rails.rb +1 -0
- data/lib/nvd3/rails.rb +8 -0
- data/lib/nvd3/rails/engine.rb +6 -0
- data/lib/nvd3/rails/version.rb +5 -0
- data/nvd3-rails.gemspec +21 -0
- data/vendor/assets/javascripts/nvd3-rails.js +7 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/HEAD +1 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/config +13 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/description +1 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/hooks/applypatch-msg.sample +15 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/hooks/commit-msg.sample +24 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/hooks/post-update.sample +8 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/hooks/pre-applypatch.sample +14 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/hooks/pre-commit.sample +50 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/hooks/pre-push.sample +53 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/hooks/pre-rebase.sample +169 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/hooks/prepare-commit-msg.sample +36 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/hooks/update.sample +128 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/index +0 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/info/exclude +6 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/logs/HEAD +1 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/logs/refs/heads/master +1 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/logs/refs/remotes/origin/HEAD +1 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/objects/pack/pack-babbb312d58dd7f03870b530a1b9a84c80918be3.idx +0 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/objects/pack/pack-babbb312d58dd7f03870b530a1b9a84c80918be3.pack +0 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/packed-refs +5 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/refs/heads/master +1 -0
- data/vendor/assets/javascripts/nvd3/.git.sample/refs/remotes/origin/HEAD +1 -0
- data/vendor/assets/javascripts/nvd3/.gitignore +24 -0
- data/vendor/assets/javascripts/nvd3/LICENSE.md +49 -0
- data/vendor/assets/javascripts/nvd3/Makefile +56 -0
- data/vendor/assets/javascripts/nvd3/README.md +43 -0
- data/vendor/assets/javascripts/nvd3/build.bat +6 -0
- data/vendor/assets/javascripts/nvd3/deprecated/bar.html +198 -0
- data/vendor/assets/javascripts/nvd3/deprecated/bar.js +250 -0
- data/vendor/assets/javascripts/nvd3/deprecated/charts/cumulativeLineChart.js +174 -0
- data/vendor/assets/javascripts/nvd3/deprecated/charts/discreteBarChart.js +157 -0
- data/vendor/assets/javascripts/nvd3/deprecated/charts/lineChart.js +159 -0
- data/vendor/assets/javascripts/nvd3/deprecated/charts/lineChartDaily.js +168 -0
- data/vendor/assets/javascripts/nvd3/deprecated/charts/stackedAreaChart.js +177 -0
- data/vendor/assets/javascripts/nvd3/deprecated/cumulativeLine.html +171 -0
- data/vendor/assets/javascripts/nvd3/deprecated/cumulativeLine.js +334 -0
- data/vendor/assets/javascripts/nvd3/deprecated/discreteBarChartWithEnabledTooltip.html +129 -0
- data/vendor/assets/javascripts/nvd3/deprecated/discreteBarChartWithEnabledTooltip.js +222 -0
- data/vendor/assets/javascripts/nvd3/deprecated/discreteBarWithAxes.html +172 -0
- data/vendor/assets/javascripts/nvd3/deprecated/discreteBarWithAxes.js +152 -0
- data/vendor/assets/javascripts/nvd3/deprecated/lineChart-old.html +83 -0
- data/vendor/assets/javascripts/nvd3/deprecated/lineChartDaily.html +109 -0
- data/vendor/assets/javascripts/nvd3/deprecated/linePlusBar.html +173 -0
- data/vendor/assets/javascripts/nvd3/deprecated/linePlusBar.js +250 -0
- data/vendor/assets/javascripts/nvd3/deprecated/lineWithFocus.html +137 -0
- data/vendor/assets/javascripts/nvd3/deprecated/lineWithFocus.js +354 -0
- data/vendor/assets/javascripts/nvd3/deprecated/lineWithFourAxes.html +144 -0
- data/vendor/assets/javascripts/nvd3/deprecated/lineWithFourAxes.js +218 -0
- data/vendor/assets/javascripts/nvd3/deprecated/lineWithLegend.html +142 -0
- data/vendor/assets/javascripts/nvd3/deprecated/lineWithLegend.js +176 -0
- data/vendor/assets/javascripts/nvd3/deprecated/monthendAxis.html +99 -0
- data/vendor/assets/javascripts/nvd3/deprecated/multiBarHorizontalWithLegend.html +258 -0
- data/vendor/assets/javascripts/nvd3/deprecated/multiBarHorizontalWithLegend.js +226 -0
- data/vendor/assets/javascripts/nvd3/deprecated/multiBarWithLegend.html +162 -0
- data/vendor/assets/javascripts/nvd3/deprecated/multiBarWithLegend.js +249 -0
- data/vendor/assets/javascripts/nvd3/deprecated/pie.js +263 -0
- data/vendor/assets/javascripts/nvd3/deprecated/scatterChart.html +110 -0
- data/vendor/assets/javascripts/nvd3/deprecated/scatterChart.js +294 -0
- data/vendor/assets/javascripts/nvd3/deprecated/scatterFisheyeChart.js +418 -0
- data/vendor/assets/javascripts/nvd3/deprecated/scatterWithLegend.html +167 -0
- data/vendor/assets/javascripts/nvd3/deprecated/scatterWithLegend.js +261 -0
- data/vendor/assets/javascripts/nvd3/deprecated/stackedArea.js +286 -0
- data/vendor/assets/javascripts/nvd3/deprecated/stackedAreaChart.html +183 -0
- data/vendor/assets/javascripts/nvd3/deprecated/stackedAreaChart_old.html +137 -0
- data/vendor/assets/javascripts/nvd3/deprecated/stackedAreaWithLegend.html +222 -0
- data/vendor/assets/javascripts/nvd3/deprecated/stackedAreaWithLegend.js +297 -0
- data/vendor/assets/javascripts/nvd3/examples/bullet.html +96 -0
- data/vendor/assets/javascripts/nvd3/examples/bulletChart.html +94 -0
- data/vendor/assets/javascripts/nvd3/examples/crossfilter.html +167 -0
- data/vendor/assets/javascripts/nvd3/examples/crossfilterWithDimentions.html +180 -0
- data/vendor/assets/javascripts/nvd3/examples/crossfilterWithTables.html +288 -0
- data/vendor/assets/javascripts/nvd3/examples/cumulativeLineChart.html +155 -0
- data/vendor/assets/javascripts/nvd3/examples/discreteBarChart.html +116 -0
- data/vendor/assets/javascripts/nvd3/examples/dynamicTimeSeries.html +148 -0
- data/vendor/assets/javascripts/nvd3/examples/historicalBar.html +157 -0
- data/vendor/assets/javascripts/nvd3/examples/horizon.html +163 -0
- data/vendor/assets/javascripts/nvd3/examples/images/grey-minus.png +0 -0
- data/vendor/assets/javascripts/nvd3/examples/images/grey-plus.png +0 -0
- data/vendor/assets/javascripts/nvd3/examples/indentedtree.html +126 -0
- data/vendor/assets/javascripts/nvd3/examples/legend.html +75 -0
- data/vendor/assets/javascripts/nvd3/examples/line.html +95 -0
- data/vendor/assets/javascripts/nvd3/examples/lineChart.html +112 -0
- data/vendor/assets/javascripts/nvd3/examples/lineChartSVGResize.html +151 -0
- data/vendor/assets/javascripts/nvd3/examples/linePlusBarChart.html +114 -0
- data/vendor/assets/javascripts/nvd3/examples/linePlusBarWithFocusChart.html +128 -0
- data/vendor/assets/javascripts/nvd3/examples/lineTimeSeries.html +142 -0
- data/vendor/assets/javascripts/nvd3/examples/lineWithFisheyeChart.html +101 -0
- data/vendor/assets/javascripts/nvd3/examples/lineWithFocusChart.html +87 -0
- data/vendor/assets/javascripts/nvd3/examples/multiBar.html +92 -0
- data/vendor/assets/javascripts/nvd3/examples/multiBarChart.html +93 -0
- data/vendor/assets/javascripts/nvd3/examples/multiBarHorizontalChart.html +388 -0
- data/vendor/assets/javascripts/nvd3/examples/multiChart.html +93 -0
- data/vendor/assets/javascripts/nvd3/examples/nations.json +1 -0
- data/vendor/assets/javascripts/nvd3/examples/pie.html +93 -0
- data/vendor/assets/javascripts/nvd3/examples/pieChart.html +114 -0
- data/vendor/assets/javascripts/nvd3/examples/scatter.html +95 -0
- data/vendor/assets/javascripts/nvd3/examples/scatterChart.html +115 -0
- data/vendor/assets/javascripts/nvd3/examples/scatterPlusLineChart.html +116 -0
- data/vendor/assets/javascripts/nvd3/examples/sparkline.html +62 -0
- data/vendor/assets/javascripts/nvd3/examples/sparklinePlus.html +67 -0
- data/vendor/assets/javascripts/nvd3/examples/stackedArea.html +155 -0
- data/vendor/assets/javascripts/nvd3/examples/stackedAreaChart.html +245 -0
- data/vendor/assets/javascripts/nvd3/examples/stream_layers.js +35 -0
- data/vendor/assets/javascripts/nvd3/lib/cie.js +155 -0
- data/vendor/assets/javascripts/nvd3/lib/crossfilter.js +1180 -0
- data/vendor/assets/javascripts/nvd3/lib/crossfilter.min.js +1 -0
- data/vendor/assets/javascripts/nvd3/lib/d3.v2.js +7033 -0
- data/vendor/assets/javascripts/nvd3/lib/d3.v2.min.js +4 -0
- data/vendor/assets/javascripts/nvd3/lib/fisheye.js +86 -0
- data/vendor/assets/javascripts/nvd3/lib/hive.js +80 -0
- data/vendor/assets/javascripts/nvd3/lib/horizon.js +192 -0
- data/vendor/assets/javascripts/nvd3/lib/sankey.js +292 -0
- data/vendor/assets/javascripts/nvd3/nv.d3.js +11762 -0
- data/vendor/assets/javascripts/nvd3/src/core.js +117 -0
- data/vendor/assets/javascripts/nvd3/src/models/axis.js +398 -0
- data/vendor/assets/javascripts/nvd3/src/models/backup/bullet.js +250 -0
- data/vendor/assets/javascripts/nvd3/src/models/backup/bulletChart.js +349 -0
- data/vendor/assets/javascripts/nvd3/src/models/bullet.js +377 -0
- data/vendor/assets/javascripts/nvd3/src/models/bulletChart.js +341 -0
- data/vendor/assets/javascripts/nvd3/src/models/cumulativeLineChart.js +609 -0
- data/vendor/assets/javascripts/nvd3/src/models/discreteBar.js +327 -0
- data/vendor/assets/javascripts/nvd3/src/models/discreteBarChart.js +290 -0
- data/vendor/assets/javascripts/nvd3/src/models/distribution.js +146 -0
- data/vendor/assets/javascripts/nvd3/src/models/historicalBar.js +289 -0
- data/vendor/assets/javascripts/nvd3/src/models/indentedTree.js +306 -0
- data/vendor/assets/javascripts/nvd3/src/models/legend.js +203 -0
- data/vendor/assets/javascripts/nvd3/src/models/line.js +286 -0
- data/vendor/assets/javascripts/nvd3/src/models/lineChart.js +359 -0
- data/vendor/assets/javascripts/nvd3/src/models/lineDynTimeSeriesChart.js +434 -0
- data/vendor/assets/javascripts/nvd3/src/models/linePlusBarChart.js +422 -0
- data/vendor/assets/javascripts/nvd3/src/models/linePlusBarWithFocusChart.js +657 -0
- data/vendor/assets/javascripts/nvd3/src/models/lineTimeSeriesChart.js +340 -0
- data/vendor/assets/javascripts/nvd3/src/models/lineWithFisheye.js +197 -0
- data/vendor/assets/javascripts/nvd3/src/models/lineWithFisheyeChart.js +324 -0
- data/vendor/assets/javascripts/nvd3/src/models/lineWithFocusChart.js +565 -0
- data/vendor/assets/javascripts/nvd3/src/models/multiAxisLineChart.js +312 -0
- data/vendor/assets/javascripts/nvd3/src/models/multiBar.js +416 -0
- data/vendor/assets/javascripts/nvd3/src/models/multiBarChart.js +445 -0
- data/vendor/assets/javascripts/nvd3/src/models/multiBarHorizontal.js +420 -0
- data/vendor/assets/javascripts/nvd3/src/models/multiBarHorizontalChart.js +415 -0
- data/vendor/assets/javascripts/nvd3/src/models/multiBarTimeSeries.js +371 -0
- data/vendor/assets/javascripts/nvd3/src/models/multiBarTimeSeriesChart.js +403 -0
- data/vendor/assets/javascripts/nvd3/src/models/multiChart.js +444 -0
- data/vendor/assets/javascripts/nvd3/src/models/ohlcBar.js +365 -0
- data/vendor/assets/javascripts/nvd3/src/models/pie.js +358 -0
- data/vendor/assets/javascripts/nvd3/src/models/pieChart.js +281 -0
- data/vendor/assets/javascripts/nvd3/src/models/scatter.js +622 -0
- data/vendor/assets/javascripts/nvd3/src/models/scatterChart.js +576 -0
- data/vendor/assets/javascripts/nvd3/src/models/scatterPlusLineChart.js +577 -0
- data/vendor/assets/javascripts/nvd3/src/models/sparkline.js +179 -0
- data/vendor/assets/javascripts/nvd3/src/models/sparklinePlus.js +291 -0
- data/vendor/assets/javascripts/nvd3/src/models/stackedArea.js +336 -0
- data/vendor/assets/javascripts/nvd3/src/models/stackedAreaChart.js +453 -0
- data/vendor/assets/javascripts/nvd3/src/nv.d3.css +671 -0
- data/vendor/assets/javascripts/nvd3/src/tooltip.js +129 -0
- data/vendor/assets/javascripts/nvd3/src/utils.js +105 -0
- data/vendor/assets/javascripts/set-nvd3-env.js.erb +5 -0
- data/vendor/assets/stylesheets/nvd3-rails.css +3 -0
- metadata +233 -0
@@ -0,0 +1,250 @@
|
|
1
|
+
|
2
|
+
nv.models.bar = function() {
|
3
|
+
var margin = {top: 20, right: 10, bottom: 80, left: 60},
|
4
|
+
width = 960,
|
5
|
+
height = 500,
|
6
|
+
animate = 500,
|
7
|
+
label ='label',
|
8
|
+
rotatedLabel = true,
|
9
|
+
showLabels = true,
|
10
|
+
id = Math.floor(Math.random() * 10000), //Create semi-unique ID in case user doesn't select one
|
11
|
+
color = d3.scale.category20(),
|
12
|
+
field ='y',
|
13
|
+
title = '';
|
14
|
+
|
15
|
+
var x = d3.scale.ordinal(),
|
16
|
+
y = d3.scale.linear(),
|
17
|
+
xAxis = d3.svg.axis().scale(x).orient('bottom'),
|
18
|
+
yAxis = d3.svg.axis().scale(y).orient('left'),
|
19
|
+
dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'tooltipShow', 'tooltipHide');
|
20
|
+
|
21
|
+
|
22
|
+
function chart(selection) {
|
23
|
+
selection.each(function(data) {
|
24
|
+
x .domain(data.map(function(d,i) { return d[label]; }))
|
25
|
+
.rangeRoundBands([0, width - margin.left - margin.right], .1);
|
26
|
+
|
27
|
+
|
28
|
+
var min = d3.min(data, function(d) { return d[field] });
|
29
|
+
var max = d3.max(data, function(d) { return d[field] });
|
30
|
+
var x0 = Math.max(-min, max);
|
31
|
+
var x1 = -x0;
|
32
|
+
|
33
|
+
// If we have no negative values, then lets stack this with just positive bars
|
34
|
+
if (min >= 0) x1 = 0;
|
35
|
+
|
36
|
+
y .domain([x1, x0])
|
37
|
+
.range([height - margin.top - margin.bottom, 0])
|
38
|
+
.nice();
|
39
|
+
|
40
|
+
xAxis.ticks( width / 100 );
|
41
|
+
yAxis.ticks( height / 36 ).tickSize(-(width - margin.right - margin.left), 0);
|
42
|
+
|
43
|
+
var parent = d3.select(this)
|
44
|
+
.on("click", function(d,i) {
|
45
|
+
dispatch.chartClick({
|
46
|
+
data: d,
|
47
|
+
index: i,
|
48
|
+
pos: d3.event,
|
49
|
+
id: id
|
50
|
+
});
|
51
|
+
});
|
52
|
+
|
53
|
+
|
54
|
+
var wrap = parent.selectAll('g.wrap').data([data]);
|
55
|
+
var gEnter = wrap.enter();
|
56
|
+
gEnter.append("text")
|
57
|
+
.attr("class", "title")
|
58
|
+
.attr("dy", ".91em")
|
59
|
+
.attr("text-anchor", "start")
|
60
|
+
.text(title);
|
61
|
+
gEnter = gEnter.append('g').attr('class', 'nvd3 wrap').attr('id','wrap-'+id).append('g');
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
gEnter.append('g').attr('class', 'x axis');
|
66
|
+
gEnter.append('g').attr('class', 'y axis');
|
67
|
+
gEnter.append('g').attr('class', 'bars');
|
68
|
+
|
69
|
+
|
70
|
+
wrap.attr('width', width)
|
71
|
+
.attr('height', height);
|
72
|
+
|
73
|
+
var g = wrap.select('g')
|
74
|
+
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
75
|
+
|
76
|
+
|
77
|
+
var bars = wrap.select('.bars').selectAll('.bar')
|
78
|
+
.data(function(d) { return d; });
|
79
|
+
|
80
|
+
bars.exit().remove();
|
81
|
+
|
82
|
+
|
83
|
+
var barsEnter = bars.enter().append('svg:rect')
|
84
|
+
.attr('class', function(d) { return d[field] < 0 ? "bar negative" : "bar positive"})
|
85
|
+
.attr("fill", function(d, i) { return color(i); })
|
86
|
+
.attr('x', 0 )
|
87
|
+
.on('mouseover', function(d,i){
|
88
|
+
d3.select(this).classed('hover', true);
|
89
|
+
dispatch.tooltipShow({
|
90
|
+
label: d[label],
|
91
|
+
value: d[field],
|
92
|
+
data: d,
|
93
|
+
index: i,
|
94
|
+
// TODO: Calculate the center to the bar
|
95
|
+
pos: [d3.event.pageX, d3.event.pageY],
|
96
|
+
id: id
|
97
|
+
});
|
98
|
+
|
99
|
+
})
|
100
|
+
.on('mouseout', function(d,i){
|
101
|
+
d3.select(this).classed('hover', false);
|
102
|
+
dispatch.tooltipHide({
|
103
|
+
label: d[label],
|
104
|
+
value: d[field],
|
105
|
+
data: d,
|
106
|
+
index: i,
|
107
|
+
id: id
|
108
|
+
});
|
109
|
+
})
|
110
|
+
.on('click', function(d,i) {
|
111
|
+
dispatch.elementClick({
|
112
|
+
label: d[label],
|
113
|
+
value: d[field],
|
114
|
+
data: d,
|
115
|
+
index: i,
|
116
|
+
pos: d3.event,
|
117
|
+
id: id
|
118
|
+
});
|
119
|
+
d3.event.stopPropagation();
|
120
|
+
})
|
121
|
+
.on('dblclick', function(d,i) {
|
122
|
+
dispatch.elementDblClick({
|
123
|
+
label: d[label],
|
124
|
+
value: d[field],
|
125
|
+
data: d,
|
126
|
+
index: i,
|
127
|
+
pos: d3.event,
|
128
|
+
id: id
|
129
|
+
});
|
130
|
+
d3.event.stopPropagation();
|
131
|
+
});
|
132
|
+
|
133
|
+
|
134
|
+
bars
|
135
|
+
.attr('class', function(d) { return d[field] < 0 ? "bar negative" : "bar positive"})
|
136
|
+
.attr('transform', function(d,i) { return 'translate(' + x(d[label]) + ',0)'; })
|
137
|
+
.attr('width', x.rangeBand )
|
138
|
+
.order()
|
139
|
+
.transition()
|
140
|
+
.duration(animate)
|
141
|
+
.attr('y', function(d) { return y(Math.max(0, d[field])); })
|
142
|
+
.attr('height', function(d) { return Math.abs(y(d[field]) - y(0)); });
|
143
|
+
|
144
|
+
|
145
|
+
g.select('.x.axis')
|
146
|
+
.attr('transform', 'translate(0,' + y.range()[0] + ')')
|
147
|
+
.call(xAxis);
|
148
|
+
|
149
|
+
|
150
|
+
if (rotatedLabel) {
|
151
|
+
g.select('.x.axis').selectAll('text').attr('text-anchor','start').attr("transform", function(d) {
|
152
|
+
return "rotate(35)translate(" + this.getBBox().height/2 + "," + '0' + ")";
|
153
|
+
});
|
154
|
+
}
|
155
|
+
if (!showLabels) {
|
156
|
+
g.select('.x.axis').selectAll('text').attr('fill', 'rgba(0,0,0,0)');
|
157
|
+
g.select('.x.axis').selectAll('line').attr('style', 'opacity: 0');
|
158
|
+
}
|
159
|
+
/*else {
|
160
|
+
g.select('.x.axis').selectAll('text').attr('fill', 'rgba(0,0,0,1)');
|
161
|
+
g.select('.x.axis').selectAll('line').attr('style', 'opacity: 1');
|
162
|
+
}*/
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
g.select('.y.axis')
|
167
|
+
.call(yAxis);
|
168
|
+
});
|
169
|
+
|
170
|
+
return chart;
|
171
|
+
}
|
172
|
+
|
173
|
+
chart.margin = function(_) {
|
174
|
+
if (!arguments.length) return margin;
|
175
|
+
margin = _;
|
176
|
+
return chart;
|
177
|
+
};
|
178
|
+
|
179
|
+
chart.width = function(_) {
|
180
|
+
if (!arguments.length) return width;
|
181
|
+
if (margin.left + margin.right + 20 > _)
|
182
|
+
width = margin.left + margin.right + 20; // Min width
|
183
|
+
else
|
184
|
+
width = _;
|
185
|
+
return chart;
|
186
|
+
};
|
187
|
+
|
188
|
+
chart.height = function(_) {
|
189
|
+
if (!arguments.length) return height;
|
190
|
+
if (margin.top + margin.bottom + 20 > _)
|
191
|
+
height = margin.top + margin.bottom + 20; // Min height
|
192
|
+
else
|
193
|
+
height = _;
|
194
|
+
return chart;
|
195
|
+
};
|
196
|
+
|
197
|
+
chart.animate = function(_) {
|
198
|
+
if (!arguments.length) return animate;
|
199
|
+
animate = _;
|
200
|
+
return chart;
|
201
|
+
};
|
202
|
+
|
203
|
+
chart.labelField = function(_) {
|
204
|
+
if (!arguments.length) return (label);
|
205
|
+
label = _;
|
206
|
+
return chart;
|
207
|
+
};
|
208
|
+
|
209
|
+
chart.dataField = function(_) {
|
210
|
+
if (!arguments.length) return (field);
|
211
|
+
field = _;
|
212
|
+
return chart;
|
213
|
+
};
|
214
|
+
|
215
|
+
chart.id = function(_) {
|
216
|
+
if (!arguments.length) return id;
|
217
|
+
id = _;
|
218
|
+
return chart;
|
219
|
+
};
|
220
|
+
|
221
|
+
chart.rotatedLabel = function(_) {
|
222
|
+
if (!arguments.length) return rotatedLabel;
|
223
|
+
rotatedLabel = _;
|
224
|
+
return chart;
|
225
|
+
};
|
226
|
+
|
227
|
+
chart.showLabels = function(_) {
|
228
|
+
if (!arguments.length) return (showLabels);
|
229
|
+
showLabels = _;
|
230
|
+
return chart;
|
231
|
+
};
|
232
|
+
|
233
|
+
chart.title = function(_) {
|
234
|
+
if (!arguments.length) return (title);
|
235
|
+
title = _;
|
236
|
+
return chart;
|
237
|
+
};
|
238
|
+
|
239
|
+
chart.xaxis = {};
|
240
|
+
// Expose the x-axis' tickFormat method.
|
241
|
+
d3.rebind(chart.xaxis, xAxis, 'tickFormat');
|
242
|
+
|
243
|
+
chart.yaxis = {};
|
244
|
+
// Expose the y-axis' tickFormat method.
|
245
|
+
d3.rebind(chart.yaxis, yAxis, 'tickFormat');
|
246
|
+
|
247
|
+
chart.dispatch = dispatch;
|
248
|
+
|
249
|
+
return chart;
|
250
|
+
}
|
@@ -0,0 +1,174 @@
|
|
1
|
+
|
2
|
+
// This technique works AS IS for month end data points
|
3
|
+
// In fact, this works for any series where each value is evenly spaced,
|
4
|
+
// and every series starts at the same value and is 1 to 1
|
5
|
+
// In other words, values at the same index, need to have the same x value
|
6
|
+
// for all series
|
7
|
+
//
|
8
|
+
// TODO: now that tooltips don't use jquery, could likely get rid of the charts
|
9
|
+
// collection by simply adding some optional functionality to the model
|
10
|
+
nv.charts.cumulativeLineChartDaily = function() {
|
11
|
+
var selector = null,
|
12
|
+
data = [],
|
13
|
+
duration = 500,
|
14
|
+
tooltip = function(key, x, y, e, graph) {
|
15
|
+
return '<h3>' + key + '</h3>' +
|
16
|
+
'<p>' + y + ' at ' + x + '</p>'
|
17
|
+
};
|
18
|
+
|
19
|
+
|
20
|
+
var graph = nv.models.cumulativeLine()
|
21
|
+
.x(function(d,i) { return i })
|
22
|
+
.color(d3.scale.category10().range()),
|
23
|
+
showTooltip = function(e) {
|
24
|
+
var offsetElement = document.getElementById(selector.substr(1)),
|
25
|
+
left = e.pos[0] + offsetElement.offsetLeft,
|
26
|
+
top = e.pos[1] + offsetElement.offsetTop,
|
27
|
+
formatX = graph.xAxis.tickFormat(),
|
28
|
+
formatY = graph.yAxis.tickFormat(),
|
29
|
+
x = formatX(graph.x()(e, e.pointIndex)),
|
30
|
+
//x = formatX(graph.x()(e.point)),
|
31
|
+
y = formatY(graph.y()(e.point)),
|
32
|
+
content = tooltip(e.series.key, x, y, e, graph);
|
33
|
+
|
34
|
+
nv.tooltip.show([left, top], content);
|
35
|
+
};
|
36
|
+
|
37
|
+
//setting component defaults
|
38
|
+
//graph.xAxis.tickFormat(d3.format(',r'));
|
39
|
+
graph.xAxis.tickFormat(function(d) {
|
40
|
+
//return d3.time.format('%x')(new Date(d))
|
41
|
+
return d3.time.format('%x')(new Date(data[0].values[d].x))
|
42
|
+
});
|
43
|
+
|
44
|
+
//graph.yAxis.tickFormat(d3.format(',.2f'));
|
45
|
+
graph.yAxis.tickFormat(d3.format(',.2%'));
|
46
|
+
|
47
|
+
|
48
|
+
//TODO: consider a method more similar to how the models are built
|
49
|
+
function chart() {
|
50
|
+
if (!selector || !data.length) return chart; //do nothing if you have nothing to work with
|
51
|
+
|
52
|
+
d3.select(selector).select('svg')
|
53
|
+
.datum(data)
|
54
|
+
.transition().duration(duration).call(graph); //consider using transition chaining like in the models
|
55
|
+
|
56
|
+
return chart;
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
// This should always only be called once, then update should be used after,
|
61
|
+
// in which case should consider the 'd3 way' and merge this with update,
|
62
|
+
// but simply do this on enter... will try another example the d3 way
|
63
|
+
chart.build = function() {
|
64
|
+
if (!selector || !data.length) return chart; //do nothing if you have nothing to work with
|
65
|
+
|
66
|
+
nv.addGraph({
|
67
|
+
generate: function() {
|
68
|
+
var container = d3.select(selector),
|
69
|
+
width = function() { return parseInt(container.style('width')) },
|
70
|
+
height = function() { return parseInt(container.style('height')) },
|
71
|
+
svg = container.append('svg');
|
72
|
+
|
73
|
+
graph
|
74
|
+
.width(width)
|
75
|
+
.height(height);
|
76
|
+
|
77
|
+
svg
|
78
|
+
.attr('width', width())
|
79
|
+
.attr('height', height())
|
80
|
+
.datum(data)
|
81
|
+
.transition().duration(duration).call(graph);
|
82
|
+
|
83
|
+
return graph;
|
84
|
+
},
|
85
|
+
callback: function(graph) {
|
86
|
+
graph.dispatch.on('tooltipShow', showTooltip);
|
87
|
+
graph.dispatch.on('tooltipHide', nv.tooltip.cleanup);
|
88
|
+
|
89
|
+
//TODO: fix issue of multiple graphs failing on resize
|
90
|
+
//TODO: create resize queue and have nv core handle resize instead of binding all to window resize
|
91
|
+
window.onresize =
|
92
|
+
function() {
|
93
|
+
// now that width and height are functions, should be automatic..of course you can always override them
|
94
|
+
d3.select(selector + ' svg')
|
95
|
+
.attr('width', graph.width()()) //need to set SVG dimensions, chart is not aware of the SVG component
|
96
|
+
.attr('height', graph.height()())
|
97
|
+
.call(graph);
|
98
|
+
};
|
99
|
+
}
|
100
|
+
});
|
101
|
+
|
102
|
+
return chart;
|
103
|
+
};
|
104
|
+
|
105
|
+
|
106
|
+
/*
|
107
|
+
// moved to chart()
|
108
|
+
chart.update = function() {
|
109
|
+
if (!selector || !data.length) return chart; //do nothing if you have nothing to work with
|
110
|
+
|
111
|
+
d3.select(selector).select('svg')
|
112
|
+
.datum(data)
|
113
|
+
.transition().duration(duration).call(graph);
|
114
|
+
|
115
|
+
return chart;
|
116
|
+
};
|
117
|
+
*/
|
118
|
+
|
119
|
+
chart.data = function(_) {
|
120
|
+
if (!arguments.length) return data;
|
121
|
+
data = _;
|
122
|
+
return chart;
|
123
|
+
};
|
124
|
+
|
125
|
+
chart.selector = function(_) {
|
126
|
+
if (!arguments.length) return selector;
|
127
|
+
selector = _;
|
128
|
+
return chart;
|
129
|
+
};
|
130
|
+
|
131
|
+
chart.duration = function(_) {
|
132
|
+
if (!arguments.length) return duration;
|
133
|
+
duration = _;
|
134
|
+
return chart;
|
135
|
+
};
|
136
|
+
|
137
|
+
chart.tooltip = function(_) {
|
138
|
+
if (!arguments.length) return tooltip;
|
139
|
+
tooltip = _;
|
140
|
+
return chart;
|
141
|
+
};
|
142
|
+
|
143
|
+
chart.xTickFormat = function(_) {
|
144
|
+
if (!arguments.length) return graph.xAxis.tickFormat();
|
145
|
+
graph.xAxis.tickFormat(typeof _ === 'function' ? _ : d3.format(_));
|
146
|
+
return chart;
|
147
|
+
};
|
148
|
+
|
149
|
+
chart.yTickFormat = function(_) {
|
150
|
+
if (!arguments.length) return graph.yAxis.tickFormat();
|
151
|
+
graph.yAxis.tickFormat(typeof _ === 'function' ? _ : d3.format(_));
|
152
|
+
return chart;
|
153
|
+
};
|
154
|
+
|
155
|
+
chart.xAxisLabel = function(_) {
|
156
|
+
if (!arguments.length) return graph.xAxis.axisLabel();
|
157
|
+
graph.xAxis.axisLabel(_);
|
158
|
+
return chart;
|
159
|
+
};
|
160
|
+
|
161
|
+
chart.yAxisLabel = function(_) {
|
162
|
+
if (!arguments.length) return graph.yAxis.axisLabel();
|
163
|
+
graph.yAxis.axisLabel(_);
|
164
|
+
return chart;
|
165
|
+
};
|
166
|
+
|
167
|
+
d3.rebind(chart, graph, 'x', 'y');
|
168
|
+
|
169
|
+
chart.graph = graph; // Give direct access for getter/setters, and dispatchers
|
170
|
+
|
171
|
+
return chart;
|
172
|
+
};
|
173
|
+
|
174
|
+
|
@@ -0,0 +1,157 @@
|
|
1
|
+
|
2
|
+
nv.charts.discreteBar = function() {
|
3
|
+
var selector = null,
|
4
|
+
data = [],
|
5
|
+
duration = 500,
|
6
|
+
tooltip = function(key, x, y, e, graph) {
|
7
|
+
return '<h3>' + x + '</h3>' +
|
8
|
+
'<p>' + y + '</p>'
|
9
|
+
};
|
10
|
+
|
11
|
+
|
12
|
+
var graph = nv.models.discreteBarWithAxes(),
|
13
|
+
showTooltip = function(e) {
|
14
|
+
var offsetElement = document.getElementById(selector.substr(1)),
|
15
|
+
left = e.pos[0] + offsetElement.offsetLeft,
|
16
|
+
top = e.pos[1] + offsetElement.offsetTop,
|
17
|
+
formatY = graph.yAxis.tickFormat(), //Assumes using same format as axis, can customize to show higher precision, etc.
|
18
|
+
formatX = graph.xAxis.tickFormat(),
|
19
|
+
x = formatX(graph.x()(e.point)),
|
20
|
+
y = formatY(graph.y()(e.point)),
|
21
|
+
content = tooltip(e.series.key, x, y, e, graph);
|
22
|
+
|
23
|
+
nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's');
|
24
|
+
};
|
25
|
+
|
26
|
+
//setting component defaults
|
27
|
+
graph.xAxis.tickFormat(function(d) { return d });
|
28
|
+
graph.yAxis.tickFormat(d3.format(',.f'));
|
29
|
+
|
30
|
+
|
31
|
+
//TODO: consider a method more similar to how the models are built
|
32
|
+
function chart() {
|
33
|
+
if (!selector || !data.length) return chart; //do nothing if you have nothing to work with
|
34
|
+
|
35
|
+
d3.select(selector).select('svg')
|
36
|
+
.datum(data)
|
37
|
+
.transition().duration(duration).call(graph); //consider using transition chaining like in the models
|
38
|
+
|
39
|
+
return chart;
|
40
|
+
}
|
41
|
+
|
42
|
+
|
43
|
+
// This should always only be called once, then update should be used after,
|
44
|
+
// in which case should consider the 'd3 way' and merge this with update,
|
45
|
+
// but simply do this on enter... should try anoter example that way
|
46
|
+
chart.build = function() {
|
47
|
+
if (!selector || !data.length) return chart; //do nothing if you have nothing to work with
|
48
|
+
|
49
|
+
nv.addGraph({
|
50
|
+
generate: function() {
|
51
|
+
var container = d3.select(selector),
|
52
|
+
width = function() { return parseInt(container.style('width')) },
|
53
|
+
height = function() { return parseInt(container.style('height')) },
|
54
|
+
svg = container.append('svg');
|
55
|
+
|
56
|
+
graph
|
57
|
+
.width(width)
|
58
|
+
.height(height);
|
59
|
+
|
60
|
+
svg
|
61
|
+
.attr('width', width())
|
62
|
+
.attr('height', height())
|
63
|
+
.datum(data)
|
64
|
+
.transition().duration(duration).call(graph);
|
65
|
+
|
66
|
+
return graph;
|
67
|
+
},
|
68
|
+
callback: function(graph) {
|
69
|
+
graph.dispatch.on('tooltipShow', showTooltip);
|
70
|
+
graph.dispatch.on('tooltipHide', nv.tooltip.cleanup);
|
71
|
+
|
72
|
+
//TODO: create resize queue and have nv core handle resize instead of binding all to window resize
|
73
|
+
nv.utils.windowResize(
|
74
|
+
function() {
|
75
|
+
// now that width and height are functions, should be automatic..of course you can always override them
|
76
|
+
d3.select(selector + ' svg')
|
77
|
+
.attr('width', graph.width()()) //need to set SVG dimensions, chart is not aware of the SVG component
|
78
|
+
.attr('height', graph.height()())
|
79
|
+
.transition().duration(duration).call(graph);
|
80
|
+
//.call(graph);
|
81
|
+
}
|
82
|
+
);
|
83
|
+
}
|
84
|
+
});
|
85
|
+
|
86
|
+
return chart;
|
87
|
+
};
|
88
|
+
|
89
|
+
|
90
|
+
/*
|
91
|
+
// moved to chart()
|
92
|
+
chart.update = function() {
|
93
|
+
if (!selector || !data.length) return chart; //do nothing if you have nothing to work with
|
94
|
+
|
95
|
+
d3.select(selector).select('svg')
|
96
|
+
.datum(data)
|
97
|
+
.transition().duration(duration).call(graph);
|
98
|
+
|
99
|
+
return chart;
|
100
|
+
};
|
101
|
+
*/
|
102
|
+
|
103
|
+
chart.data = function(_) {
|
104
|
+
if (!arguments.length) return data;
|
105
|
+
data = _;
|
106
|
+
return chart;
|
107
|
+
};
|
108
|
+
|
109
|
+
chart.selector = function(_) {
|
110
|
+
if (!arguments.length) return selector;
|
111
|
+
selector = _;
|
112
|
+
return chart;
|
113
|
+
};
|
114
|
+
|
115
|
+
chart.duration = function(_) {
|
116
|
+
if (!arguments.length) return duration;
|
117
|
+
duration = _;
|
118
|
+
return chart;
|
119
|
+
};
|
120
|
+
|
121
|
+
chart.tooltip = function(_) {
|
122
|
+
if (!arguments.length) return tooltip;
|
123
|
+
tooltip = _;
|
124
|
+
return chart;
|
125
|
+
};
|
126
|
+
|
127
|
+
chart.xTickFormat = function(_) {
|
128
|
+
if (!arguments.length) return graph.xAxis.tickFormat();
|
129
|
+
graph.xAxis.tickFormat(typeof _ === 'function' ? _ : d3.format(_));
|
130
|
+
return chart;
|
131
|
+
};
|
132
|
+
|
133
|
+
chart.yTickFormat = function(_) {
|
134
|
+
if (!arguments.length) return graph.yAxis.tickFormat();
|
135
|
+
graph.yAxis.tickFormat(typeof _ === 'function' ? _ : d3.format(_));
|
136
|
+
return chart;
|
137
|
+
};
|
138
|
+
|
139
|
+
chart.xAxisLabel = function(_) {
|
140
|
+
if (!arguments.length) return graph.xAxis.axisLabel();
|
141
|
+
graph.xAxis.axisLabel(_);
|
142
|
+
return chart;
|
143
|
+
};
|
144
|
+
|
145
|
+
chart.yAxisLabel = function(_) {
|
146
|
+
if (!arguments.length) return graph.yAxis.axisLabel();
|
147
|
+
graph.yAxis.axisLabel(_);
|
148
|
+
return chart;
|
149
|
+
};
|
150
|
+
|
151
|
+
d3.rebind(chart, graph, 'x', 'y', 'staggerLabels');
|
152
|
+
|
153
|
+
chart.graph = graph; // Give direct access for getter/setters, and dispatchers
|
154
|
+
|
155
|
+
return chart;
|
156
|
+
};
|
157
|
+
|