chart-cell.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <a-popover trigger="hover"
  3. placement="left"
  4. @visibleChange="onOpenChange">
  5. <template #content>
  6. <div class="echart" :id="('popover-'+id)" :style="chartStylePopover"></div>
  7. </template>
  8. <div class="echart" :id="('chart-'+id)" :style="chartStyle"></div>
  9. </a-popover>
  10. </template>
  11. <script lang="ts">
  12. import {defineComponent, computed, markRaw} from 'vue';
  13. import * as echarts from "echarts";
  14. export default defineComponent ({
  15. props:{
  16. datas : [] as any
  17. },
  18. name : 'ChartCell',
  19. data() {
  20. return{
  21. loading : false,
  22. chartStyle: { width: "100%", height: "100%" },
  23. chartStylePopover: { width: "460px", height: "300px" },
  24. cell : null as any,
  25. chart : null as any,
  26. serieType : 'line',
  27. id : Date.now(),
  28. hovered : false
  29. }
  30. },
  31. setup(props, context){
  32. console.log("ChartCell setup" , props, context);
  33. const datas = computed(() => {
  34. return props.datas ? props.datas : []
  35. });
  36. return{
  37. datas
  38. }
  39. },
  40. mounted(){
  41. console.log("mounted" , this.id);
  42. this.readerCell();
  43. //this.readerChart();
  44. },
  45. methods:{
  46. getxAxis:function (show){
  47. let xAxis = {type : 'category', data : [] as any, show : show, axisLabel:{rotate:50}};
  48. //数据组装,这里根据业务组装x轴数据
  49. xAxis.data = ['2023年1月', '2023年2月', '2023年3月', '2023年4月', '2023年5月', '2023年6月',
  50. '2023年7月', '2023年8月', '2023年9月', '2023年10月', '2023年11月', '2023年12月'];
  51. return xAxis;
  52. },
  53. getyAxis:function (){
  54. return {type: 'value', min:'dataMin', max:'dataMax', show:false };
  55. },
  56. getSeries:function (showSymbol){
  57. let series = [{type: this.serieType, data:[] as any, showSymbol:showSymbol}];
  58. //数据组装,这里根据业务组装y轴数据
  59. series[0].data = [820, 932, 901, 934, 1290, 1330,
  60. 2000, 3000, 4000, 5000, 5000, 6000];
  61. console.log("getSeries", series);
  62. return series;
  63. },
  64. readerCell: function () {
  65. const option = {
  66. grid: { top: 0, left: 0, right: 0, bottom: 0, containLabel: false },
  67. xAxis: this.getxAxis(false),
  68. yAxis: this.getyAxis(),
  69. series: this.getSeries(false)
  70. };
  71. console.log("readerCell-"+this.serieType,option);
  72. if(this.cell==null)
  73. this.cell = markRaw(echarts.init(document.getElementById('chart-'+this.id) as HTMLElement));
  74. if(this.cell!=null) {
  75. this.cell.setOption(option);
  76. this.cell.resize();
  77. }
  78. },
  79. readerChart: function () {
  80. let xAxis = this.getxAxis(true);
  81. xAxis['nameRotate'] = 0;
  82. const option = {
  83. title: { left: 'center', text: '累产量趋势图' },
  84. tooltip: { trigger: 'item', triggerOn:"mousemove",showContent:true },
  85. grid: { top:'15%', left: '8%', right: '3%', bottom: '6%', containLabel: true },
  86. xAxis: xAxis,
  87. yAxis: { name:'累产量', nameGap: 50, nameLocation:'center', type: 'value'},
  88. series: this.getSeries(true)
  89. };
  90. console.log("readerChart-"+this.serieType,option);
  91. if(this.chart==null)
  92. this.chart = markRaw(echarts.init(document.getElementById('popover-'+this.id) as HTMLElement));
  93. if(this.chart!=null) {
  94. this.chart.setOption(option);
  95. this.chart.resize();
  96. }
  97. },
  98. onOpenChange:function(visible){
  99. console.log("onOpenChange", visible);
  100. if(visible) {
  101. setTimeout(this.readerChart, 100);
  102. }
  103. else{
  104. this.chart.dispose();
  105. this.chart = null;
  106. }
  107. }
  108. }
  109. })
  110. </script>
  111. <style lang="less" scoped></style>