display-chart-bar.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <div class="echart" :id="('chart-'+id)" :style="chartStyle"></div>
  3. </template>
  4. <script lang="ts">
  5. import {defineComponent, computed} from 'vue';
  6. import * as echarts from "echarts";
  7. export default defineComponent ({
  8. props:{
  9. bases : [] as any,
  10. measures : [] as any,
  11. wheres : [] as any
  12. },
  13. name : 'DisplayChartBar',
  14. data() {
  15. return{
  16. dataList : [] as any,
  17. loading : false,
  18. chartStyle: { width: "100%", height: "400px" },
  19. chart : null as any,
  20. serieType : 'bar',
  21. id : Date.now()
  22. }
  23. },
  24. setup(props, context){
  25. console.log("DisplayChart setup" , props, context);
  26. const bases = computed(() => {
  27. console.log("props.bases", props.bases)
  28. return props.bases ? props.bases : []
  29. });
  30. const measures = computed(() => {
  31. return props.measures ? props.measures : []
  32. });
  33. const wheres = computed(() => {
  34. return props.wheres ? props.wheres : []
  35. });
  36. return{
  37. bases, measures, wheres
  38. }
  39. },
  40. mounted(){
  41. console.log("mounted" ,this.id);
  42. },
  43. methods:{
  44. getxAxis:function (){
  45. let xAxis = { type : 'category', data : [] as any, axisTick: { alignWithLabel: true }};
  46. if(this.bases.length==0 || this.dataList.length==0) return xAxis;
  47. for(let i=0;i<this.dataList.length;i++){
  48. let arys = [] as any;
  49. for(let n=0;n<this.bases.length;n++){
  50. let colName = "RENAME"+this.bases[n].disOrder;
  51. if(this.dataList[i][colName]) arys.push(this.dataList[i][colName]);
  52. }
  53. xAxis.data.push(arys.join('_'));
  54. }
  55. console.log("getxAxis", xAxis);
  56. return xAxis;
  57. },
  58. getyAxis:function (){
  59. return { type: 'value'};
  60. },
  61. getAxis:function (xy){
  62. if(xy=="x") return this.getxAxis();
  63. else if(xy=="y") return this.getyAxis();
  64. },
  65. getSeries:function (){
  66. let series = [] as any;
  67. if(this.measures.length==0 || this.dataList.length==0) return series;
  68. series.slice(0,1);
  69. for(let i=0;i<this.measures.length;i++) {
  70. const it = this.measures[i];
  71. const colName = "RENAME"+it.disOrder;
  72. let col = {name: (it.displayName?it.displayName:it.fieldName), type: this.serieType, data:[] as any};
  73. for(let n=0;n<this.dataList.length;n++){
  74. const data = this.dataList[n];
  75. col.data.push(data[colName]?data[colName]:0);
  76. }
  77. series.push(col);
  78. }
  79. console.log("getSeries", series);
  80. return series;
  81. },
  82. getLegends:function (){
  83. let legend = {data:[] as any};
  84. for(let i=0;i<this.measures.length;i++) {
  85. const it = this.measures[i];
  86. legend.data.push((it.displayName?it.displayName:it.fieldName));
  87. }
  88. console.log("getLegends", legend);
  89. return legend;
  90. },
  91. setData : function (result){
  92. console.log(result);
  93. if(result["code"]!=0) this.dataList = [];
  94. else{
  95. this.dataList = result["rows"];
  96. this.loading = false;
  97. }
  98. this.readerChart();
  99. },
  100. reader:function (){
  101. this.dataList = [];
  102. this.readerChart();
  103. },
  104. readerChart: function () {
  105. const option = {
  106. tooltip: { trigger: 'item', triggerOn:"mousemove",showContent:true },
  107. toolbox: { show: true, feature: { saveAsImage: {} }},
  108. grid: { left: '2%', right: '2%', bottom: '2%', containLabel: true },
  109. legend : this.getLegends(),
  110. xAxis: this.getAxis("x"),
  111. yAxis: this.getAxis("y"),
  112. series: this.getSeries()
  113. };
  114. console.log("readerChart-"+this.serieType,option);
  115. if(this.chart==null)
  116. this.chart = echarts.init(document.getElementById('chart-'+this.id) as HTMLElement);
  117. if(this.chart!=null) {
  118. this.chart.setOption(option);
  119. let sender=this.chart;
  120. setTimeout(function () {
  121. sender.resize();
  122. }, 100);
  123. }
  124. },
  125. resize: function (){
  126. if(this.chart!=null) this.chart.resize();
  127. }
  128. }
  129. })
  130. </script>
  131. <style lang="less" scoped></style>