nvd3.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*-----nvd3-chart1-----*/
  2. historicalBarChart = [{
  3. key: "Cumulative Return",
  4. values: [{
  5. "label": "A",
  6. "value": 24,
  7. "color": "#6c5ffc"
  8. }, {
  9. "label": "B",
  10. "value": 10,
  11. "color": "#05c3fb"
  12. }, {
  13. "label": "C",
  14. "value": 35,
  15. "color": "#09ad95"
  16. }, {
  17. "label": "D",
  18. "value": 150,
  19. "color": "#6c5ffc"
  20. }, {
  21. "label": "E",
  22. "value": 120,
  23. "color": "#1170e4"
  24. }, {
  25. "label": "F",
  26. "value": 95,
  27. "color": "#f82649"
  28. }, {
  29. "label": "G",
  30. "value": 13,
  31. "color": "#17a2b8"
  32. }, {
  33. "label": "H",
  34. "value": 5,
  35. "color": "#ecb403"
  36. }]
  37. }];
  38. nv.addGraph(function() {
  39. var chart = nv.models.discreteBarChart().x(function(d) {
  40. return d.label
  41. }).y(function(d) {
  42. return d.value
  43. }).staggerLabels(true)
  44. //.staggerLabels(historicalBarChart[0].values.length > 8)
  45. .showValues(true).duration(250);
  46. d3.select('#nvd3-chart1 svg').datum(historicalBarChart).call(chart);
  47. nv.utils.windowResize(chart.update);
  48. return chart;
  49. });
  50. /*-----nvd3-chart2-----*/
  51. var chart;
  52. nv.addGraph(function() {
  53. chart = nv.models.historicalBarChart();
  54. chart.margin({
  55. left: 50,
  56. bottom: 50
  57. }).useInteractiveGuideline(true).duration(250);
  58. // chart sub-models (ie. xAxis, yAxis, etc) when accessed directly, return themselves, not the parent chart, so need to chain separately
  59. chart.xAxis.axisLabel("").tickFormat(d3.format(',.1f'));
  60. chart.yAxis.axisLabel('').tickFormat(d3.format(',.2f'));
  61. chart.showXAxis(true);
  62. d3.select('#nvd3-chart2').datum(sinData()).transition().call(chart);
  63. nv.utils.windowResize(chart.update);
  64. chart.dispatch.on('stateChange', function(e) {
  65. nv.log('New State:', JSON.stringify(e));
  66. });
  67. return chart;
  68. });
  69. //Simple test data generators
  70. function sinAndCos() {
  71. 'use strict'
  72. var sin = [],
  73. cos = [];
  74. for (var i = 0; i < 10; i++) {
  75. sin.push({
  76. x: i,
  77. y: Math.sin(i / 10)
  78. });
  79. cos.push({
  80. x: i,
  81. y: .5 * Math.cos(i / 10)
  82. });
  83. }
  84. return [{
  85. values: sin,
  86. key: "Sine Wave",
  87. color: "#6c5ffc"
  88. }, {
  89. values: cos,
  90. key: "Cosine Wave",
  91. color: "#6c5ffc"
  92. }];
  93. }
  94. function sinData() {
  95. 'use strict'
  96. var sin = [];
  97. for (var i = 0; i < 10; i++) {
  98. sin.push({
  99. x: i,
  100. y: Math.sin(i / 10) * Math.random() * 10
  101. });
  102. }
  103. return [{
  104. values: sin,
  105. key: "Sine Wave",
  106. color: "#6c5ffc"
  107. }];
  108. }
  109. /*-----nvd3-chart3-----*/
  110. nv.addGraph(function() {
  111. var chart = nv.models.lineChart();
  112. var fitScreen = false;
  113. var width = 600;
  114. var height = 300;
  115. var zoom = 1;
  116. chart.useInteractiveGuideline(true);
  117. chart.xAxis.tickFormat(d3.format(',r'));
  118. chart.lines.dispatch.on("elementClick", function(evt) {
  119. console.log(evt);
  120. });
  121. chart.yAxis.axisLabel('Voltage (v)').tickFormat(d3.format(',.2f'));
  122. d3.select('#nvd3-chart3 svg').attr('perserveAspectRatio', 'xMinYMid').attr('width', width).attr('height', height).datum(sinAndCos());
  123. setChartViewBox();
  124. resizeChart();
  125. nv.utils.windowResize(resizeChart);
  126. d3.select('#zoomIn').on('click', zoomIn);
  127. d3.select('#zoomOut').on('click', zoomOut);
  128. function setChartViewBox() {
  129. var w = width * zoom,
  130. h = height * zoom;
  131. chart.width(w).height(h);
  132. d3.select('#nvd3-chart3 svg').attr('viewBox', '0 0 ' + w + ' ' + h).transition().duration(500).call(chart);
  133. }
  134. function zoomOut() {
  135. zoom += .25;
  136. setChartViewBox();
  137. }
  138. function zoomIn() {
  139. if (zoom <= .5) return;
  140. zoom -= .25;
  141. setChartViewBox();
  142. }
  143. // This resize simply sets the SVG's dimensions, without a need to recall the chart code
  144. // Resizing because of the viewbox and perserveAspectRatio settings
  145. // This scales the interior of the chart unlike the above
  146. function resizeChart() {
  147. var container = d3.select('#nvd3-chart3');
  148. var svg = container.select('svg');
  149. if (fitScreen) {
  150. // resize based on container's width AND HEIGHT
  151. var windowSize = nv.utils.windowSize();
  152. svg.attr("width", windowSize.width);
  153. svg.attr("height", windowSize.height);
  154. } else {
  155. // resize based on container's width
  156. var aspect = chart.width() / chart.height();
  157. var targetWidth = parseInt(container.style('width'));
  158. svg.attr("width", targetWidth);
  159. svg.attr("height", Math.round(targetWidth / aspect));
  160. }
  161. }
  162. return chart;
  163. });
  164. function sinAndCos() {
  165. 'use strict'
  166. var sin = [],
  167. cos = [];
  168. for (var i = 0; i < 100; i++) {
  169. sin.push({
  170. x: i,
  171. y: Math.sin(i / 10)
  172. });
  173. cos.push({
  174. x: i,
  175. y: .5 * Math.cos(i / 10)
  176. });
  177. }
  178. return [{
  179. values: sin,
  180. key: "Sine Wave",
  181. color: "#6c5ffc"
  182. }, {
  183. values: cos,
  184. key: "Cosine Wave",
  185. color: "#05c3fb"
  186. }];
  187. }