How to plot per day cost in PHP

Avatar

sdg.matiaan
2019-05-27 09:25

<?php
ini_set('max_execution_time', 300);
// this shows a graph of your daily cost. you need to fill in the username, password, account number and date range
// yes this report is slow. it is also server intensive, so you want to cache the output so that it wont overwhelm the server
$username = 'sdg.demo';
$password = 'demo';
$account = '26336';
$start = '2017-04-01';
$end = '2017-04-10';
$curr = $start;
$chars = '';
$tag = null;
$prevDay = 0;
$profile = array();
function startElements($parser, $name, $attrs) {
  global $tag, $chars;
  $tag = $name;
  $chars = '';
}
function characterData($parser, $data) {
  global $chars;
  $chars .= $data;
}
function endElements($parser, $name) {
  global $curr, $chars, $profile;
  switch ($name)
  {
  case 'AMOUNT': @$profile[$curr] += $chars * 1; break;
  }
}
while ($curr < $end)
{
  $curr = date('Y-m-d', strtotime($curr) + 24 * 60 * 60);
  $profileData = file_get_contents('https://sdg-adam.pnpscada.com:445/getProvisionalBill.jsp?LOGIN=' . $username . '&PWD=' . $password . '&key1=' . $account . '&startdate=' .
start . '&enddate=' . $curr . '&TGIDX=0');
  $parser = xml_parser_create();
  xml_set_element_handler($parser, 'startElements', 'endElements');
  xml_set_character_data_handler($parser, 'characterData');
  xml_parse($parser, $profileData);
  xml_parser_free($parser);
  $pd = $prevDay;
  $prevDay = $profile[$curr];
  $profile[$curr] -= $pd;
}
?>
<script type='text/javascript'>
var data = [
<?php
$first = true;
foreach ($profile as $date => $kwh) {
  if ($first) $first = false; else echo ',';
  echo '['' . $date . '',' . $kwh . ']';
}
?>
];
</script>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>
<script type='text/javascript' src='http://adam.pnpscada.com/lib/jquery.jqplot.min.js'></script>
<script type='text/javascript' src='http://adam.pnpscada.com/lib/plugins/jqplot.dateAxisRenderer.min.js'></script>
<link rel='stylesheet' type='text/css' href='http://adam.pnpscada.com/lib/jquery.jqplot.min.css' />
<div id='chart1'></div>
<script type='text/javascript'>
$(function() {
  plot1 = $.jqplot('chart1', [data], {
    axes:{
      xaxis:{
        renderer:$.jqplot.DateAxisRenderer
      }
    }
  });
});
</script>

Avatar

sdg.matiaan
2017-04-24 17:12

remember to get a bill that adds up nicely:
1) for fixed charges make it a per day cost
2) for demand charges tick the 'prorata' checkbox

Avatar

sdg.matiaan
2017-04-25 09:45

note the start should always be from the first day of the month and the end should not go into the next month. so 2017-01-01 to 2017-02-01 is fine but 2017-01-01 to 2017-02-02 will not give correct results

Avatar

sdg.matiaan
2019-05-27 09:26

You should get the following output:

Avatar

sdg.matiaan
2019-05-29 16:29

the wiki turns the 2 quotes on line 51 to apostrophies, so this is what it should look like:

Please log in to post a comment