chart.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * This is a charting wrapper, it is designed to simplify the interface to d3
  3. * dependencies:
  4. * - jqplot.js charting library http://jqplot.org for documentation and more
  5. * - utils.js has utilities and design patterns used for internal purposes
  6. * - dom.js creates legend objects
  7. *
  8. * @TODO: Find a way to mix & match charts. Some people think it's visually appealing.
  9. */
  10. if(!jx){
  11. var jx = {}
  12. }
  13. jx.chart = {} ;
  14. jx.jqplot = {}
  15. /***
  16. * adding dependencies and libraries in case they are overwhelming for users
  17. * This
  18. */
  19. jx.jqplot.init = function(){
  20. body = document.getElementsByTagName('head') ;
  21. if(body != null){
  22. body = body [0]
  23. link = jx.dom.get.instance('LINK') ;
  24. link.href = 'https://the-phi.com/pub/js/jqplot/jqplot.css' ;
  25. link.rel = 'stylesheet'
  26. link.type = 'type/css'
  27. document.head.appendChild(link);
  28. var url = [
  29. 'https://the-phi.com/pub/js/jqplot/excanvas.js',
  30. 'https://the-phi.com/pub/js/jqplot/jqplot.js',
  31. 'https://the-phi.com/pub/js/jqplot/plugins/jqplot.mobile.js',
  32. 'https://the-phi.com/pub/js/jqplot/plugins/jqplot.barRenderer.js',
  33. 'https://the-phi.com/pub/js/jqplot/plugins/jqplot.donutRenderer.js',
  34. 'https://the-phi.com/pub/js/jqplot/plugins/jqplot.categoryAxisRenderer.js',
  35. 'https://the-phi.com/pub/js/jqplot/plugins/jqplot.canvasAxisLabelRenderer.js',
  36. 'https://the-phi.com/pub/js/jqplot/plugins/jqplot.pointLabels.js',
  37. 'https://the-phi.com/pub/js/jqplot/plugins/jqplot.canvasTextRenderer.js'
  38. ] ;
  39. // for(i in url){
  40. // script = jx.dom.get.instance('SCRIPT') ;
  41. // script.setAttribute('src',url[i]) ;
  42. // script.language = 'javascript'
  43. // document.head.appendChild(script)
  44. // }
  45. jx.utils.patterns.visitor(url,function(_url_){
  46. script = jx.dom.get.instance('SCRIPT') ;
  47. script.setAttribute('src',_url_) ;
  48. script.language = 'javascript'
  49. document.write(script.innerHTML)
  50. // document.head.appendChild(script)
  51. })
  52. }
  53. }
  54. jx.jqplot.line = {}
  55. jx.jqplot.line.options= function(){
  56. var options = {
  57. animateReplot:true,
  58. series:[{showMarker:true}],
  59. grid:{
  60. drawGridLines:false,
  61. background:'white',
  62. borderColor:'white'
  63. },
  64. seriesDefaults:{
  65. rendererOptions: {
  66. smooth: true,
  67. animation: {
  68. show: true
  69. }
  70. },
  71. pointLabels: {show: false}
  72. },
  73. axes:{
  74. xaxis:{
  75. tickOptions:{
  76. showGridline: true
  77. },
  78. // label:'Angle (radians)'
  79. renderer: $.jqplot.CategoryAxisRenderer,
  80. min:0,
  81. pad:2
  82. },
  83. yaxis:{
  84. // label:'Cosine'
  85. min:0,
  86. tickOptions:{
  87. showGridline: false
  88. },
  89. labelRenderer: $.jqplot.CanvasAxisLabelRenderer
  90. }
  91. },
  92. legend:{}
  93. } ;
  94. return options;
  95. }
  96. /**
  97. * @id DOM identifier
  98. * @series matrix of values to be ploted
  99. * @labels xaxis labels
  100. * @lnames names of series (vectors in the matrix)
  101. */
  102. jx.jqplot.line.render = function(id,series,labels,lnames){
  103. jx.dom.set.value(id,'')
  104. var options = jx.jqplot.line.options() ;
  105. options.axes.xaxis.ticks = labels ;
  106. if(series.length > 1){
  107. options.legend.show = true
  108. options.legend.location = 'ne'
  109. options.legend.placement = 'outsideGrid'
  110. options.series = lnames;
  111. }
  112. return $.jqplot(id,series,options)
  113. }
  114. jx.jqplot.bar = {}
  115. jx.jqplot.bar.options = function(){
  116. var options = {
  117. animateReplot:true,
  118. grid:{
  119. background:'white',
  120. drawGridLines:false,
  121. borderColor:'white'
  122. },
  123. seriesDefaults:{
  124. renderer:$.jqplot.BarRenderer,
  125. rendererOptions: {
  126. highlightMouseDown: true,
  127. smooth: true,
  128. animation: {
  129. speed:900,
  130. show: true
  131. }
  132. },
  133. pointLabels: {show: true}
  134. },
  135. axes: {
  136. xaxis: {
  137. tickOptions:{
  138. showGridline: false
  139. },
  140. renderer: $.jqplot.CategoryAxisRenderer,
  141. // ticks:labels,
  142. showTickMarks:false,
  143. pad:0
  144. },
  145. yaxis:{
  146. tickOptions:{
  147. showGridline: true
  148. }
  149. }
  150. },
  151. legend: {
  152. // show: keys.length > 1,
  153. location: 'ne',
  154. xoffset:2,
  155. placement: 'outsideGrid',
  156. show:true
  157. }
  158. }
  159. return options;
  160. }
  161. /**
  162. * Creating a bar chart (columns actually)
  163. * @id DOM identifier
  164. * @series array of matrices of values (in the y-axis)
  165. * @labels x-axis labels
  166. * @lnames names of vectors in the series
  167. */
  168. jx.jqplot.bar.render = function(id,series,labels,lnames){
  169. jx.dom.set.value(id,'')
  170. var options = jx.jqplot.bar.options()
  171. options.axes.xaxis.ticks = labels ;
  172. //options.legend.show = true
  173. options.series = lnames;
  174. return $.jqplot(id, series,options)
  175. }
  176. jx.jqplot.stackedBar = {}
  177. jx.jqplot.stackedBar.options = jx.jqplot.bar.options ;
  178. jx.jqplot.stackedBar.render = function(id,series,label,lnames){
  179. jx.dom.set.value(id,'')
  180. var options = jx.jqplot.stackedBar.options()
  181. options.axes.xaxis.ticks = labels ;
  182. options.legend.show = true
  183. options.series = lnames;
  184. options.stackSeries= true,
  185. // options.seriesDefaults.label = lnames[0];
  186. options.seriesDefaults.pointLabels.show= false;
  187. return $.jqplot(id, series,options)
  188. }
  189. jx.jqplot.donut = {}
  190. jx.jqplot.donut.options= function(){
  191. var options = {
  192. animate:true,
  193. animateReplot:true,
  194. grid:{
  195. drawGridLines:false,
  196. background:'transparent',
  197. borderColor:'white',
  198. borderWidth:'1px'
  199. },
  200. seriesDefaults: {
  201. renderer:$.jqplot.DonutRenderer,
  202. rendererOptions:{
  203. // fill:false,
  204. sliceMargin: 3,
  205. startAngle: -90,
  206. showDataLabels: true,
  207. // dataLabels: 'value'
  208. dataLabels:'percent'
  209. }
  210. },
  211. legend: { show:true, location: 'e' }
  212. }
  213. return options;
  214. }
  215. /**
  216. * @id DOM identifier
  217. * @series vector of values
  218. * @labels labels of the values in the series
  219. */
  220. jx.jqplot.donut.render = function(id,series){
  221. jx.dom.set.value(id,'')
  222. var options = jx.jqplot.donut.options() ;
  223. return $.jqplot(id,series,options)
  224. }