scenelineEcharts.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <Card title="用户趋势分析" :loading="loading">
  3. <template #extra>
  4. <condition type="2" @change="Search" @expor="handleExport" />
  5. <!-- <div class="condition">
  6. <div class="selct" style="display: inline-block;">
  7. <span style="margin-right:15px">颗粒度</span>
  8. <Select
  9. v-model:value="value"
  10. style="width: 100px;margin-right:30px"
  11. placeholder="请选择颗粒度"
  12. :options="options"
  13. :defaultValue="1"
  14. @change="handleChange"
  15. ></Select>
  16. </div>
  17. <a-button type="primary" @click="handleExport">导出</a-button>
  18. </div> -->
  19. </template>
  20. <div ref="chartRef" :style="{ height, width }"></div>
  21. </Card>
  22. </template>
  23. <script lang="ts">
  24. import { basicProps } from './props';
  25. // import { dateUtil } from '/@/utils/dateUtil';
  26. import { Card, Select } from 'ant-design-vue';
  27. import type { SelectProps } from 'ant-design-vue';
  28. </script>
  29. <script lang="ts" setup>
  30. import { ref, Ref, watch, onMounted, reactive } from 'vue';
  31. import { userTrend } from '/@/api/statistics/index';
  32. import condition from './condition.vue';
  33. // import type { dataItemType } from './props';
  34. import { useECharts } from '/@/hooks/web/useECharts';
  35. import { exportElsxFile, } from '/@/utils/file/download';
  36. const props = defineProps({
  37. loading: Boolean,
  38. ...basicProps,
  39. });
  40. const value = ref(1);
  41. const SearchData = reactive({
  42. startTime: '',
  43. endTime: '',
  44. dataType: 0,
  45. type: 2,
  46. });
  47. const options = ref<SelectProps['options']>([
  48. {
  49. value: 1,
  50. label: '日',
  51. },
  52. {
  53. value: 2,
  54. label: '周',
  55. },
  56. {
  57. value: 3,
  58. label: '月',
  59. },
  60. ]);
  61. const viewStaticsData = ref<number[]>([]);
  62. const shareStaticsData = ref<number[]>([]);
  63. const yixStringData = ref<string[]>([]);
  64. const maxSize = ref(0);
  65. const chartRef = ref<HTMLDivElement | null>(null);
  66. const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  67. onMounted(() => {
  68. getAddUser();
  69. });
  70. function handleChange(val){
  71. console.log('handleChange',val)
  72. SearchData.value = val
  73. getAddUser()
  74. }
  75. function Search(val) {
  76. const { startTime, endTime, dataType, type } = val;
  77. console.log('params', val);
  78. SearchData.startTime = startTime;
  79. SearchData.endTime = endTime;
  80. SearchData.dataType = type;
  81. SearchData.type = dataType;
  82. getAddUser();
  83. }
  84. function handleExport(){
  85. console.log('props',props.propsData)
  86. let fields = {
  87. 'time':'日期',
  88. 'num':'数量',
  89. }
  90. let data = yixStringData.value.map((ele,index) => {
  91. return {
  92. 'time':ele,
  93. 'num':viewStaticsData.value && viewStaticsData.value[index] || 0,
  94. }
  95. })
  96. exportElsxFile(data, fields,'用户趋势')
  97. }
  98. async function getAddUser() {
  99. let xdata = [], yData=[]
  100. const data = await userTrend(SearchData);
  101. data.map(ele => {
  102. xdata.push(ele.groupKey)
  103. yData.push(ele.count)
  104. })
  105. yixStringData.value = xdata
  106. viewStaticsData.value = yData
  107. handlesetOptions()
  108. }
  109. function handlesetOptions() {
  110. setOptions({
  111. tooltip: {
  112. trigger: 'axis',
  113. axisPointer: {
  114. lineStyle: {
  115. width: 1,
  116. color: '#019680',
  117. },
  118. },
  119. },
  120. legend: {
  121. orient: 'horizontal',
  122. bottom: 0,
  123. },
  124. // grid: { left: '2%', right: '2%', top: '10%', bottom: '10%', containLabel: true },
  125. xAxis: {
  126. type: 'category',
  127. // data: [...new Array(30)].map((_item, index) => `${index + 1}日`),
  128. data: yixStringData.value,
  129. },
  130. yAxis: {
  131. type: 'value',
  132. // max: maxSize.value,
  133. splitNumber: 4,
  134. },
  135. series: [
  136. {
  137. data: viewStaticsData.value,
  138. type: 'line',
  139. itemStyle: { color: '#38a0ff' },
  140. // barMaxWidth: 80,
  141. name: '用户数量',
  142. },
  143. // {
  144. // data: shareStaticsData.value,
  145. // type: 'line',
  146. // itemStyle: { color: '#4cca73' },
  147. // // barMaxWidth: 80,
  148. // name: '用户分享数',
  149. // },
  150. ],
  151. });
  152. }
  153. // props.viewStatics,
  154. watch(
  155. // () => [props.viewStatics, props.shareStatics],
  156. // ([data1, data2]) => {
  157. () => props.loading,
  158. () => {
  159. console.log('viewStatics-data');
  160. // viewStaticsData.value = data1.reduce<number[]>(
  161. // (prev: number[], current) => prev.concat(Number(current.amount)),
  162. // [],
  163. // );
  164. // yixStringData.value = data1.reduce<string[]>(
  165. // (prev: string[], current) => prev.concat(current.date),
  166. // [],
  167. // );
  168. // shareStaticsData.value = data2.reduce<number[]>(
  169. // (prev: number[], current) => prev.concat(Number(current.amount)),
  170. // [],
  171. // );
  172. const maxNumber = Math.max(...viewStaticsData.value.concat(shareStaticsData.value));
  173. const pow = Math.pow(10, maxNumber.toString().length - 1);
  174. maxSize.value = maxNumber > 10 ? Math.floor(maxNumber / 10) * 10 + pow * 2 : 10;
  175. console.log('maxSize', maxSize.value);
  176. handlesetOptions();
  177. },
  178. {
  179. immediate: true,
  180. deep: true,
  181. },
  182. );
  183. </script>