line_chart.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <div class="line_chart" ref="line_chart">1</div>
  3. </template>
  4. <script>
  5. // import * as echarts from "echarts";
  6. import * as echarts from 'echarts/core';
  7. import { LineChart } from 'echarts/charts';
  8. import 'echarts/lib/component/dataZoom';
  9. import {
  10. TitleComponent,
  11. TooltipComponent,
  12. GridComponent,
  13. } from 'echarts/components';
  14. import { CanvasRenderer } from 'echarts/renderers';
  15. echarts.use([
  16. TitleComponent,
  17. TooltipComponent,
  18. GridComponent,
  19. LineChart,
  20. CanvasRenderer,
  21. ]);
  22. let chart = undefined;
  23. export default {
  24. name: 'line_chart',
  25. props: ['list'],
  26. watch: {},
  27. data() {
  28. return {};
  29. },
  30. filters: {},
  31. methods: {
  32. createChart() {
  33. if (!this.$refs.line_chart) return;
  34. chart = echarts.init(this.$refs.line_chart);
  35. let base = +new Date(1968, 9, 3);
  36. let oneDay = 24 * 3600 * 1000;
  37. let date = [];
  38. let data = [Math.random() * 300];
  39. for (let i = 1; i < 8; i++) {
  40. var now = new Date((base += oneDay));
  41. date.push(
  42. [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/')
  43. );
  44. data.push(Math.round((Math.random() - 0.5) * 20 + data[i - 1]));
  45. }
  46. let option = {
  47. tooltip: {
  48. trigger: 'axis',
  49. position: function (pt) {
  50. return [pt[0], '10%'];
  51. },
  52. },
  53. xAxis: {
  54. type: 'category',
  55. boundaryGap: false,
  56. data: date,
  57. },
  58. yAxis: {
  59. type: 'value',
  60. boundaryGap: [0, '100%'],
  61. },
  62. series: [
  63. {
  64. name: 'Fake Data',
  65. type: 'line',
  66. symbol: 'none',
  67. sampling: 'lttb',
  68. smooth: true,
  69. itemStyle: {
  70. color: '#1684fc',
  71. },
  72. areaStyle: {
  73. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  74. {
  75. offset: 0,
  76. color: '#1684fc',
  77. },
  78. {
  79. offset: 1,
  80. color: '#1684fc00',
  81. },
  82. ]),
  83. },
  84. data: data,
  85. },
  86. ],
  87. };
  88. option && chart.setOption(option);
  89. },
  90. },
  91. mounted() {
  92. let w = this.$refs.line_chart.offsetWidth || 0;
  93. this.$refs.line_chart.style.height = w/16*9 + 'px';
  94. this.createChart();
  95. },
  96. beforeUnmount: function () {
  97. chart.dispose();
  98. },
  99. components: {},
  100. };
  101. </script>
  102. <style>
  103. .line_chart {
  104. width: 100%;
  105. }
  106. </style>