Datavis.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <div ref="Datavis" :style="{ width: '100%', height: '360px' }"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts';
  6. import Papa from 'papaparse';
  7. import { listManagement, getManagement, delManagement, addManagement, updateManagement,runairtopre ,getfilecontent} from "@/api/aircraft/management";
  8. import axios from "axios";
  9. export default {
  10. props: {
  11. aircraftdataview: {
  12. type: Object,
  13. },
  14. filepath:null,
  15. dataOption:[],
  16. openDatavis: {
  17. type: Boolean,
  18. },
  19. },
  20. data() {
  21. return {
  22. display: false,
  23. Datavis: null,
  24. chartData: [], // 定义空数组用于保存CSV数据
  25. };
  26. },
  27. computed: {
  28. selectedIds() {
  29. return this.dataOption.map((item) => item.id);
  30. },
  31. },
  32. methods: {
  33. createDatavis() {
  34. const xAxisData = Array.from({ length: this.chartData.length - 1 }, (_, i) => i + 1); // 生成从1到chartData行数-1的数组作为横坐标数据
  35. const option = {
  36. animationDuration: 3000,
  37. legend: {
  38. textStyle: {
  39. color: 'white',
  40. },
  41. },
  42. yAxis: {
  43. axisLabel: {
  44. color: 'white',
  45. },
  46. nameTextStyle: {
  47. color: 'white',
  48. },
  49. },
  50. series: [],
  51. xAxis: {
  52. data: xAxisData,
  53. },
  54. };
  55. const usedColors = []; // 用于存储已使用的颜色
  56. // 将每一列数据都添加到折线图中
  57. if(this.chartData.length >0 ){
  58. for (let i = 0; i < this.chartData[0].length; i++) {
  59. const columnId = i; // 列id就是i
  60. if (!this.selectedIds.includes(columnId)) { // 如果当前列的id不在selectedIds中,则跳过该列
  61. continue;
  62. }
  63. const seriesData = this.chartData.slice(1).map((row) => parseFloat(row[columnId])); // 获取y轴数据,从第二行开始的第i列的数据
  64. let color;
  65. do {
  66. color = this.getRandomColor(); // 随机生成颜色
  67. } while (usedColors.includes(color)); // 如果生成的颜色已经被使用过,则重新生成
  68. usedColors.push(color); // 将颜色添加到已使用的颜色数组中
  69. option.series.push({
  70. name: this.dataOption.find((item) => item.id === columnId).label, // 使用对应列的名称作为折线的名称
  71. data: seriesData,
  72. type: 'line',
  73. itemStyle: {
  74. color: color, // 随机生成折线的颜色
  75. },
  76. animationEasing: 'linear',
  77. animationDelay: (idx) => idx * 8,
  78. });
  79. }
  80. }
  81. this.Datavis.setOption(option);
  82. },
  83. getRandomColor() {
  84. const letters = '0123456789ABCDEF';
  85. let color = '#';
  86. for (let i = 0; i < 6; i++) {
  87. color += letters[Math.floor(Math.random() * 16)];
  88. }
  89. return color;
  90. },
  91. async getresponse(){
  92. const response = await axios.get(process.env.VUE_APP_BASE_API + this.filepath, { responseType: 'text' });
  93. const csvData = response.data;
  94. const rows = csvData.split('\n');
  95. this.chartData = rows.map(row => row.split(','));
  96. this.createDatavis()
  97. },
  98. },
  99. mounted() {
  100. this.Datavis = echarts.init(this.$refs.Datavis);
  101. this.getresponse()
  102. },
  103. watch: {
  104. dataOption(newValue) {
  105. this.Datavis.dispose();
  106. this.Datavis = echarts.init(this.$refs.Datavis);
  107. this.createDatavis();
  108. }
  109. },
  110. beforeUnmount() {
  111. if (this.Datavis) {
  112. this.Datavis.dispose();
  113. this.Datavis = null;
  114. }
  115. },
  116. };
  117. </script>