DojoD3.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. define([
  2. "dojo/_base/declare",
  3. "dojo/_base/lang",
  4. "dojo/dom",
  5. "dojo/dom-construct",
  6. "dojo/dom-geometry",
  7. "dojo/Evented",
  8. "d3/d3.v3.min"
  9. ], function (declare, lang, dom, domConstruct, domGeom, Evented) {
  10. return declare([Evented], {
  11. constructor: function () {
  12. },
  13. resize: function () {
  14. var _debounce = function (fn, timeout) {
  15. var timeoutID = -1;
  16. return function () {
  17. if (timeoutID > -1) {
  18. window.clearTimeout(timeoutID);
  19. }
  20. timeoutID = window.setTimeout(fn, timeout);
  21. }
  22. };
  23. var _debounced_draw = _debounce(lang.hitch(this, function () {
  24. domConstruct.empty(this.target.domNodeID);
  25. this.renderTo(this._target);
  26. if (this.hasData()) {
  27. this.display();
  28. }
  29. }), 125);
  30. _debounced_draw();
  31. },
  32. renderTo: function (_target) {
  33. this._target = _target;
  34. this.calcGeom();
  35. this.injectStyleSheet();
  36. this.Svg = d3.select(this.target.domDivID).append("svg")
  37. .attr("width", this.target.width)
  38. .attr("height", this.target.height)
  39. ;
  40. this.SvgG = this.Svg.append("g");
  41. },
  42. calcGeom: function () {
  43. var node = dom.byId(this._target.domNodeID);
  44. var pos = domGeom.position(node);
  45. var pad = domGeom.getPadExtents(node);
  46. this.target = lang.mixin({
  47. domDivID: "#" + this._target.domNodeID,
  48. width: pos.w - pad.w,
  49. height: pos.h - pad.h,
  50. margin: { top: 0, right: 0, bottom: 0, left: 0 }
  51. }, this._target);
  52. lang.mixin(this.target, {
  53. diameter: Math.min(this.target.width, this.target.height)
  54. });
  55. lang.mixin(this.target, {
  56. radius: this.target.diameter / 2
  57. });
  58. return this.target;
  59. },
  60. injectStyleSheet: function() {
  61. if (this.target.css) {
  62. var styleNode = dom.byId(this.target.domNodeID + "Style");
  63. if (styleNode) {
  64. domConstruct.destroy(this.target.domNodeID + "Style");
  65. }
  66. var style = domConstruct.create("style", {
  67. id: this.target.domNodeID + "Style",
  68. innerHTML: this.target.css
  69. });
  70. dojo.query("head").append(style);
  71. }
  72. }
  73. });
  74. });