chart_a.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div class="chart_a">
  3. <div id="chart_a" style="width: 80%;height:400px;"></div>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. series: [],
  11. optionKeys: [],
  12. time: []
  13. };
  14. },
  15. props: {
  16. option: { type: Object }
  17. },
  18. created() {},
  19. watch: {
  20. option(nVal, oVal) {
  21. this.optionKeys = Object.keys(this.option);
  22. this.series = [];
  23. Object.keys(this.option).forEach(key => {
  24. this.series.push({
  25. name: "通道" + key,
  26. type: "line",
  27. data: this.option[key].data.reverse(),
  28. markPoint: {
  29. data: [
  30. { type: "max", name: "最大值" },
  31. { type: "min", name: "最小值" }
  32. ]
  33. },
  34. markLine: {
  35. data: [{ type: "average", name: "平均值" }]
  36. }
  37. });
  38. });
  39. this.time = this.option[this.optionKeys[0]].time.reverse();
  40. this.drawChart();
  41. }
  42. },
  43. mounted() {
  44. this.drawChart();
  45. },
  46. methods: {
  47. drawChart() {
  48. // 基于准备好的dom,初始化echarts实例
  49. let myChart = this.$echarts.init(
  50. document.getElementById("chart_a")
  51. );
  52. // 指定图表的配置项和数据
  53. let option = {
  54. title: {
  55. text: "温度",
  56. // subtext: this.subtext,
  57. left: 100
  58. },
  59. tooltip: {
  60. trigger: "axis",
  61. formatter: function(params) {
  62. let str = `${params[0].axisValue}`;
  63. params.forEach(item => {
  64. str += `<br /><span style='width:10px;height:10px;background-color:#f00;border-radius:50%;display: inline-block;margin-right:3px;'></span>${item.seriesName}:${item.value} ℃`;
  65. });
  66. return str;
  67. }
  68. },
  69. toolbox: {
  70. show: true,
  71. feature: {
  72. dataZoom: {
  73. yAxisIndex: "none"
  74. },
  75. dataView: { readOnly: false },
  76. magicType: { type: ["line", "bar"] },
  77. restore: {},
  78. saveAsImage: {}
  79. }
  80. },
  81. color: {
  82. type: "linear",
  83. x: 0,
  84. y: 0,
  85. x2: 0,
  86. y2: 1,
  87. colorStops: [
  88. {
  89. offset: 0,
  90. color: "red" // 0% 处的颜色
  91. },
  92. {
  93. offset: 1,
  94. color: "blue" // 100% 处的颜色
  95. }
  96. ]
  97. },
  98. xAxis: {
  99. type: "category",
  100. boundaryGap: false,
  101. data: this.time
  102. },
  103. yAxis: {
  104. type: "value",
  105. axisLabel: {
  106. formatter: "{value} °C"
  107. },
  108. scale: true
  109. },
  110. series: this.series
  111. };
  112. // 使用刚指定的配置项和数据显示图表。
  113. myChart.setOption(option);
  114. }
  115. }
  116. };
  117. </script>
  118. <style lang="scss" scoped>
  119. </style>