nvd3-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. data/.gitmodules +3 -0
  2. data/README.md +101 -0
  3. data/lib/nvd3-rails.rb +1 -0
  4. data/lib/nvd3/rails.rb +8 -0
  5. data/lib/nvd3/rails/engine.rb +6 -0
  6. data/lib/nvd3/rails/version.rb +5 -0
  7. data/nvd3-rails.gemspec +21 -0
  8. data/vendor/assets/javascripts/nvd3-rails.js +7 -0
  9. data/vendor/assets/javascripts/nvd3/.git.sample/HEAD +1 -0
  10. data/vendor/assets/javascripts/nvd3/.git.sample/config +13 -0
  11. data/vendor/assets/javascripts/nvd3/.git.sample/description +1 -0
  12. data/vendor/assets/javascripts/nvd3/.git.sample/hooks/applypatch-msg.sample +15 -0
  13. data/vendor/assets/javascripts/nvd3/.git.sample/hooks/commit-msg.sample +24 -0
  14. data/vendor/assets/javascripts/nvd3/.git.sample/hooks/post-update.sample +8 -0
  15. data/vendor/assets/javascripts/nvd3/.git.sample/hooks/pre-applypatch.sample +14 -0
  16. data/vendor/assets/javascripts/nvd3/.git.sample/hooks/pre-commit.sample +50 -0
  17. data/vendor/assets/javascripts/nvd3/.git.sample/hooks/pre-push.sample +53 -0
  18. data/vendor/assets/javascripts/nvd3/.git.sample/hooks/pre-rebase.sample +169 -0
  19. data/vendor/assets/javascripts/nvd3/.git.sample/hooks/prepare-commit-msg.sample +36 -0
  20. data/vendor/assets/javascripts/nvd3/.git.sample/hooks/update.sample +128 -0
  21. data/vendor/assets/javascripts/nvd3/.git.sample/index +0 -0
  22. data/vendor/assets/javascripts/nvd3/.git.sample/info/exclude +6 -0
  23. data/vendor/assets/javascripts/nvd3/.git.sample/logs/HEAD +1 -0
  24. data/vendor/assets/javascripts/nvd3/.git.sample/logs/refs/heads/master +1 -0
  25. data/vendor/assets/javascripts/nvd3/.git.sample/logs/refs/remotes/origin/HEAD +1 -0
  26. data/vendor/assets/javascripts/nvd3/.git.sample/objects/pack/pack-babbb312d58dd7f03870b530a1b9a84c80918be3.idx +0 -0
  27. data/vendor/assets/javascripts/nvd3/.git.sample/objects/pack/pack-babbb312d58dd7f03870b530a1b9a84c80918be3.pack +0 -0
  28. data/vendor/assets/javascripts/nvd3/.git.sample/packed-refs +5 -0
  29. data/vendor/assets/javascripts/nvd3/.git.sample/refs/heads/master +1 -0
  30. data/vendor/assets/javascripts/nvd3/.git.sample/refs/remotes/origin/HEAD +1 -0
  31. data/vendor/assets/javascripts/nvd3/.gitignore +24 -0
  32. data/vendor/assets/javascripts/nvd3/LICENSE.md +49 -0
  33. data/vendor/assets/javascripts/nvd3/Makefile +56 -0
  34. data/vendor/assets/javascripts/nvd3/README.md +43 -0
  35. data/vendor/assets/javascripts/nvd3/build.bat +6 -0
  36. data/vendor/assets/javascripts/nvd3/deprecated/bar.html +198 -0
  37. data/vendor/assets/javascripts/nvd3/deprecated/bar.js +250 -0
  38. data/vendor/assets/javascripts/nvd3/deprecated/charts/cumulativeLineChart.js +174 -0
  39. data/vendor/assets/javascripts/nvd3/deprecated/charts/discreteBarChart.js +157 -0
  40. data/vendor/assets/javascripts/nvd3/deprecated/charts/lineChart.js +159 -0
  41. data/vendor/assets/javascripts/nvd3/deprecated/charts/lineChartDaily.js +168 -0
  42. data/vendor/assets/javascripts/nvd3/deprecated/charts/stackedAreaChart.js +177 -0
  43. data/vendor/assets/javascripts/nvd3/deprecated/cumulativeLine.html +171 -0
  44. data/vendor/assets/javascripts/nvd3/deprecated/cumulativeLine.js +334 -0
  45. data/vendor/assets/javascripts/nvd3/deprecated/discreteBarChartWithEnabledTooltip.html +129 -0
  46. data/vendor/assets/javascripts/nvd3/deprecated/discreteBarChartWithEnabledTooltip.js +222 -0
  47. data/vendor/assets/javascripts/nvd3/deprecated/discreteBarWithAxes.html +172 -0
  48. data/vendor/assets/javascripts/nvd3/deprecated/discreteBarWithAxes.js +152 -0
  49. data/vendor/assets/javascripts/nvd3/deprecated/lineChart-old.html +83 -0
  50. data/vendor/assets/javascripts/nvd3/deprecated/lineChartDaily.html +109 -0
  51. data/vendor/assets/javascripts/nvd3/deprecated/linePlusBar.html +173 -0
  52. data/vendor/assets/javascripts/nvd3/deprecated/linePlusBar.js +250 -0
  53. data/vendor/assets/javascripts/nvd3/deprecated/lineWithFocus.html +137 -0
  54. data/vendor/assets/javascripts/nvd3/deprecated/lineWithFocus.js +354 -0
  55. data/vendor/assets/javascripts/nvd3/deprecated/lineWithFourAxes.html +144 -0
  56. data/vendor/assets/javascripts/nvd3/deprecated/lineWithFourAxes.js +218 -0
  57. data/vendor/assets/javascripts/nvd3/deprecated/lineWithLegend.html +142 -0
  58. data/vendor/assets/javascripts/nvd3/deprecated/lineWithLegend.js +176 -0
  59. data/vendor/assets/javascripts/nvd3/deprecated/monthendAxis.html +99 -0
  60. data/vendor/assets/javascripts/nvd3/deprecated/multiBarHorizontalWithLegend.html +258 -0
  61. data/vendor/assets/javascripts/nvd3/deprecated/multiBarHorizontalWithLegend.js +226 -0
  62. data/vendor/assets/javascripts/nvd3/deprecated/multiBarWithLegend.html +162 -0
  63. data/vendor/assets/javascripts/nvd3/deprecated/multiBarWithLegend.js +249 -0
  64. data/vendor/assets/javascripts/nvd3/deprecated/pie.js +263 -0
  65. data/vendor/assets/javascripts/nvd3/deprecated/scatterChart.html +110 -0
  66. data/vendor/assets/javascripts/nvd3/deprecated/scatterChart.js +294 -0
  67. data/vendor/assets/javascripts/nvd3/deprecated/scatterFisheyeChart.js +418 -0
  68. data/vendor/assets/javascripts/nvd3/deprecated/scatterWithLegend.html +167 -0
  69. data/vendor/assets/javascripts/nvd3/deprecated/scatterWithLegend.js +261 -0
  70. data/vendor/assets/javascripts/nvd3/deprecated/stackedArea.js +286 -0
  71. data/vendor/assets/javascripts/nvd3/deprecated/stackedAreaChart.html +183 -0
  72. data/vendor/assets/javascripts/nvd3/deprecated/stackedAreaChart_old.html +137 -0
  73. data/vendor/assets/javascripts/nvd3/deprecated/stackedAreaWithLegend.html +222 -0
  74. data/vendor/assets/javascripts/nvd3/deprecated/stackedAreaWithLegend.js +297 -0
  75. data/vendor/assets/javascripts/nvd3/examples/bullet.html +96 -0
  76. data/vendor/assets/javascripts/nvd3/examples/bulletChart.html +94 -0
  77. data/vendor/assets/javascripts/nvd3/examples/crossfilter.html +167 -0
  78. data/vendor/assets/javascripts/nvd3/examples/crossfilterWithDimentions.html +180 -0
  79. data/vendor/assets/javascripts/nvd3/examples/crossfilterWithTables.html +288 -0
  80. data/vendor/assets/javascripts/nvd3/examples/cumulativeLineChart.html +155 -0
  81. data/vendor/assets/javascripts/nvd3/examples/discreteBarChart.html +116 -0
  82. data/vendor/assets/javascripts/nvd3/examples/dynamicTimeSeries.html +148 -0
  83. data/vendor/assets/javascripts/nvd3/examples/historicalBar.html +157 -0
  84. data/vendor/assets/javascripts/nvd3/examples/horizon.html +163 -0
  85. data/vendor/assets/javascripts/nvd3/examples/images/grey-minus.png +0 -0
  86. data/vendor/assets/javascripts/nvd3/examples/images/grey-plus.png +0 -0
  87. data/vendor/assets/javascripts/nvd3/examples/indentedtree.html +126 -0
  88. data/vendor/assets/javascripts/nvd3/examples/legend.html +75 -0
  89. data/vendor/assets/javascripts/nvd3/examples/line.html +95 -0
  90. data/vendor/assets/javascripts/nvd3/examples/lineChart.html +112 -0
  91. data/vendor/assets/javascripts/nvd3/examples/lineChartSVGResize.html +151 -0
  92. data/vendor/assets/javascripts/nvd3/examples/linePlusBarChart.html +114 -0
  93. data/vendor/assets/javascripts/nvd3/examples/linePlusBarWithFocusChart.html +128 -0
  94. data/vendor/assets/javascripts/nvd3/examples/lineTimeSeries.html +142 -0
  95. data/vendor/assets/javascripts/nvd3/examples/lineWithFisheyeChart.html +101 -0
  96. data/vendor/assets/javascripts/nvd3/examples/lineWithFocusChart.html +87 -0
  97. data/vendor/assets/javascripts/nvd3/examples/multiBar.html +92 -0
  98. data/vendor/assets/javascripts/nvd3/examples/multiBarChart.html +93 -0
  99. data/vendor/assets/javascripts/nvd3/examples/multiBarHorizontalChart.html +388 -0
  100. data/vendor/assets/javascripts/nvd3/examples/multiChart.html +93 -0
  101. data/vendor/assets/javascripts/nvd3/examples/nations.json +1 -0
  102. data/vendor/assets/javascripts/nvd3/examples/pie.html +93 -0
  103. data/vendor/assets/javascripts/nvd3/examples/pieChart.html +114 -0
  104. data/vendor/assets/javascripts/nvd3/examples/scatter.html +95 -0
  105. data/vendor/assets/javascripts/nvd3/examples/scatterChart.html +115 -0
  106. data/vendor/assets/javascripts/nvd3/examples/scatterPlusLineChart.html +116 -0
  107. data/vendor/assets/javascripts/nvd3/examples/sparkline.html +62 -0
  108. data/vendor/assets/javascripts/nvd3/examples/sparklinePlus.html +67 -0
  109. data/vendor/assets/javascripts/nvd3/examples/stackedArea.html +155 -0
  110. data/vendor/assets/javascripts/nvd3/examples/stackedAreaChart.html +245 -0
  111. data/vendor/assets/javascripts/nvd3/examples/stream_layers.js +35 -0
  112. data/vendor/assets/javascripts/nvd3/lib/cie.js +155 -0
  113. data/vendor/assets/javascripts/nvd3/lib/crossfilter.js +1180 -0
  114. data/vendor/assets/javascripts/nvd3/lib/crossfilter.min.js +1 -0
  115. data/vendor/assets/javascripts/nvd3/lib/d3.v2.js +7033 -0
  116. data/vendor/assets/javascripts/nvd3/lib/d3.v2.min.js +4 -0
  117. data/vendor/assets/javascripts/nvd3/lib/fisheye.js +86 -0
  118. data/vendor/assets/javascripts/nvd3/lib/hive.js +80 -0
  119. data/vendor/assets/javascripts/nvd3/lib/horizon.js +192 -0
  120. data/vendor/assets/javascripts/nvd3/lib/sankey.js +292 -0
  121. data/vendor/assets/javascripts/nvd3/nv.d3.js +11762 -0
  122. data/vendor/assets/javascripts/nvd3/src/core.js +117 -0
  123. data/vendor/assets/javascripts/nvd3/src/models/axis.js +398 -0
  124. data/vendor/assets/javascripts/nvd3/src/models/backup/bullet.js +250 -0
  125. data/vendor/assets/javascripts/nvd3/src/models/backup/bulletChart.js +349 -0
  126. data/vendor/assets/javascripts/nvd3/src/models/bullet.js +377 -0
  127. data/vendor/assets/javascripts/nvd3/src/models/bulletChart.js +341 -0
  128. data/vendor/assets/javascripts/nvd3/src/models/cumulativeLineChart.js +609 -0
  129. data/vendor/assets/javascripts/nvd3/src/models/discreteBar.js +327 -0
  130. data/vendor/assets/javascripts/nvd3/src/models/discreteBarChart.js +290 -0
  131. data/vendor/assets/javascripts/nvd3/src/models/distribution.js +146 -0
  132. data/vendor/assets/javascripts/nvd3/src/models/historicalBar.js +289 -0
  133. data/vendor/assets/javascripts/nvd3/src/models/indentedTree.js +306 -0
  134. data/vendor/assets/javascripts/nvd3/src/models/legend.js +203 -0
  135. data/vendor/assets/javascripts/nvd3/src/models/line.js +286 -0
  136. data/vendor/assets/javascripts/nvd3/src/models/lineChart.js +359 -0
  137. data/vendor/assets/javascripts/nvd3/src/models/lineDynTimeSeriesChart.js +434 -0
  138. data/vendor/assets/javascripts/nvd3/src/models/linePlusBarChart.js +422 -0
  139. data/vendor/assets/javascripts/nvd3/src/models/linePlusBarWithFocusChart.js +657 -0
  140. data/vendor/assets/javascripts/nvd3/src/models/lineTimeSeriesChart.js +340 -0
  141. data/vendor/assets/javascripts/nvd3/src/models/lineWithFisheye.js +197 -0
  142. data/vendor/assets/javascripts/nvd3/src/models/lineWithFisheyeChart.js +324 -0
  143. data/vendor/assets/javascripts/nvd3/src/models/lineWithFocusChart.js +565 -0
  144. data/vendor/assets/javascripts/nvd3/src/models/multiAxisLineChart.js +312 -0
  145. data/vendor/assets/javascripts/nvd3/src/models/multiBar.js +416 -0
  146. data/vendor/assets/javascripts/nvd3/src/models/multiBarChart.js +445 -0
  147. data/vendor/assets/javascripts/nvd3/src/models/multiBarHorizontal.js +420 -0
  148. data/vendor/assets/javascripts/nvd3/src/models/multiBarHorizontalChart.js +415 -0
  149. data/vendor/assets/javascripts/nvd3/src/models/multiBarTimeSeries.js +371 -0
  150. data/vendor/assets/javascripts/nvd3/src/models/multiBarTimeSeriesChart.js +403 -0
  151. data/vendor/assets/javascripts/nvd3/src/models/multiChart.js +444 -0
  152. data/vendor/assets/javascripts/nvd3/src/models/ohlcBar.js +365 -0
  153. data/vendor/assets/javascripts/nvd3/src/models/pie.js +358 -0
  154. data/vendor/assets/javascripts/nvd3/src/models/pieChart.js +281 -0
  155. data/vendor/assets/javascripts/nvd3/src/models/scatter.js +622 -0
  156. data/vendor/assets/javascripts/nvd3/src/models/scatterChart.js +576 -0
  157. data/vendor/assets/javascripts/nvd3/src/models/scatterPlusLineChart.js +577 -0
  158. data/vendor/assets/javascripts/nvd3/src/models/sparkline.js +179 -0
  159. data/vendor/assets/javascripts/nvd3/src/models/sparklinePlus.js +291 -0
  160. data/vendor/assets/javascripts/nvd3/src/models/stackedArea.js +336 -0
  161. data/vendor/assets/javascripts/nvd3/src/models/stackedAreaChart.js +453 -0
  162. data/vendor/assets/javascripts/nvd3/src/nv.d3.css +671 -0
  163. data/vendor/assets/javascripts/nvd3/src/tooltip.js +129 -0
  164. data/vendor/assets/javascripts/nvd3/src/utils.js +105 -0
  165. data/vendor/assets/javascripts/set-nvd3-env.js.erb +5 -0
  166. data/vendor/assets/stylesheets/nvd3-rails.css +3 -0
  167. metadata +233 -0
