trendChart.vue 6.7 KB

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