PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/backbone-d3.js

https://github.com/jondejong/Backbone-d3
JavaScript | 75 lines | 67 code | 2 blank | 6 comment | 15 complexity | 01c26a6365b6e4b73d7671ef6c089910 MD5 | raw file
  1. (function() {
  2. Backbone.d3 = {
  3. PlotView: Backbone.View.extend({
  4. initialize: function(collection, settings) {
  5. _.bindAll(this);
  6. this.collection = collection;
  7. this.collection.bind('change', this.redraw);
  8. this.collection.bind('add', this.redraw);
  9. this.collection.bind('remove', this.redraw);
  10. this.collection.bind('reset', this.draw);
  11. this.settings = settings || {};
  12. var divname = this.settings.div || "#plot";
  13. this.div = d3.select(divname)
  14. // time taken in transitions
  15. this.duration = this.settings.duration || 500;
  16. this.collection.fetch();
  17. // TODO: make the chart a member var of the View
  18. // for easier access/fine grained control
  19. },
  20. plotdata: function() {
  21. var data = [];
  22. this.collection.forEach(function(datapoint) {
  23. data.push(datapoint.get('value'));
  24. })
  25. return data;
  26. },
  27. draw: function() {
  28. // Draw the plot
  29. if (this.plotdata().length > 0) {
  30. this.plot({
  31. newPlot: true
  32. });
  33. this.caption();
  34. }
  35. },
  36. redraw: function() {
  37. // transition the plot
  38. this.plot({
  39. newPlot: false
  40. });
  41. },
  42. plot: function() {
  43. if (console){ console.log("Not implemented in base class"); }
  44. return;
  45. },
  46. caption: function(){
  47. if (this.settings.caption || this.collection.caption){
  48. var caption = this.settings.caption || this.collection.caption;
  49. if (typeof Markdown == "object") {
  50. try {
  51. var converter = Markdown.getSanitizingConverter();
  52. caption = converter.makeHtml(caption);
  53. } catch (err) {
  54. // do nothing
  55. var pass = true;
  56. };
  57. }
  58. var captiondiv = $('<div/>', {class: "caption", html: caption});
  59. $(this.settings.div).append(captiondiv);
  60. }
  61. }
  62. }),
  63. PlotCollection: Backbone.Collection.extend({
  64. initialize: function(models, settings) {
  65. this.settings = settings || {};
  66. this.plottype = this.settings.plottype || this.plottype || "bar";
  67. this.caption = this.settings.caption || false;
  68. if (models) this.reset(models, {silent: true});
  69. }
  70. }),
  71. Canned: {}
  72. }
  73. })();