cost.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <section>
  3. <el-col :span="24" style="padding-bottom: 0px;text-align:center;margin-top:10px;">
  4. <!-- <el-date-picker
  5. v-model="dateRange"
  6. type="monthrange"
  7. align="right"
  8. unlink-panels
  9. :clearable="false"
  10. range-separator="至"
  11. start-placeholder="开始月份"
  12. end-placeholder="结束月份"
  13. :picker-options="pickerOptions"
  14. value-format="yyyy-MM"
  15. @change="getEchart">
  16. </el-date-picker> -->
  17. <el-date-picker
  18. v-model="dateRange" :editable="false"
  19. format="yyyy-MM-dd" value-format="yyyy-MM-dd"
  20. @change="getEchart"
  21. :clearable="true"
  22. range-separator="至"
  23. type="daterange"
  24. start-placeholder="开始日期"
  25. end-placeholder="结束日期"
  26. ></el-date-picker>
  27. <el-radio-group v-model="radio" @change="getEchart">
  28. <el-radio-button label="项目"></el-radio-button>
  29. <el-radio-button label="部门"></el-radio-button>
  30. </el-radio-group>
  31. </el-col>
  32. <div id="container" :style="'height:'+containerHeight+'px'"></div>
  33. <div style="position:fixed;top:120px;right:150px;"><el-button @click="exportProjectData()">导出数据</el-button></div>
  34. </section>
  35. </template>
  36. <script>
  37. import util from "../../common/js/util";
  38. export default {
  39. data() {
  40. return {
  41. dateRange:[],
  42. user: JSON.parse(sessionStorage.getItem("user")),
  43. radio: sessionStorage.radio!=null?sessionStorage.radio:'项目',
  44. containerHeight: 0,
  45. myChart: null,
  46. params: null,
  47. };
  48. },
  49. methods: {
  50. exportProjectData() {
  51. var param = {};
  52. if (this.dateRange != null) {
  53. param = {startDate:this.dateRange[0], endDate: this.dateRange[1]};
  54. }
  55. this.http.post("/project/exportTimeCost", param,
  56. res => {
  57. this.listLoading = false;
  58. if (res.code == "ok") {
  59. var aTag = document.createElement('a');
  60. aTag.download = "项目工时成本统计.xls";
  61. aTag.href = res.data;
  62. aTag.click();
  63. } else {
  64. this.$message({
  65. message: res.msg,
  66. type: "error"
  67. });
  68. }
  69. },
  70. error => {
  71. this.listLoading = false;
  72. this.$message({
  73. message: error,
  74. type: "error"
  75. });
  76. });
  77. },
  78. getEchart(){
  79. sessionStorage.radio = this.radio;
  80. var _this = this;
  81. console.log(this.dateRange);
  82. var param = {};
  83. if (this.dateRange != null) {
  84. param = {startDate:this.dateRange[0], endDate: this.dateRange[1]};
  85. }
  86. this.http.post(this.radio=='项目'?this.port.project.listCost:this.port.project.depCost, param,
  87. res => {
  88. if (res.code == "ok") {
  89. var xList = [], yList = [], list = res.data.costList,
  90. totalMoneyCost = (this.radio=='项目'?res.data.totalMoneyCost:res.data.totalCostMoney);
  91. for(var i in list) {
  92. if(this.radio=='项目') {
  93. xList.push(list[i].project);
  94. yList.push({
  95. "value": list[i].costMoney,
  96. "id": list[i].id,
  97. "cost": list[i].cost
  98. });
  99. } else {
  100. xList.push(list[i].departmentName);
  101. yList.push({
  102. "value": list[i].costMoney,
  103. "id": list[i].departmentId,
  104. "cost": list[i].costTime
  105. });
  106. }
  107. }
  108. var myChart = echarts.init(document.getElementById("container"));
  109. _this.myChart = myChart;
  110. var option = {
  111. title: {
  112. text: '工时成本总计' + totalMoneyCost.toFixed(2) + '元',
  113. left:'left',
  114. },
  115. // 工具箱
  116. toolbox: {
  117. show: true,
  118. feature:{
  119. saveAsImage:{
  120. show:true
  121. },
  122. restore:{
  123. show:true
  124. },
  125. // dataView:{
  126. // show:true
  127. // },
  128. // dataZoom:{
  129. // show:true
  130. // },
  131. magicType:{
  132. type:['line','bar']
  133. },
  134. }
  135. },
  136. tooltip:{
  137. trigger:'axis',
  138. formatter: function (params,ticket,callback) {
  139. var res = params[0].name + "<br/>工作成本"+" : " + params[0].data.value
  140. + "元 <br/>工作时长"+" : " + params[0].data.cost + "小时";
  141. _this.params = params;
  142. return res;
  143. }
  144. },
  145. xAxis: {
  146. data: xList,
  147. axisLabel: {
  148. interval:0,rotate:20
  149. }
  150. },
  151. yAxis: [{
  152. type : 'value',
  153. axisLabel: {
  154. formatter:'{value} (元)'
  155. }
  156. }],
  157. series: [{
  158. name: '工作成本(元)',
  159. type: 'bar',
  160. barMaxWidth: 30,
  161. data: yList,
  162. }]
  163. };
  164. myChart.setOption(option);
  165. // myChart.on('click', function(params) {
  166. // if(_this.radio=='项目') {
  167. // _this.$router.push("/cost/" + params.data.id + "/" + params.name);
  168. // } else {
  169. // _this.$router.push("/costDep/" + params.data.id + "/" + params.name);
  170. // }
  171. // });
  172. myChart.getZr().on('click', params => {
  173. const pointInPixel = [params.offsetX, params.offsetY];
  174. if (myChart.containPixel('grid', pointInPixel)) {
  175. console.log(_this.params)
  176. if(_this.radio=='项目') {
  177. if (_this.dateRange != null) {
  178. _this.$router.push("/cost/" + _this.params[0].data.id + "/" + _this.params[0].name
  179. +"?startDate="+_this.dateRange[0]+"&endDate="+_this.dateRange[1]);
  180. } else {
  181. _this.$router.push("/cost/" + _this.params[0].data.id + "/" + _this.params[0].name);
  182. }
  183. } else {
  184. if (_this.dateRange != null) {
  185. _this.$router.push("/costDep/" + _this.params[0].data.id + "/" + _this.params[0].name
  186. +"?startDate="+_this.dateRange[0]+"&endDate="+_this.dateRange[1]);
  187. } else {
  188. _this.$router.push("/costDep/" + _this.params[0].data.id + "/" + _this.params[0].name);
  189. }
  190. }
  191. }
  192. });
  193. } else {
  194. this.$message({
  195. message: res.msg,
  196. type: "error"
  197. });
  198. }
  199. },
  200. error => {
  201. this.$message({
  202. message: error,
  203. type: "error"
  204. });
  205. });
  206. },
  207. },
  208. created() {
  209. },
  210. mounted() {
  211. this.containerHeight = window.innerHeight - 140
  212. const that = this;
  213. window.onresize = function temp() {
  214. this.containerHeight = window.innerHeight - 100
  215. };
  216. if (this.$route.query.startDate != null) {
  217. this.dateRange = [this.$route.query.startDate, this.$route.query.endDate];
  218. } else {
  219. //默认查看本月
  220. var now = new Date();
  221. var t = util.formatDate.format(now, 'yyyy-MM-dd');
  222. var startStr = util.formatDate.format(new Date(), 'yyyy-MM') + "-01";
  223. this.dateRange = [startStr,t];
  224. }
  225. this.getEchart();
  226. var _this = this;
  227. window.addEventListener("resize", function() {
  228. _this.myChart.resize();
  229. });
  230. }
  231. };
  232. </script>
  233. <style lang="scss" scoped>
  234. #container {
  235. display: inline-block;
  236. width: 100%;
  237. margin-top: 10px;
  238. }
  239. </style>
  240. <style lang="scss">
  241. </style>