PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/visualize/public/js/backbone-d3/visualisations/line.js

http://github.com/jspears/bobamo
JavaScript | 108 lines | 87 code | 12 blank | 9 comment | 9 complexity | 20def3139c600b7e8b8c9117206e32bc MD5 | raw file
Possible License(s): Apache-2.0
  1. define(['Backbone.d3', 'd3'], function (Backbone, d3) {
  2. return Backbone.d3.Canned['Line'] = {
  3. View:Backbone.d3.PlotView.extend({
  4. initialize:function (collection, settings) {
  5. Backbone.d3.PlotView.prototype.initialize.apply(this, [collection, settings]);
  6. this.w = settings.w || 200;
  7. this.h = settings.h || 200;
  8. this.size = 20;
  9. },
  10. plot:function (options, data) {
  11. var w = this.w;
  12. var h = this.h;
  13. var interpolation = this.settings.interpolation || "linear";
  14. var x = d3.scale.linear()
  15. .domain([0, this.size])
  16. .range([10, w - 10]);
  17. var y = d3.scale.linear()
  18. .domain([-1, 1])
  19. .rangeRound([10, h - 10]);
  20. // Draw axes & label
  21. // line
  22. var chart = null;
  23. var line = d3.svg.line()
  24. .x(function (d, i) {
  25. return x(d.x)
  26. })
  27. .y(function (d, i) {
  28. return y(d.y)
  29. })
  30. .interpolate(interpolation);
  31. if (options.newPlot) {
  32. chart = this.div.append("svg:svg");
  33. chart.selectAll("circle")
  34. .data(data).enter().append("svg:circle")
  35. .attr("cx", function (d, i) {
  36. return x(d.x)
  37. })
  38. .attr("cy", function (d, i) {
  39. return y(d.y)
  40. })
  41. .attr("id", function (d) {
  42. return d.x + '-' + d.y
  43. })
  44. .attr("r", 0)
  45. .transition()
  46. .duration(this.duration)
  47. .attr("r", this.settings.pointsize || 3);
  48. chart.append("svg:path").attr("d", line(_.sortBy(data, function (d) {
  49. return d.x;
  50. })));
  51. } else {
  52. chart = this.div.selectAll("svg");
  53. var circles = chart.selectAll("circle").data(data);
  54. circles.enter().insert("svg:circle", "circle")
  55. .attr("cx", function (d, i) {
  56. return x(d.x)
  57. })
  58. .attr("cy", function (d, i) {
  59. return y(d.y)
  60. })
  61. .attr("id", function (d) {
  62. return d.x + '-' + d.y
  63. })
  64. .attr("r", 0)
  65. .transition()
  66. .duration(this.duration)
  67. .attr("r", this.settings.pointsize || 3);
  68. // TODO: transition to grown the line between points
  69. chart.selectAll("path")
  70. // sort is needed to keep the line drawing left to right, other
  71. // wise goes a bit etcher sketch
  72. .data([_.sortBy(data, function (d) {
  73. return d.x;
  74. })])
  75. .attr("d", line);
  76. }
  77. // TODO: label points
  78. // TODO: different shapes
  79. // TODO: support multiple datasets in one plot
  80. },
  81. datamap:function (datapoint) {
  82. return {x:datapoint.get('x'), y:datapoint.get('y')};
  83. },
  84. plotdata:function () {
  85. var data =this.collection.map(this.datamap, this)
  86. // Needed for scolling plots
  87. if (data.length > this.size) {
  88. return _.last(data, this.size);
  89. } else {
  90. return data;
  91. }
  92. }
  93. })
  94. }
  95. })
  96. ;