PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/jspears/bobamo
JavaScript | 79 lines | 67 code | 6 blank | 6 comment | 6 complexity | 8fe8a8e4150efcc6ee4bbe06c6f87644 MD5 | raw file
Possible License(s): Apache-2.0
  1. define(['Backbone.d3', 'd3'], function (Backbone, d3) {
  2. return Backbone.d3.Canned['Scatter'] = {
  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) {
  11. var w = this.w;
  12. var h = this.h;
  13. var data = this.plotdata();
  14. var x = d3.scale.linear()
  15. .domain([0, 20])
  16. .range([0, w]);
  17. var y = d3.scale.linear()
  18. .domain([-1, 1])
  19. .rangeRound([10, h - 10]);
  20. // Draw axes & label
  21. // Scatter
  22. var chart = null;
  23. if (options.newPlot) {
  24. chart = this.div.append("svg:svg");
  25. chart.selectAll("circle")
  26. .data(data).enter().append("svg:circle")
  27. .attr("cx", function (d) {
  28. return x(d.x)
  29. })
  30. .attr("cy", function (d) {
  31. return y(d.y)
  32. })
  33. .attr("r", 0)
  34. .transition()
  35. .duration(this.duration)
  36. .attr("r", 5);
  37. } else {
  38. chart = this.div.selectAll("svg");
  39. var circle = chart.selectAll("circle").data(data);
  40. circle.enter().insert("svg:circle", "circle")
  41. .attr("cx", function (d, i) {
  42. return x(d.x)
  43. })
  44. .attr("cy", function (d) {
  45. return y(d.y)
  46. })
  47. .attr("id", function (d) {
  48. return d.x + '-' + d.y
  49. })
  50. .attr("r", 0)
  51. .transition()
  52. .duration(this.duration)
  53. .attr("r", 5);
  54. }
  55. // TODO: label points
  56. // TODO: different shapes
  57. // TODO: support multiple datasets in one scat
  58. },
  59. datamap:function (datapoint) {
  60. return {x:datapoint.get('x'), y:datapoint.get('y')};
  61. },
  62. plotdata:function () {
  63. var data = this.collection.map(this.datamap, this)
  64. // Needed for scolling plots
  65. if (data.length > this.size) {
  66. return _.last(data, this.size);
  67. } else {
  68. return data;
  69. }
  70. }
  71. })
  72. }
  73. });