Index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <template>
  2. <el-container class="portrait">
  3. <el-header style="height: auto; padding-top: 1em">
  4. <el-card>
  5. <el-form ref="form" size="small" :inline="true" label-width="120px">
  6. <el-form-item label="选择应用">
  7. <el-select v-model="appSelect" placeholder="请选择">
  8. <el-option
  9. v-for="item in appList"
  10. :key="item.appid"
  11. :label="item.name"
  12. :value="item.appid"
  13. />
  14. </el-select>
  15. </el-form-item>
  16. <el-form-item style="float: right">
  17. <el-button type="primary" @click="getData"> 查询 </el-button>
  18. </el-form-item>
  19. </el-form>
  20. </el-card>
  21. </el-header>
  22. <el-main>
  23. <el-row :gutter="20">
  24. <el-col :span="12">
  25. <el-card>
  26. <template #header>
  27. <span>用户性别分布</span>
  28. </template>
  29. <div ref="sex"></div>
  30. </el-card>
  31. </el-col>
  32. <el-col :span="12">
  33. <el-card>
  34. <template #header>
  35. <span>用户年龄分布</span>
  36. </template>
  37. <div ref="age"></div>
  38. </el-card>
  39. </el-col>
  40. </el-row>
  41. <br />
  42. <el-card>
  43. <template #header>
  44. <span>用户标签分布</span>
  45. </template>
  46. <div ref="tag"></div>
  47. </el-card>
  48. <br />
  49. <el-card>
  50. <template #header>
  51. <span>陕西省内用户分布</span>
  52. </template>
  53. <el-row :gutter="20">
  54. <el-col :span="12">
  55. <div ref="city"></div>
  56. </el-col>
  57. <el-col :span="12">
  58. <el-table :data="tableProData" style="width: 100%">
  59. <el-table-column prop="category" label="地名" />
  60. <el-table-column center prop="ct" label="访问人数" />
  61. </el-table>
  62. </el-col>
  63. </el-row>
  64. </el-card>
  65. <br />
  66. <el-row :gutter="20">
  67. <el-col :span="12">
  68. <el-card>
  69. <template #header>
  70. <span>用户机型分布</span>
  71. </template>
  72. <div ref="phone"></div>
  73. </el-card>
  74. </el-col>
  75. <el-col :span="12">
  76. <el-card>
  77. <template #header>
  78. <span>APP栏目用户分布</span>
  79. </template>
  80. <div ref="category"></div>
  81. </el-card>
  82. </el-col>
  83. </el-row>
  84. <br />
  85. <el-row :gutter="20">
  86. <el-col :span="12">
  87. <el-card>
  88. <template #header>
  89. <span>起点号用户访问排行</span>
  90. </template>
  91. <div ref="publisher"></div>
  92. </el-card>
  93. </el-col>
  94. <el-col :span="12">
  95. <el-card>
  96. <template #header>
  97. <span>重点节目用户访问排行</span>
  98. </template>
  99. <div ref="program"></div>
  100. </el-card>
  101. </el-col>
  102. </el-row>
  103. </el-main>
  104. </el-container>
  105. </template>
  106. <script setup>
  107. import {
  108. getAppList,
  109. getSexData,
  110. getAgeData,
  111. getCityData,
  112. getTagData,
  113. getPhoneData,
  114. getCategoryData,
  115. getPublisherData,
  116. getProgramData
  117. } from '@/api/portrait.js';
  118. import { ref, onMounted } from 'vue';
  119. import { createChart, numform, chart } from './tool';
  120. import config from './config';
  121. const appList = ref([]);
  122. const tableProData = ref([]);
  123. const appSelect = ref('');
  124. const sex = ref(null);
  125. const age = ref(null);
  126. const tag = ref(null);
  127. const city = ref(null);
  128. const phone = ref(null);
  129. const category = ref(null);
  130. const publisher = ref(null);
  131. const program = ref(null);
  132. let sexChart = undefined;
  133. let ageChart = undefined;
  134. let tagChart = undefined;
  135. let cityChart = undefined;
  136. let phoneChart = undefined;
  137. let categoryChart = undefined;
  138. let publisherChart = undefined;
  139. let programChart = undefined;
  140. const createSex = (list = []) => {
  141. if (!sex.value || !list || list.length == 0) return;
  142. const p = {
  143. ...config.pieOption
  144. };
  145. p.series[0].data = list.map(v => {
  146. return {
  147. name: v.category,
  148. value: v.ct,
  149. selected: true
  150. };
  151. });
  152. sexChart = createChart(sex.value, sex.value.offsetWidth / 2, p, sexChart);
  153. };
  154. const createAge = (list = []) => {
  155. if (!age.value || !list || list.length == 0) return;
  156. const p = {
  157. ...config.pieOption
  158. };
  159. p.series[0].radius = ['40%', '70%'];
  160. p.series[0].itemStyle = {
  161. borderRadius: 5,
  162. borderColor: '#fff',
  163. borderWidth: 1
  164. };
  165. p.series[0].data = list.map(v => {
  166. return {
  167. name: v.category,
  168. value: v.ct,
  169. selected: true
  170. };
  171. });
  172. ageChart = createChart(age.value, age.value.offsetWidth / 2, p, ageChart);
  173. };
  174. const createTag = (list = []) => {
  175. if (!tag.value || !list || list.length == 0) return;
  176. let category = list.map(v => {
  177. return {
  178. name: v.category,
  179. value: v.ct
  180. };
  181. });
  182. let yName = []; // y轴名称
  183. let bgData = []; // 最大值用作背景显示的数据
  184. let maxValue = category[0].value; //最大值
  185. category = category.reverse();
  186. category.forEach(element => {
  187. yName.push(element.name);
  188. bgData.push({
  189. name: element.name,
  190. value: maxValue
  191. });
  192. });
  193. const p = {
  194. ...config.tagOption
  195. };
  196. p.xAxis.max = maxValue;
  197. p.yAxis[0].data = yName;
  198. p.yAxis[1].data = Array.from(
  199. { length: yName.length },
  200. (_, i) => i + 1
  201. ).reverse();
  202. p.yAxis[2].data = category;
  203. p.series[0].data = bgData;
  204. p.series[1].data = category;
  205. p.series[2].data = bgData;
  206. p.series[2].symbolBoundingData = maxValue;
  207. tagChart = createChart(tag.value, tag.value.offsetWidth / 2, p, tagChart);
  208. };
  209. const createCity = (list = []) => {
  210. if (!city.value || !list || list.length == 0) return;
  211. tableProData.value = list;
  212. let max = -Infinity,
  213. min = Infinity,
  214. values = [];
  215. for (let i = 0; i < list.length; i++) {
  216. const v = list[i];
  217. if (v.ct > max) max = v.ct;
  218. if (v.ct < min) min = v.ct;
  219. values.push({
  220. name: v.category,
  221. value: v.ct
  222. // distribution: ((v.ct / t) * 100).toFixed(2) - 0 + '%'
  223. });
  224. }
  225. const p = {
  226. ...config.cityOption
  227. };
  228. p.visualMap.min = min;
  229. p.visualMap.max = max;
  230. p.tooltip.formatter = data => {
  231. if (isNaN(data.value)) return '';
  232. return `${data.name}<br/>${numform(data.value)}`;
  233. };
  234. p.visualMap.text = [max, min];
  235. p.series[0].data = values;
  236. cityChart = createChart(city.value, city.value.offsetWidth, p, cityChart);
  237. };
  238. const createPhone = (list = []) => {
  239. if (!phone.value || !list || list.length == 0) return;
  240. list = list.reverse();
  241. const p = {
  242. ...config.phoneOption
  243. };
  244. p.series[0].data = list.map(v => {
  245. return {
  246. value: v.ct,
  247. name: v.category
  248. };
  249. });
  250. phoneChart = createChart(
  251. phone.value,
  252. phone.value.offsetWidth / 2,
  253. p,
  254. phoneChart
  255. );
  256. };
  257. const createCategory = (list = []) => {
  258. if (!category.value || !list || list.length == 0) return;
  259. list = list.reverse();
  260. const p = {
  261. ...config.pieOption
  262. };
  263. p.series[0].radius = ['40%', '70%'];
  264. p.series[0].itemStyle = {
  265. borderRadius: 5,
  266. borderColor: '#fff',
  267. borderWidth: 1
  268. };
  269. p.series[0].data = list.map(v => {
  270. return {
  271. name: v.category,
  272. value: v.ct,
  273. selected: true
  274. };
  275. });
  276. categoryChart = createChart(
  277. category.value,
  278. category.value.offsetWidth / 2,
  279. p,
  280. categoryChart
  281. );
  282. };
  283. const createPublisher = (list = []) => {
  284. if (!publisher.value || !list || list.length == 0) return;
  285. const keys = [],
  286. values = [];
  287. let max = -Infinity;
  288. for (let i = 0; i < list.length; i++) {
  289. const v = list[i];
  290. keys.push(v.category);
  291. values.push(v.ct);
  292. if (max < v.ct) max = v.ct;
  293. }
  294. const p = {
  295. ...config.publisherOption
  296. };
  297. p.xAxis[0].data = keys;
  298. p.series[0].data = values;
  299. p.series[0].itemStyle = {
  300. normal: {
  301. borderRadius: [5, 5, 0, 0],
  302. color: new chart.graphic.LinearGradient(0, 0, 1, 0, [
  303. {
  304. offset: 0,
  305. color: '#8ba6d6'
  306. },
  307. {
  308. offset: 1,
  309. color: '#a085ee'
  310. }
  311. ])
  312. }
  313. };
  314. publisherChart = createChart(
  315. publisher.value,
  316. publisher.value.offsetWidth,
  317. p,
  318. publisherChart
  319. );
  320. };
  321. const createProgram = (list = []) => {
  322. if (!program.value || !list || list.length == 0) return;
  323. const keys = [],
  324. values = [];
  325. let max = -Infinity;
  326. list = list.reverse();
  327. for (let i = 0; i < list.length; i++) {
  328. const v = list[i];
  329. keys.push(v.category);
  330. values.push(v.ct);
  331. if (max < v.ct) max = v.ct;
  332. }
  333. const p = {
  334. ...JSON.parse(JSON.stringify(config.publisherOption))
  335. };
  336. p.xAxis[0].data = keys;
  337. p.series[0].data = values;
  338. const xAxis = JSON.parse(JSON.stringify(p.xAxis));
  339. const yAxis = JSON.parse(JSON.stringify(p.yAxis));
  340. p.yAxis = xAxis;
  341. p.xAxis = yAxis;
  342. p.grid = {
  343. top: '5%',
  344. left: 5,
  345. right: 5,
  346. bottom: '6%',
  347. containLabel: true
  348. };
  349. p.series[0].label.offset = [50, 0];
  350. p.series[0].label.position = 'right';
  351. p.series[0].label.align = 'right';
  352. p.series[0].itemStyle = {
  353. normal: {
  354. borderRadius: [0, 5, 5, 0],
  355. color: new chart.graphic.LinearGradient(0, 0, 1, 0, [
  356. {
  357. offset: 0,
  358. color: '#8ba6d6'
  359. },
  360. {
  361. offset: 1,
  362. color: '#a085ee'
  363. }
  364. ])
  365. }
  366. };
  367. p.series[0].label.formatter = data => {
  368. if (isNaN(data.value)) return '';
  369. return numform(data.value);
  370. };
  371. programChart = createChart(
  372. program.value,
  373. publisher.value.offsetWidth,
  374. p,
  375. programChart
  376. );
  377. };
  378. const getData = () => {
  379. Promise.all([
  380. getSexData({ appid: appSelect.value }),
  381. getAgeData({ appid: appSelect.value }),
  382. getCityData({ appid: appSelect.value }),
  383. getTagData({ appid: appSelect.value }),
  384. getPhoneData({ appid: appSelect.value }),
  385. getCategoryData({ appid: appSelect.value }),
  386. getPublisherData({ appid: appSelect.value }),
  387. getProgramData({ appid: appSelect.value })
  388. ]).then(list => {
  389. createSex(list[0]);
  390. createAge(list[1]);
  391. createCity(list[2]);
  392. createTag(list[3]);
  393. createPhone(list[4]);
  394. createCategory(list[5]);
  395. createPublisher(list[6]);
  396. createProgram(list[7]);
  397. });
  398. };
  399. onMounted(() => {
  400. getAppList().then(res => {
  401. appSelect.value = (res || [])[0].appid;
  402. appList.value = res || [];
  403. getData();
  404. });
  405. });
  406. </script>