index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // pages/data/index.js
  2. import http from '../../utils/http'
  3. import util from '../../utils/util'
  4. import api from '../../utils/api'
  5. const app = getApp()
  6. import * as echarts from '../../ec-canvas/echarts';
  7. let chart = null;
  8. let option = {}
  9. function initChart(canvas, width, height, dpr) {
  10. chart = echarts.init(canvas, null, {
  11. width: width,
  12. height: height,
  13. devicePixelRatio: dpr // new
  14. });
  15. canvas.setChart(chart);
  16. option = {
  17. legend: {
  18. data: []
  19. },
  20. xAxis: {
  21. type: 'category',
  22. data: []
  23. },
  24. yAxis: {
  25. type: 'value',
  26. show: false
  27. },
  28. series: [{
  29. data: [],
  30. type: 'bar'
  31. }]
  32. };
  33. chart.setOption(option);
  34. return chart;
  35. }
  36. Page({
  37. /**
  38. * 页面的初始数据
  39. */
  40. data: {
  41. projects: [],
  42. project_ids: [],
  43. project_names: '',
  44. year_money: '0',
  45. month_money: '0',
  46. projectShow: false,
  47. index: 0,
  48. show: false,
  49. isSearch: true,
  50. charts: [{
  51. name: '柱状图',
  52. type: 'bar'
  53. }, {
  54. name: '折线图',
  55. type: 'line'
  56. }, {
  57. name: '饼状图',
  58. type: 'pie'
  59. }, {
  60. name: '明细图',
  61. type: 'detail'
  62. }, {
  63. name: '雷达图',
  64. type: 'radar'
  65. }],
  66. chartIndex: 1,
  67. dateTypes: [{
  68. name: '按年',
  69. type: 'year'
  70. }, {
  71. name: '按月',
  72. type: 'month'
  73. }],
  74. dateIndex: 0,
  75. dateShow: false,
  76. years_months: [],
  77. date: '',
  78. start: '',
  79. end: '',
  80. max_date: '',
  81. min_date: '',
  82. filter: {},
  83. data: [],
  84. ec: {
  85. onInit: initChart
  86. },
  87. detail_data: [],
  88. orderBy: 'asc'
  89. },
  90. /**
  91. * 生命周期函数--监听页面加载
  92. */
  93. onLoad: function (options) {
  94. var that = this
  95. api.getByName(this, 'projects/getAll', 'projects', {});
  96. },
  97. updateValue: function(e) {
  98. var name = e.currentTarget.dataset.name
  99. var value = e.currentTarget.dataset.value
  100. this.setData({
  101. [name]: value
  102. })
  103. if(name == 'orderBy') {
  104. this.getChartData()
  105. }
  106. },
  107. updateDate(e) {
  108. this.setData({
  109. date: e.detail.date
  110. })
  111. this.getYearAndMonthMoney()
  112. this.getChartData()
  113. },
  114. getYearAndMonthMoney() {
  115. var that = this
  116. http({
  117. url: 'data/getYearAndMonthMoney',
  118. data: {
  119. date: this.data.date
  120. },
  121. success: function(res) {
  122. if(res.code == 0) {
  123. that.setData(res.data)
  124. }
  125. }
  126. })
  127. },
  128. getChartData: function() {
  129. var chartIndex = this.data.chartIndex
  130. var that = this
  131. if(chartIndex == 1) {
  132. http({
  133. url: 'data/projectStat',
  134. data: {
  135. date: this.data.date,
  136. orderBy: this.data.orderBy
  137. },
  138. success: function(res) {
  139. if(res.code == 0) {
  140. that.setData({
  141. data: res.data
  142. })
  143. that.updateChart()
  144. }
  145. }
  146. })
  147. }
  148. },
  149. updateChart: function () {
  150. var data = this.data.data
  151. option.xAxis.data = data.names;
  152. option.legend = {
  153. data: data.legends
  154. }
  155. var chartIndex = this.data.chartIndex
  156. if(chartIndex == 1) {
  157. option.xAxis.show = true
  158. var values = data.values
  159. option.series = [{
  160. label: {
  161. show: true,
  162. position: 'top'
  163. },
  164. name: data.names,
  165. type: 'bar',
  166. data: values
  167. }]
  168. }
  169. chart.setOption(option)
  170. },
  171. radioChange: function (e) {
  172. var dateIndex = e.currentTarget.dataset.index
  173. if (dateIndex == this.data.dateIndex) return false
  174. var start_date = this.data.start_date
  175. var end_date = this.data.end_date
  176. var start_date = dateIndex == 1 ? start_date + '-01' : start_date.substr(0, 7)
  177. var end_date = dateIndex == 1 ? end_date + '-01' : end_date.substr(0, 7)
  178. this.setData({
  179. dateIndex,
  180. start_date,
  181. end_date
  182. })
  183. },
  184. switchShow: function (e) {
  185. var name = e.currentTarget.dataset.name
  186. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  187. this.setData({
  188. [name]: show
  189. })
  190. if (name == 'dateShow' && !show) {
  191. this.getData()
  192. }
  193. },
  194. /**
  195. * 生命周期函数--监听页面初次渲染完成
  196. */
  197. onReady: function () {
  198. },
  199. /**
  200. * 生命周期函数--监听页面显示
  201. */
  202. onShow: function () {
  203. this.getTabBar().init();
  204. },
  205. navigate: function (e) {
  206. var url = e.currentTarget.dataset.url
  207. wx.navigateTo({
  208. url: url,
  209. })
  210. },
  211. /**
  212. * 生命周期函数--监听页面隐藏
  213. */
  214. onHide: function () {
  215. },
  216. /**
  217. * 生命周期函数--监听页面卸载
  218. */
  219. onUnload: function () {
  220. },
  221. /**
  222. * 页面相关事件处理函数--监听用户下拉动作
  223. */
  224. onPullDownRefresh: function () {
  225. },
  226. /**
  227. * 页面上拉触底事件的处理函数
  228. */
  229. onReachBottom: function () {
  230. },
  231. /**
  232. * 用户点击右上角分享
  233. */
  234. onShareAppMessage: function () {
  235. }
  236. })