chart.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div :id="'container'+index" class="chart">
  3. </div>
  4. </template>
  5. <script>
  6. import Highcharts from 'highcharts/highstock';
  7. import HighchartsMore from 'highcharts/highcharts-more';
  8. import HightchartsExport from 'highcharts/modules/exporting';
  9. import HighchartszhCN from '@/utils/highcahrts-zh_CN'
  10. HighchartszhCN(Highcharts)
  11. HighchartsMore(Highcharts)
  12. HightchartsExport(Highcharts)
  13. const elementResizeDetectorMaker = require("element-resize-detector");//引入监听dom变化的组件
  14. const erd = elementResizeDetectorMaker();
  15. import { defaultOpts } from '@/utils/defaultOptions'
  16. export default {
  17. name:'',
  18. props: {
  19. chartId: {
  20. type: Number,
  21. },
  22. options: {
  23. type: Object,
  24. },
  25. //maxHeight
  26. height: {
  27. type: String,
  28. default: ''
  29. },
  30. minHeight: {
  31. type: String,
  32. default: ''
  33. },
  34. //多个chart的情况
  35. index: {
  36. type: String,
  37. default: '1'
  38. },
  39. chartInfo: {
  40. type: Object,
  41. default: () => {}
  42. }
  43. },
  44. watch: {
  45. 'options': {
  46. handler(newval) {
  47. console.log('重绘')
  48. this.init()
  49. },
  50. deep:true
  51. }
  52. },
  53. data () {
  54. return {
  55. chart:null,
  56. };
  57. },
  58. methods: {
  59. init() {
  60. /* eta1.4.1增加了主题色
  61. 仍然以defaultOpts为最底层配置 外层配置在这里统一设置了如legend chart背景色
  62. x轴y轴系列等样式还是需要在具体的options中单独设置 */
  63. let themeOptions = this.setThemeOptions();
  64. const options = {...defaultOpts,...themeOptions,...this.options}
  65. console.log(themeOptions,this.options)
  66. let thatThis = this
  67. //stock不支持线形图只支持时间图 部分图用原始chart
  68. let is_linear = this.options.series
  69. ? this.options.series.some(_ => _.chartType === 'linear')
  70. : false ;
  71. Highcharts.setOptions({ global: { useUTC: false } });
  72. this.chart = is_linear
  73. ? Highcharts.chart(`container${this.index}`, options)
  74. : Highcharts.stockChart(`container${this.index}`,options);
  75. let that = this;
  76. if(!$('#right')[0]) return
  77. erd.listenTo($('#right')[0], function (element) { //执行监听
  78. that.$nextTick(function () {
  79. that.chart.reflow(); //变化重新渲染
  80. })
  81. })
  82. },
  83. // 点击Y轴标题监听事件
  84. clickYAxisTitle(){
  85. this.$emit('clickYAxisTitle')
  86. },
  87. // 点击X轴标题监听事件 散点图
  88. clickXAxisTitle(){
  89. // 目前点击Y轴标题和X轴都是同一个方法 所以直接派发跟Y轴点击的方法
  90. this.$emit('clickYAxisTitle')
  91. },
  92. //主题色一些外层公用配置 目前只有绘图区和legend和colors
  93. setThemeOptions() {
  94. if(!this.chartInfo||!this.chartInfo.ChartThemeStyle) return {}
  95. let chartTheme = JSON.parse(this.chartInfo.ChartThemeStyle)
  96. return {
  97. legend: {
  98. ...defaultOpts.legend,
  99. ...chartTheme.legendOptions
  100. },
  101. chart: {
  102. ...defaultOpts.chart,
  103. ...chartTheme.drawOption,
  104. spacing: chartTheme.legendOptions.verticalAlign==='bottom' ? [23,10,2,10] : [2,10,2,10],//图例在底部顶部空间留大点给单位
  105. },
  106. colors: chartTheme.colorsOptions
  107. }
  108. },
  109. initHeight() {
  110. /* 1550以下 */
  111. if($(window).width() <= 1710) {
  112. $(`#container${this.index}`)[0].style.height = this.minHeight || '350px';
  113. }else {
  114. $(`#container${this.index}`)[0].style.height = this.height || '440px';
  115. }
  116. // if(this.options.isRelevanceChart){//相关性图表设置x轴在y轴0刻度
  117. // this.$nextTick(()=>{
  118. // const h=$(`#container${this.index}`)[0].style.height
  119. // if(h=='500px'){
  120. // this.options.xAxis.offset=-200.5
  121. // }else if(h=='440px'){
  122. // this.options.xAxis.offset=-170.5
  123. // }else if(h=='350px'){
  124. // this.options.xAxis.offset=-125.5
  125. // }
  126. // // let h = $('.highcharts-yaxis .highcharts-axis-line')[0].getBoundingClientRect().height;
  127. // // this.options.xAxis.offset=-h/2;
  128. // })
  129. // }
  130. }
  131. },
  132. created() {},
  133. mounted() {
  134. this.initHeight();
  135. this.init();
  136. window.addEventListener('resize', this.initHeight);
  137. },
  138. destroyed() {
  139. window.removeEventListener('resize', this.initHeight);
  140. }
  141. }
  142. </script>
  143. <style lang='scss' scoped>
  144. .chart {
  145. /* width: 100%; */
  146. min-height:300px;
  147. }
  148. </style>