PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/jspears/bobamo
JavaScript | 78 lines | 69 code | 3 blank | 6 comment | 15 complexity | 1983060873406fe32857e2d7d9e5bd8d MD5 | raw file
Possible License(s): Apache-2.0
  1. define('Backbone.d3', ['Backbone', 'underscore','d3'], function (Backbone, _, d3) {
  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('fetch', this.redraw);
  11. this.collection.bind('reset', this.draw);
  12. this.settings = settings || {};
  13. var divname = this.settings.div || "#plot";
  14. this.div = d3.select(divname)
  15. // time taken in transitions
  16. this.duration = this.settings.duration || 500;
  17. this.collection.fetch();
  18. // TODO: make the chart a member var of the View
  19. // for easier access/fine grained control
  20. },
  21. plotdata:function () {
  22. return this.collection.pluck('value');
  23. },
  24. draw:function () {
  25. // Draw the plot
  26. var plotdata = this.plotdata();
  27. if (plotdata.length > 0) {
  28. this.plot({
  29. newPlot:true
  30. }, plotdata);
  31. this.caption();
  32. }
  33. },
  34. redraw:function () {
  35. // transition the plot
  36. this.plot({
  37. newPlot:false
  38. }, this.plotdata());
  39. },
  40. plot:function (opt, plotdata) {
  41. if (console) {
  42. console.log("Not implemented in base class");
  43. }
  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. }
  59. var captiondiv = $('<div/>', {class:"caption", html:caption});
  60. $(this.settings.div).append(captiondiv);
  61. }
  62. }
  63. }),
  64. PlotCollection:Backbone.Collection.extend({
  65. initialize:function (models, settings) {
  66. this.settings = settings || {};
  67. this.plottype = this.settings.plottype || this.plottype || "bar";
  68. this.caption = this.settings.caption || false;
  69. if (models) this.reset(models, {silent:true});
  70. }
  71. }),
  72. Canned:{}
  73. }
  74. return Backbone;
  75. });