Guest User

Untitled

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