LineChart.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <div class="project-card">
  3. <div class="select-device title">
  4. <div class="flex">
  5. <img src="/@/assets/project/project-icon4.svg" v-if="index" class="icon" />
  6. <img src="/@/assets/project/project-icon3.svg" v-else class="icon" />
  7. {{ $t('message.projects.screen.lineChart.title') }}
  8. </div>
  9. <div class="flex">
  10. <el-date-picker
  11. class="date-picker-wrap"
  12. v-model="params.timeRange"
  13. :clearable="false"
  14. type="datetimerange"
  15. :range-separator="$t('message.projects.screen.lineChart.date.rangeSeparator')"
  16. :start-placeholder="$t('message.projects.screen.lineChart.date.startPlaceholder')"
  17. :end-placeholder="$t('message.projects.screen.lineChart.date.endPlaceholder')"
  18. format="MM-DD HH:mm"
  19. value-format="YYYY-MM-DD HH:mm:00"
  20. date-format="YYYY/MM/DD"
  21. time-format="hh:mm"
  22. @change="getChartData"
  23. style="width: 230px; margin-right: 10px"
  24. />
  25. <el-select style="width: 150px" v-model="params.properties" multiple collapse-tags @change="getChartData" :placeholder="$t('message.projects.screen.lineChart.selects.properties')">
  26. <el-option v-for="row in options" :key="row.key" :label="row.name" :value="row.key"></el-option>
  27. </el-select>
  28. </div>
  29. </div>
  30. <section class="line">
  31. <Chart height="22.5vh" ref="chartRef"></Chart>
  32. </section>
  33. </div>
  34. </template>
  35. <script setup lang="ts">
  36. import { onMounted, reactive, ref, watch } from "vue";
  37. import { getLineMultiOption } from "/@/components/chart/options";
  38. import Chart from "/@/components/chart/index.vue";
  39. import api from "/@/api/projects";
  40. import { dayjs } from "element-plus";
  41. import { useStore } from "/@/store/index";
  42. const store = useStore();
  43. const props = defineProps({
  44. index: {
  45. type: [String, Number],
  46. dafault: 0,
  47. },
  48. deviceKey: {
  49. type: String,
  50. dafault: "",
  51. },
  52. });
  53. let deviceCode = "";
  54. const chartRef = ref();
  55. const theme = ref("light");
  56. const options = ref<any[]>([]);
  57. const params = reactive({
  58. timeRange: [dayjs().subtract(1, "hour").format("YYYY-MM-DD HH:mm:00"), dayjs().format("YYYY-MM-DD HH:mm:00")],
  59. properties: [] as string[],
  60. });
  61. watch(
  62. () => store.state.themeConfig.themeConfig.isIsDark,
  63. (isIsDark) => {
  64. theme.value = isIsDark ? "dark" : "light";
  65. if (params.properties.length) getChartData();
  66. },
  67. {
  68. immediate: true,
  69. }
  70. );
  71. watch(() => props.deviceKey!, getData);
  72. onMounted(() => {
  73. chartRef.value?.hideLoading();
  74. });
  75. function getData(code: string) {
  76. params.properties = [];
  77. options.value = [];
  78. deviceCode = code;
  79. if (!code) return chartRef.value.draw();
  80. chartRef.value?.loading();
  81. api.screen.propertyList(code).then((res: any) => {
  82. const list = res?.Data || [];
  83. options.value = list;
  84. if (!list.length) return chartRef.value.draw();
  85. const i = Number(props.index);
  86. if (list[i]) {
  87. params.properties = [list[i].key];
  88. } else {
  89. params.properties = [list[0].key];
  90. }
  91. getChartData();
  92. });
  93. }
  94. function getChartData() {
  95. chartRef.value.loading();
  96. if (!params.properties.length) return chartRef.value.draw();
  97. api.screen
  98. .chartData({
  99. deviceKey: deviceCode,
  100. dateRange: params.timeRange,
  101. properties: params.properties,
  102. })
  103. .then((res: any) => {
  104. if (!res) return chartRef.value.draw();
  105. const values = Object.values(res) as any[];
  106. const legend = Object.keys(res).map((key) => options.value.find((item) => item.key === key)?.name);
  107. chartRef.value?.draw(
  108. getLineMultiOption({
  109. datas: values.map((arr: any) => (arr || []).map((item: any) => item.dataValue)),
  110. legend,
  111. xAxis: (values[0] || []).map((item: any) => item.dataTime),
  112. theme: theme.value,
  113. })
  114. );
  115. });
  116. }
  117. </script>
  118. <style lang="scss" scoped>
  119. .select-device {
  120. color: var(--primary-color);
  121. display: flex;
  122. align-items: center;
  123. white-space: nowrap;
  124. justify-content: space-between;
  125. }
  126. </style>