Guest User

Untitled

a guest
Apr 25th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.47 KB | None | 0 0
  1. window.onload = allDraw;
  2. function allDraw() {
  3. draw('water', 'hot', 'cold', 'Вода', 'Горячая', 'Холодная'); //не отрисовываются значения
  4. // draw('electricity', 'day', 'night', 'Электричество', 'День', 'Ночь'); так работает
  5. }
  6.  
  7. var mainChart;
  8. function draw(type, subtype1, subtype2, title, subtitle1, subtitle2)
  9. {
  10. var start_date = document.querySelector('div#last_payment div.' + type + ' span').innerHTML.slice(6);
  11. start_date = new Date(moment(start_date, 'DD-MM-YYYY'));
  12. end_date = new Date(moment().startOf('day'));
  13. var date = new Array;
  14. var i = 0;
  15. date[i] = new Date(moment(start_date).add(1, 'days'));
  16. do{
  17. date[i+1] = new Date(moment(date[i]).add(1, 'days'));
  18. i++;
  19. }while(date[i] < end_date);
  20. end_date = moment(end_date).add(1, 'days');
  21. var values = new Array;
  22. var resultData;
  23. function arrayFill(values, consumed_values) {
  24. for (var i = 0; i < consumed_values.length; i++) {
  25. values[i] = consumed_values[i].innerHTML;
  26. }
  27. }
  28. values[1] = new Array();
  29. values[2] = new Array();
  30. arrayFill(values[1], document.querySelectorAll('ul.' + type + '.' + subtype1 + ' li'));
  31. arrayFill(values[2], document.querySelectorAll('ul.' + type + '.' + subtype2 + ' li'));
  32.  
  33. var firstData = {
  34. label: subtitle1,
  35. data: values[1],
  36. backgroundColor: 'rgba(233,202,145,0.9)',
  37. borderColor: 'rgba(175,119,33, 1)'
  38. };
  39.  
  40. var secondData = {
  41. label: subtitle2,
  42. data: values[2],
  43. backgroundColor: 'rgba(184,202,222,0.9)',
  44. borderColor: 'rgba(37,78,136, 1)'
  45. };
  46. var resultData = {
  47. labels: date,
  48. datasets: [firstData, secondData]
  49. };
  50. }
  51.  
  52. var options = {
  53. scales: {
  54. yAxes: [{
  55. scaleLabel: {
  56. display: true,
  57. labelString: 'Значения',
  58. fontSize: 17
  59. },
  60. ticks: {
  61. beginAtZero: true
  62. }
  63. }],
  64. xAxes: [{
  65. type: 'time',
  66. time: {
  67. min: start_date,
  68. max: end_date,
  69. unit: 'day',
  70. unitStepSize: 2
  71. }
  72. }]
  73. },
  74. legend: {
  75. display: true
  76. },
  77. title: {
  78. display: true,
  79. text: title,
  80. fontSize: 19
  81. }
  82. };
  83.  
  84. var canvas = document.querySelector('canvas#canvas');
  85. var ctx = canvas.getContext('2d');
  86. mainChart = new Chart(ctx, {
  87. type: 'bar',
  88. data: resultData,
  89. options: options
  90. });
  91. }
  92.  
  93. <ul class="electricity day">
  94. {% set count = 0 %}
  95. {% for i in data.electricity.day %}
  96. <li>{{ data.electricity.day[count] }}</li>
  97. {% set count = count + 1 %}
  98. {% endfor %}
  99. </ul>
  100. <ul class="electricity night">
  101. {% set count = 0 %}
  102. {% for i in data.electricity.night %}
  103. <li>{{ data.electricity.night[count] }}</li>
  104. {% set count = count + 1 %}
  105. {% endfor %}
  106. </ul>
  107. <ul class="water hot">
  108. {% set count = 0 %}
  109. {% for i in data.water.hot %}
  110. <li>{{ data.water.hot[count] }}</li>
  111. {% set count = count + 1 %}
  112. {% endfor %}
  113. </ul>
  114. <ul class="water cold">
  115. {% set count = 0 %}
  116. {% for i in data.water.cold %}
  117. <li>{{ data.water.cold[count] }}</li>
  118. {% set count = count + 1 %}
  119. {% endfor %}
  120. </ul>
  121.  
  122. window.onload = allDraw; function allDraw() {
  123. draw('water','cold','hot','Вода','Холодная','Горячая', lastPaymentDate('water'), todayDate()); }
  124.  
  125. function todayDate() {
  126. var today_date = new Date(moment().startOf('day'));
  127. return today_date;
  128. }
  129. function lastPaymentDate(type) {
  130. var last_payment_date = document.querySelector('div.' + type + ' span').innerHTML.slice(6);
  131. last_payment_date = new Date(moment(last_payment_date, 'DD-MM-YYYY'));
  132. return last_payment_date;
  133. }
  134.  
  135. var mainChart;
  136. function draw(type, subtype1, subtype2, title, subtitle1, subtitle2, start_date, end_date)
  137. {
  138. function arrayFill(values, consumed_values) {
  139. for (var i = 0; i < consumed_values.length; i++) {
  140. values[i] = consumed_values[i].innerHTML;
  141. }
  142. console.log('Значения в массиве: ' + values);
  143. }
  144. var date = new Array;
  145. var i = 0;
  146. date[i] = new Date(moment(start_date).add(1, 'days'));
  147. do {
  148. date[i + 1] = new Date(moment(date[i]).add(1, 'days'));
  149. i++;
  150. } while (date[i] < end_date);
  151. end_date = moment(end_date).add(1, 'days');
  152. var resultData;
  153. var values = new Array;
  154.  
  155. values[1] = new Array;
  156. values[2] = new Array;
  157. labelDisplay = true;
  158.  
  159. var array1 = function () {
  160. return new Promise(function (resolve, reject) {
  161. arrayFill(values[1], document.querySelectorAll('div.try1 span'));
  162. console.log('Первый массив');
  163. resolve();
  164. });
  165. };
  166.  
  167. var array2 = function () {
  168. return new Promise(function (resolve, reject) {
  169. arrayFill(values[2], document.querySelectorAll('div.try2 span'));
  170. console.log('Второй массив');
  171. resolve();
  172. });
  173. };
  174.  
  175. var chartSettings = function () {
  176. return new Promise(function (resolve, reject) {
  177. var firstData = {
  178. label: subtitle1,
  179. data: values[1],
  180. backgroundColor: 'rgba(233,202,145,0.9)',
  181. borderColor: 'rgba(175,119,33, 1)'
  182. };
  183.  
  184. var secondData = {
  185. label: subtitle2,
  186. data: values[2],
  187. backgroundColor: 'rgba(184,202,222,0.9)',
  188. borderColor: 'rgba(37,78,136, 1)'
  189. };
  190.  
  191. resultData = {
  192. labels: date,
  193. datasets: [firstData, secondData]
  194. };
  195. console.log('Настройки '+ resultData.datasets);
  196. resolve();
  197. });
  198. };
  199.  
  200. var chartCreation = function () {
  201. return new Promise(function (resolve, reject) {
  202. var options = {
  203. responsiveAnimationDuration: 1000,
  204. scales: {
  205. yAxes: [{
  206. scaleLabel: {
  207. display: true,
  208. labelString: 'Значения',
  209. fontSize: 17
  210. },
  211. ticks: {
  212. beginAtZero: true
  213. }
  214. }],
  215. xAxes: [{
  216. type: 'time',
  217. time: {
  218. min: start_date,
  219. max: end_date,
  220. unit: 'day',
  221. unitStepSize: 2
  222. }
  223. }]
  224. },
  225. legend: {
  226. display: true
  227. },
  228. title: {
  229. display: true,
  230. text: title,
  231. fontSize: 19
  232. }
  233. };
  234. var canvas = document.querySelector('canvas#canvas');
  235. var ctx = canvas.getContext('2d');
  236. mainChart = new Chart(ctx, {
  237. type: 'bar',
  238. data: resultData,
  239. options: options
  240. });
  241. console.log('Создание графика: ' + mainChart.data);
  242. resolve();
  243. }
  244. )
  245. };
  246.  
  247. array1().then(function () {
  248. return array2();
  249. }).then(function () {
  250. return chartSettings();
  251. }).then(function () {
  252. return chartCreation();
  253. }).then(function () {
  254. console.log('График построен'+ mainChart.data);
  255. })
  256. }
  257.  
  258. <canvas id="canvas"></canvas>
  259. <div class="water">
  260. <span class="date">Дата: 20-04-2018</span>
  261. </div>
  262. <div class="try1">
  263. <span>1</span><span>2</span><span>1</span><span>3</span><span>2</span>
  264. <span>6</span>
  265. </div> <div class="try2">
  266. <span>5</span><span>6</span><span>3</span><span>2</span><span>5</span>
  267. <span>1</span>
  268. </div>
Add Comment
Please, Sign In to add comment