body-record.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. (function() {
  2. var request = getRequest(),
  3. patient = request.patiCode || '';
  4. var tabIndex = 0,
  5. dayIndex = 0,
  6. statusIndex = 1;
  7. // 绘制折线图初始定义
  8. var ctx = document.getElementById('canvas').getContext("2d");
  9. var dataCanvas = {
  10. labels : [],
  11. datasets : [
  12. {
  13. fillColor : "rgba(220,220,220,0.5)",
  14. strokeColor : "rgba(220,220,220,1)",
  15. pointColor : "rgba(220,220,220,1)",
  16. pointStrokeColor : "#fff",
  17. data : []
  18. },
  19. {
  20. fillColor : "rgba(151,187,205,0.5)",
  21. strokeColor : "rgba(151,187,205,1)",
  22. pointColor : "rgba(151,187,205,1)",
  23. pointStrokeColor : "rgba(151,187,205,1)",
  24. data : []
  25. }
  26. ]
  27. },
  28. defaults = {
  29. scaleStartValue : 0,
  30. scaleShowLabels : true,
  31. datasetFill : false, // 是否填充数据集
  32. animation : true, // 是否执行动画
  33. animationSteps : 60, // 动画的时间
  34. animationEasing : "easeOutQuart", // 动画的特效
  35. onAnimationComplete : null // 动画完成时的执行函数
  36. };
  37. var myLineChart = null;
  38. // new Chart(ctx).Line(dataCanvas, defaults);
  39. $('.tab-record span').click(function() {
  40. tabIndex = $(this).index();
  41. // 初始化数据
  42. if(tabIndex == 1) {
  43. statusIndex = 0;
  44. $('.status-record').hide();
  45. } else {
  46. var ele = $('.status-record span').get(0);
  47. indexHover(ele);
  48. statusIndex = 1;
  49. $('.status-record').show();
  50. }
  51. indexHover(this);
  52. dayIndex = 0;
  53. var ele = $('.day-record span').get(0);
  54. indexHover(ele);
  55. canvasDataDefault();
  56. });
  57. $('.day-record span').click(function() {
  58. dayIndex = $(this).index();
  59. if(tabIndex == 0) {
  60. var ele = $('.status-record span').get(0);
  61. indexHover(ele);
  62. statusIndex = 1;
  63. }
  64. indexHover(this);
  65. canvasDataDefault();
  66. });
  67. $('.status-record span').click(function() {
  68. statusIndex = $(this).index() + 1;
  69. indexHover(this);
  70. canvasDataDefault();
  71. });
  72. // 点击指示特效
  73. function indexHover(ele) {
  74. $(ele).siblings().removeClass('f-hover');
  75. $(ele).addClass('f-hover');
  76. };
  77. // 绘图数据格式化
  78. function canvasDataDefault() {
  79. dataCanvas.labels = [];
  80. dataCanvas.datasets[0].data = [];
  81. dataCanvas.datasets[1].data = [];
  82. // 切换数据
  83. getBodyRecord(tabIndex, dayIndex, statusIndex);
  84. }
  85. // 时间推算
  86. function getDateFromCurrentDate(fromDate, dayInterval) {
  87. var curDate = new Date(Date.parse(fromDate.replace(/-/g, "/")));
  88. curDate.setDate(curDate.getDate() + dayInterval);
  89. var year = curDate.getFullYear();
  90. var month = (curDate.getMonth() + 1) < 10 ? "0" + (curDate.getMonth() + 1) : (curDate.getMonth() + 1);
  91. var day = curDate.getDate() < 10 ? "0" + curDate.getDate() : curDate.getDate();
  92. return year + "-" + month + "-" + day + " 00:00:00";
  93. };
  94. // 血糖标准
  95. function getNormalLevel(index) {
  96. return [
  97. [],
  98. [6.1, 3.9],
  99. [7.8, 4.4],
  100. [6.1, 3.9],
  101. [7.8, 4.4],
  102. [6.1, 3.9],
  103. [7.8, 4.4],
  104. [6.1, 3.9]
  105. ][index];
  106. };
  107. // 数据统计
  108. function totalStauts(zc, pg, pd) {
  109. $('.is-ok').html("正常:" + zc + "次");
  110. $('.is-height').html("偏高:" + pg + "次");
  111. $('.is-low').html("偏低:" + pd + "次");
  112. }
  113. // 获取最后一条数据
  114. var dataLast = {patient: patient};
  115. var lastXT = '',
  116. lastXY = '';
  117. bodyRecordApis.getBodyRecoredLast({data: dataLast}).then(function(res){
  118. if(res.status == 200){
  119. if(!res.data.xt || !res.data.xy) {
  120. showWarningMsg("该居民无体征记录");
  121. return;
  122. }
  123. lastXT = res.data.xt.recordDate;
  124. lastXY = res.data.xy.recordDate;
  125. // 初始化执行
  126. getBodyRecord(tabIndex, dayIndex, statusIndex);
  127. }else{
  128. showWarningMsg(res.msg)
  129. }
  130. })
  131. // 获取数据
  132. function getBodyRecord(tab, day, statu) {
  133. if(!lastXT || !lastXY) {
  134. showWarningMsg("该居民无体征记录");
  135. return;
  136. };
  137. var xtMax = getNormalLevel(statusIndex)[0],
  138. xtMin = getNormalLevel(statusIndex)[1],
  139. xyMax = [139, 89],
  140. xymin = [90, 60],
  141. toNormal = 0,
  142. toHigh = 0,
  143. toLow = 0;
  144. totalStauts(toNormal, toHigh, toLow);
  145. $('.danwei').html(tab == 0 ? '单位(mmol/L)' : '单位(mmHg)');
  146. var dayNum = 0;
  147. switch(day) {
  148. case 0:
  149. dayNum = 7;break;
  150. case 1:
  151. dayNum = 30;break;
  152. case 2:
  153. dayNum = 90;break;
  154. case 3:
  155. dayNum = 365 / 2;break;
  156. }
  157. var type = (tab == 0 ? 1 : 2),
  158. startdate = (tab == 0 ? lastXT : lastXY),
  159. enddate = getDateFromCurrentDate(startdate, -dayNum);
  160. $('.data-record').html('<' + enddate.substr(0, 10) + '至' + startdate.substr(0,10) + '>');
  161. var recordData = {
  162. type: type,
  163. begin: enddate,
  164. end: startdate,
  165. gi_type: statu,
  166. patient: patient
  167. };
  168. // 获取体征记录
  169. bodyRecordApis.getBodyRecordData({data: recordData}).then(function(res){
  170. if(myLineChart) {// 如果构造过,解析
  171. myLineChart.destroy();
  172. }
  173. if(res.status == 200){
  174. var list = res.list,
  175. len = 0;
  176. if(list) {
  177. len = list.length;
  178. } else {
  179. showWarningMsg("当前状态下无居民体征记录");return;
  180. };
  181. list.reverse();
  182. for(var i = 0; i < len; i++) {
  183. if(len == 1) {
  184. dataCanvas.labels = [enddate.substr(5, 5), list[i].date.substr(5, 5)];
  185. dataCanvas.datasets[0].data = [null, list[i].value1];
  186. } else {
  187. dataCanvas.labels[i] = list[i].date.substr(5, 5);
  188. dataCanvas.datasets[0].data[i] = list[i].value1;
  189. }
  190. if(tabIndex == 1) {
  191. if(len == 1) {
  192. dataCanvas.datasets[1].data = [null, list[i].value2];
  193. } else {
  194. dataCanvas.datasets[1].data[i] = list[i].value2;
  195. }
  196. // 血压判断
  197. if(list[i].value1 > xyMax[0] || list[i].value2 > xyMax[1]) {
  198. ++toHigh;
  199. } else if(list[i].value1 < xyMin[0] || list[i].value2 < xyMin[1]) {
  200. ++toLow;
  201. } else {
  202. ++toNormal;
  203. }
  204. } else {
  205. // 血糖判断
  206. if(list[i].value1 > xtMax) {
  207. ++toHigh;
  208. } else if(list[i].value1 < xtMin) {
  209. ++toLow;
  210. } else {
  211. ++toNormal;
  212. };
  213. }
  214. }
  215. totalStauts(toNormal, toHigh, toLow);
  216. // 绘制图
  217. myLineChart = new Chart(ctx).Line(dataCanvas, defaults);
  218. }else{
  219. showWarningMsg(res.msg)
  220. }
  221. })
  222. }
  223. })(jQuery)