chart.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const moment = require("moment");
  2. // mock data
  3. const visitData = [];
  4. const beginDay = new Date().getTime();
  5. const fakeY = [7, 5, 4, 2, 4, 7, 5, 6, 5, 9, 6, 3, 1, 5, 3, 6, 5];
  6. for (let i = 0; i < fakeY.length; i += 1) {
  7. visitData.push({
  8. x: moment(new Date(beginDay + 1000 * 60 * 60 * 24 * i)).format(
  9. "YYYY-MM-DD"
  10. ),
  11. y: fakeY[i]
  12. });
  13. }
  14. const visitData2 = [];
  15. const fakeY2 = [1, 6, 4, 8, 3, 7, 2];
  16. for (let i = 0; i < fakeY2.length; i += 1) {
  17. visitData2.push({
  18. x: moment(new Date(beginDay + 1000 * 60 * 60 * 24 * i)).format(
  19. "YYYY-MM-DD"
  20. ),
  21. y: fakeY2[i]
  22. });
  23. }
  24. const salesData = [];
  25. for (let i = 0; i < 12; i += 1) {
  26. salesData.push({
  27. x: `${i + 1}月`,
  28. y: Math.floor(Math.random() * 1000) + 200
  29. });
  30. }
  31. const searchData = [];
  32. for (let i = 0; i < 50; i += 1) {
  33. searchData.push({
  34. index: i + 1,
  35. keyword: `搜索关键词-${i}`,
  36. count: Math.floor(Math.random() * 1000),
  37. range: Math.floor(Math.random() * 100),
  38. status: Math.floor((Math.random() * 10) % 2)
  39. });
  40. }
  41. const salesTypeData = [
  42. {
  43. x: "家用电器",
  44. y: 4544
  45. },
  46. {
  47. x: "食用酒水",
  48. y: 3321
  49. },
  50. {
  51. x: "个护健康",
  52. y: 3113
  53. },
  54. {
  55. x: "服饰箱包",
  56. y: 2341
  57. },
  58. {
  59. x: "母婴产品",
  60. y: 1231
  61. },
  62. {
  63. x: "其他",
  64. y: 1231
  65. }
  66. ];
  67. const salesTypeDataOnline = [
  68. {
  69. x: "家用电器",
  70. y: 244
  71. },
  72. {
  73. x: "食用酒水",
  74. y: 321
  75. },
  76. {
  77. x: "个护健康",
  78. y: 311
  79. },
  80. {
  81. x: "服饰箱包",
  82. y: 41
  83. },
  84. {
  85. x: "母婴产品",
  86. y: 121
  87. },
  88. {
  89. x: "其他",
  90. y: 111
  91. }
  92. ];
  93. const salesTypeDataOffline = [
  94. {
  95. x: "家用电器",
  96. y: 99
  97. },
  98. {
  99. x: "食用酒水",
  100. y: 188
  101. },
  102. {
  103. x: "个护健康",
  104. y: 344
  105. },
  106. {
  107. x: "服饰箱包",
  108. y: 255
  109. },
  110. {
  111. x: "其他",
  112. y: 65
  113. }
  114. ];
  115. const offlineData = [];
  116. for (let i = 0; i < 10; i += 1) {
  117. offlineData.push({
  118. name: `Stores ${i}`,
  119. cvr: Math.ceil(Math.random() * 9) / 10
  120. });
  121. }
  122. const offlineChartData = [];
  123. for (let i = 0; i < 20; i += 1) {
  124. offlineChartData.push({
  125. x: new Date().getTime() + 1000 * 60 * 30 * i,
  126. y1: Math.floor(Math.random() * 100) + 10,
  127. y2: Math.floor(Math.random() * 100) + 10
  128. });
  129. }
  130. const radarOriginData = [
  131. {
  132. name: "个人",
  133. ref: 10,
  134. koubei: 8,
  135. output: 4,
  136. contribute: 5,
  137. hot: 7
  138. },
  139. {
  140. name: "团队",
  141. ref: 3,
  142. koubei: 9,
  143. output: 6,
  144. contribute: 3,
  145. hot: 1
  146. },
  147. {
  148. name: "部门",
  149. ref: 4,
  150. koubei: 1,
  151. output: 6,
  152. contribute: 5,
  153. hot: 7
  154. }
  155. ];
  156. const radarData = [];
  157. const radarTitleMap = {
  158. ref: "引用",
  159. koubei: "口碑",
  160. output: "产量",
  161. contribute: "贡献",
  162. hot: "热度"
  163. };
  164. radarOriginData.forEach(item => {
  165. Object.keys(item).forEach(key => {
  166. if (key !== "name") {
  167. radarData.push({
  168. name: item.name,
  169. label: radarTitleMap[key],
  170. value: item[key]
  171. });
  172. }
  173. });
  174. });
  175. const getFakeChartData = {
  176. visitData,
  177. visitData2,
  178. salesData,
  179. searchData,
  180. offlineData,
  181. offlineChartData,
  182. salesTypeData,
  183. salesTypeDataOnline,
  184. salesTypeDataOffline,
  185. radarData
  186. };
  187. module.exports = {
  188. "GET /api/fake_chart_data": getFakeChartData,
  189. [`GET /api/dashboard/chart`](req, res) {
  190. let result = null;
  191. switch (req.method) {
  192. case "GET":
  193. result = [100, 40, 78, 10, 30, 48];
  194. break;
  195. default:
  196. result = null;
  197. }
  198. // 返回你的mock数据。比如:
  199. res.json(result);
  200. }
  201. };