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,263 @@
|
|
1
|
+
|
2
|
+
nv.models.pie = function() {
|
3
|
+
var margin = {top: 20, right: 20, bottom: 20, left: 20},
|
4
|
+
width = 500,
|
5
|
+
height = 500,
|
6
|
+
animate = 2000,
|
7
|
+
radius = Math.min(width-(margin.right+margin.left), height-(margin.top+margin.bottom)) / 2,
|
8
|
+
label ='label',
|
9
|
+
field ='y',
|
10
|
+
id = Math.floor(Math.random() * 10000), //Create semi-unique ID in case user doesn't select one
|
11
|
+
color = d3.scale.category20(),
|
12
|
+
showLabels = true,
|
13
|
+
donut = false,
|
14
|
+
title = '';
|
15
|
+
|
16
|
+
var lastWidth = 0,
|
17
|
+
lastHeight = 0;
|
18
|
+
|
19
|
+
|
20
|
+
var dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'tooltipShow', 'tooltipHide');
|
21
|
+
|
22
|
+
function chart(selection) {
|
23
|
+
selection.each(function(data) {
|
24
|
+
|
25
|
+
var svg = d3.select(this)
|
26
|
+
.on("click", function(d,i) {
|
27
|
+
dispatch.chartClick({
|
28
|
+
data: d,
|
29
|
+
index: i,
|
30
|
+
pos: d3.event,
|
31
|
+
id: id
|
32
|
+
});
|
33
|
+
});
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
var background = svg.selectAll('svg.margin').data([data]);
|
38
|
+
var parent = background.enter();
|
39
|
+
parent.append("text")
|
40
|
+
.attr("class", "title")
|
41
|
+
.attr("dy", ".91em")
|
42
|
+
.attr("text-anchor", "start")
|
43
|
+
.text(title);
|
44
|
+
parent.append('svg')
|
45
|
+
.attr('class','margin')
|
46
|
+
.attr('x', margin.left)
|
47
|
+
.attr('y', margin.top)
|
48
|
+
.style('overflow','visible');
|
49
|
+
|
50
|
+
var wrap = background.selectAll('g.wrap').data([data]);
|
51
|
+
wrap.exit().remove();
|
52
|
+
var wEnter = wrap.enter();
|
53
|
+
|
54
|
+
wEnter
|
55
|
+
.append('g')
|
56
|
+
.attr('class', 'wrap')
|
57
|
+
.attr('id','wrap-'+id)
|
58
|
+
.append('g')
|
59
|
+
.attr('class', 'pie');
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
wrap
|
64
|
+
.attr('width', width) //-(margin.left+margin.right))
|
65
|
+
.attr('height', height) //-(margin.top+margin.bottom))
|
66
|
+
.attr("transform", "translate(" + radius + "," + radius + ")");
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
var arc = d3.svg.arc()
|
72
|
+
.outerRadius((radius-(radius / 5)));
|
73
|
+
|
74
|
+
if (donut) arc.innerRadius(radius / 2);
|
75
|
+
|
76
|
+
|
77
|
+
// Setup the Pie chart and choose the data element
|
78
|
+
var pie = d3.layout.pie()
|
79
|
+
.value(function (d) { return d[field]; });
|
80
|
+
|
81
|
+
var slices = background.select('.pie').selectAll(".slice")
|
82
|
+
.data(pie);
|
83
|
+
|
84
|
+
slices.exit().remove();
|
85
|
+
|
86
|
+
var ae = slices.enter().append("svg:g")
|
87
|
+
.attr("class", "slice")
|
88
|
+
.on('mouseover', function(d,i){
|
89
|
+
d3.select(this).classed('hover', true);
|
90
|
+
dispatch.tooltipShow({
|
91
|
+
label: d.data[label],
|
92
|
+
value: d.data[field],
|
93
|
+
data: d.data,
|
94
|
+
index: i,
|
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.data[label],
|
104
|
+
value: d.data[field],
|
105
|
+
data: d.data,
|
106
|
+
index: i,
|
107
|
+
id: id
|
108
|
+
});
|
109
|
+
})
|
110
|
+
.on('click', function(d,i) {
|
111
|
+
dispatch.elementClick({
|
112
|
+
label: d.data[label],
|
113
|
+
value: d.data[field],
|
114
|
+
data: d.data,
|
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.data[label],
|
124
|
+
value: d.data[field],
|
125
|
+
data: d.data,
|
126
|
+
index: i,
|
127
|
+
pos: d3.event,
|
128
|
+
id: id
|
129
|
+
});
|
130
|
+
d3.event.stopPropagation();
|
131
|
+
});
|
132
|
+
|
133
|
+
var paths = ae.append("svg:path")
|
134
|
+
.attr('class','path')
|
135
|
+
.attr("fill", function(d, i) { return color(i); });
|
136
|
+
//.attr('d', arc);
|
137
|
+
|
138
|
+
slices.select('.path')
|
139
|
+
.attr('d', arc)
|
140
|
+
.transition()
|
141
|
+
.ease("bounce")
|
142
|
+
.duration(animate)
|
143
|
+
.attrTween("d", tweenPie);
|
144
|
+
|
145
|
+
if (showLabels) {
|
146
|
+
// This does the normal label
|
147
|
+
ae.append("text");
|
148
|
+
|
149
|
+
slices.select("text")
|
150
|
+
.transition()
|
151
|
+
.duration(animate)
|
152
|
+
.ease('bounce')
|
153
|
+
.attr("transform", function(d) {
|
154
|
+
d.outerRadius = radius + 10; // Set Outer Coordinate
|
155
|
+
d.innerRadius = radius + 15; // Set Inner Coordinate
|
156
|
+
return "translate(" + arc.centroid(d) + ")";
|
157
|
+
})
|
158
|
+
.attr("text-anchor", "middle") //center the text on it's origin
|
159
|
+
.style("font", "bold 12px Arial")
|
160
|
+
.text(function(d, i) { return d.data[label]; });
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
// Computes the angle of an arc, converting from radians to degrees.
|
165
|
+
function angle(d) {
|
166
|
+
var a = (d.startAngle + d.endAngle) * 90 / Math.PI - 90;
|
167
|
+
return a > 90 ? a - 180 : a;
|
168
|
+
}
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
function tweenPie(b) {
|
175
|
+
b.innerRadius = 0;
|
176
|
+
var i = d3.interpolate({startAngle: 0, endAngle: 0}, b);
|
177
|
+
return function(t) {
|
178
|
+
return arc(i(t));
|
179
|
+
};
|
180
|
+
}
|
181
|
+
|
182
|
+
|
183
|
+
});
|
184
|
+
|
185
|
+
return chart;
|
186
|
+
}
|
187
|
+
|
188
|
+
chart.margin = function(_) {
|
189
|
+
if (!arguments.length) return margin;
|
190
|
+
margin = _;
|
191
|
+
return chart;
|
192
|
+
};
|
193
|
+
|
194
|
+
chart.width = function(_) {
|
195
|
+
if (!arguments.length) return width;
|
196
|
+
if (margin.left + margin.right + 20 > _) {
|
197
|
+
width = margin.left + margin.right + 20; // Min width
|
198
|
+
} else {
|
199
|
+
width = _;
|
200
|
+
}
|
201
|
+
radius = Math.min(width-(margin.left+margin.right), height-(margin.top+margin.bottom)) / 2;
|
202
|
+
return chart;
|
203
|
+
};
|
204
|
+
|
205
|
+
chart.height = function(_) {
|
206
|
+
if (!arguments.length) return height;
|
207
|
+
if (margin.top + margin.bottom + 20 > _) {
|
208
|
+
height = margin.top + margin.bottom + 20; // Min height
|
209
|
+
} else {
|
210
|
+
height = _;
|
211
|
+
}
|
212
|
+
radius = Math.min(width-(margin.left+margin.right), height-(margin.top+margin.bottom)) / 2;
|
213
|
+
return chart;
|
214
|
+
};
|
215
|
+
|
216
|
+
chart.animate = function(_) {
|
217
|
+
if (!arguments.length) return animate;
|
218
|
+
animate = _;
|
219
|
+
return chart;
|
220
|
+
};
|
221
|
+
|
222
|
+
chart.labelField = function(_) {
|
223
|
+
if (!arguments.length) return (label);
|
224
|
+
label = _;
|
225
|
+
return chart;
|
226
|
+
};
|
227
|
+
|
228
|
+
chart.dataField = function(_) {
|
229
|
+
if (!arguments.length) return (field);
|
230
|
+
field = _;
|
231
|
+
return chart;
|
232
|
+
};
|
233
|
+
|
234
|
+
chart.showLabels = function(_) {
|
235
|
+
if (!arguments.length) return (showLabels);
|
236
|
+
showLabels = _;
|
237
|
+
return chart;
|
238
|
+
};
|
239
|
+
|
240
|
+
chart.donut = function(_) {
|
241
|
+
if (!arguments.length) return (donut);
|
242
|
+
donut = _;
|
243
|
+
return chart;
|
244
|
+
};
|
245
|
+
|
246
|
+
chart.title = function(_) {
|
247
|
+
if (!arguments.length) return (title);
|
248
|
+
title = _;
|
249
|
+
return chart;
|
250
|
+
};
|
251
|
+
|
252
|
+
chart.id = function(_) {
|
253
|
+
if (!arguments.length) return id;
|
254
|
+
id = _;
|
255
|
+
return chart;
|
256
|
+
};
|
257
|
+
|
258
|
+
chart.dispatch = dispatch;
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
return chart;
|
263
|
+
}
|
@@ -0,0 +1,110 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<meta charset="utf-8">
|
3
|
+
|
4
|
+
<link href="../src/d3.css" rel="stylesheet" type="text/css">
|
5
|
+
|
6
|
+
<style>
|
7
|
+
|
8
|
+
body {
|
9
|
+
overflow-y:scroll;
|
10
|
+
margin: 0;
|
11
|
+
padding: 0;
|
12
|
+
}
|
13
|
+
|
14
|
+
svg {
|
15
|
+
overflow: hidden;
|
16
|
+
}
|
17
|
+
|
18
|
+
div {
|
19
|
+
border: 0;
|
20
|
+
margin: 0;
|
21
|
+
}
|
22
|
+
|
23
|
+
/*
|
24
|
+
#offsetDiv {
|
25
|
+
margin-left: 100px;
|
26
|
+
margin-top: 100px;
|
27
|
+
}
|
28
|
+
*/
|
29
|
+
|
30
|
+
#test1 {
|
31
|
+
margin: 0;
|
32
|
+
height: 500px;
|
33
|
+
}
|
34
|
+
|
35
|
+
#test1 svg {
|
36
|
+
height: 500px;
|
37
|
+
}
|
38
|
+
|
39
|
+
</style>
|
40
|
+
|
41
|
+
<body>
|
42
|
+
|
43
|
+
<div id="offsetDiv">
|
44
|
+
<div id="test1" class="chartWrap">
|
45
|
+
<svg></svg>
|
46
|
+
</div>
|
47
|
+
</div>
|
48
|
+
|
49
|
+
<script src="../lib/d3.v2.js"></script>
|
50
|
+
<script src="../lib/fisheye.js"></script>
|
51
|
+
<script src="../nv.d3.js"></script>
|
52
|
+
<script src="../src/tooltip.js"></script>
|
53
|
+
<script src="../src/models/legend.js"></script>
|
54
|
+
<script src="../src/models/axis.js"></script>
|
55
|
+
<script src="../src/models/distribution.js"></script>
|
56
|
+
<script src="../src/models/scatter.js"></script>
|
57
|
+
<script src="../src/models/scatterChart.js"></script>
|
58
|
+
<script>
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
//Format A
|
63
|
+
nv.addGraph(function() {
|
64
|
+
var chart = nv.models.scatterChart()
|
65
|
+
.showDistX(true)
|
66
|
+
.showDistY(true)
|
67
|
+
//.height(500)
|
68
|
+
.color(d3.scale.category10().range());
|
69
|
+
|
70
|
+
chart.xAxis.tickFormat(d3.format('.02f'))
|
71
|
+
chart.yAxis.tickFormat(d3.format('.02f'))
|
72
|
+
|
73
|
+
d3.select('#test1 svg')
|
74
|
+
.datum(randomData(4,40))
|
75
|
+
.transition().duration(500)
|
76
|
+
.call(chart);
|
77
|
+
|
78
|
+
nv.utils.windowResize(chart.update);
|
79
|
+
|
80
|
+
return chart;
|
81
|
+
});
|
82
|
+
|
83
|
+
|
84
|
+
function randomData(groups, points) { //# groups,# points per group
|
85
|
+
var data = [],
|
86
|
+
shapes = ['circle', 'cross', 'triangle-up', 'triangle-down', 'diamond', 'square'],
|
87
|
+
random = d3.random.normal();
|
88
|
+
|
89
|
+
for (i = 0; i < groups; i++) {
|
90
|
+
data.push({
|
91
|
+
key: 'Group ' + i,
|
92
|
+
values: []
|
93
|
+
});
|
94
|
+
|
95
|
+
for (j = 0; j < points; j++) {
|
96
|
+
data[i].values.push({
|
97
|
+
x: random(),
|
98
|
+
y: random(),
|
99
|
+
size: Math.random(),
|
100
|
+
shape: shapes[j % 6]
|
101
|
+
});
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
return data;
|
106
|
+
}
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
</script>
|
@@ -0,0 +1,294 @@
|
|
1
|
+
|
2
|
+
nv.models.scatterChart = function() {
|
3
|
+
var margin = {top: 30, right: 20, bottom: 50, left: 60},
|
4
|
+
width = null,
|
5
|
+
height = null,
|
6
|
+
color = d3.scale.category20().range(),
|
7
|
+
showDistX = false,
|
8
|
+
showDistY = false,
|
9
|
+
showLegend = true,
|
10
|
+
tooltips = true,
|
11
|
+
tooltipX = function(key, x, y) { return '<strong>' + x + '</strong>' },
|
12
|
+
tooltipY = function(key, x, y) { return '<strong>' + y + '</strong>' },
|
13
|
+
tooltip = function(key, x, y, e, graph) {
|
14
|
+
return '<h3>' + key + '</h3>' +
|
15
|
+
'<p>' + y + ' at ' + x + '</p>'
|
16
|
+
};
|
17
|
+
|
18
|
+
|
19
|
+
var scatter = nv.models.scatter(),
|
20
|
+
x = scatter.xScale(),
|
21
|
+
y = scatter.yScale(),
|
22
|
+
xAxis = nv.models.axis().orient('bottom').scale(x).tickPadding(10),
|
23
|
+
yAxis = nv.models.axis().orient('left').scale(y).tickPadding(10),
|
24
|
+
legend = nv.models.legend().height(30),
|
25
|
+
distX = nv.models.distribution().axis('x').scale(x),
|
26
|
+
distY = nv.models.distribution().axis('y').scale(y),
|
27
|
+
dispatch = d3.dispatch('tooltipShow', 'tooltipHide'),
|
28
|
+
x0, y0; //TODO: abstract distribution component and have old scales stored there
|
29
|
+
|
30
|
+
var showTooltip = function(e, offsetElement) {
|
31
|
+
//TODO: make tooltip style an option between single or dual on axes (maybe on all charts with axes?)
|
32
|
+
|
33
|
+
//var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
|
34
|
+
//top = e.pos[1] + ( offsetElement.offsetTop || 0),
|
35
|
+
var leftX = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
|
36
|
+
topX = y.range()[0] + margin.top + ( offsetElement.offsetTop || 0),
|
37
|
+
leftY = x.range()[0] + margin.left + ( offsetElement.offsetLeft || 0 ),
|
38
|
+
topY = e.pos[1] + ( offsetElement.offsetTop || 0),
|
39
|
+
xVal = xAxis.tickFormat()(scatter.x()(e.point)),
|
40
|
+
yVal = yAxis.tickFormat()(scatter.y()(e.point)),
|
41
|
+
contentX = tooltipX(e.series.key, xVal, yVal, e, chart),
|
42
|
+
contentY = tooltipY(e.series.key, xVal, yVal, e, chart);
|
43
|
+
//content = tooltip(e.series.key, xVal, yVal, e, chart);
|
44
|
+
|
45
|
+
nv.tooltip.show([leftX, topX], contentX, 'n', 1);
|
46
|
+
nv.tooltip.show([leftY, topY], contentY, 'e', 1);
|
47
|
+
//nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's');
|
48
|
+
};
|
49
|
+
|
50
|
+
|
51
|
+
function chart(selection) {
|
52
|
+
selection.each(function(data) {
|
53
|
+
var container = d3.select(this),
|
54
|
+
that = this;
|
55
|
+
|
56
|
+
//TODO: decide if this makes sense to add into all the models for ease of updating (updating without needing the selection)
|
57
|
+
chart.update = function() { selection.transition().call(chart) };
|
58
|
+
|
59
|
+
|
60
|
+
var availableWidth = (width || parseInt(container.style('width')) || 960)
|
61
|
+
- margin.left - margin.right,
|
62
|
+
availableHeight = (height || parseInt(container.style('height')) || 400)
|
63
|
+
- margin.top - margin.bottom;
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
x0 = x0 || scatter.xScale();
|
68
|
+
y0 = y0 || scatter.yScale();
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
var wrap = container.selectAll('g.wrap.scatterChart').data([data]);
|
73
|
+
var gEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 scatterChart chart-' + scatter.id()).append('g');
|
74
|
+
|
75
|
+
gEnter.append('g').attr('class', 'legendWrap');
|
76
|
+
gEnter.append('g').attr('class', 'x axis');
|
77
|
+
gEnter.append('g').attr('class', 'y axis');
|
78
|
+
gEnter.append('g').attr('class', 'scatterWrap');
|
79
|
+
gEnter.append('g').attr('class', 'distWrap');
|
80
|
+
|
81
|
+
var g = wrap.select('g')
|
82
|
+
|
83
|
+
if (showLegend) {
|
84
|
+
legend.width( availableWidth / 2 );
|
85
|
+
|
86
|
+
wrap.select('.legendWrap')
|
87
|
+
.datum(data)
|
88
|
+
.call(legend);
|
89
|
+
|
90
|
+
if ( margin.top != legend.height()) {
|
91
|
+
margin.top = legend.height();
|
92
|
+
availableHeight = (height || parseInt(container.style('height')) || 400)
|
93
|
+
- margin.top - margin.bottom;
|
94
|
+
}
|
95
|
+
|
96
|
+
wrap.select('.legendWrap')
|
97
|
+
.attr('transform', 'translate(' + (availableWidth / 2) + ',' + (-margin.top) +')');
|
98
|
+
}
|
99
|
+
|
100
|
+
|
101
|
+
scatter
|
102
|
+
.width(availableWidth)
|
103
|
+
.height(availableHeight)
|
104
|
+
.color(data.map(function(d,i) {
|
105
|
+
return d.color || color[i % color.length];
|
106
|
+
}).filter(function(d,i) { return !data[i].disabled }))
|
107
|
+
|
108
|
+
|
109
|
+
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
110
|
+
|
111
|
+
|
112
|
+
var scatterWrap = wrap.select('.scatterWrap')
|
113
|
+
.datum(data.filter(function(d) { return !d.disabled }));
|
114
|
+
d3.transition(scatterWrap).call(scatter);
|
115
|
+
|
116
|
+
|
117
|
+
xAxis
|
118
|
+
.ticks( availableWidth / 100 )
|
119
|
+
.tickSize( -availableHeight , 0);
|
120
|
+
|
121
|
+
g.select('.x.axis')
|
122
|
+
.attr('transform', 'translate(0,' + y.range()[0] + ')');
|
123
|
+
d3.transition(g.select('.x.axis'))
|
124
|
+
.call(xAxis);
|
125
|
+
|
126
|
+
|
127
|
+
yAxis
|
128
|
+
.ticks( availableHeight / 36 )
|
129
|
+
.tickSize( -availableWidth, 0);
|
130
|
+
|
131
|
+
d3.transition(g.select('.y.axis'))
|
132
|
+
.call(yAxis);
|
133
|
+
|
134
|
+
|
135
|
+
distX
|
136
|
+
.width(availableWidth)
|
137
|
+
.color(data.map(function(d,i) {
|
138
|
+
return d.color || color[i % color.length];
|
139
|
+
}).filter(function(d,i) { return !data[i].disabled }));
|
140
|
+
gEnter.select('.distWrap').append('g')
|
141
|
+
.attr('class', 'distributionX')
|
142
|
+
.attr('transform', 'translate(0,' + y.range()[0] + ')');
|
143
|
+
g.select('.distributionX')
|
144
|
+
.datum(data.filter(function(d) { return !d.disabled }))
|
145
|
+
.call(distX);
|
146
|
+
|
147
|
+
|
148
|
+
distY
|
149
|
+
.width(availableHeight)
|
150
|
+
.color(data.map(function(d,i) {
|
151
|
+
return d.color || color[i % color.length];
|
152
|
+
}).filter(function(d,i) { return !data[i].disabled }));
|
153
|
+
gEnter.select('.distWrap').append('g')
|
154
|
+
.attr('class', 'distributionY')
|
155
|
+
.attr('transform', 'translate(-' + distY.size() + ',0)');
|
156
|
+
g.select('.distributionY')
|
157
|
+
.datum(data.filter(function(d) { return !d.disabled }))
|
158
|
+
.call(distY);
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
legend.dispatch.on('legendClick', function(d,i, that) {
|
163
|
+
d.disabled = !d.disabled;
|
164
|
+
|
165
|
+
if (!data.filter(function(d) { return !d.disabled }).length) {
|
166
|
+
data.map(function(d) {
|
167
|
+
d.disabled = false;
|
168
|
+
wrap.selectAll('.series').classed('disabled', false);
|
169
|
+
return d;
|
170
|
+
});
|
171
|
+
}
|
172
|
+
|
173
|
+
selection.transition().call(chart)
|
174
|
+
});
|
175
|
+
|
176
|
+
/*
|
177
|
+
legend.dispatch.on('legendMouseover', function(d, i) {
|
178
|
+
d.hover = true;
|
179
|
+
selection.transition().call(chart)
|
180
|
+
});
|
181
|
+
|
182
|
+
legend.dispatch.on('legendMouseout', function(d, i) {
|
183
|
+
d.hover = false;
|
184
|
+
selection.transition().call(chart)
|
185
|
+
});
|
186
|
+
*/
|
187
|
+
|
188
|
+
|
189
|
+
scatter.dispatch.on('elementMouseover.tooltip', function(e) {
|
190
|
+
d3.select('.chart-' + scatter.id() + ' .series-' + e.seriesIndex + ' .distx-' + e.pointIndex)
|
191
|
+
.attr('y1', e.pos[1] - availableHeight);
|
192
|
+
d3.select('.chart-' + scatter.id() + ' .series-' + e.seriesIndex + ' .disty-' + e.pointIndex)
|
193
|
+
.attr('x2', e.pos[0] + distX.size());
|
194
|
+
|
195
|
+
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
|
196
|
+
dispatch.tooltipShow(e);
|
197
|
+
});
|
198
|
+
if (tooltips) dispatch.on('tooltipShow', function(e) { showTooltip(e, that.parentNode) } ); // TODO: maybe merge with above?
|
199
|
+
|
200
|
+
scatter.dispatch.on('elementMouseout.tooltip', function(e) {
|
201
|
+
dispatch.tooltipHide(e);
|
202
|
+
|
203
|
+
d3.select('.chart-' + scatter.id() + ' .series-' + e.seriesIndex + ' .distx-' + e.pointIndex)
|
204
|
+
.attr('y1', 0);
|
205
|
+
d3.select('.chart-' + scatter.id() + ' .series-' + e.seriesIndex + ' .disty-' + e.pointIndex)
|
206
|
+
.attr('x2', distY.size());
|
207
|
+
});
|
208
|
+
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
|
209
|
+
|
210
|
+
|
211
|
+
//store old scales for use in transitions on update, to animate from old to new positions, and sizes
|
212
|
+
x0 = x.copy();
|
213
|
+
y0 = y.copy();
|
214
|
+
|
215
|
+
});
|
216
|
+
|
217
|
+
return chart;
|
218
|
+
}
|
219
|
+
|
220
|
+
|
221
|
+
chart.dispatch = dispatch;
|
222
|
+
chart.legend = legend;
|
223
|
+
chart.xAxis = xAxis;
|
224
|
+
chart.yAxis = yAxis;
|
225
|
+
|
226
|
+
d3.rebind(chart, scatter, 'interactive', 'shape', 'size', 'xScale', 'yScale', 'zScale', 'xDomain', 'yDomain', 'sizeDomain', 'forceX', 'forceY', 'forceSize', 'clipVoronoi', 'clipRadius', 'fisheye', 'fisheyeRadius');
|
227
|
+
|
228
|
+
|
229
|
+
chart.margin = function(_) {
|
230
|
+
if (!arguments.length) return margin;
|
231
|
+
margin = _;
|
232
|
+
return chart;
|
233
|
+
};
|
234
|
+
|
235
|
+
chart.width = function(_) {
|
236
|
+
if (!arguments.length) return width;
|
237
|
+
width = _;
|
238
|
+
return chart;
|
239
|
+
};
|
240
|
+
|
241
|
+
chart.height = function(_) {
|
242
|
+
if (!arguments.length) return height;
|
243
|
+
height = _;
|
244
|
+
return chart;
|
245
|
+
};
|
246
|
+
|
247
|
+
chart.color = function(_) {
|
248
|
+
if (!arguments.length) return color;
|
249
|
+
color = _;
|
250
|
+
legend.color(_);
|
251
|
+
distX.color(_);
|
252
|
+
distY.color(_);
|
253
|
+
return chart;
|
254
|
+
};
|
255
|
+
|
256
|
+
chart.showDistX = function(_) {
|
257
|
+
if (!arguments.length) return showDistX;
|
258
|
+
showDistX = _;
|
259
|
+
return chart;
|
260
|
+
};
|
261
|
+
|
262
|
+
chart.showDistY = function(_) {
|
263
|
+
if (!arguments.length) return showDistY;
|
264
|
+
showDistY = _;
|
265
|
+
return chart;
|
266
|
+
};
|
267
|
+
|
268
|
+
chart.showControls = function(_) {
|
269
|
+
if (!arguments.length) return showControls;
|
270
|
+
showControls = _;
|
271
|
+
return chart;
|
272
|
+
};
|
273
|
+
|
274
|
+
chart.showLegend = function(_) {
|
275
|
+
if (!arguments.length) return showLegend;
|
276
|
+
showLegend = _;
|
277
|
+
return chart;
|
278
|
+
};
|
279
|
+
|
280
|
+
chart.tooltips = function(_) {
|
281
|
+
if (!arguments.length) return tooltips;
|
282
|
+
tooltips = _;
|
283
|
+
return chart;
|
284
|
+
};
|
285
|
+
|
286
|
+
chart.tooltipContent = function(_) {
|
287
|
+
if (!arguments.length) return tooltip;
|
288
|
+
tooltip = _;
|
289
|
+
return chart;
|
290
|
+
};
|
291
|
+
|
292
|
+
|
293
|
+
return chart;
|
294
|
+
}
|