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,609 @@
|
|
1
|
+
|
2
|
+
nv.models.cumulativeLineChart = function() {
|
3
|
+
|
4
|
+
//============================================================
|
5
|
+
// Public Variables with Default Settings
|
6
|
+
//------------------------------------------------------------
|
7
|
+
|
8
|
+
var lines = nv.models.line()
|
9
|
+
, xAxis = nv.models.axis()
|
10
|
+
, yAxis = nv.models.axis()
|
11
|
+
, legend = nv.models.legend()
|
12
|
+
, controls = nv.models.legend()
|
13
|
+
;
|
14
|
+
|
15
|
+
var margin = {top: 30, right: 30, bottom: 50, left: 60}
|
16
|
+
, color = nv.utils.defaultColor()
|
17
|
+
, width = null
|
18
|
+
, height = null
|
19
|
+
, showLegend = true
|
20
|
+
, tooltips = true
|
21
|
+
, showControls = true
|
22
|
+
, rescaleY = true
|
23
|
+
, tooltip = function(key, x, y, e, graph) {
|
24
|
+
return '<h3>' + key + '</h3>' +
|
25
|
+
'<p>' + y + ' at ' + x + '</p>'
|
26
|
+
}
|
27
|
+
, x //can be accessed via chart.xScale()
|
28
|
+
, y //can be accessed via chart.yScale()
|
29
|
+
, id = lines.id()
|
30
|
+
, state = { index: 0, rescaleY: rescaleY }
|
31
|
+
, noData = 'No Data Available.'
|
32
|
+
, dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'stateChange', 'changeState')
|
33
|
+
;
|
34
|
+
|
35
|
+
xAxis
|
36
|
+
.orient('bottom')
|
37
|
+
.tickPadding(7)
|
38
|
+
;
|
39
|
+
yAxis
|
40
|
+
.orient('left')
|
41
|
+
;
|
42
|
+
|
43
|
+
//============================================================
|
44
|
+
|
45
|
+
|
46
|
+
//============================================================
|
47
|
+
// Private Variables
|
48
|
+
//------------------------------------------------------------
|
49
|
+
|
50
|
+
var dx = d3.scale.linear()
|
51
|
+
, index = {i: 0, x: 0}
|
52
|
+
;
|
53
|
+
|
54
|
+
var showTooltip = function(e, offsetElement) {
|
55
|
+
var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
|
56
|
+
top = e.pos[1] + ( offsetElement.offsetTop || 0),
|
57
|
+
x = xAxis.tickFormat()(lines.x()(e.point, e.pointIndex)),
|
58
|
+
y = yAxis.tickFormat()(lines.y()(e.point, e.pointIndex)),
|
59
|
+
content = tooltip(e.series.key, x, y, e, chart);
|
60
|
+
|
61
|
+
nv.tooltip.show([left, top], content, null, null, offsetElement);
|
62
|
+
};
|
63
|
+
|
64
|
+
/*
|
65
|
+
//Moved to see if we can get better behavior to fix issue #315
|
66
|
+
var indexDrag = d3.behavior.drag()
|
67
|
+
.on('dragstart', dragStart)
|
68
|
+
.on('drag', dragMove)
|
69
|
+
.on('dragend', dragEnd);
|
70
|
+
|
71
|
+
function dragStart(d,i) {
|
72
|
+
d3.select(chart.container)
|
73
|
+
.style('cursor', 'ew-resize');
|
74
|
+
}
|
75
|
+
|
76
|
+
function dragMove(d,i) {
|
77
|
+
d.x += d3.event.dx;
|
78
|
+
d.i = Math.round(dx.invert(d.x));
|
79
|
+
|
80
|
+
d3.select(this).attr('transform', 'translate(' + dx(d.i) + ',0)');
|
81
|
+
chart.update();
|
82
|
+
}
|
83
|
+
|
84
|
+
function dragEnd(d,i) {
|
85
|
+
d3.select(chart.container)
|
86
|
+
.style('cursor', 'auto');
|
87
|
+
chart.update();
|
88
|
+
}
|
89
|
+
*/
|
90
|
+
|
91
|
+
//============================================================
|
92
|
+
|
93
|
+
|
94
|
+
function chart(selection) {
|
95
|
+
selection.each(function(data) {
|
96
|
+
var container = d3.select(this).classed('nv-chart-' + id, true),
|
97
|
+
that = this;
|
98
|
+
|
99
|
+
var availableWidth = (width || parseInt(container.style('width')) || 960)
|
100
|
+
- margin.left - margin.right,
|
101
|
+
availableHeight = (height || parseInt(container.style('height')) || 400)
|
102
|
+
- margin.top - margin.bottom;
|
103
|
+
|
104
|
+
|
105
|
+
chart.update = function() { chart(selection) };
|
106
|
+
chart.container = this;
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
var indexDrag = d3.behavior.drag()
|
111
|
+
.on('dragstart', dragStart)
|
112
|
+
.on('drag', dragMove)
|
113
|
+
.on('dragend', dragEnd);
|
114
|
+
|
115
|
+
|
116
|
+
function dragStart(d,i) {
|
117
|
+
d3.select(chart.container)
|
118
|
+
.style('cursor', 'ew-resize');
|
119
|
+
}
|
120
|
+
|
121
|
+
function dragMove(d,i) {
|
122
|
+
index.x = d3.event.x;
|
123
|
+
index.i = Math.round(dx.invert(index.x));
|
124
|
+
updateZero();
|
125
|
+
}
|
126
|
+
|
127
|
+
function dragEnd(d,i) {
|
128
|
+
d3.select(chart.container)
|
129
|
+
.style('cursor', 'auto');
|
130
|
+
|
131
|
+
// update state and send stateChange with new index
|
132
|
+
state.index = index.i;
|
133
|
+
dispatch.stateChange(state);
|
134
|
+
}
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
//------------------------------------------------------------
|
140
|
+
// Display No Data message if there's nothing to show.
|
141
|
+
|
142
|
+
if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
|
143
|
+
var noDataText = container.selectAll('.nv-noData').data([noData]);
|
144
|
+
|
145
|
+
noDataText.enter().append('text')
|
146
|
+
.attr('class', 'nvd3 nv-noData')
|
147
|
+
.attr('dy', '-.7em')
|
148
|
+
.style('text-anchor', 'middle');
|
149
|
+
|
150
|
+
noDataText
|
151
|
+
.attr('x', margin.left + availableWidth / 2)
|
152
|
+
.attr('y', margin.top + availableHeight / 2)
|
153
|
+
.text(function(d) { return d });
|
154
|
+
|
155
|
+
return chart;
|
156
|
+
} else {
|
157
|
+
container.selectAll('.nv-noData').remove();
|
158
|
+
}
|
159
|
+
|
160
|
+
//------------------------------------------------------------
|
161
|
+
|
162
|
+
|
163
|
+
//------------------------------------------------------------
|
164
|
+
// Setup Scales
|
165
|
+
|
166
|
+
x = lines.xScale();
|
167
|
+
y = lines.yScale();
|
168
|
+
|
169
|
+
|
170
|
+
if (!rescaleY) {
|
171
|
+
var seriesDomains = data
|
172
|
+
.filter(function(series) { return !series.disabled })
|
173
|
+
.map(function(series,i) {
|
174
|
+
var initialDomain = d3.extent(series.values, lines.y());
|
175
|
+
|
176
|
+
//account for series being disabled when losing 95% or more
|
177
|
+
if (initialDomain[0] < -.95) initialDomain[0] = -.95;
|
178
|
+
|
179
|
+
return [
|
180
|
+
(initialDomain[0] - initialDomain[1]) / (1 + initialDomain[1]),
|
181
|
+
(initialDomain[1] - initialDomain[0]) / (1 + initialDomain[0])
|
182
|
+
];
|
183
|
+
});
|
184
|
+
|
185
|
+
var completeDomain = [
|
186
|
+
d3.min(seriesDomains, function(d) { return d[0] }),
|
187
|
+
d3.max(seriesDomains, function(d) { return d[1] })
|
188
|
+
]
|
189
|
+
|
190
|
+
lines.yDomain(completeDomain);
|
191
|
+
} else {
|
192
|
+
lines.yDomain(null);
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
dx .domain([0, data[0].values.length - 1]) //Assumes all series have same length
|
197
|
+
.range([0, availableWidth])
|
198
|
+
.clamp(true);
|
199
|
+
|
200
|
+
//------------------------------------------------------------
|
201
|
+
|
202
|
+
|
203
|
+
var data = indexify(index.i, data);
|
204
|
+
|
205
|
+
|
206
|
+
//------------------------------------------------------------
|
207
|
+
// Setup containers and skeleton of chart
|
208
|
+
|
209
|
+
var wrap = container.selectAll('g.nv-wrap.nv-cumulativeLine').data([data]);
|
210
|
+
var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-cumulativeLine').append('g');
|
211
|
+
var g = wrap.select('g');
|
212
|
+
|
213
|
+
gEnter.append('g').attr('class', 'nv-x nv-axis');
|
214
|
+
gEnter.append('g').attr('class', 'nv-y nv-axis');
|
215
|
+
gEnter.append('g').attr('class', 'nv-background');
|
216
|
+
gEnter.append('g').attr('class', 'nv-linesWrap');
|
217
|
+
gEnter.append('g').attr('class', 'nv-legendWrap');
|
218
|
+
gEnter.append('g').attr('class', 'nv-controlsWrap');
|
219
|
+
|
220
|
+
//------------------------------------------------------------
|
221
|
+
|
222
|
+
|
223
|
+
//------------------------------------------------------------
|
224
|
+
// Legend
|
225
|
+
|
226
|
+
if (showLegend) {
|
227
|
+
legend.width(availableWidth);
|
228
|
+
|
229
|
+
g.select('.nv-legendWrap')
|
230
|
+
.datum(data)
|
231
|
+
.call(legend);
|
232
|
+
|
233
|
+
if ( margin.top != legend.height()) {
|
234
|
+
margin.top = legend.height();
|
235
|
+
availableHeight = (height || parseInt(container.style('height')) || 400)
|
236
|
+
- margin.top - margin.bottom;
|
237
|
+
}
|
238
|
+
|
239
|
+
g.select('.nv-legendWrap')
|
240
|
+
.attr('transform', 'translate(0,' + (-margin.top) +')')
|
241
|
+
}
|
242
|
+
|
243
|
+
//------------------------------------------------------------
|
244
|
+
|
245
|
+
|
246
|
+
//------------------------------------------------------------
|
247
|
+
// Controls
|
248
|
+
|
249
|
+
if (showControls) {
|
250
|
+
var controlsData = [
|
251
|
+
{ key: 'Re-scale y-axis', disabled: !rescaleY }
|
252
|
+
];
|
253
|
+
|
254
|
+
controls.width(140).color(['#444', '#444', '#444']);
|
255
|
+
g.select('.nv-controlsWrap')
|
256
|
+
.datum(controlsData)
|
257
|
+
.attr('transform', 'translate(0,' + (-margin.top) +')')
|
258
|
+
.call(controls);
|
259
|
+
}
|
260
|
+
|
261
|
+
//------------------------------------------------------------
|
262
|
+
|
263
|
+
|
264
|
+
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
265
|
+
|
266
|
+
|
267
|
+
// Show error if series goes below 100%
|
268
|
+
var tempDisabled = data.filter(function(d) { return d.tempDisabled });
|
269
|
+
|
270
|
+
wrap.select('.tempDisabled').remove(); //clean-up and prevent duplicates
|
271
|
+
if (tempDisabled.length) {
|
272
|
+
wrap.append('text').attr('class', 'tempDisabled')
|
273
|
+
.attr('x', availableWidth / 2)
|
274
|
+
.attr('y', '-.71em')
|
275
|
+
.style('text-anchor', 'end')
|
276
|
+
.text(tempDisabled.map(function(d) { return d.key }).join(', ') + ' values cannot be calculated for this time period.');
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
//------------------------------------------------------------
|
282
|
+
// Main Chart Component(s)
|
283
|
+
|
284
|
+
gEnter.select('.nv-background')
|
285
|
+
.append('rect');
|
286
|
+
|
287
|
+
g.select('.nv-background rect')
|
288
|
+
.attr('width', availableWidth)
|
289
|
+
.attr('height', availableHeight);
|
290
|
+
|
291
|
+
lines
|
292
|
+
//.x(function(d) { return d.x })
|
293
|
+
.y(function(d) { return d.display.y })
|
294
|
+
.width(availableWidth)
|
295
|
+
.height(availableHeight)
|
296
|
+
.color(data.map(function(d,i) {
|
297
|
+
return d.color || color(d, i);
|
298
|
+
}).filter(function(d,i) { return !data[i].disabled && !data[i].tempDisabled }));
|
299
|
+
|
300
|
+
|
301
|
+
|
302
|
+
var linesWrap = g.select('.nv-linesWrap')
|
303
|
+
.datum(data.filter(function(d) { return !d.disabled && !d.tempDisabled }));
|
304
|
+
|
305
|
+
//d3.transition(linesWrap).call(lines);
|
306
|
+
linesWrap.call(lines);
|
307
|
+
|
308
|
+
|
309
|
+
var indexLine = linesWrap.selectAll('.nv-indexLine')
|
310
|
+
.data([index]);
|
311
|
+
indexLine.enter().append('rect').attr('class', 'nv-indexLine')
|
312
|
+
.attr('width', 3)
|
313
|
+
.attr('x', -2)
|
314
|
+
.attr('fill', 'red')
|
315
|
+
.attr('fill-opacity', .5)
|
316
|
+
.call(indexDrag)
|
317
|
+
|
318
|
+
indexLine
|
319
|
+
.attr('transform', function(d) { return 'translate(' + dx(d.i) + ',0)' })
|
320
|
+
.attr('height', availableHeight)
|
321
|
+
|
322
|
+
//------------------------------------------------------------
|
323
|
+
|
324
|
+
|
325
|
+
//------------------------------------------------------------
|
326
|
+
// Setup Axes
|
327
|
+
|
328
|
+
xAxis
|
329
|
+
.scale(x)
|
330
|
+
//Suggest how many ticks based on the chart width and D3 should listen (70 is the optimal number for MM/DD/YY dates)
|
331
|
+
.ticks( Math.min(data[0].values.length,availableWidth/70) )
|
332
|
+
.tickSize(-availableHeight, 0);
|
333
|
+
|
334
|
+
g.select('.nv-x.nv-axis')
|
335
|
+
.attr('transform', 'translate(0,' + y.range()[0] + ')');
|
336
|
+
d3.transition(g.select('.nv-x.nv-axis'))
|
337
|
+
.call(xAxis);
|
338
|
+
|
339
|
+
|
340
|
+
yAxis
|
341
|
+
.scale(y)
|
342
|
+
.ticks( availableHeight / 36 )
|
343
|
+
.tickSize( -availableWidth, 0);
|
344
|
+
|
345
|
+
d3.transition(g.select('.nv-y.nv-axis'))
|
346
|
+
.call(yAxis);
|
347
|
+
|
348
|
+
//------------------------------------------------------------
|
349
|
+
|
350
|
+
|
351
|
+
//============================================================
|
352
|
+
// Event Handling/Dispatching (in chart's scope)
|
353
|
+
//------------------------------------------------------------
|
354
|
+
|
355
|
+
|
356
|
+
function updateZero() {
|
357
|
+
indexLine
|
358
|
+
.data([index]);
|
359
|
+
|
360
|
+
chart.update();
|
361
|
+
}
|
362
|
+
|
363
|
+
g.select('.nv-background rect')
|
364
|
+
.on('click', function() {
|
365
|
+
index.x = d3.mouse(this)[0];
|
366
|
+
index.i = Math.round(dx.invert(index.x));
|
367
|
+
|
368
|
+
// update state and send stateChange with new index
|
369
|
+
state.index = index.i;
|
370
|
+
dispatch.stateChange(state);
|
371
|
+
|
372
|
+
updateZero();
|
373
|
+
});
|
374
|
+
|
375
|
+
lines.dispatch.on('elementClick', function(e) {
|
376
|
+
index.i = e.pointIndex;
|
377
|
+
index.x = dx(index.i);
|
378
|
+
|
379
|
+
// update state and send stateChange with new index
|
380
|
+
state.index = index.i;
|
381
|
+
dispatch.stateChange(state);
|
382
|
+
|
383
|
+
updateZero();
|
384
|
+
});
|
385
|
+
|
386
|
+
controls.dispatch.on('legendClick', function(d,i) {
|
387
|
+
d.disabled = !d.disabled;
|
388
|
+
rescaleY = !d.disabled;
|
389
|
+
|
390
|
+
state.rescaleY = rescaleY;
|
391
|
+
dispatch.stateChange(state);
|
392
|
+
|
393
|
+
//selection.transition().call(chart);
|
394
|
+
selection.call(chart);
|
395
|
+
});
|
396
|
+
|
397
|
+
|
398
|
+
legend.dispatch.on('legendClick', function(d,i) {
|
399
|
+
d.disabled = !d.disabled;
|
400
|
+
|
401
|
+
if (!data.filter(function(d) { return !d.disabled }).length) {
|
402
|
+
data.map(function(d) {
|
403
|
+
d.disabled = false;
|
404
|
+
wrap.selectAll('.nv-series').classed('disabled', false);
|
405
|
+
return d;
|
406
|
+
});
|
407
|
+
}
|
408
|
+
|
409
|
+
state.disabled = data.map(function(d) { return !!d.disabled });
|
410
|
+
dispatch.stateChange(state);
|
411
|
+
|
412
|
+
//selection.transition().call(chart);
|
413
|
+
selection.call(chart);
|
414
|
+
});
|
415
|
+
|
416
|
+
/*
|
417
|
+
//
|
418
|
+
legend.dispatch.on('legendMouseover', function(d, i) {
|
419
|
+
d.hover = true;
|
420
|
+
selection.transition().call(chart)
|
421
|
+
});
|
422
|
+
|
423
|
+
legend.dispatch.on('legendMouseout', function(d, i) {
|
424
|
+
d.hover = false;
|
425
|
+
selection.transition().call(chart)
|
426
|
+
});
|
427
|
+
*/
|
428
|
+
|
429
|
+
dispatch.on('tooltipShow', function(e) {
|
430
|
+
if (tooltips) showTooltip(e, that.parentNode);
|
431
|
+
});
|
432
|
+
|
433
|
+
|
434
|
+
// Update chart from a state object passed to event handler
|
435
|
+
dispatch.on('changeState', function(e) {
|
436
|
+
|
437
|
+
if (typeof e.disabled !== 'undefined') {
|
438
|
+
data.forEach(function(series,i) {
|
439
|
+
series.disabled = e.disabled[i];
|
440
|
+
});
|
441
|
+
|
442
|
+
state.disabled = e.disabled;
|
443
|
+
}
|
444
|
+
|
445
|
+
|
446
|
+
if (typeof e.index !== 'undefined') {
|
447
|
+
index.i = e.index;
|
448
|
+
index.x = dx(index.i);
|
449
|
+
|
450
|
+
state.index = e.index;
|
451
|
+
|
452
|
+
indexLine
|
453
|
+
.data([index]);
|
454
|
+
}
|
455
|
+
|
456
|
+
|
457
|
+
if (typeof e.rescaleY !== 'undefined') {
|
458
|
+
rescaleY = e.rescaleY;
|
459
|
+
}
|
460
|
+
|
461
|
+
selection.call(chart);
|
462
|
+
});
|
463
|
+
|
464
|
+
//============================================================
|
465
|
+
|
466
|
+
});
|
467
|
+
|
468
|
+
return chart;
|
469
|
+
}
|
470
|
+
|
471
|
+
|
472
|
+
//============================================================
|
473
|
+
// Event Handling/Dispatching (out of chart's scope)
|
474
|
+
//------------------------------------------------------------
|
475
|
+
|
476
|
+
lines.dispatch.on('elementMouseover.tooltip', function(e) {
|
477
|
+
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
|
478
|
+
dispatch.tooltipShow(e);
|
479
|
+
});
|
480
|
+
|
481
|
+
lines.dispatch.on('elementMouseout.tooltip', function(e) {
|
482
|
+
dispatch.tooltipHide(e);
|
483
|
+
});
|
484
|
+
|
485
|
+
dispatch.on('tooltipHide', function() {
|
486
|
+
if (tooltips) nv.tooltip.cleanup();
|
487
|
+
});
|
488
|
+
|
489
|
+
//============================================================
|
490
|
+
|
491
|
+
|
492
|
+
//============================================================
|
493
|
+
// Expose Public Variables
|
494
|
+
//------------------------------------------------------------
|
495
|
+
|
496
|
+
// expose chart's sub-components
|
497
|
+
chart.dispatch = dispatch;
|
498
|
+
chart.lines = lines;
|
499
|
+
chart.legend = legend;
|
500
|
+
chart.xAxis = xAxis;
|
501
|
+
chart.yAxis = yAxis;
|
502
|
+
|
503
|
+
d3.rebind(chart, lines, 'defined', 'isArea', 'x', 'y', 'size', 'xDomain', 'yDomain', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id');
|
504
|
+
|
505
|
+
chart.margin = function(_) {
|
506
|
+
if (!arguments.length) return margin;
|
507
|
+
margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
|
508
|
+
margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
|
509
|
+
margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
|
510
|
+
margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
|
511
|
+
return chart;
|
512
|
+
};
|
513
|
+
|
514
|
+
chart.width = function(_) {
|
515
|
+
if (!arguments.length) return width;
|
516
|
+
width = _;
|
517
|
+
return chart;
|
518
|
+
};
|
519
|
+
|
520
|
+
chart.height = function(_) {
|
521
|
+
if (!arguments.length) return height;
|
522
|
+
height = _;
|
523
|
+
return chart;
|
524
|
+
};
|
525
|
+
|
526
|
+
chart.color = function(_) {
|
527
|
+
if (!arguments.length) return color;
|
528
|
+
color = nv.utils.getColor(_);
|
529
|
+
legend.color(color);
|
530
|
+
return chart;
|
531
|
+
};
|
532
|
+
|
533
|
+
chart.rescaleY = function(_) {
|
534
|
+
if (!arguments.length) return rescaleY;
|
535
|
+
rescaleY = _
|
536
|
+
return rescaleY;
|
537
|
+
};
|
538
|
+
|
539
|
+
chart.showControls = function(_) {
|
540
|
+
if (!arguments.length) return showControls;
|
541
|
+
showControls = _;
|
542
|
+
return chart;
|
543
|
+
};
|
544
|
+
|
545
|
+
chart.showLegend = function(_) {
|
546
|
+
if (!arguments.length) return showLegend;
|
547
|
+
showLegend = _;
|
548
|
+
return chart;
|
549
|
+
};
|
550
|
+
|
551
|
+
chart.tooltips = function(_) {
|
552
|
+
if (!arguments.length) return tooltips;
|
553
|
+
tooltips = _;
|
554
|
+
return chart;
|
555
|
+
};
|
556
|
+
|
557
|
+
chart.tooltipContent = function(_) {
|
558
|
+
if (!arguments.length) return tooltip;
|
559
|
+
tooltip = _;
|
560
|
+
return chart;
|
561
|
+
};
|
562
|
+
|
563
|
+
chart.state = function(_) {
|
564
|
+
if (!arguments.length) return state;
|
565
|
+
state = _;
|
566
|
+
return chart;
|
567
|
+
};
|
568
|
+
|
569
|
+
chart.noData = function(_) {
|
570
|
+
if (!arguments.length) return noData;
|
571
|
+
noData = _;
|
572
|
+
return chart;
|
573
|
+
};
|
574
|
+
|
575
|
+
//============================================================
|
576
|
+
|
577
|
+
|
578
|
+
//============================================================
|
579
|
+
// Functions
|
580
|
+
//------------------------------------------------------------
|
581
|
+
|
582
|
+
/* Normalize the data according to an index point. */
|
583
|
+
function indexify(idx, data) {
|
584
|
+
return data.map(function(line, i) {
|
585
|
+
var v = lines.y()(line.values[idx], idx);
|
586
|
+
|
587
|
+
//TODO: implement check below, and disable series if series loses 100% or more cause divide by 0 issue
|
588
|
+
if (v < -.95) {
|
589
|
+
//if a series loses more than 100%, calculations fail.. anything close can cause major distortion (but is mathematically correct till it hits 100)
|
590
|
+
line.tempDisabled = true;
|
591
|
+
return line;
|
592
|
+
}
|
593
|
+
|
594
|
+
line.tempDisabled = false;
|
595
|
+
|
596
|
+
line.values = line.values.map(function(point, pointIndex) {
|
597
|
+
point.display = {'y': (lines.y()(point, pointIndex) - v) / (1 + v) };
|
598
|
+
return point;
|
599
|
+
})
|
600
|
+
|
601
|
+
return line;
|
602
|
+
})
|
603
|
+
}
|
604
|
+
|
605
|
+
//============================================================
|
606
|
+
|
607
|
+
|
608
|
+
return chart;
|
609
|
+
}
|