common.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.charting.axis2d.common"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.axis2d.common"] = true;
  8. dojo.provide("dojox.charting.axis2d.common");
  9. dojo.require("dojox.gfx");
  10. (function(){
  11. var g = dojox.gfx;
  12. var clearNode = function(s){
  13. s.marginLeft = "0px";
  14. s.marginTop = "0px";
  15. s.marginRight = "0px";
  16. s.marginBottom = "0px";
  17. s.paddingLeft = "0px";
  18. s.paddingTop = "0px";
  19. s.paddingRight = "0px";
  20. s.paddingBottom = "0px";
  21. s.borderLeftWidth = "0px";
  22. s.borderTopWidth = "0px";
  23. s.borderRightWidth = "0px";
  24. s.borderBottomWidth = "0px";
  25. };
  26. var getBoxWidth = function(n){
  27. // marginBox is incredibly slow, so avoid it if we can
  28. if(n["getBoundingClientRect"]){
  29. var bcr = n.getBoundingClientRect();
  30. return bcr.width || (bcr.right - bcr.left);
  31. }else{
  32. return dojo.marginBox(n).w;
  33. }
  34. };
  35. dojo.mixin(dojox.charting.axis2d.common, {
  36. // summary:
  37. // Common methods to be used by any axis. This is considered "static".
  38. createText: {
  39. gfx: function(chart, creator, x, y, align, text, font, fontColor){
  40. // summary:
  41. // Use dojox.gfx to create any text.
  42. // chart: dojox.charting.Chart2D
  43. // The chart to create the text into.
  44. // creator: dojox.gfx.Surface
  45. // The graphics surface to use for creating the text.
  46. // x: Number
  47. // Where to create the text along the x axis (CSS left).
  48. // y: Number
  49. // Where to create the text along the y axis (CSS top).
  50. // align: String
  51. // How to align the text. Can be "left", "right", "center".
  52. // text: String
  53. // The text to render.
  54. // font: String
  55. // The font definition, a la CSS "font".
  56. // fontColor: String|dojo.Color
  57. // The color of the resultant text.
  58. // returns: dojox.gfx.Text
  59. // The resultant GFX object.
  60. return creator.createText({
  61. x: x, y: y, text: text, align: align
  62. }).setFont(font).setFill(fontColor); // dojox.gfx.Text
  63. },
  64. html: function(chart, creator, x, y, align, text, font, fontColor, labelWidth){
  65. // summary:
  66. // Use the HTML DOM to create any text.
  67. // chart: dojox.charting.Chart2D
  68. // The chart to create the text into.
  69. // creator: dojox.gfx.Surface
  70. // The graphics surface to use for creating the text.
  71. // x: Number
  72. // Where to create the text along the x axis (CSS left).
  73. // y: Number
  74. // Where to create the text along the y axis (CSS top).
  75. // align: String
  76. // How to align the text. Can be "left", "right", "center".
  77. // text: String
  78. // The text to render.
  79. // font: String
  80. // The font definition, a la CSS "font".
  81. // fontColor: String|dojo.Color
  82. // The color of the resultant text.
  83. // labelWidth: Number?
  84. // The maximum width of the resultant DOM node.
  85. // returns: DOMNode
  86. // The resultant DOMNode (a "div" element).
  87. // setup the text node
  88. var p = dojo.doc.createElement("div"), s = p.style, boxWidth;
  89. clearNode(s);
  90. s.font = font;
  91. p.innerHTML = String(text).replace(/\s/g, " ");
  92. s.color = fontColor;
  93. // measure the size
  94. s.position = "absolute";
  95. s.left = "-10000px";
  96. dojo.body().appendChild(p);
  97. var size = g.normalizedLength(g.splitFontString(font).size);
  98. // do we need to calculate the label width?
  99. if(!labelWidth){
  100. boxWidth = getBoxWidth(p);
  101. }
  102. // new settings for the text node
  103. dojo.body().removeChild(p);
  104. s.position = "relative";
  105. if(labelWidth){
  106. s.width = labelWidth + "px";
  107. // s.border = "1px dotted grey";
  108. switch(align){
  109. case "middle":
  110. s.textAlign = "center";
  111. s.left = (x - labelWidth / 2) + "px";
  112. break;
  113. case "end":
  114. s.textAlign = "right";
  115. s.left = (x - labelWidth) + "px";
  116. break;
  117. default:
  118. s.left = x + "px";
  119. s.textAlign = "left";
  120. break;
  121. }
  122. }else{
  123. switch(align){
  124. case "middle":
  125. s.left = Math.floor(x - boxWidth / 2) + "px";
  126. // s.left = Math.floor(x - p.offsetWidth / 2) + "px";
  127. break;
  128. case "end":
  129. s.left = Math.floor(x - boxWidth) + "px";
  130. // s.left = Math.floor(x - p.offsetWidth) + "px";
  131. break;
  132. //case "start":
  133. default:
  134. s.left = Math.floor(x) + "px";
  135. break;
  136. }
  137. }
  138. s.top = Math.floor(y - size) + "px";
  139. s.whiteSpace = "nowrap"; // hack for WebKit
  140. // setup the wrapper node
  141. var wrap = dojo.doc.createElement("div"), w = wrap.style;
  142. clearNode(w);
  143. w.width = "0px";
  144. w.height = "0px";
  145. // insert nodes
  146. wrap.appendChild(p)
  147. chart.node.insertBefore(wrap, chart.node.firstChild);
  148. return wrap; // DOMNode
  149. }
  150. }
  151. });
  152. })();
  153. }