bar-chart.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. (function () {
  2. Vue.component('bar-chart', {
  3. template: '<div class="chart_box" >\
  4. <div v-show="!isEmpty" :id="chartid" class="chart_box"></div>\
  5. <div v-show="isEmpty" class="chart-no-data noData">\
  6. <p v-show="isEmpty" style="text-align: center">{{title}}</p>\
  7. <img src="../../bigData/images/noData.png" alt="">\
  8. 暂无数据\
  9. </div>\
  10. </div>',
  11. props: ['config','legend'],
  12. data: function () {
  13. return {
  14. chartid: _.uniqueId("chart_"),
  15. chart: null,
  16. isEmpty: false,
  17. title:''
  18. }
  19. },
  20. mounted: function () {
  21. var that =this;
  22. this.$nextTick(function () {
  23. that.chart = echarts.init(document.getElementById(that.chartid));
  24. if (that.config) {
  25. that.chart.setOption(that.setOption( that.config));
  26. }
  27. })
  28. },
  29. methods: {
  30. setOption: function (options) {
  31. var vm = this;
  32. if(!options)return
  33. var options = $.extend({}, defaults, options);
  34. vm.chart.on('click', function (param){
  35. vm.$emit('click-item', {param:param});
  36. });
  37. return options
  38. },
  39. getByteLen:function(val) {
  40. var len = 0;
  41. for (var i = 0; i < val.length; i++) {
  42. var a = val.charAt(i);
  43. if (a.match(/[^\x00-\xff]/ig) != null) {
  44. len += 2;
  45. } else {
  46. len += 1;
  47. }
  48. }
  49. return len;
  50. },
  51. createLegend:function() {
  52. var top = '';
  53. // if(this.isbig){
  54. // top='15%'
  55. // }
  56. var showPercentArr = [] //用来获取每个图例对应的数据
  57. var total = 0 //总数用来计算百分比 因为formatter拿不到
  58. var maxStr = 0; //取最长的那个的长度 用来对齐
  59. var percent = 0; //临时变量 最后一个用1减去这个
  60. var legengdArr = []; //临时变量 最后一个用1减去这个
  61. var vm = this;
  62. this.config.series[0].data.map(function(v){
  63. maxStr = Math.max(maxStr, vm.getByteLen(v.name.toString()))
  64. total += Number(v.value);
  65. })
  66. showPercentArr = this.config.series[0].data.map(function(v, i, arr) {
  67. var nowPercent = 0
  68. if (total && i != arr.length - 1) {
  69. nowPercent = Math.round((v.value / total * 100));
  70. percent += nowPercent
  71. if (percent >= 100) {
  72. nowPercent = nowPercent - (percent - 100)
  73. percent = 100
  74. }
  75. v.percent = nowPercent
  76. v.p = nowPercent + '%'
  77. }
  78. if (i == arr.length - 1) {
  79. v.percent = 100 - percent
  80. v.p = (100 - percent) + '%'
  81. }
  82. if (!total || total == 0) {
  83. v.p = '0'
  84. v.percent = 0
  85. v.nofilter = true
  86. }
  87. return v
  88. })
  89. legengdArr = showPercentArr.filter(function(v){
  90. return Number(v.percent) != 0 || v.nofilter
  91. }).map(function(v) {
  92. return v.name
  93. })
  94. if (this.config.series && !this.config.listLegend && !this.config.smallPie) {
  95. return {
  96. data: legengdArr,
  97. // top: 30,
  98. // top: 'bottom',
  99. bottom:10,
  100. orient: 'vertical',
  101. textStyle: {
  102. color: ['#b5e1fc']
  103. },
  104. // right: this.config.leR || 10,
  105. // left: this.config.leL || '55%',
  106. // align:'right',
  107. formatter: function(name) {
  108. //
  109. var str = '',
  110. n = maxStr - vm.getByteLen(name);
  111. // for(var c=0;c<n;c++){
  112. // str +=''
  113. // }
  114. showPercentArr.map(function(v) {
  115. if (v.name == name) {
  116. str += v.p
  117. }
  118. })
  119. return name + ' ' + str
  120. }
  121. };
  122. } else {
  123. return {
  124. data: legengdArr,
  125. top: 30,
  126. formatter: function(name) {
  127. //
  128. var str = '',
  129. n = maxStr - vm.getByteLen(name);
  130. // for(var c=0;c<n;c++){
  131. // str +=''
  132. // }
  133. showPercentArr.map(function(v) {
  134. if (v.name == name) {
  135. str += v.p
  136. }
  137. })
  138. return name + ' ' + str
  139. }
  140. }
  141. }
  142. return {};
  143. },
  144. showSpecificLegends: function(arr) {
  145. let selected = {};
  146. let option = this.chart.getOption()
  147. let legend = option.legend[0]
  148. if (legend && legend.data) {
  149. _.each(legend.data, function (name) {
  150. selected[name] = _.indexOf(arr, name) > -1
  151. })
  152. legend.selected = selected;
  153. }
  154. this.chart.setOption(option);
  155. },
  156. hasData:function() {
  157. let series = this.config.series
  158. let data = []
  159. _.each(series, function(o){
  160. data = data.concat(o.data)
  161. })
  162. return data.length
  163. },
  164. smallPie(option) { //太小的格子数字展示就在里面吧
  165. // option.series[0].label = {
  166. // normal: {
  167. // position: 'inner',
  168. // formatter: '{d}%'
  169. // }
  170. // }
  171. // option.series[0].labelLine = {
  172. // normal: {
  173. // show: false
  174. // }
  175. // }
  176. option.series[0].radius = ['30', '50%'];
  177. option.series[0].center = ['50%', '60%'];
  178. }
  179. },
  180. watch: {
  181. config: function (data) {
  182. if (data) {
  183. if(!data.series[0].data){
  184. this.isEmpty = true;
  185. this.title =data.title && data.title.text; //无数据时渲染的标题
  186. }else if(data.series[0].data.length == 0){
  187. this.isEmpty = true;
  188. this.title =data.title && data.title.text; //无数据时渲染的标题
  189. }else{
  190. this.isEmpty = false;
  191. }
  192. if(this.legend){
  193. data.legend = this.createLegend();
  194. }
  195. this.chart.setOption(this.setOption(data));
  196. }
  197. }
  198. }
  199. })
  200. var defaults = {
  201. }
  202. })()