Advertisement
Shell_Casing

code

Dec 18th, 2018
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // user inputs
  3. const form = document.querySelector('#form');
  4. const tagSelector = document.querySelector('#tagSelector');
  5. const startDate = document.querySelector('#startDate');
  6. const endDate = document.querySelector('#endDate');
  7. const startTime = document.querySelector('#startTime');
  8. const endTime = document.querySelector('#endTime');
  9. const count = document.querySelector('#count');
  10. const interval = document.querySelector('#interval');
  11. const plotButton = document.querySelector('#plotButton');
  12. const warning = document.querySelector('#warning');
  13.  
  14.  
  15.  
  16. const ctx = document.querySelector('#chart').getContext('2d');
  17.  
  18. // holds the tags
  19. let tagsArray = [];
  20.  
  21. let valuesArray = [];
  22. let timeArray = [];
  23.  
  24. let chartType = {
  25.     bar: 'bar',
  26.     line: 'line'
  27. };
  28.  
  29. let data = {
  30.     labels: timeArray,
  31.     datasets: [{
  32.         label: tagSelector.value,
  33.         data: valuesArray,
  34.         backgroundColor: [
  35.             'rgba(255, 99, 132, 0.2)',
  36.             'rgba(54, 162, 235, 0.2)',
  37.             'rgba(255, 206, 86, 0.2)',
  38.             'rgba(75, 192, 192, 0.2)',
  39.             'rgba(153, 102, 255, 0.2)',
  40.             'rgba(255, 159, 64, 0.2)'
  41.         ],
  42.         borderColor: [
  43.             'rgba(255,99,132,1)',
  44.             'rgba(54, 162, 235, 1)',
  45.             'rgba(255, 206, 86, 1)',
  46.             'rgba(75, 192, 192, 1)',
  47.             'rgba(153, 102, 255, 1)',
  48.             'rgba(255, 159, 64, 1)'
  49.         ],
  50.         borderWidth: 1
  51.     }]
  52. };
  53.  
  54. let options = {
  55.     scales: {
  56.         yAxes: [{
  57.             ticks: {
  58.                 beginAtZero:false
  59.             }
  60.         }]
  61.     }
  62. };
  63.  
  64.  
  65.  
  66. // gets tags
  67. async function getTags() {
  68.     try {
  69.  
  70.         let xhr = new XMLHttpRequest();
  71.         xhr.open('GET', API.tagsUrl, true);
  72.         xhr.setRequestHeader('Authorization', 'Bearer ' + API.access_token);
  73.  
  74.         // xhr.open('GET', `./data/tags - verbose.json`, true);
  75.  
  76.         xhr.onload = async () => {
  77.             if(xhr.status === 200) {
  78.                 // console.log(xhr.responseText);
  79.                 let response = await JSON.parse(xhr.responseText);
  80.                 let tags = response.Tags;
  81.                 // console.log(tags);
  82.                 tags.map(tag => {
  83.                     let allTags = tag.Tagname;
  84.                     // console.log(allTags);
  85.                     tagsArray.push(allTags);
  86.                     // console.log(tagsArray);
  87.                     populateTagsInput();
  88.                 });
  89.             }
  90.         };
  91.  
  92.         xhr.send();
  93.  
  94.     } catch (e) {
  95.         console.log(e);
  96.     }
  97. }
  98.  
  99.  
  100. // populates the tags dropdown menu
  101. function populateTagsInput() {
  102.     let tagOption = document.createElement('option');
  103.     tagsArray.forEach(tag => {
  104.         tagOption.textContent = tag;
  105.         tagOption.setAttribute('value', tag);
  106.         tagSelector.append(tagOption);
  107.     });
  108. }
  109.  
  110.  
  111. plotButton.addEventListener('click', checkIfFormIsFullyFilled);
  112. // plotButton.addEventListener('click', getValuesAndPlotChart);
  113.  
  114.  
  115. function checkIfFormIsFullyFilled(e) {
  116.     e.preventDefault();
  117.     console.log('button clicked');
  118.     if (startDate.value !== '' && startTime.value !== '' && endDate.value !== '' && endTime.value !== '' && count.value !== '' && interval.value !== '') {
  119.         getValuesAndPlotChart();
  120.     } else {
  121.         warning.style.display = 'block';
  122.     }
  123. }
  124.  
  125.  
  126. function getValuesAndPlotChart() {
  127.  
  128.     let queryUrl = generateQueryUrl();
  129.     console.log(queryUrl);
  130.  
  131.     try {
  132.  
  133.         let xhr = new XMLHttpRequest();
  134.         xhr.open('GET', queryUrl, true);
  135.         xhr.setRequestHeader('Authorization', 'Bearer ' + API.access_token);
  136.  
  137.         // xhr.open('GET', `./data/WIN-9DBOGP80695.Simulation00052 - OG.json`, true);
  138.  
  139.         xhr.onload = async () => {
  140.             if(xhr.status === 200) {
  141.                 // console.log(xhr.responseText);
  142.                 let historianData = await JSON.parse(xhr.responseText);
  143.                 let timeStampsAndValues = historianData.Data[0].Samples;
  144.                 console.log(timeStampsAndValues);
  145.                 timeStampsAndValues.forEach(value => {
  146.                     timeArray.push(simplifyTime(value.TimeStamp));
  147.                     // valuesArray.push(Math.ceil(value.Value));
  148.                     valuesArray.push((parseInt(value.Value)).toFixed(0));
  149.                     plotChart();
  150.                 })
  151.  
  152.             }
  153.         };
  154.  
  155.         xhr.send();
  156.  
  157.     } catch (e) {
  158.         console.log(e);
  159.     }
  160.  
  161. }
  162.  
  163.  
  164. // grabs the form inputs and builds the API query URL
  165. function generateQueryUrl() {
  166.     // change interval value to milliseconds
  167.     const milliseconds = Math.ceil((parseInt(interval.value))*1000);
  168.     return `${API.dataUrl}/${tagSelector.value}/${startDate.value}T${startTime.value}/${endDate.value}T${endTime.value}/1/${count.value}/${milliseconds}`;
  169. }
  170.  
  171.  
  172. // trims off the seconds
  173. function simplifyTime(timestamp) {
  174.     return timestamp.slice(0, 17);
  175. }
  176.  
  177.  
  178. function plotChart() {
  179.     const chart = new Chart(ctx, {
  180.         type: chartType.bar,
  181.         data: data,
  182.         options: options
  183.     });
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement