pie-chart.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. (function () {
  2. Vue.component('pie-chart', {
  3. template: '<div class="chart_box" >\
  4. <div :id="chartid" class="chart_box"></div>\
  5. <div class="tipdiv" :style="{color:color}" ><span class="tiptitle">{{title}}</span><span class="tipnumber">{{config}}</span></div>\
  6. </div>',
  7. props: ['config', 'color', 'title'],
  8. data: function () {
  9. return {
  10. chartid: _.uniqueId("chart_"),
  11. chart: null,
  12. percent:'',
  13. data:[]
  14. }
  15. },
  16. mounted: function () {
  17. this.$nextTick(function () {
  18. this.chart = echarts.init(document.getElementById(this.chartid));
  19. console.log('来过')
  20. console.log(this.chart)
  21. if (this.config) {
  22. var newdata =[];
  23. newdata[0] =parseInt(this.config)/100*100;
  24. newdata[1] =100- newdata[0];
  25. this.data = newdata;
  26. this.chart.setOption(this.setOption( this.data));
  27. }
  28. })
  29. },
  30. methods: {
  31. setOption: function (data) {
  32. var vm =this;
  33. option = {
  34. tooltip: {
  35. trigger: 'item',
  36. formatter: "{a} <br/>{b}: {c} ({d}%)",
  37. show: false
  38. },
  39. legend:{
  40. show:false
  41. },
  42. series: [{
  43. type: 'pie',
  44. radius: ['50%', '70%'],
  45. label: {
  46. normal: {
  47. show: false,
  48. position: 'center'
  49. }
  50. },
  51. labelLine: {
  52. normal: {
  53. show: false
  54. }
  55. },
  56. itemStyle: {
  57. normal: {
  58.               //好,这里就是重头戏了,定义一个list,然后根据所以取得不同的值,这样就实现了,
  59. color: function(params) {
  60. // build a color map as your need.
  61. var colorList = [
  62. vm.color, '#dbe9f2',
  63. ];
  64. return colorList[params.dataIndex]
  65. },
  66.               //以下为是否显示,显示位置和显示格式的设置了
  67. label: {
  68. show: true,
  69. position: 'top',
  70. // formatter: '{c}'
  71. formatter: '{b}\n{c}'
  72. }
  73. }
  74. },
  75. data: data
  76. }]
  77. };
  78. return option
  79. }
  80. },
  81. watch: {
  82. config: function (data) {
  83. if (data) {
  84. var newdata =[];
  85. newdata[0] =parseInt(this.config)/100*100;
  86. newdata[1] =100- newdata[0];
  87. this.data = newdata;
  88. this.chart.setOption(this.setOption(this.data));
  89. }
  90. }
  91. }
  92. })
  93. })()