• uni-app:实现页面效果4(echarts数据可视化)


    效果

    代码

    1. <template>
    2. <view>
    3. <view>
    4. <view class="title">概况view>
    5. <view class="line_position">
    6. <view class="line1">
    7. <view class="item">
    8. <view class="one">今日销售额(万元)view>
    9. <view class="two">
    10. <view class="left">{{line1_info.daysale_allamount}}view>
    11. <view class="right">{{line1_info.daychangeRate}}view>
    12. view>
    13. <view class="three">
    14. <image :src="getImagePath(line1_info.daychangeRate)" alt="">image>
    15. view>
    16. view>
    17. <view class="item">
    18. <view class="one">本周销售额(万元)view>
    19. <view class="two">
    20. <view class="left">{{line1_info.weeksale_allamount}}view>
    21. <view class="right">{{line1_info.weekchangeRate}}view>
    22. view>
    23. <view class="three">
    24. <image :src="getImagePath(line1_info.weekchangeRate)" alt="">image>
    25. view>
    26. view>
    27. <view class="item">
    28. <view class="one">本月销售额(万元)view>
    29. <view class="two">
    30. <view class="left">{{line1_info.monthsale_allamount}}view>
    31. <view class="right">{{line1_info.monthchangeRate}}view>
    32. view>
    33. <view class="three">
    34. <image :src="getImagePath(line1_info.monthchangeRate)" alt="">image>
    35. view>
    36. view>
    37. <view class="item">
    38. <view class="one">今年销售额(万元)view>
    39. <view class="two">
    40. <view class="left">{{line1_info.yearsale_allamount}}view>
    41. <view class="right">{{line1_info.yearchangeRate}}view>
    42. view>
    43. <view class="three">
    44. <image :src="getImagePath(line1_info.yearchangeRate)" alt="">image>
    45. view>
    46. view>
    47. view>
    48. view>
    49. view>
    50. <view>
    51. <view class="title">销售额统计view>
    52. <view class="tab_position">
    53. <view class="tab-bar">
    54. <text class="tab" :class="{ 'active': activeTab === '0' }" @click="changeTab('0')">周统计text>
    55. <text class="tab" :class="{ 'active': activeTab === '1' }" @click="changeTab('1')">月统计text>
    56. <text class="tab" :class="{ 'active': activeTab === '2' }" @click="changeTab('2')">年统计text>
    57. view>
    58. view>
    59. <view v-show="activeTab === '0'">
    60. <view class="out">
    61. <view id="myChart">view>
    62. view>
    63. view>
    64. <view v-show="activeTab === '1'">
    65. <view class="out">
    66. <view id="myChart1">view>
    67. view>
    68. view>
    69. <view v-show="activeTab === '2'">
    70. <view class="out">
    71. <view id="myChart2">view>
    72. view>
    73. view>
    74. view>
    75. view>
    76. template>
    77. <script>
    78. // import echarts from '@/static/js/echarts.js' // 引入文件
    79. import * as echarts from 'echarts';
    80. export default {
    81. data() {
    82. return {
    83. line1_info: '',
    84. down: getApp().globalData.icon + 'report/down.png',
    85. up: getApp().globalData.icon + 'report/up.png',
    86. line: getApp().globalData.icon + 'report/line.png',
    87. activeTab: '0', //默认分页面
    88. weekinfo: [], // 定义一个空数组用于存储周统计数据
    89. monthinfo: [], // 定义一个空数组用于存储月统计数据
    90. }
    91. },
    92. methods: {
    93. //切换分页面
    94. changeTab(index) {
    95. this.activeTab = index;
    96. },
    97. //图片展示
    98. getImagePath(rate) {
    99. if (rate === "无法计算") {
    100. return this.line;
    101. } else if (rate && rate.startsWith("-")) {
    102. return this.down;
    103. } else {
    104. return this.up;
    105. }
    106. },
    107. getdata() {
    108. uni.request({
    109. url: getApp().globalData.position + 'Other/select_sale_ekanbaninfo',
    110. data: {},
    111. header: {
    112. "Content-Type": "application/x-www-form-urlencoded"
    113. },
    114. method: 'POST',
    115. dataType: 'json',
    116. success: res => {
    117. //行1:年月日总额统计
    118. this.line1_info = res.data;
    119. //行2:周统计
    120. var weekinfo = res.data.week_info;
    121. this.weekinfo = weekinfo;
    122. //行2:月统计
    123. var monthinfo = res.data.month_info;
    124. this.monthinfo = monthinfo;
    125. //行2:年统计
    126. var yearinfo = res.data.year_info;
    127. this.yearinfo = yearinfo;
    128. console.log(this.yearinfo)
    129. //显示图表
    130. this.echart();
    131. },
    132. fail(res) {
    133. console.log("查询失败")
    134. }
    135. });
    136. },
    137. //绘制图标
    138. echart() {
    139. // 周统计金额
    140. const myChart = echarts.init(document.getElementById('myChart'))
    141. // 提取日期和对应的值
    142. var dates = this.weekinfo.date;
    143. var values = this.weekinfo.total_amount;
    144. var weeks = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
    145. // 进行图表的配置和数据处理
    146. myChart.setOption({
    147. //配置网格组件,用于定义图表的位置和大小
    148. grid: {
    149. top: '15%', // 增加top的值来创建间距
    150. left: '2%',
    151. right: '2%',
    152. bottom: '2%', // 增加bottom的值来创建间距
    153. containLabel: true, //自动计算并包含坐标轴标签、刻度和标题等内容在内。
    154. },
    155. color: ['#e57b7b'],
    156. tooltip: {
    157. trigger: 'item',
    158. axisPointer: {
    159. type: 'line'
    160. },
    161. formatter: function(params) {
    162. var value = values[params.dataIndex];
    163. var date = dates[params.dataIndex];
    164. var week = weeks[params.dataIndex];
    165. var marker = params.marker; // 添加marker(小圆点)
    166. return marker + ' ' + date + '
      '
      + week + ' : ' + value + '万元';
    167. }
    168. },
    169. xAxis: {
    170. // name: '日期',
    171. data: weeks,
    172. axisLine: {
    173. show: false // 隐藏纵坐标轴的横线
    174. },
    175. //隐藏小刻度短线
    176. axisTick: { // x轴刻度相关配置
    177. show: false, // 是否显示x轴刻度线
    178. },
    179. nameTextStyle: {
    180. fontSize: 12 // 设置横轴名称字体大小为12
    181. }
    182. },
    183. yAxis: {
    184. name: '金额(万元)',
    185. splitLine: {
    186. show: false // 隐藏纵坐标轴的背景横线
    187. },
    188. axisLine: {
    189. show: false // 隐藏纵坐标轴的竖线
    190. },
    191. //隐藏小刻度短线
    192. axisTick: { // x轴刻度相关配置
    193. show: false, // 是否显示x轴刻度线
    194. },
    195. axisLabel: {
    196. fontSize: 12 // 设置横轴标签字体大小为12
    197. },
    198. nameTextStyle: {
    199. fontSize: 12 // 设置横轴名称字体大小为12
    200. }
    201. },
    202. series: [{
    203. barWidth: '8',
    204. name: '销量',
    205. type: 'bar',
    206. data: values,
    207. itemStyle: {
    208. show: true,
    209. position: 'top',
    210. textStyle: {
    211. // color: '#515dc3',
    212. fontSize: 12
    213. }
    214. }
    215. }]
    216. });
    217. //月统计
    218. // 周统计金额
    219. const myChart1 = echarts.init(document.getElementById('myChart1'))
    220. // 提取日期和对应的值
    221. var dates1 = this.monthinfo.date;
    222. var values1 = this.monthinfo.total_amount;
    223. var months1 = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
    224. var months1_chinese = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
    225. // 进行图表的配置和数据处理
    226. myChart1.setOption({
    227. //配置网格组件,用于定义图表的位置和大小
    228. grid: {
    229. top: '15%', // 增加top的值来创建间距
    230. left: '2%',
    231. right: '2%',
    232. bottom: '2%', // 增加bottom的值来创建间距
    233. containLabel: true, //自动计算并包含坐标轴标签、刻度和标题等内容在内。
    234. },
    235. color: ['#5588d4'],
    236. tooltip: {
    237. trigger: 'item',
    238. axisPointer: {
    239. type: 'line'
    240. },
    241. formatter: function(params) {
    242. var value = values1[params.dataIndex];
    243. var date = dates1[params.dataIndex];
    244. var month = months1_chinese[params.dataIndex];
    245. var marker = params.marker; // 添加marker(小圆点)
    246. return marker + ' ' + date + '
      '
      + month + ' : ' + value + '万元';
    247. }
    248. },
    249. xAxis: {
    250. // name: '日期',
    251. data: months1,
    252. axisLine: {
    253. show: false // 隐藏纵坐标轴的横线
    254. },
    255. //隐藏小刻度短线
    256. axisTick: { // x轴刻度相关配置
    257. show: false, // 是否显示x轴刻度线
    258. },
    259. nameTextStyle: {
    260. fontSize: 12 // 设置横轴名称字体大小为12
    261. }
    262. },
    263. yAxis: {
    264. name: '金额(万元)',
    265. splitLine: {
    266. show: false // 隐藏纵坐标轴的背景横线
    267. },
    268. axisLine: {
    269. show: false // 隐藏纵坐标轴的竖线
    270. },
    271. //隐藏小刻度短线
    272. axisTick: { // x轴刻度相关配置
    273. show: false, // 是否显示x轴刻度线
    274. },
    275. axisLabel: {
    276. fontSize: 12 // 设置横轴标签字体大小为12
    277. },
    278. nameTextStyle: {
    279. fontSize: 12 // 设置横轴名称字体大小为12
    280. }
    281. },
    282. series: [{
    283. barWidth: '6',
    284. name: '销量',
    285. type: 'bar',
    286. data: values1,
    287. itemStyle: {
    288. show: true,
    289. position: 'top',
    290. textStyle: {
    291. // color: '#515dc3',
    292. fontSize: 12
    293. }
    294. }
    295. }]
    296. });
    297. //近五年年统计
    298. // 年统计金额
    299. const myChart2 = echarts.init(document.getElementById('myChart2'))
    300. // 提取日期和对应的值
    301. var dates2 = this.yearinfo.date;
    302. var values2 = this.yearinfo.total_amount;
    303. // 进行图表的配置和数据处理
    304. myChart2.setOption({
    305. //配置网格组件,用于定义图表的位置和大小
    306. grid: {
    307. top: '15%', // 增加top的值来创建间距
    308. left: '2%',
    309. right: '2%',
    310. bottom: '2%', // 增加bottom的值来创建间距
    311. containLabel: true, //自动计算并包含坐标轴标签、刻度和标题等内容在内。
    312. },
    313. color: ['#71aa77'],
    314. tooltip: {
    315. trigger: 'item',
    316. axisPointer: {
    317. type: 'line'
    318. },
    319. formatter: function(params) {
    320. var value = values2[params.dataIndex];
    321. var date = dates2[params.dataIndex];
    322. var marker = params.marker; // 添加marker(小圆点)
    323. return marker + ' ' + date + '年' + '
      '
      + value + '万元';
    324. }
    325. },
    326. xAxis: {
    327. // name: '日期',
    328. data: dates2,
    329. axisLine: {
    330. show: false // 隐藏纵坐标轴的横线
    331. },
    332. //隐藏小刻度短线
    333. axisTick: { // x轴刻度相关配置
    334. show: false, // 是否显示x轴刻度线
    335. },
    336. nameTextStyle: {
    337. fontSize: 12 // 设置横轴名称字体大小为12
    338. }
    339. },
    340. yAxis: {
    341. name: '金额(万元)',
    342. splitLine: {
    343. show: false // 隐藏纵坐标轴的背景横线
    344. },
    345. axisLine: {
    346. show: false // 隐藏纵坐标轴的竖线
    347. },
    348. //隐藏小刻度短线
    349. axisTick: { // x轴刻度相关配置
    350. show: false, // 是否显示x轴刻度线
    351. },
    352. axisLabel: {
    353. fontSize: 12 // 设置横轴标签字体大小为12
    354. },
    355. nameTextStyle: {
    356. fontSize: 12 // 设置横轴名称字体大小为12
    357. }
    358. },
    359. series: [{
    360. barWidth: '8',
    361. name: '销量',
    362. type: 'bar',
    363. data: values2,
    364. itemStyle: {
    365. normal: {
    366. label: {
    367. show: true,
    368. position: 'top',
    369. textStyle: {
    370. // color: '#515dc3',
    371. fontSize: 12
    372. }
    373. }
    374. }
    375. }
    376. }]
    377. })
    378. },
    379. },
    380. onLoad() {
    381. this.getdata();
    382. },
    383. }
    384. script>
    385. <style lang="scss">
    386. //第一行内容
    387. .title {
    388. // border:1px solid black;
    389. padding-left: 2%;
    390. font-size: 105%;
    391. }
    392. .line_position {
    393. overflow-x: scroll;
    394. scroll-behavior: smooth; // 设置滚动平滑过渡
    395. //隐藏滚动条
    396. &::-webkit-scrollbar {
    397. width: 0; // 隐藏默认的滚动条
    398. height: 0;
    399. }
    400. &::-webkit-scrollbar-thumb {
    401. background-color: transparent; // 隐藏滚动条拖拽的小方块
    402. }
    403. .line1 {
    404. width: 400%;
    405. display: flex;
    406. // border:1px solid red;
    407. .item:nth-child(1) {
    408. background-color: #515dc3;
    409. }
    410. .item:nth-child(2) {
    411. background-color: #ff9a3e;
    412. }
    413. .item:nth-child(3) {
    414. background-color: #38bf80;
    415. }
    416. .item:nth-child(4) {
    417. background-color: #fc5959;
    418. }
    419. .item {
    420. width: 40%;
    421. margin: 1% 2%;
    422. border-radius: 5px;
    423. padding: 1% 1% 0 1%;
    424. // border:1px solid red;
    425. color: white;
    426. .two {
    427. display: flex;
    428. margin: 5% 0;
    429. align-items: center;
    430. // border:1px solid red;
    431. .left {
    432. width: 70%;
    433. font-size: 24px;
    434. // border:1px solid red;
    435. }
    436. .right {
    437. text-align: right;
    438. width: 30%;
    439. // border:1px solid red;
    440. }
    441. }
    442. .three {
    443. text-align: center;
    444. // border:1px solid red;
    445. image {
    446. width: 60px;
    447. height: 50px;
    448. }
    449. }
    450. }
    451. }
    452. }
    453. //第二行内容
    454. .tab_position {
    455. width: 100%;
    456. display: flex;
    457. justify-content: center;
    458. }
    459. .tab-bar {
    460. display: flex;
    461. justify-content: space-between;
    462. width: 90%;
    463. // border: 1px solid black;
    464. background-color: #f2f2f2;
    465. border-radius: 15px;
    466. margin: 3% 1%;
    467. }
    468. .tab {
    469. padding: 5px;
    470. font-size: 16px;
    471. cursor: pointer;
    472. // border: 1px solid black;
    473. width: 33%;
    474. text-align: center;
    475. // border-bottom: 1px solid #ccc;
    476. }
    477. .active {
    478. color: white;
    479. background-color: #515dc3;
    480. border-radius: 15px;
    481. /* background-color:#74bfe7; */
    482. // border-bottom: 1px solid #74bfe7;
    483. }
    484. //图标
    485. .out {
    486. width: 800rpx;
    487. // border:1px solid red;
    488. display: flex;
    489. justify-content: center;
    490. }
    491. #myChart {
    492. width: 700rpx;
    493. height: 600rpx;
    494. }
    495. #myChart1 {
    496. width: 700rpx;
    497. height: 600rpx;
    498. }
    499. #myChart2 {
    500. width: 700rpx;
    501. height: 600rpx;
    502. }
    503. style>

    这样写会发现在手机端无法正常运

    现改用renderjs进行重写

    后端代码

    1. //查询销售看板需要使用的数据
    2. public function select_sale_ekanbaninfo(){
    3. $yesterday = strtotime('-1 day', strtotime(date('Y-m-d')));//获得昨天的时间戳
    4. $today = strtotime(date('Y-m-d')); // 获取当前日期的时间戳
    5. $data['today'] = $today;
    6. // 获取本周开始和结束时间戳
    7. $thisWeekStart = strtotime('this week', $today);
    8. $thisWeekEnd = strtotime('next week', $today) - 1;
    9. // 获取上一周开始和结束时间戳
    10. $lastWeekStart = strtotime('-1 week', $thisWeekStart);
    11. $lastWeekEnd = strtotime('-1 week', $thisWeekEnd);
    12. // 获取本月的开始和结束时间戳
    13. $thisMonthStart = strtotime('first day of this month', $today);
    14. $thisMonthEnd = strtotime('last day of this month', $today);
    15. // 获取上一个月的开始和结束时间戳
    16. $lastMonthStart = strtotime('first day of last month', $today);
    17. $lastMonthEnd = strtotime('last day of last month', $today);
    18. // 获取今年的开始和结束时间戳
    19. $thisYearStart = strtotime('first day of January', $today);
    20. $thisYearEnd = strtotime('last day of December', $today);
    21. // 获取去年的开始和结束时间戳
    22. $lastYearStart = strtotime('-1 year', $thisYearStart);
    23. $lastYearEnd = strtotime('-1 year', $thisYearEnd);
    24. // $oneMonthAgo = strtotime('-1 month'); // 获取一个月前的时间戳
    25. // $oneYearAgo = strtotime('-1 year'); // 获取一年前的时间戳
    26. // 获取昨天的总金额
    27. $data['yesterday_allamount'] = Db::table('so_headers_all')
    28. ->where('creation_date', '>=', $yesterday)
    29. ->where('creation_date', '<', $today)
    30. ->sum('order_all_amount');
    31. //当天的总金额
    32. $data['daysale_allamount'] = Db::table('so_headers_all')
    33. ->where('creation_date', '>=', $today) // 筛选 creation_date 大于等于今天的记录
    34. ->sum('order_all_amount'); // 求和 order_all_amount 列的值
    35. // 计算日涨幅比率
    36. if ($data['yesterday_allamount'] == 0) {
    37. $data['weekchangeRate'] = '无法计算';
    38. }
    39. else{
    40. $data['daychangeRate'] = ($data['daysale_allamount'] - $data['yesterday_allamount']) / $data['yesterday_allamount'] * 100;
    41. if ($data['daychangeRate'] >= 0) {
    42. $data['daychangeRate'] = '+' . number_format($data['daychangeRate'], 2) . '%';
    43. } else {
    44. $data['daychangeRate'] = number_format($data['daychangeRate'], 2) . '%';
    45. }
    46. }
    47. $data['daysale_allamount'] = number_format($data['daysale_allamount']/10000.0, 2); // 格式化结果保留两位小数
    48. //近一周的总金额
    49. // 获取本周的总金额
    50. $data['weeksale_allamount'] = Db::table('so_headers_all')
    51. ->where('creation_date', '>=', $thisWeekStart)
    52. ->where('creation_date', '<=', $thisWeekEnd)
    53. ->sum('order_all_amount');
    54. // 获取上一周的总金额
    55. $data['lastweeksale_allamount'] = Db::table('so_headers_all')
    56. ->where('creation_date', '>=', $lastWeekStart)
    57. ->where('creation_date', '<=', $lastWeekEnd)
    58. ->sum('order_all_amount');
    59. // 计算周涨幅比率
    60. if ($data['lastweeksale_allamount'] == 0) {
    61. $data['weekchangeRate'] = '无法计算';
    62. } else {
    63. $data['weekchangeRate'] = ($data['weeksale_allamount'] - $data['lastweeksale_allamount']) / $data['lastweeksale_allamount'] * 100;
    64. if ($data['weekchangeRate'] >= 0) {
    65. $data['weekchangeRate'] = '+' . number_format($data['weekchangeRate'], 2) . '%';
    66. } else {
    67. $data['weekchangeRate'] = number_format($data['weekchangeRate'], 2) . '%';
    68. }
    69. // $data['weekchangeRate'] = number_format(($data['weeksale_allamount'] - $data['lastweeksale_allamount']) / $data['lastweeksale_allamount'] * 100, 2) . '%';
    70. }
    71. $data['weeksale_allamount'] = number_format($data['weeksale_allamount']/10000.0, 2);
    72. // 获取本月的总金额
    73. $data['monthsale_allamount'] = Db::table('so_headers_all')
    74. ->where('creation_date', '>=', $thisMonthStart)
    75. ->where('creation_date', '<=', $thisMonthEnd)
    76. ->sum('order_all_amount');
    77. // 获取上一个月的总金额
    78. $data['lastmonthsale_allamount'] = Db::table('so_headers_all')
    79. ->where('creation_date', '>=', $lastMonthStart)
    80. ->where('creation_date', '<=', $lastMonthEnd)
    81. ->sum('order_all_amount');
    82. // 计算月涨幅比率
    83. if ($data['lastmonthsale_allamount'] == 0) {
    84. $data['monthchangeRate'] = '无法计算';
    85. } else {
    86. $data['monthchangeRate'] = ($data['monthsale_allamount'] - $data['lastmonthsale_allamount']) / $data['lastmonthsale_allamount'] * 100;
    87. if ($data['monthchangeRate'] >= 0) {
    88. $data['monthchangeRate'] = '+' . number_format($data['monthchangeRate'], 2) . '%';
    89. } else {
    90. $data['monthchangeRate'] = number_format($data['monthchangeRate'], 2) . '%';
    91. }
    92. // $data['monthchangeRate'] = number_format(($data['monthsale_allamount'] - $data['lastmonthsale_allamount']) / $data['lastmonthsale_allamount'] * 100, 2) . '%';
    93. }
    94. $data['monthsale_allamount'] = number_format($data['monthsale_allamount']/10000.0, 2);
    95. // 获取今年的总金额
    96. $data['yearsale_allamount'] = Db::table('so_headers_all')
    97. ->where('creation_date', '>=', $thisYearStart)
    98. ->where('creation_date', '<=', $thisYearEnd)
    99. ->sum('order_all_amount');
    100. // 获取去年的总金额
    101. $data['lastyearsale_allamount'] = Db::table('so_headers_all')
    102. ->where('creation_date', '>=', $lastYearStart)
    103. ->where('creation_date', '<=', $lastYearEnd)
    104. ->sum('order_all_amount');
    105. // 计算年涨幅
    106. if ($data['lastyearsale_allamount'] == 0) {
    107. $data['yearchangeRate'] = '无法计算';
    108. } else {
    109. $data['yearchangeRate'] = ($data['yearsale_allamount'] - $data['lastyearsale_allamount']) / $data['lastyearsale_allamount'] * 100;
    110. if ($data['yearchangeRate'] >= 0) {
    111. $data['yearchangeRate'] = '+' . number_format($data['yearchangeRate'], 2) . '%';
    112. } else {
    113. $data['yearchangeRate'] = number_format($data['yearchangeRate'], 2) . '%';
    114. }
    115. // $data['yearchangeRate'] = number_format(($data['yearsale_allamount'] - $data['lastyearsale_allamount']) / $data['lastyearsale_allamount'] * 100, 2) . '%';
    116. }
    117. $data['yearsale_allamount'] = number_format($data['yearsale_allamount']/10000.0, 2);
    118. //查询本周每天的的总金额数
    119. //获取本周的起始日期和结束日期
    120. $weekStart = date('Y-m-d', strtotime('this week Monday'));
    121. $weekEnd = date('Y-m-d', strtotime('this week Sunday'));
    122. // 构造日期范围数组(从周一到周天)
    123. $dateRange = [];
    124. $currentDate = $weekStart;
    125. while ($currentDate <= $weekEnd) {
    126. $dateRange[] = $currentDate;
    127. $currentDate = date('Y-m-d', strtotime($currentDate . ' +1 day'));
    128. }
    129. // 查询每天的总金额数
    130. $result = Db::table('so_headers_all')
    131. ->field("DATE_FORMAT(FROM_UNIXTIME(creation_date), '%Y-%m-%d') AS date, IFNULL(SUM(order_all_amount), 0) AS total_amount")
    132. ->whereTime('creation_date', '>=', $weekStart)
    133. ->whereTime('creation_date', '<=', $weekEnd)
    134. ->group('date')
    135. ->select();
    136. //去掉逗号,转换为
    137. foreach ($result as &$item) {
    138. $item['total_amount'] = round(($item['total_amount'] / 10000),2);
    139. }
    140. // 构造最终结果数组
    141. $dateArray = [];
    142. $totalAmountArray = [];
    143. foreach ($dateRange as $date) {
    144. $found = false;
    145. foreach ($result as $row) {
    146. if ($row['date'] == $date) {
    147. $dateArray[] = $row['date'];
    148. $totalAmountArray[] = $row['total_amount'];
    149. $found = true;
    150. break;
    151. }
    152. }
    153. if (!$found) {
    154. $weekdayIndex = date('w', strtotime($date));
    155. $dateArray[] = $date;
    156. $totalAmountArray[] = 0;
    157. }
    158. }
    159. $data['week_info']['date'] = $dateArray;
    160. $data['week_info']['total_amount'] = $totalAmountArray;
    161. //查询今年中每月的总金额数据
    162. // 获取当前年份
    163. $year = date('Y');
    164. $result1 = Db::table('so_headers_all')
    165. ->field("DATE_FORMAT(FROM_UNIXTIME(creation_date), '%Y-%m') AS month, IFNULL(SUM(order_all_amount), 0) AS total_amount")
    166. ->whereTime('creation_date', '>=', strtotime($year . '-01-01'))
    167. ->whereTime('creation_date', '<=', strtotime($year . '-12-31'))
    168. ->group('month')
    169. ->select();
    170. //去掉逗号,转换为
    171. foreach ($result1 as &$item) {
    172. $item['total_amount'] = round(($item['total_amount'] / 10000),2);
    173. }
    174. // 构造最终结果数组
    175. $dateArray1 = [];
    176. $totalAmountArray1 = [];
    177. for ($i = 1; $i <= 12; $i++) {
    178. //str_pad 函数用于在字符串的左侧(或右侧)填充指定字符,达到指定长度。这里,使用 str_pad 函数在 $i 左侧填充字符 '0',直到 $i 的长度达到 2。
    179. $month = str_pad($i, 2, '0', STR_PAD_LEFT);
    180. $dateArray1[] = $year . '-' . $month;
    181. $totalAmountArray1[$year . '-' . $month] = 0;
    182. }
    183. // 将查询结果填充到对应的月份位置
    184. foreach ($result1 as $item) {
    185. $totalAmountArray1[$item['month']] = $item['total_amount'];
    186. }
    187. // 最终结果数组
    188. foreach ($dateArray1 as $date) {
    189. $finalResult[] = [
    190. 'date' => $date,
    191. 'total_amount' => $totalAmountArray1[$date]
    192. ];
    193. $data['month_info']['date'][] = $date;
    194. $data['month_info']['total_amount'][] = $totalAmountArray1[$date];
    195. }
    196. //查询近五年总金额数据
    197. // 获取当前年份
    198. $currentYear = date('Y');
    199. // 构造最终结果数组
    200. $data['year_info']['date'] = [];
    201. $data['year_info']['total_amount'] = [];
    202. for ($i = $currentYear - 5; $i <= $currentYear; $i++) {
    203. $year = (string) $i;
    204. $result = Db::table('so_headers_all')
    205. ->field("IFNULL(SUM(order_all_amount), 0) AS total_amount")
    206. ->whereTime('creation_date', '>=', strtotime($year . '-01-01'))
    207. ->whereTime('creation_date', '<=', strtotime($year . '-12-31'))
    208. ->find();
    209. $totalAmount = round(($result['total_amount'] / 10000), 2);
    210. $data['year_info']['date'][] = $year;
    211. $data['year_info']['total_amount'][] = $totalAmount;
    212. }
    213. echo json_encode($data);
    214. }

    重写代码

    1. <template>
    2. <view>
    3. <view>
    4. <view class="title">概况view>
    5. <view class="line_position">
    6. <view class="line1">
    7. <view class="item">
    8. <view class="one">今日销售额(万元)view>
    9. <view class="two">
    10. <view class="left">{{line1_info.daysale_allamount}}view>
    11. <view class="right">{{line1_info.daychangeRate}}view>
    12. view>
    13. <view class="three">
    14. <image :src="getImagePath(line1_info.daychangeRate)" alt="">image>
    15. view>
    16. view>
    17. <view class="item">
    18. <view class="one">本周销售额(万元)view>
    19. <view class="two">
    20. <view class="left">{{line1_info.weeksale_allamount}}view>
    21. <view class="right">{{line1_info.weekchangeRate}}view>
    22. view>
    23. <view class="three">
    24. <image :src="getImagePath(line1_info.weekchangeRate)" alt="">image>
    25. view>
    26. view>
    27. <view class="item">
    28. <view class="one">本月销售额(万元)view>
    29. <view class="two">
    30. <view class="left">{{line1_info.monthsale_allamount}}view>
    31. <view class="right">{{line1_info.monthchangeRate}}view>
    32. view>
    33. <view class="three">
    34. <image :src="getImagePath(line1_info.monthchangeRate)" alt="">image>
    35. view>
    36. view>
    37. <view class="item">
    38. <view class="one">今年销售额(万元)view>
    39. <view class="two">
    40. <view class="left">{{line1_info.yearsale_allamount}}view>
    41. <view class="right">{{line1_info.yearchangeRate}}view>
    42. view>
    43. <view class="three">
    44. <image :src="getImagePath(line1_info.yearchangeRate)" alt="">image>
    45. view>
    46. view>
    47. view>
    48. view>
    49. view>
    50. <view>
    51. <view class="title">销售额统计view>
    52. <view class="tab_position">
    53. <view class="tab-bar">
    54. <text class="tab" :class="{ 'active': activeTab === '0' }" @click="changeTab('0')">周统计text>
    55. <text class="tab" :class="{ 'active': activeTab === '1' }" @click="changeTab('1')">月统计text>
    56. <text class="tab" :class="{ 'active': activeTab === '2' }" @click="changeTab('2')">年统计text>
    57. view>
    58. view>
    59. <view v-show="activeTab === '0'">
    60. <view class="out">
    61. <view :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts">view>
    62. view>
    63. view>
    64. <view v-show="activeTab === '1'">
    65. <view class="out">
    66. <view :prop="option1" :change:prop="echarts.updateEcharts1" id="echarts1" class="echarts">view>
    67. view>
    68. view>
    69. <view v-show="activeTab === '2'">
    70. <view class="out">
    71. <view :prop="option2" :change:prop="echarts.updateEcharts2" id="echarts2" class="echarts">view>
    72. view>
    73. view>
    74. view>
    75. view>
    76. template>
    77. <script>
    78. // import echarts from '@/static/js/echarts.js' // 引入文件
    79. import * as echarts from 'echarts';
    80. export default {
    81. data() {
    82. return {
    83. line1_info: '',
    84. down: getApp().globalData.icon + 'report/down.png',
    85. up: getApp().globalData.icon + 'report/up.png',
    86. line: getApp().globalData.icon + 'report/line.png',
    87. activeTab: '0', //默认分页面
    88. weekinfo: [], // 定义一个空数组用于存储周统计数据
    89. monthinfo: [], // 定义一个空数组用于存储月统计数据
    90. yearinfo: [], // 定义一个空数组用于存储年统计数据
    91. option: '',
    92. option1: '',
    93. option2: '',
    94. }
    95. },
    96. mounted() {
    97. this.getData(); // 在组件挂载后调用获取数据的方法
    98. },
    99. methods: {
    100. //切换分页面
    101. changeTab(index) {
    102. this.activeTab = index;
    103. },
    104. //图片展示
    105. getImagePath(rate) {
    106. if (rate === "无法计算") {
    107. return this.line;
    108. } else if (rate && rate.startsWith("-")) {
    109. return this.down;
    110. } else {
    111. return this.up;
    112. }
    113. },
    114. getData() {
    115. uni.request({
    116. url: getApp().globalData.position + 'Other/select_sale_ekanbaninfo',
    117. data: {},
    118. header: {
    119. "Content-Type": "application/x-www-form-urlencoded"
    120. },
    121. method: 'POST',
    122. dataType: 'json',
    123. success: res => {
    124. //行1:年月日总额统计
    125. this.line1_info = res.data;
    126. //行2:周统计
    127. var weekinfo = res.data.week_info;
    128. this.weekinfo = weekinfo;
    129. //行2:月统计
    130. var monthinfo = res.data.month_info;
    131. this.monthinfo = monthinfo;
    132. //行2:年统计
    133. var yearinfo = res.data.year_info;
    134. this.yearinfo = yearinfo;
    135. // console.log(this.yearinfo)
    136. //显示图表
    137. this.echart();
    138. },
    139. fail(res) {
    140. console.log("查询失败")
    141. }
    142. });
    143. },
    144. //绘制图标
    145. echart() {
    146. // 周统计金额
    147. // 提取日期和对应的值
    148. var dates = this.weekinfo.date;
    149. var values = this.weekinfo.total_amount;
    150. var weeks = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
    151. // 进行图表的配置和数据处理
    152. this.option = {
    153. //配置网格组件,用于定义图表的位置和大小
    154. grid: {
    155. top: '15%', // 增加top的值来创建间距
    156. left: '2%',
    157. right: '2%',
    158. bottom: '2%', // 增加bottom的值来创建间距
    159. containLabel: true, //自动计算并包含坐标轴标签、刻度和标题等内容在内。
    160. },
    161. color: ['#e57b7b'],
    162. tooltip: {
    163. trigger: 'item',
    164. axisPointer: {
    165. type: 'line'
    166. },
    167. formatter: function(params) {
    168. var value = values[params.dataIndex];
    169. var date = dates[params.dataIndex];
    170. var week = weeks[params.dataIndex];
    171. var marker = params.marker; // 添加marker(小圆点)
    172. return marker + ' ' + date + '
      '
      + week + ' : ' + value + '万元';
    173. }
    174. },
    175. xAxis: {
    176. // name: '日期',
    177. data: weeks,
    178. axisLine: {
    179. show: false // 隐藏纵坐标轴的横线
    180. },
    181. //隐藏小刻度短线
    182. axisTick: { // x轴刻度相关配置
    183. show: false, // 是否显示x轴刻度线
    184. },
    185. nameTextStyle: {
    186. fontSize: 12 // 设置横轴名称字体大小为12
    187. }
    188. },
    189. yAxis: {
    190. name: '金额(万元)',
    191. splitLine: {
    192. show: false // 隐藏纵坐标轴的背景横线
    193. },
    194. axisLine: {
    195. show: false // 隐藏纵坐标轴的竖线
    196. },
    197. //隐藏小刻度短线
    198. axisTick: { // x轴刻度相关配置
    199. show: false, // 是否显示x轴刻度线
    200. },
    201. axisLabel: {
    202. fontSize: 12 // 设置横轴标签字体大小为12
    203. },
    204. nameTextStyle: {
    205. fontSize: 12 // 设置横轴名称字体大小为12
    206. }
    207. },
    208. series: [{
    209. barWidth: '8',
    210. name: '销量',
    211. type: 'bar',
    212. data: values,
    213. itemStyle: {
    214. normal: {
    215. label: {
    216. show: true,
    217. position: 'top',
    218. textStyle: {
    219. // color: '#515dc3',
    220. fontSize: 12
    221. }
    222. }
    223. }
    224. }
    225. }]
    226. };
    227. //月统计
    228. // 月统计金额
    229. // 提取日期和对应的值
    230. var dates1 = this.monthinfo.date;
    231. var values1 = this.monthinfo.total_amount;
    232. var months1 = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
    233. var months1_chinese = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"];
    234. // 进行图表的配置和数据处理
    235. this.option1 = {
    236. //配置网格组件,用于定义图表的位置和大小
    237. grid: {
    238. top: '15%', // 增加top的值来创建间距
    239. left: '2%',
    240. right: '2%',
    241. bottom: '2%', // 增加bottom的值来创建间距
    242. containLabel: true, //自动计算并包含坐标轴标签、刻度和标题等内容在内。
    243. },
    244. color: ['#5588d4'],
    245. tooltip: {
    246. trigger: 'item',
    247. axisPointer: {
    248. type: 'line'
    249. },
    250. formatter: function(params) {
    251. var value = values1[params.dataIndex];
    252. var date = dates1[params.dataIndex];
    253. var month = months1_chinese[params.dataIndex];
    254. var marker = params.marker; // 添加marker(小圆点)
    255. return marker + ' ' + date + '
      '
      + month + ' : ' + value + '万元';
    256. }
    257. },
    258. xAxis: {
    259. // name: '日期',
    260. data: months1,
    261. axisLine: {
    262. show: false // 隐藏纵坐标轴的横线
    263. },
    264. //隐藏小刻度短线
    265. axisTick: { // x轴刻度相关配置
    266. show: false, // 是否显示x轴刻度线
    267. },
    268. nameTextStyle: {
    269. fontSize: 12 // 设置横轴名称字体大小为12
    270. }
    271. },
    272. yAxis: {
    273. name: '金额(万元)',
    274. splitLine: {
    275. show: false // 隐藏纵坐标轴的背景横线
    276. },
    277. axisLine: {
    278. show: false // 隐藏纵坐标轴的竖线
    279. },
    280. //隐藏小刻度短线
    281. axisTick: { // x轴刻度相关配置
    282. show: false, // 是否显示x轴刻度线
    283. },
    284. axisLabel: {
    285. fontSize: 12 // 设置横轴标签字体大小为12
    286. },
    287. nameTextStyle: {
    288. fontSize: 12 // 设置横轴名称字体大小为12
    289. }
    290. },
    291. series: [{
    292. barWidth: '6',
    293. name: '销量',
    294. type: 'bar',
    295. data: values1,
    296. itemStyle: {
    297. normal: {
    298. label: {
    299. show: true,
    300. position: 'top',
    301. textStyle: {
    302. // color: '#515dc3',
    303. fontSize: 12
    304. }
    305. }
    306. }
    307. }
    308. }]
    309. };
    310. //近五年年统计
    311. // 年统计金额
    312. // 提取日期和对应的值
    313. var dates2 = this.yearinfo.date;
    314. var values2 = this.yearinfo.total_amount;
    315. // 进行图表的配置和数据处理
    316. this.option2 = {
    317. //配置网格组件,用于定义图表的位置和大小
    318. grid: {
    319. top: '15%', // 增加top的值来创建间距
    320. left: '2%',
    321. right: '2%',
    322. bottom: '2%', // 增加bottom的值来创建间距
    323. containLabel: true, //自动计算并包含坐标轴标签、刻度和标题等内容在内。
    324. },
    325. color: ['#71aa77'],
    326. tooltip: {
    327. trigger: 'item',
    328. axisPointer: {
    329. type: 'line'
    330. },
    331. formatter: function(params) {
    332. var value = values2[params.dataIndex];
    333. var date = dates2[params.dataIndex];
    334. var marker = params.marker; // 添加marker(小圆点)
    335. return marker + ' ' + date + '年' + '
      '
      + value + '万元';
    336. }
    337. },
    338. xAxis: {
    339. // name: '日期',
    340. data: dates2,
    341. axisLine: {
    342. show: false // 隐藏纵坐标轴的横线
    343. },
    344. //隐藏小刻度短线
    345. axisTick: { // x轴刻度相关配置
    346. show: false, // 是否显示x轴刻度线
    347. },
    348. nameTextStyle: {
    349. fontSize: 12 // 设置横轴名称字体大小为12
    350. }
    351. },
    352. yAxis: {
    353. name: '金额(万元)',
    354. splitLine: {
    355. show: false // 隐藏纵坐标轴的背景横线
    356. },
    357. axisLine: {
    358. show: false // 隐藏纵坐标轴的竖线
    359. },
    360. //隐藏小刻度短线
    361. axisTick: { // x轴刻度相关配置
    362. show: false, // 是否显示x轴刻度线
    363. },
    364. axisLabel: {
    365. fontSize: 12 // 设置横轴标签字体大小为12
    366. },
    367. nameTextStyle: {
    368. fontSize: 12 // 设置横轴名称字体大小为12
    369. }
    370. },
    371. series: [{
    372. barWidth: '8',
    373. name: '销量',
    374. type: 'bar',
    375. data: values2,
    376. itemStyle: {
    377. normal: {
    378. label: {
    379. show: true,
    380. position: 'top',
    381. textStyle: {
    382. // color: '#515dc3',
    383. fontSize: 12
    384. }
    385. }
    386. }
    387. }
    388. }]
    389. }
    390. },
    391. },
    392. }
    393. script>
    394. <script module="echarts" lang="renderjs">
    395. let myChart
    396. let myChart1
    397. let myChart2
    398. export default {
    399. mounted() {
    400. // 首先判断window.echarts是否存在,如果存在则调用initEcharts1方法进行初始化。
    401. if (typeof window.echarts === 'function') {
    402. this.initEcharts()
    403. this.initEcharts1()
    404. this.initEcharts2()
    405. } else {
    406. // 如果不存在,则动态创建一个