index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. },
  27. series: [{
  28. data: [],
  29. type: 'bar'
  30. }]
  31. };
  32. chart.setOption(option);
  33. return chart;
  34. }
  35. Page({
  36. /**
  37. * 页面的初始数据
  38. */
  39. data: {
  40. projects: [],
  41. project_ids: [],
  42. project_names: '',
  43. total_project: '0',
  44. total_month: '0',
  45. projectShow: false,
  46. index: 0,
  47. show: false,
  48. isSearch: true,
  49. charts: [{
  50. name: '柱状图',
  51. type: 'bar'
  52. }, {
  53. name: '折线图',
  54. type: 'line'
  55. }, {
  56. name: '饼状图',
  57. type: 'pie'
  58. }, {
  59. name: '明细图',
  60. type: 'detail'
  61. }, {
  62. name: '雷达图',
  63. type: 'radar'
  64. }],
  65. chartIndex: 0,
  66. dateTypes: [{
  67. name: '按年',
  68. type: 'year'
  69. }, {
  70. name: '按月',
  71. type: 'month'
  72. }],
  73. dateIndex: 0,
  74. dateShow: false,
  75. years_months: [],
  76. date: '',
  77. yearDate: '',
  78. monthDate: '',
  79. max_date: (new Date()).getTime(),
  80. min_date: (new Date()).getTime(),
  81. filter: {},
  82. data: [],
  83. ec: {
  84. onInit: initChart
  85. }
  86. },
  87. /**
  88. * 生命周期函数--监听页面加载
  89. */
  90. onLoad: function (options) {
  91. var that = this
  92. api.getByName(this, 'projects/getAll', 'projects', {}, function (res) {
  93. that.getData()
  94. });
  95. api.getByName(this, 'data/getYearsAndMonths', 'years_months');
  96. // var maxDate = (new Date()).getTime()
  97. // var minDate = (new Date()).getTime() - 2 * 365 * 24 * 3600 * 1000
  98. // this.setData({
  99. // minDate,
  100. // maxDate
  101. // })
  102. this.getDateInfo()
  103. app.resetDataFilter()
  104. },
  105. switchCheck: function(e) {
  106. var index = e.currentTarget.dataset.index
  107. var projects = this.data.projects
  108. projects[index].checked = projects[index].checked ? false : true
  109. this.setData({projects})
  110. },
  111. closeProject: function(e) {
  112. var project_ids = []
  113. var project_names = []
  114. var projects = this.data.projects
  115. for(var i = 0; i < projects.length; ++i) {
  116. if(projects[i].checked) {
  117. project_ids.push(projects[i].id)
  118. project_names.push(projects[i].name)
  119. }
  120. }
  121. project_names = project_names.join(',')
  122. this.setData({project_ids, project_names})
  123. this.switchShow(e)
  124. this.getData()
  125. },
  126. getData() {
  127. var ids = this.data.project_ids
  128. if (ids.length <= 0) return false
  129. var data = Object.assign({}, {
  130. project_ids: ids,
  131. date: this.data.date,
  132. type: this.data.dateIndex == 0 ? 'year' : 'month'
  133. }, this.data.filter)
  134. var that = this
  135. api.getByName(this, 'data/getStat', 'data', data, function(res) {
  136. that.updateChart()
  137. });
  138. },
  139. getDateInfo() {
  140. var that = this
  141. http({
  142. url: 'data/getDateInfo',
  143. data: {},
  144. success: function (res) {
  145. if (res.code == 0) {
  146. that.setData(res.data)
  147. that.setData({
  148. yearDate: res.data.date
  149. })
  150. }
  151. }
  152. })
  153. },
  154. onDateChange: function (e) {
  155. var yearDate = e.detail.value[0].value + '-' + e.detail.value[1].value
  156. this.setData({
  157. yearDate
  158. })
  159. },
  160. dateConfirm: function (e) {
  161. this.switchShow(e)
  162. this.updateDate()
  163. },
  164. monthDateConfirm: function (e) {
  165. var date = e.detail
  166. var monthDate = util.formatDate(date)
  167. this.setData({
  168. monthDate
  169. })
  170. this.switchShow(e)
  171. this.updateDate()
  172. },
  173. updateDate: function () {
  174. var date = this.data.dateIndex == 0 ? this.data.yearDate : this.data.monthDate
  175. this.setData({
  176. date
  177. })
  178. this.getData()
  179. },
  180. onChange: function (e) {
  181. var index = e.detail.value
  182. var name = e.currentTarget.dataset.name
  183. this.setData({
  184. [name]: index
  185. })
  186. if(name == 'chartIndex') {
  187. this.updateChart()
  188. }
  189. },
  190. updateChart: function() {
  191. var data = this.data.data
  192. option.xAxis.data = data.names;
  193. option.legend = {
  194. data: data.legends
  195. }
  196. var type = this.data.charts[this.data.chartIndex].type
  197. var values = data.values
  198. for(var i = 0; i < values.length; ++i) {
  199. option.series[i] = {
  200. label: {
  201. show: true,
  202. position: 'top'
  203. },
  204. name: data.legends[i],
  205. type: type,
  206. data: values[i]
  207. }
  208. }
  209. chart.setOption(option)
  210. },
  211. radioChange: function (e) {
  212. var dateIndex = e.currentTarget.dataset.index
  213. this.setData({
  214. dateIndex
  215. })
  216. },
  217. switchShow: function (e) {
  218. var name = e.currentTarget.dataset.name
  219. var show = e.currentTarget.dataset.show ? e.currentTarget.dataset.show : !this.data[name]
  220. this.setData({
  221. [name]: show
  222. })
  223. },
  224. /**
  225. * 生命周期函数--监听页面初次渲染完成
  226. */
  227. onReady: function () {
  228. },
  229. /**
  230. * 生命周期函数--监听页面显示
  231. */
  232. onShow: function () {
  233. this.getTabBar().init();
  234. this.setData({
  235. project_id: ''
  236. })
  237. var filter = wx.getStorageSync('sg-data-filters')
  238. this.setData({
  239. filter: filter
  240. })
  241. },
  242. navigate: function(e) {
  243. var url = e.currentTarget.dataset.url
  244. wx.navigateTo({
  245. url: url,
  246. })
  247. },
  248. /**
  249. * 生命周期函数--监听页面隐藏
  250. */
  251. onHide: function () {
  252. },
  253. /**
  254. * 生命周期函数--监听页面卸载
  255. */
  256. onUnload: function () {
  257. },
  258. /**
  259. * 页面相关事件处理函数--监听用户下拉动作
  260. */
  261. onPullDownRefresh: function () {
  262. },
  263. /**
  264. * 页面上拉触底事件的处理函数
  265. */
  266. onReachBottom: function () {
  267. },
  268. /**
  269. * 用户点击右上角分享
  270. */
  271. onShareAppMessage: function () {
  272. }
  273. })