25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

227 lines
5.9 KiB

  1. GraphViewer= function(parent, w, h) {
  2. this.show_labels = true;
  3. this.font_size = 10;
  4. if(!parent) {
  5. this.wrapper = document.createElement('div')
  6. parent = this.wrapper
  7. }
  8. this.body = $a(parent, 'div', 'gr_body');
  9. if(w&&h) {
  10. $w(this.body, w + 'px');
  11. $w(this.body, h + 'px');
  12. }
  13. this._y_name = $a(parent, 'div', 'gr_y_name');
  14. this._x_name = $a(parent, 'div', 'gr_x_name');
  15. this._y_labels = $a(parent, 'div', 'gr_y_labels');
  16. this._x_labels = $a(parent, 'div', 'gr_x_labels');
  17. this.legend_area = $a(parent, 'div', 'gr_legend_area');
  18. this.title_area = $a(parent, 'div', 'gr_title_area');
  19. this.main_area = $a(parent, 'div', 'gr_main_area');
  20. this.set_horizontal();
  21. //this.set_vertical();
  22. }
  23. GraphViewer.prototype.clear = function() {
  24. this.series = [];
  25. this.xlabels = [];
  26. this.xtitle = null;
  27. this.ytitle = null;
  28. }
  29. GraphViewer.prototype.set_vertical = function() {
  30. this.k_barwidth = 'width';
  31. this.k_barstart = 'left';
  32. this.k_barlength = 'height';
  33. this.k_barbase = 'bottom';
  34. this.k_bartop = 'top';
  35. this.k_gridborder = 'borderTop';
  36. this.y_name = this._y_name;
  37. this.x_name = this._x_name;
  38. this.y_labels = this._y_labels;
  39. this.x_labels = this._x_labels;
  40. this.vertical = true;
  41. }
  42. GraphViewer.prototype.set_horizontal = function() {
  43. this.k_barwidth = 'height';
  44. this.k_barstart = 'top';
  45. this.k_barlength = 'width';
  46. this.k_barbase = 'left';
  47. this.k_bartop = 'right';
  48. this.k_gridborder = 'borderRight';
  49. this.y_name = this._x_name;
  50. this.x_name = this._y_name;
  51. this.y_labels = this._x_labels;
  52. this.x_labels = this._y_labels;
  53. this.vertical = false;
  54. }
  55. GraphViewer.prototype.set_title = function(t) {
  56. this.title_area.innerHTML = t;
  57. }
  58. GraphViewer.prototype.add_series = function(label, color, values, borderColor) {
  59. var s = new GraphViewer.GraphSeries(this, label);
  60. s.color = color;
  61. s.borderColor = borderColor;
  62. s.data = values;
  63. this.series[this.series.length] = s;
  64. //this.xlabels[this.xlabels.length] = label;
  65. }
  66. GraphViewer.prototype.refresh = function() {
  67. //
  68. this.legend_area.innerHTML = '';
  69. this.main_area.innerHTML = '';
  70. this.x_labels.innerHTML = '';
  71. this.y_labels.innerHTML = '';
  72. this.x_name.innerHTML = '';
  73. this.y_name.innerHTML = '';
  74. // get max
  75. var maxx=null;
  76. var legendheight = 12;
  77. for(i=0;i<this.series.length;i++) {
  78. var series_max = this.series[i].get_max();
  79. if(!maxx)maxx = series_max;
  80. if(series_max > maxx)maxx = series_max;
  81. // show series names
  82. var tmp = $a(this.legend_area, 'div', 'gr_legend');
  83. tmp.style.backgroundColor = this.series[i].color;
  84. if(this.series[i].borderColor)
  85. tmp.style.border = '1px solid ' + this.series[i].borderColor;
  86. tmp.style.top = (i*(legendheight + 2)) + 'px';
  87. tmp.style.height= legendheight + 'px';
  88. var tmp1 = $a(this.legend_area, 'div', 'gr_legend');
  89. tmp1.style.top = (i*(legendheight + 2)) + 'px';
  90. tmp1.style.left = '30px';
  91. $w(tmp1, '80px');
  92. tmp1.innerHTML = this.series[i].name;
  93. }
  94. if(maxx==0)maxx = 1;
  95. this.maxx = 1.1 * maxx;
  96. // y - axis grid
  97. var xfn = fmt_money;
  98. // smart grid
  99. if(maxx>1) {
  100. var nchars = (cint(maxx)+'').length;
  101. var gstep = Math.pow(10, (nchars-1));
  102. while(flt(maxx / gstep) < 4) {
  103. gstep = gstep / 2;
  104. }
  105. } else {
  106. var gstep = maxx / 6;
  107. }
  108. var curstep = gstep;
  109. while(curstep < this.maxx) {
  110. var gr = $a(this.main_area, 'div', 'gr_grid');
  111. gr.style[this.k_bartop] = (100-((flt(curstep)/this.maxx) * 100)) + '%';
  112. gr.style[this.k_barwidth] = '100%';
  113. gr.style[this.k_gridborder] = '1px dashed #888';
  114. var ylab = $a(this.y_labels, 'div', 'gr_label');
  115. ylab.style[this.k_bartop] = (99-((flt(curstep)/this.maxx)*100)) + '%';
  116. ylab.style[this.k_barstart] = '10%';
  117. ylab.innerHTML = xfn(curstep);
  118. curstep += gstep;
  119. }
  120. if(this.vertical) {
  121. this.x_name.innerHTML = this.xtitle;
  122. middletext(this.y_name, this.ytitle);
  123. } else {
  124. middletext(this.x_name, this.xtitle);
  125. this.y_name.innerHTML = this.ytitle;
  126. }
  127. // make X units
  128. this.xunits = [];
  129. this.xunit_width = (100 / this.xlabels.length);
  130. if(this.series[0]){
  131. for(i=0;i<this.xlabels.length;i++) {
  132. this.xunits[this.xunits.length] = new GraphViewer.GraphXUnit(this, i, this.xlabels[i]);
  133. }
  134. }
  135. }
  136. GraphViewer.GraphSeries= function(graph, name) {
  137. this.graph = graph;
  138. this.name = name;
  139. }
  140. GraphViewer.GraphSeries.prototype.get_max = function() {
  141. var m;
  142. for(t=0;t<this.data.length;t++) {
  143. if(!m)m = this.data[t];
  144. if(this.data[t]>m)m=this.data[t]
  145. }
  146. return m;
  147. }
  148. GraphViewer.GraphXUnit= function(graph, idx, label) {
  149. this.body = $a(graph.main_area, 'div', 'gr_xunit');
  150. this.body.style[graph.k_barstart] = (idx * graph.xunit_width) + '%';
  151. this.body.style[graph.k_barwidth] = graph.xunit_width + '%';
  152. this.body.style[graph.k_barlength] = '100%';
  153. this.show(graph, label, idx);
  154. //
  155. if(graph.show_labels) {
  156. this.label = $a(graph.x_labels, 'div', 'gr_label');
  157. this.label.style[graph.k_barstart] = (idx * graph.xunit_width) + '%';
  158. this.label.style[graph.k_barwidth] = graph.xunit_width + '%';
  159. if(graph.vertical) {
  160. $y(this.label,{height:'100%',top:'10%'});
  161. this.label.innerHTML = label;
  162. } else {
  163. middletext(this.label, label);
  164. }
  165. }
  166. }
  167. GraphViewer.GraphXUnit.prototype.show = function(graph, l, idx) {
  168. var bar_width = (100 / (graph.series.length + 1));
  169. //if(bar_width>15) bar_width = 15;
  170. //if(bar_width<20) bar_width = 20;
  171. start = (100 - (graph.series.length*bar_width)) / 2
  172. for(var i=0;i<graph.series.length; i++) {
  173. var v = graph.series[i].data[idx];
  174. var b = $a(this.body, 'div', 'gr_bar');
  175. b.style[graph.k_barbase] = '0%';
  176. b.style[graph.k_barstart] = start + '%';
  177. b.style[graph.k_barwidth] = bar_width + '%';
  178. b.style[graph.k_barlength] = (v / graph.maxx * 100) + '%';
  179. if(graph.series[i].color)b.style.backgroundColor = graph.series[i].color;
  180. if(graph.series[i].borderColor)
  181. b.style.border = '1px solid ' + graph.series[i].borderColor;
  182. start += bar_width;
  183. }
  184. }
  185. function middletext(par, t, size) {
  186. if(!size)size = 10;
  187. var tb = $a(par, 'div', 'absdiv');
  188. tb.style.top = ((par.clientHeight - size) / 2) + 'px';
  189. tb.innerHTML = t;
  190. }