trendChart.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <view class="uni-stat--x p-m">
  3. <view class="uni-stat-card-header">趋势图</view>
  4. <!-- 时间纬度 -->
  5. <view class="flex">
  6. <uni-stat-tabs type="box" :current="dateTabs.index" :tabs="dateTabs.list" @change="dateTabsChange" />
  7. <uni-datetime-picker type="datetimerange" v-model="dateTabs.time" :end="Date.now()" return-type="timestamp" :clear-icon="false" class="uni-stat-datetime-picker" @change="datePickerChange" />
  8. </view>
  9. <uni-stat-tabs type="box" :current="statTabs.index" :tabs="statTabs.list" @change="statTabsChange" />
  10. <view class="uni-charts-box">
  11. <qiun-data-charts type="area" :chartData="chartData" :opts="opts" :errorMessage="errorMessage"/>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import {
  17. formatterData, // 格式化字段数据
  18. fillTrendChartData,
  19. stringifyQuery, // 对象转JQL查询字符串
  20. parseDateTime, // 格式化时间
  21. debounce, // 防抖函数
  22. stringifyField,
  23. stringifyGroupField,
  24. getTimeOfSomeDayAgo,
  25. } from '@/js_sdk/uni-stat/util.js'
  26. import timeUtil from "@/js_sdk/uni-stat/timeUtil.js"
  27. import {
  28. fieldsGroupMap,
  29. } from '../fieldsMap.js'
  30. let statTabsList = [];
  31. fieldsGroupMap.forEach(item => {
  32. const _id = item.group
  33. const name = item.title
  34. if (_id && name) {
  35. statTabsList.push({
  36. _id,
  37. name,
  38. list: item.list
  39. })
  40. }
  41. });
  42. export default {
  43. props: {
  44. query: {
  45. type: [Object],
  46. default: function(){
  47. return {}
  48. }
  49. },
  50. },
  51. data() {
  52. return {
  53. tableName: 'uni-stat-pay-result',
  54. chartData: {},
  55. errorMessage: "",
  56. opts: {
  57. color: ["#1890FF","#91CB74","#FAC858","#EE6666","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
  58. padding: [15,15,0,15],
  59. legend: {},
  60. enableScroll: true,
  61. dataLabel: false, // 是否显示数据标签
  62. xAxis: {
  63. disableGrid: true,
  64. itemCount: 24,
  65. fontSize:12,
  66. boundaryGap:"center", // center justify
  67. },
  68. yAxis: {
  69. gridType: "dash",
  70. dashLength: 2,
  71. data:[
  72. {
  73. tofix:2
  74. }
  75. ]
  76. },
  77. extra: {
  78. area: {
  79. type: "straight",
  80. opacity: 0.2,
  81. addLine: true,
  82. width: 2,
  83. gradient: false,
  84. },
  85. },
  86. legend:{
  87. position:"bottom"
  88. }
  89. },
  90. // 时间选项
  91. dateTabs: {
  92. time: [],
  93. timeStr:"",
  94. index: 2, // 默认最近7天
  95. list: [
  96. { _id: 1, name: '昨天', dimension:"hour" },
  97. { _id: 0, name: '今天', dimension:"hour" },
  98. { _id: 7, name: '最近七天', dimension:"day" },
  99. { _id: 30, name: '最近30天', dimension:"day" },
  100. { _id: 90, name: '最近90天', dimension:"day" },
  101. { _id: 372, name: '月纬度', dimension:"month" },
  102. { _id: 1116, name: '季纬度', dimension:"quarter" },
  103. { _id: 4392, name: '年纬度', dimension:"year" },
  104. ]
  105. },
  106. statTabs:{
  107. index: 0,
  108. list: statTabsList
  109. },
  110. queryMode: 0
  111. }
  112. },
  113. created() {
  114. this.getCloudDataDebounce = debounce(() => {
  115. this.getCloudData();
  116. }, 300);
  117. this.getCloudDataDebounce();
  118. },
  119. methods: {
  120. // 获取云端数据
  121. getCloudData(obj={}) {
  122. let query = this.query;
  123. if (!query.appid){
  124. this.errorMessage = "请先选择应用";
  125. return; // 如果appid为空,则不进行查询
  126. }
  127. this.errorMessage = "";
  128. let insideQuery = this.getWhere();
  129. let where = {
  130. ...query,
  131. ...insideQuery
  132. };
  133. let fieldsMap = this.statTabs.list[this.statTabs.index].list;
  134. where = stringifyQuery(where, true, ['uni_platform']);
  135. //console.log('trendChart-where: ', where);
  136. const db = uniCloud.database();
  137. db.collection(this.tableName)
  138. .where(where)
  139. .field(`${stringifyField(fieldsMap)}, start_time`)
  140. .groupBy(`start_time`)
  141. .groupField(stringifyGroupField(fieldsMap))
  142. .orderBy('start_time', 'asc')
  143. .limit(100)
  144. .get({
  145. getCount: true
  146. })
  147. .then(res => {
  148. let {
  149. count,
  150. data
  151. } = res.result;
  152. data = fillTrendChartData(data, insideQuery, fieldsMap); // 补全数据
  153. // 数据格式化
  154. data = formatterData({
  155. fieldsMap,
  156. data,
  157. formatter: false
  158. });
  159. //console.log('trendChartData: ', data)
  160. this.setChartData(data, fieldsMap, insideQuery);
  161. }).catch((err) => {
  162. console.error(err);
  163. }).finally(() => {})
  164. },
  165. // 设置图表数据
  166. setChartData(data, fieldsMap, insideQuery){
  167. let chartData = {
  168. categories: [],
  169. series: []
  170. };
  171. fieldsMap.map((item, index) => {
  172. if (item.trendChart) {
  173. chartData.series.push({
  174. name: item.title,
  175. data:[]
  176. });
  177. }
  178. });
  179. for (const item of data) {
  180. const x = this.formatDate(item.start_time, insideQuery.dimension);
  181. chartData.categories.push(x);
  182. fieldsMap.map((item2, index) => {
  183. if (item2.trendChart) {
  184. let y = Number(item[item2.field]);
  185. chartData.series[index].data.push(y);
  186. }
  187. });
  188. }
  189. this.chartData = chartData;
  190. },
  191. formatDate(date, type){
  192. let d = new Date(date);
  193. let year = d.getFullYear();
  194. let month = d.getMonth() + 1;
  195. let day = d.getDate();
  196. let hour = d.getHours();
  197. let quarter = Math.floor((d.getMonth() + 3) / 3); //季度
  198. if (month < 10) month = "0"+ month;
  199. if (day < 10) day = "0"+ day;
  200. if (type === 'hour') {
  201. return `${hour}时`;
  202. } else if (type === 'month') {
  203. return `${year}-${month}`;
  204. } else if (type === 'quarter') {
  205. return `${year}/Q${quarter}`;
  206. } else if (type === 'year') {
  207. return `${year}`;
  208. } else {
  209. return parseDateTime(d);
  210. }
  211. },
  212. // 监听 - 日期选择更改
  213. datePickerChange(time) {
  214. this.dateTabs.time = time;
  215. //this.dateTabs.index = null;
  216. this.queryMode = 1;
  217. this.getCloudData();
  218. },
  219. // 监听 - 日期选择更改
  220. dateTabsChange(id, index) {
  221. this.dateTabs.index = index;
  222. this.queryMode = 0;
  223. this.getCloudData();
  224. },
  225. // 监听 - 统计字段纬度更改
  226. statTabsChange(id, index, name) {
  227. this.statTabs.index = index;
  228. this.getCloudData({
  229. field: id,
  230. name
  231. });
  232. },
  233. // 获取查询条件
  234. getWhere() {
  235. const day = 24 * 60 * 60 * 1000;
  236. let start_time = [];
  237. let item = this.dateTabs.list[this.dateTabs.index] || {};
  238. if (typeof item._id === "number" && this.queryMode === 0) {
  239. let start = getTimeOfSomeDayAgo(item._id);
  240. let end = timeUtil.getOffsetStartAndEnd("day", 0).endTime; // end默认=今天的截止时间
  241. if (item._id == 1) {
  242. // 如果是查昨天,则特殊处理下,end=昨天的截止时间
  243. end = timeUtil.getOffsetStartAndEnd("day", 0, start).endTime;
  244. }
  245. start_time = [start, end];
  246. } else if (this.dateTabs.time){
  247. start_time = this.dateTabs.time; // 当前选择的时间
  248. }
  249. let dimension = item.dimension || "day"; // 获取时间纬度
  250. this.dateTabs.timeStr = `${timeUtil.timeFormat(start_time[0])} ~ ${timeUtil.timeFormat(start_time[1])}`;
  251. this.dateTabs.time = start_time;
  252. return {
  253. dimension, // 时间纬度
  254. start_time, // 时间范围
  255. }
  256. }
  257. },
  258. watch: {
  259. query: {
  260. deep: true,
  261. handler(val) {
  262. this.getCloudDataDebounce();
  263. }
  264. }
  265. },
  266. computed: {
  267. }
  268. }
  269. </script>
  270. <style lang="scss" scoped>
  271. .justify-center{
  272. justify-content: center;
  273. }
  274. </style>