@@ -0,0 +1,144 @@
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
+ }
11
+
12
+ text {
13
+ font: 12px sans-serif;
14
+ }
15
+
16
+ #chart1 {
17
+ height: 500px;
18
+ margin: 10px;
19
+ min-width: 100px;
20
+ min-height: 100px;
21
+ /*
22
+ Minimum height and width is a good idea to prevent negative SVG dimensions...
23
+ For example width should be =< margin.left + margin.right + 1,
24
+ of course 1 pixel for the entire chart would not be very useful, BUT should not have errors
25
+ */
26
+ }
27
+
28
+ </style>
29
+ <body>
30
+
31
+ <div id="chart1">
32
+ </div>
33
+
34
+ <script src="../lib/d3.v2.js"></script>
35
+ <script src="../nv.d3.js"></script>
36
+ <script src="../src/tooltip.js"></script>
37
+ <script src="../src/models/legend.js"></script>
38
+ <script src="../src/models/axis.js"></script>
39
+ <script src="../src/models/line.js"></script>
40
+ <script src="../src/models/lineWithFourAxes.js"></script>
41
+ <script>
42
+
43
+ /************
44
+ * Considering making an nv.charts object which will contain abstractions similar to the one below,
45
+ * but with the usual d3 reusable style.
46
+ * I could make this abstraction inside the layer above, but this layer incorporates some jQuery, so
47
+ * thinking nv.charts can be the glue that's not 100% d3
48
+ ************/
49
+
50
+ var selector = '#chart1',
51
+ chart = nv.models.lineWithLegend(),
52
+ data = sinAndCos(),
53
+ xTickFormat = d3.format(',r'),
54
+ yTickFormat = d3.format(',.2f'),
55
+ xAxisLabel = null,
56
+ yAxisLabel = 'Voltage (v)',
57
+ duration = 500;
58
+
59
+ nv.addGraph({
60
+ generate: function() {
61
+ var container = d3.select(selector),
62
+ width = function() { return parseInt(container.style('width')) },
63
+ height = function() { return parseInt(container.style('height')) },
64
+ svg = container.append('svg');
65
+
66
+ chart
67
+ .width(width)
68
+ .height(height)
69
+
70
+ chart.xAxis
71
+ .tickFormat(xTickFormat);
72
+
73
+ chart.yAxis
74
+ .tickFormat(yTickFormat)
75
+ .axisLabel(yAxisLabel);
76
+
77
+ svg
78
+ .attr('width', width())
79
+ .attr('height', height())
80
+ .datum(data)
81
+ .transition().duration(duration).call(chart);
82
+
83
+ return chart;
84
+ },
85
+ callback: function(chart) {
86
+ var showTooltip = function(e) {
87
+ var offsetElement = document.getElementById(selector.substr(1)),
88
+ left = e.pos[0] + offsetElement.offsetLeft,
89
+ top = e.pos[1] + offsetElement.offsetTop,
90
+ formatY = chart.yAxis.tickFormat(), //Assumes using same format as axis, can customize to show higher precision, etc.
91
+ formatX = chart.xAxis.tickFormat();
92
+
93
+ // uses the chart's getX and getY, you may customize if x position is not the same as the value you want
94
+ // ex. daily data without weekends, x is the index, while you want the date
95
+ var content = '<h3>' + e.series.key + '</h3>' +
96
+ '<p>' +
97
+ formatY(chart.y()(e.point)) + ' at ' + formatX(chart.x()(e.point)) +
98
+ '</p>';
99
+
100
+ nv.tooltip.show([left, top], content);
101
+ };
102
+
103
+ chart.dispatch.on('tooltipShow', showTooltip);
104
+ chart.dispatch.on('tooltipHide', nv.tooltip.cleanup);
105
+
106
+
107
+ window.onresize =
108
+ function() {
109
+ // now that width and height are functions, should be automatic..of course you can always override them
110
+ d3.select('#chart1 svg')
111
+ .attr('width', chart.width()()) //need to set SVG dimensions, chart is not aware of the SVG component
112
+ .attr('height', chart.height()())
113
+ .call(chart);
114
+ };
115
+ }
116
+ });
117
+
118
+
119
+
120
+ function sinAndCos() {
121
+ var sin = [],
122
+ cos = [];
123
+
124
+ for (var i = 0; i < 100; i++) {
125
+ sin.push({x: i, y: Math.sin(i/10)});
126
+ cos.push({x: i, y: .5 * Math.cos(i/10)});
127
+ }
128
+
129
+ return [
130
+ {
131
+ values: sin,
132
+ key: "Sine Wave",
133
+ color: "#ff7f0e"
134
+ },
135
+ {
136
+ values: cos,
137
+ key: "Cosine Wave",
138
+ color: "#2ca02c"
139
+ }
140
+ ];
141
+ }
142
+
143
+
144
+ </script>
@@ -0,0 +1,218 @@
1
+
2
+ nv.models.lineWithLegend = function() {
3
+ var margin = {top: 60, right: 60, bottom: 40, left: 60},
4
+ getWidth = function() { return 960 },
5
+ getHeight = function() { return 500 },
6
+ dotRadius = function() { return 2.5 },
7
+ getX = function(d) { return d.x },
8
+ getY = function(d) { return d.y },
9
+ color = d3.scale.category20().range(),
10
+ dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
11
+
12
+ var x = d3.scale.linear(),
13
+ y = d3.scale.linear(),
14
+ xAxis = nv.models.axis().scale(x).orient('bottom'),
15
+ yAxis = nv.models.axis().scale(y).orient('left'),
16
+ x2Axis = nv.models.axis().scale(x).orient('top'),
17
+ y2Axis = nv.models.axis().scale(y).orient('right'),
18
+ legend = nv.models.legend().height(30),
19
+ lines = nv.models.line();
20
+
21
+
22
+ function chart(selection) {
23
+ selection.each(function(data) {
24
+ var width = getWidth(),
25
+ height = getHeight(),
26
+ availableWidth = width - margin.left - margin.right,
27
+ availableHeight = height - margin.top - margin.bottom;
28
+
29
+ var series = data.filter(function(d) { return !d.disabled })
30
+ .map(function(d) {
31
+ return d.values.map(function(d,i) {
32
+ return { x: getX(d,i), y: getY(d,i) }
33
+ })
34
+ });
35
+
36
+ x .domain(d3.extent(d3.merge(series), function(d) { return d.x } ))
37
+ .range([0, availableWidth]);
38
+
39
+ y .domain(d3.extent(d3.merge(series), function(d) { return d.y } ))
40
+ .range([availableHeight, 0]);
41
+
42
+ lines
43
+ .width(availableWidth)
44
+ .height(availableHeight)
45
+ .color(data.map(function(d,i) {
46
+ return d.color || color[i % 10];
47
+ }).filter(function(d,i) { return !data[i].disabled }))
48
+
49
+
50
+ var wrap = d3.select(this).selectAll('g.wrap').data([data]);
51
+ var gEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 lineWithLegend').append('g');
52
+
53
+ gEnter.append('g').attr('class', 'x axis');
54
+ gEnter.append('g').attr('class', 'y axis');
55
+ gEnter.append('g').attr('class', 'x2 axis');
56
+ gEnter.append('g').attr('class', 'y2 axis');
57
+ gEnter.append('g').attr('class', 'linesWrap');
58
+ gEnter.append('g').attr('class', 'legendWrap');
59
+
60
+
61
+ legend.dispatch.on('legendClick', function(d,i) {
62
+ d.disabled = !d.disabled;
63
+
64
+ if (!data.filter(function(d) { return !d.disabled }).length) {
65
+ data.map(function(d) {
66
+ d.disabled = false;
67
+ wrap.selectAll('.series').classed('disabled', false);
68
+ return d;
69
+ });
70
+ }
71
+
72
+ selection.transition().call(chart);
73
+ });
74
+
75
+ /*
76
+ //
77
+ legend.dispatch.on('legendMouseover', function(d, i) {
78
+ d.hover = true;
79
+ selection.transition().call(chart)
80
+ });
81
+
82
+ legend.dispatch.on('legendMouseout', function(d, i) {
83
+ d.hover = false;
84
+ selection.transition().call(chart)
85
+ });
86
+ */
87
+
88
+ lines.dispatch.on('elementMouseover.tooltip', function(e) {
89
+ dispatch.tooltipShow({
90
+ point: e.point,
91
+ series: e.series,
92
+ pos: [e.pos[0] + margin.left, e.pos[1] + margin.top],
93
+ seriesIndex: e.seriesIndex,
94
+ pointIndex: e.pointIndex
95
+ });
96
+ });
97
+
98
+ lines.dispatch.on('elementMouseout.tooltip', function(e) {
99
+ dispatch.tooltipHide(e);
100
+ });
101
+
102
+
103
+ //TODO: margins should be adjusted based on what components are used: axes, axis labels, legend
104
+ margin.top = legend.height() + 20; // 20 is for the x2 axis... this should be done in a better place, but just doing this to show the 4 axes in an example
105
+
106
+ var g = wrap.select('g')
107
+ .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
108
+
109
+
110
+ legend.width(width/2 - margin.right);
111
+
112
+ g.select('.legendWrap')
113
+ .datum(data)
114
+ .attr('transform', 'translate(' + (width/2 - margin.left) + ',' + (-margin.top) +')')
115
+ .call(legend);
116
+
117
+
118
+ var linesWrap = g.select('.linesWrap')
119
+ .datum(data.filter(function(d) { return !d.disabled }))
120
+
121
+
122
+ d3.transition(linesWrap).call(lines);
123
+
124
+
125
+ xAxis
126
+ .domain(x.domain())
127
+ .range(x.range())
128
+ .ticks( width / 100 )
129
+ .tickSize(-availableHeight, 0);
130
+
131
+ g.select('.x.axis')
132
+ .attr('transform', 'translate(0,' + y.range()[0] + ')');
133
+ d3.transition(g.select('.x.axis'))
134
+ .call(xAxis);
135
+
136
+ yAxis
137
+ .domain(y.domain())
138
+ .range(y.range())
139
+ .ticks( height / 36 )
140
+ .tickSize(-availableWidth, 0);
141
+
142
+ d3.transition(g.select('.y.axis'))
143
+ .call(yAxis);
144
+
145
+ x2Axis
146
+ .domain(x.domain())
147
+ .range(x.range())
148
+ .ticks( width / 100 )
149
+ .tickSize(-availableHeight, 0);
150
+
151
+ d3.transition(g.select('.x2.axis'))
152
+ .call(x2Axis);
153
+
154
+ y2Axis
155
+ .domain(y.domain())
156
+ .range(y.range())
157
+ .ticks( height / 36 )
158
+ .tickSize(-availableWidth, 0);
159
+
160
+ g.select('.y2.axis')
161
+ .attr('transform', 'translate('+ x.range()[1] + ',0)');
162
+ d3.transition(g.select('.y2.axis'))
163
+ .call(y2Axis);
164
+ });
165
+
166
+ return chart;
167
+ }
168
+
169
+ chart.dispatch = dispatch;
170
+ chart.legend = legend;
171
+ chart.xAxis = xAxis;
172
+ chart.yAxis = yAxis;
173
+
174
+ d3.rebind(chart, lines, 'interactive');
175
+ //consider rebinding x and y as well
176
+
177
+ chart.x = function(_) {
178
+ if (!arguments.length) return getX;
179
+ getX = _;
180
+ lines.x(_);
181
+ return chart;
182
+ };
183
+
184
+ chart.y = function(_) {
185
+ if (!arguments.length) return getY;
186
+ getY = _;
187
+ lines.y(_);
188
+ return chart;
189
+ };
190
+
191
+ chart.margin = function(_) {
192
+ if (!arguments.length) return margin;
193
+ margin = _;
194
+ return chart;
195
+ };
196
+
197
+ chart.width = function(_) {
198
+ if (!arguments.length) return getWidth;
199
+ getWidth = d3.functor(_);
200
+ return chart;
201
+ };
202
+
203
+ chart.height = function(_) {
204
+ if (!arguments.length) return getHeight;
205
+ getHeight = d3.functor(_);
206
+ return chart;
207
+ };
208
+
209
+ chart.dotRadius = function(_) {
210
+ if (!arguments.length) return dotRadius;
211
+ dotRadius = d3.functor(_);
212
+ lines.dotRadius = _;
213
+ return chart;
214
+ };
215
+
216
+
217
+ return chart;
218
+ }
@@ -0,0 +1,142 @@
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
+ }
11
+
12
+ text {
13
+ font: 12px sans-serif;
14
+ }
15
+
16
+ svg {
17
+ display: block;
18
+ }
19
+
20
+ #chart1 {
21
+ height: 500px;
22
+ margin: 10px;
23
+ min-width: 100px;
24
+ min-height: 100px;
25
+ /*
26
+ Minimum height and width is a good idea to prevent negative SVG dimensions...
27
+ For example width should be =< margin.left + margin.right + 1,
28
+ of course 1 pixel for the entire chart would not be very useful, BUT should not have errors
29
+ */
30
+ }
31
+
32
+ </style>
33
+ <body>
34
+
35
+ <div id="chart1">
36
+ </div>
37
+
38
+ <script src="../lib/d3.v2.js"></script>
39
+ <script src="../nv.d3.js"></script>
40
+ <script src="../src/tooltip.js"></script>
41
+ <script src="../src/models/legend.js"></script>
42
+ <script src="../src/models/axis.js"></script>
43
+ <script src="../src/models/scatter.js"></script>
44
+ <script src="../src/models/line.js"></script>
45
+ <script src="../src/models/lineWithLegend.js"></script>
46
+ <script>
47
+
48
+
49
+ var selector = '#chart1',
50
+ chart = nv.models.lineWithLegend(),
51
+ data = sinAndCos(),
52
+ xTickFormat = d3.format(',r'),
53
+ yTickFormat = d3.format(',.2f'),
54
+ xAxisLabel = null,
55
+ yAxisLabel = 'Voltage (v)',
56
+ duration = 500;
57
+
58
+ nv.addGraph({
59
+ generate: function() {
60
+ var container = d3.select(selector),
61
+ width = function() { return parseInt(container.style('width')) },
62
+ height = function() { return parseInt(container.style('height')) },
63
+ svg = container.append('svg').style('height', '500px');
64
+
65
+ chart
66
+ //.width(width)
67
+ //.height(height);
68
+
69
+ chart.xAxis
70
+ .tickFormat(xTickFormat);
71
+
72
+ chart.yAxis
73
+ .tickFormat(yTickFormat)
74
+ .axisLabel(yAxisLabel);
75
+
76
+ svg
77
+ //.attr('width', width())
78
+ //.attr('height', height())
79
+ .datum(data)
80
+ .transition().duration(duration).call(chart);
81
+
82
+ return chart;
83
+ },
84
+ callback: function(chart) {
85
+ var showTooltip = function(e) {
86
+ var offsetElement = document.getElementById(selector.substr(1)),
87
+ left = e.pos[0] + offsetElement.offsetLeft,
88
+ top = e.pos[1] + offsetElement.offsetTop,
89
+ formatY = chart.yAxis.tickFormat(), //Assumes using same format as axis, can customize to show higher precision, etc.
90
+ formatX = chart.xAxis.tickFormat();
91
+
92
+ // uses the chart's getX and getY, you may customize if x position is not the same as the value you want
93
+ // ex. daily data without weekends, x is the index, while you want the date
94
+ var content = '<h3>' + e.series.key + '</h3>' +
95
+ '<p>' +
96
+ formatY(chart.y()(e.point)) + ' at ' + formatX(chart.x()(e.point)) +
97
+ '</p>';
98
+
99
+ nv.tooltip.show([left, top], content);
100
+ };
101
+
102
+ chart.dispatch.on('tooltipShow', showTooltip);
103
+ chart.dispatch.on('tooltipHide', nv.tooltip.cleanup);
104
+
105
+
106
+ window.onresize= function() {
107
+ // now that width and height are functions, should be automatic..of course you can always override them
108
+ d3.select('#chart1 svg')
109
+ //.attr('width', chart.width()()) //need to set SVG dimensions, chart is not aware of the SVG component
110
+ //.attr('height', chart.height()())
111
+ .call(chart);
112
+ };
113
+ }
114
+ });
115
+
116
+
117
+
118
+ function sinAndCos() {
119
+ var sin = [],
120
+ cos = [];
121
+
122
+ for (var i = 0; i < 100; i++) {
123
+ sin.push({x: i, y: Math.sin(i/10)});
124
+ cos.push({x: i, y: .5 * Math.cos(i/10)});
125
+ }
126
+
127
+ return [
128
+ {
129
+ values: sin,
130
+ key: "Sine Wave",
131
+ color: "#ff7f0e"
132
+ },
133
+ {
134
+ values: cos,
135
+ key: "Cosine Wave",
136
+ color: "#2ca02c"
137
+ }
138
+ ];
139
+ }
140
+
141
+
142
+ </script>