PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/ample/languages/chart/elements/radar.js

https://github.com/marsch/amplesdk
JavaScript | 188 lines | 141 code | 20 blank | 27 comment | 15 complexity | c3e826f03086edf3c513493aba107a24 MD5 | raw file
  1. /*
  2. * Ample SDK - JavaScript GUI Framework
  3. *
  4. * Copyright (c) 2009 Sergey Ilinsky
  5. * Dual licensed under the MIT and GPL licenses.
  6. * See: http://www.amplesdk.com/about/licensing/
  7. *
  8. */
  9. var cChartElement_radar = function(){};
  10. cChartElement_radar.prototype = new cChartElement("radar");
  11. cChartElement_radar.handlers = {
  12. 'DOMNodeInsertedIntoDocument': function(oEvent) {
  13. this.refresh();
  14. }
  15. };
  16. cChartElement_radar.prototype.refresh = function() {
  17. // Draw Grid
  18. var d = [];
  19. // circles
  20. for (var n = 0, l = 5; n < l; n++) {
  21. d.push( "M150," + (150 - 100 * (n + 1) / l) +
  22. "A" + 100 * (n + 1) / l + "," + 100 * (n + 1) / l + " 0 0,0 150," + (150 + 100 * (n + 1) / l) +
  23. "A" + 100 * (n + 1) / l + "," + 100 * (n + 1) / l + " 0 0,0 150," + (150 - 100 * (n + 1) / l) +
  24. "z");
  25. }
  26. // lines
  27. for (var n = 0, l = 5; n < l; n++)
  28. d.push( "M150,150" +
  29. "L" + (150 - 100 * Math.cos(Math.PI / 2 + 2 * Math.PI * n / l)) + "," + (150 - 100 * Math.sin(Math.PI / 2 + 2 * Math.PI * n / l))+
  30. "z");
  31. cChartElement.setPath(this.$getContainer("grid"), d.join(''));
  32. // marks
  33. var d = [];
  34. for (var n = 0, l = 5; n < l; n++)
  35. d.push( "M" + (150 - 100 * Math.cos(Math.PI / 2 + 2 * Math.PI * n / l)) + "," + (150 - 100 * Math.sin(Math.PI / 2 + 2 * Math.PI * n / l))+
  36. "L" + (150 -(100 + 5) * Math.cos(Math.PI / 2 + 2 * Math.PI * n / l)) + "," + (150 -(100 + 5) * Math.sin(Math.PI / 2 + 2 * Math.PI * n / l))+
  37. "z");
  38. cChartElement.setPath(this.$getContainer("rAxisMarks"), d.join(''));
  39. // Draw values
  40. var nSize = 3;
  41. for (var nGroup = 0, nGroups = this.childNodes.length, oGroup; oGroup = this.childNodes[nGroup]; nGroup++) {
  42. d = [];
  43. for (var nItem = 0, nItems = oGroup.childNodes.length, oItem; oItem = oGroup.childNodes[nItem]; nItem++) {
  44. var nValue = oItem.getAttribute("value") * 1 * 2,
  45. nX = (150 - nValue * Math.cos(Math.PI / 2 + 2 * Math.PI * nItem / nItems)),
  46. nY = (150 - nValue * Math.sin(Math.PI / 2 + 2 * Math.PI * nItem / nItems));
  47. // Set point
  48. cChartElement.setPath(oItem.$getContainer("path"), "M" + (nX - nSize) + "," + nY +
  49. "a" + nSize + "," + nSize + " 0 0,0 " + nSize * 2 + ",0 " +
  50. "a" + nSize + "," + nSize + " 0 0,0-" + nSize * 2 + ",0 " +
  51. "z");
  52. //
  53. d.push((nItem ? "L" : "M") + nX + "," + nY);
  54. }
  55. cChartElement.setPath(oGroup.$getContainer("line"), d.join(" ") + "z");
  56. cChartElement.setPath(oGroup.$getContainer("shadow"), d.join(" ") + "z");
  57. cChartElement.setPath(oGroup.$getContainer("area"), d.join(" ") + "z");
  58. // Draw legend
  59. var nXPath = 280,
  60. nYPath =(50 + (nGroups - nGroup) * 20);
  61. cChartElement.setPath(oGroup.$getContainer("path"), "M" + (nXPath - 5) + "," + (nYPath - 5) + "h10 v10 h-10 v-10 z");
  62. cChartElement.setTextPosition(oGroup.$getContainer("label"),
  63. nXPath + 20, nYPath + 5);
  64. }
  65. };
  66. if (!cChartElement.useVML) {
  67. cChartElement_radar.prototype.$getTagOpen = function() {
  68. return '<div class="c-radar' +(this.hasAttribute("class") ? ' ' + this.getAttribute("class") : '')+ '" style="' + this.getAttribute("style") + '">\
  69. <svg:svg class="c-radar--canvas" viewBox="0 0 400 300" style="width:inherit;height:inherit" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">\
  70. <svg:text class="c-radar--title" y="30" x="150">' + this.getAttribute("title")+ '</svg:text>\
  71. <svg:rect x="260" y="50" width="120" height="120" rx="10" class="c-legend"/>\
  72. <svg:path class="c-grid c-radar--grid"/>\
  73. <svg:g class="c-rAxis">\
  74. <svg:path class="c-radar--rAxis" d="m150,150 v-100" style="fill:none"/>\
  75. <svg:path id="r' + this.uniqueID + '" d="M140,150 v-150" style="fill:none;stroke:none"/>\
  76. <svg:text class="c-rAxis--label c-radar--rAxisLabel"><svg:textPath xlink:href="#r' + this.uniqueID + '">' + this.getAttribute("rAxisLabel")+ '</svg:textPath></svg:text>\
  77. <svg:path class="c-rAxis--marks c-radar--rAxisMarks" />\
  78. </svg:g>\
  79. <svg:g class="c-radar--gateway">';
  80. };
  81. cChartElement_radar.prototype.$getTagClose = function() {
  82. return ' </svg:g>\
  83. </svg:svg>\
  84. </div>';
  85. };
  86. }
  87. else {
  88. // Redefine handlers
  89. (function() {
  90. // DOMNodeInsertedIntoDocument
  91. var fHandlerInserted = cChartElement_radar.handlers['DOMNodeInsertedIntoDocument'];
  92. cChartElement_radar.handlers['DOMNodeInsertedIntoDocument'] = function(oEvent) {
  93. if (fHandlerInserted)
  94. fHandlerInserted.call(this, oEvent);
  95. //
  96. cChartElement_radar.recalcCSS(this);
  97. if (navigator.userAgent.match(/MSIE ([0-9.]+)/) && RegExp.$1 * 1 == 8)
  98. cChartElement_radar.resize(this);
  99. //
  100. this.$getContainer().attachEvent("onresize", cChartElement_radar.onresize);
  101. };
  102. // DOMNodeRemovedFromDocument
  103. var fHandlerRemoved = cChartElement_radar.handlers['DOMNodeRemovedFromDocument'];
  104. cChartElement_radar.handlers['DOMNodeRemovedFromDocument'] = function(oEvent) {
  105. if (fHandlerRemoved)
  106. fHandlerRemoved.call(this, oEvent);
  107. //
  108. this.$getContainer().detachEvent("onresize", cChartElement_radar.onresize);
  109. };
  110. })();
  111. cChartElement_radar.resize = function(oInstance) {
  112. //
  113. var oElement= oInstance.$getContainer(),
  114. oCanvas = oInstance.$getContainer("canvas"),
  115. oRect = oElement.getBoundingClientRect(),
  116. nWidth = oRect.right - oRect.left - (parseInt(oElement.currentStyle.borderWidth) || 0) * 2,
  117. nHeight = Math.round(nWidth * 3 / 4);
  118. oCanvas.style.display = "none";
  119. oCanvas.style.width = nWidth + "px";
  120. oCanvas.style.height = nHeight + "px";
  121. // oElement.style.width = nWidth + "px";
  122. oElement.style.height = nHeight + "px";
  123. // TODO: recalc relevant CSS recursively (font-size, stroke-width)
  124. // IE8 performance bug
  125. setTimeout(function(){
  126. oCanvas.style.display = "";
  127. }, 0);
  128. };
  129. cChartElement_radar.onresize = function(oEvent) {
  130. var oElement;
  131. if ((oElement = ample.$instance(oEvent.srcElement)) && oElement instanceof cChartElement)
  132. cChartElement_radar.resize(oElement);
  133. };
  134. cChartElement_radar.recalcCSS = function(oElement) {
  135. cChartElement.applyCSS(oElement.$getContainer("title"));
  136. cChartElement.applyCSS(oElement.$getContainer("legend"));
  137. cChartElement.applyCSS(oElement.$getContainer("grid"));
  138. cChartElement.applyCSS(oElement.$getContainer("rAxis"));
  139. cChartElement.applyCSS(oElement.$getContainer("rAxisMarks"));
  140. cChartElement.applyCSS(oElement.$getContainer("rAxisLabel"));
  141. };
  142. cChartElement_radar.prototype.$getTagOpen = function() {
  143. return '<div class="c-radar' +(this.hasAttribute("class") ? ' ' + this.getAttribute("class") : '')+ '" style="overflow:hidden;' + this.getAttribute("style") + '">\
  144. <chart2vml:group class="c-radar--canvas" style="position:absolute;display:none;" coordOrigin="0 0" coordSize="400 300">\
  145. <chart2vml:shape class="c-radar--title" path="m0,0 l300,0" fillcolor="black" stroked="false" allowoverlap="true" style="position:absolute;width:100%;height:100%;top:30px;xleft:150px">\
  146. <chart2vml:path textpathok="true" />\
  147. <chart2vml:textpath on="true" string="' + this.getAttribute("title")+ '" style="v-text-align:center"/>\
  148. </chart2vml:shape>\
  149. <chart2vml:shape path="' + cChartElement.roundRectPath(260, 50, 120, 120, 10, 10) + '" class="c-legend c-radar--legend" style="position:absolute;width:100%;height:100%"/>\
  150. <chart2vml:shape class="c-grid c-radar--grid" style="position:absolute;width:100%;height:100%" fillcolor="black"/>\
  151. <chart2vml:group class="c-rAxis" style="position:absolute;width:100%;height:100%">\
  152. <chart2vml:shape class="c-radar--rAxis" path="m150,150 v-100" style="position:absolute;width:100%;height:100%"/>\
  153. <chart2vml:shape class="c-rAxis--label c-radar--rAxisLabel" path="m140,150 r0,-100 e" fillcolor="black" stroked="false" allowoverlap="true" style="position:absolute;width:100%;height:100%">\
  154. <chart2vml:path textpathok="true" />\
  155. <chart2vml:textpath on="true" string="' + this.getAttribute("rAxisLabel")+ '" style="v-text-align:right"/>\
  156. </chart2vml:shape>\
  157. <chart2vml:shape class="c-rAxis--marks c-radar--rAxisMarks" stroked="true" filled="false" style="position:absolute;width:100%;height:100%" />\
  158. </chart2vml:group>\
  159. <chart2vml:group class="c-radar--gateway" style="position:absolute;width:100%;height:100%">';
  160. };
  161. cChartElement_radar.prototype.$getTagClose = function() {
  162. return ' </chart2vml:group>\
  163. </chart2vml:group>\
  164. </div>';
  165. };
  166. }
  167. // Register Element
  168. ample.extend(cChartElement_radar);