Advertisement
girinovey

Graphs balance and profit from sqlite db

Feb 23rd, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.27 KB | None | 0 0
  1. <html>
  2.   <head>
  3.     <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js                                                                                                                                                                                               "></script>
  4.     <script type="text/javascript">
  5. <?php
  6. // connect to DB
  7. $dbfile = "sqlite:/var/www/anubis/db.sqlite3";
  8.  
  9. class TableRows extends RecursiveIteratorIterator {
  10.     function __construct($it) {
  11.         parent::__construct($it, self::LEAVES_ONLY);
  12.     }
  13.  
  14.     function current() {
  15.         if (self::key() == 'date') {
  16.                 $dt = DateTime::createFromFormat( "U", parent::current() );
  17.                 return 'new Date('. $dt->format("Y, n - 1, j, G, p\a\\r\s\\e\I\\                                                                                                                                                                                               n\\t(i, 10), s")."),";
  18.         }
  19.         return parent::current() . ",";
  20.     }
  21.  
  22.     function beginChildren() {
  23.         echo "[";
  24.     }
  25.  
  26.     function endChildren() {
  27.         echo "]," . "\n";
  28.     }
  29. }
  30.  
  31. try {
  32.     $conn = new PDO($dbfile);
  33.     // set the PDO error mode to exception
  34.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  35.  
  36.     function makeDataDays($days) {
  37.         makeData("today -$days days");
  38.     }
  39.  
  40.     function makeData($modifier) {
  41.         global $conn;
  42.         $dt = new DateTime();
  43.         $dt->modify($modifier);
  44.         $date = $dt->format('U');
  45.         $stmt = $conn->prepare(
  46.         "SELECT date,
  47.                100000000*balance,
  48.                100000000*(balance-T.total),
  49.                100.0*((balance/T.total)-1.0)
  50.        FROM balance,
  51.           (SELECT b2.balance as total
  52.              FROM balance b2
  53.              WHERE b2.date > $date
  54.              ORDER BY b2.date
  55.              LIMIT 1) T
  56.        WHERE date > $date
  57.        ORDER BY date
  58. ;");
  59.         $stmt->execute();
  60.         // set the resulting array to associative
  61.         $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
  62.         echo '[';
  63.         foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as                                                                                                                                                                                                $k=>$v) {
  64.             echo $v;
  65.         }
  66.         echo ']';
  67.     }
  68. ?>
  69.     </script>
  70.     <script type="text/javascript">
  71.       google.charts.load('current', {'packages':['corechart']});
  72.       google.charts.setOnLoadCallback(function() {
  73.                 drawChart(<?php makeData("last Sunday"); ?>, 'Week to date', 'ch                                                                                                                                                                                               artweek');
  74.                 drawChart(<?php makeData("first day of this month"); ?>, 'Month                                                                                                                                                                                                to date', 'chartmonth');
  75.                 drawChart(<?php makeDataDays(7); ?>, '7 days', 'chart7');
  76.                 drawChart(<?php makeDataDays(30); ?>, '30 days', 'chart30');
  77.                 drawChart(<?php makeData("10 years ago"); ?>, 'All Time', 'chart                                                                                                                                                                                               all');
  78.       });
  79.  
  80.       function addDataHeader(data) {
  81.         return [['date', 'Balance', 'Profit', '%Profit']].concat(data);
  82.       }
  83.  
  84.       function drawChart(raw_data, title, domid) {
  85.         var data = google.visualization.arrayToDataTable(addDataHeader(raw_data)                                                                                                                                                                                               );
  86.  
  87.         var options = {
  88.           title: title,
  89.           //curveType: 'function',
  90.           legend: { position: 'bottom' },
  91.         series: {
  92.           0: {targetAxisIndex: 0},
  93.           1: {targetAxisIndex: 0},
  94.           2: {targetAxisIndex: 1},
  95.         },
  96.         vAxes: {
  97.           // Adds titles to each axis.
  98.           0: {title: 'satoshi'},
  99.           1: {title: '%'},
  100.         }
  101.         };
  102.  
  103.         var chart = new google.visualization.LineChart(document.getElementById(d                                                                                                                                                                                               omid));
  104.         chart.draw(data, options);
  105.       }
  106.     </script>
  107.   </head>
  108.   <body>
  109.     <div id="chartweek" style="width: 900px; height: 500px"></div>
  110.     <div id="chartmonth" style="width: 900px; height: 500px"></div>
  111.     <div id="chart7" style="width: 900px; height: 500px"></div>
  112.     <div id="chart30" style="width: 900px; height: 500px"></div>
  113.     <div id="chartall" style="width: 900px; height: 500px"></div>
  114.   </body>
  115. </html>
  116. <?php
  117. } catch(PDOException $e) {
  118.     echo "Connection failed: " . $e->getMessage();
  119. }
  120. $conn = null;
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement