0

I made a dashboard on my website that has 3 different visualization charts, the data is from my database, and the problem that I'm encountering is that the other two are not showing eventhough the first one shows itself. moreover, if I hide or remove the first chart, the second one appears, but the third one still doesn't show. refer to images below:

First Chart Shows the following two are not.

The second chart only shows itself when I delete the code of the first chart.

The 3rd chart is the same case as 2nd, needed to remove the first and second charts just to see the third one.

here is my code:

<!--CHARTS-->
<!--BAR CHART-->
<div class="container-fluid px-4">
<?php 
// Attempt select query execution
try{
  $barsql = "SELECT * FROM castro_rentals.unit";   
  $barresult = $pdo->query($barsql);
  if($barresult->rowCount() > 0) {
    $price = array();
    $unit_no = array();

    while($row = $barresult->fetch()) {
        $price[] = $row["price"];
        $unit_no[] = $row["unit_no"];
    }

  unset($barresult);
  } else {
    echo "No records matching your query were found.";
  }
} catch(PDOException $e){
  die("ERROR: Could not able to execute $barsql. " . $e->getMessage());
}
 
// Close connection
unset($pdo);
?>


<div class="card mb-4">
                            <!--BAR CHART-->
                            <div class="card-header">
                                <i class="fas fa-chart-bar me-1"></i>
                                Unit Prices
                            </div>
                            <div class="card-body">
                                <canvas id="myBarChart" width="100%" height="50"></canvas>
                                </div>
                                <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
                                <script>
                                    //SETUP BLOCK
                                    const price = <?php echo json_encode($price); ?>;
                                    const unit_no = <?php echo json_encode($unit_no); ?>;
                                    const data = {
                                    labels: unit_no,
                                      datasets: [{
                                        label: 'Price',
                                        data: price,
                                        backgroundColor: ['rgba(57, 43, 90, .6)'],
                                        borderColor:'rgb(57, 43, 90)',
                                        borderWidth: 2
                                    }]
                                  };
                                    //CONFIG BLOCK
                                    const config = {
                                    type: 'bar',
                                    data,
                                    options: {
                                      scales: {
                                        y: {
                                          beginAtZero: true
                                        }
                                      }
                                    }
                                };
                                    //RENDER BLOCK
                                    const myBarChart = new Chart(
                                        document.getElementById('myBarChart'),
                                        config
                                        );
                                </script>
                            <div class="card-footer small text-muted">Updated yesterday at 11:59 PM</div>
</div>

        
                        <div class="row">
                            <div class="col-lg-6">
                                <!--LINE CHART-->
                                <?php 
                                include('config/dbcon.php');
                                // Attempt select query execution
                                try{
                                  $linesql = "SELECT * FROM castro_rentals.payment";   
                                  $lineresult = $pdo->query($linesql);
                                  if($lineresult->rowCount() > 0) {
                                    $amount = array();
                                    $paymentdate = array();

                                    while($row = $lineresult->fetch()) {
                                        $amount[] = $row["amount"];
                                        $paymentdate[] = $row["paymentdate"];
                                    }

                                  unset($lineresult);
                                  } else {
                                    echo "No records matching your query were found.";
                                  }
                                } catch(PDOException $e){
                                  die("ERROR: Could not able to execute $linesql. " . $e->getMessage());
                                }
                                 
                                // Close connection
                                unset($pdo);
                                ?>
                                <div class="card mb-4">
                                    <div class="card-header">
                                        <i class="fas fa-chart-line me-1"></i>
                                        Monthly Income
                                    </div>
                                    <div class="card-body">
                                        <canvas id="myLineChart" width="100%" height="50"></canvas>
                                        </div>
                                        <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
                                <script>
                                    //SETUP BLOCK
                                    const amount = <?php echo json_encode($amount); ?>;
                                    const paymentdate = <?php echo json_encode($paymentdate); ?>;
                                    const data = {
                                    labels: paymentdate,
                                      datasets: [{
                                        label: 'Amount',
                                        data: amount,
                                        backgroundColor: ['rgba(57, 43, 90, .6)'],
                                        borderColor:'rgb(57, 43, 90)',
                                        borderWidth: 2
                                    }]
                                  };
                                    //CONFIG BLOCK
                                    const config = {
                                    type: 'line',
                                    data,
                                    options: {
                                      scales: {
                                        y: {
                                          beginAtZero: true
                                        }
                                      }
                                    }
                                };
                                    //RENDER BLOCK
                                    const myLineChart = new Chart(
                                        document.getElementById('myLineChart'),
                                        config
                                        );
                                </script>
                                    <div class="card-footer small text-muted">Updated yesterday at 11:59 PM</div>
                                </div>
                            </div>

                            <!--PIE CHART-->
                            <div class="col-lg-6">
                                <!--PIE CHART-->
<?php 
include('config/dbcon.php');
// Attempt select query execution
try{
  $piesql = "SELECT * FROM castro_rentals.property";   
  $pieresult = $pdo->query($piesql);
  if($pieresult->rowCount() > 0) {
    $propertyname = array();
    $paymentdate = array();

    while($row = $pieresult->fetch()) {
        $propertyname[] = $row["propertyname"];
        $propertytype[] = $row["propertytype"];
    }

  unset($pieresult);
  } else {
    echo "No records matching your query were found.";
  }
} catch(PDOException $e){
  die("ERROR: Could not able to execute $piesql. " . $e->getMessage());
}
 
// Close connection
unset($pdo);
?>
                                <div class="card mb-4">
                                    <div class="card-header">
                                        <i class="fas fa-chart-pie me-1"></i>
                                        # of Property Type
                                    </div>
                                    <div class="card-body">
                                        <canvas id="myPieChart" width="100%" height="50"></canvas>
                                        </div>
                                        <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
                                <script>
                                    //SETUP BLOCK
                                    const propertyname = <?php echo json_encode($propertyname); ?>;
                                    const propertytype = <?php echo json_encode($propertytype); ?>;
                                    const data = {
                                    labels: propertytype,
                                      datasets: [{
                                        label: 'propertyname',
                                        data: propertyname,
                                        backgroundColor: ['rgba(57, 43, 90, .6)'],
                                        borderColor:'rgb(57, 43, 90)',
                                        borderWidth: 2
                                    }]
                                  };
                                    //CONFIG BLOCK
                                    const config = {
                                    type: 'pie',
                                    data,
                                    options: {
                                      scales: {
                                        y: {
                                          beginAtZero: true
                                        }
                                      }
                                    }
                                };
                                    //RENDER BLOCK
                                    const myPieChart = new Chart(
                                        document.getElementById('myPieChart'),
                                        config
                                        );
                                </script>
                                    <div class="card-footer small text-muted">Updated yesterday at 11:59 PM</div>
                                </div>
                            </div>
                        </div>

The only thing I tried to do is to play with the <div> before the <canvas> I thought it wouldn't show because of spacing and size problems but I tried modifying, removing, resizing them, it still didn't show.

Uwe
  • 385
  • 1
  • 5
  • 14
  • Could you try to rename your `const` variables foreach chart. Seems you are creating global variables for config and data, which can't be changed afterwards. see [const keyword scope in Javascript](https://stackoverflow.com/questions/12272836/const-keyword-scope-in-javascript) – Uwe Nov 20 '22 at 08:54
  • You repeat things that don't need repeating, like `include('config/dbcon.php');` and ``. Do them once, at the start of your script. – KIKO Software Nov 20 '22 at 08:56
  • As soon as you start repeating the same, or similar, code you should try to use a function, so you only use that code once. See: [Don't repeat yourself](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself). – KIKO Software Nov 20 '22 at 08:58
  • @KIKOSoftware I tried removing the redundant 'include('config/dbcon.php'); and ' The first chart is still showing but I received an error "Warning: Undefined variable $pdo on line 175" and "Fatal error: Uncaught Error: Call to a member function query() on null" can you tell me how use a function? I'm quite new to these stuffs. – definitelynotaprogrammer Nov 20 '22 at 09:55
  • Don't use `unset($pdo);`, it causes the error, and is not needed. – KIKO Software Nov 20 '22 at 09:57
  • @KIKOSoftware here are the codes from line 173 to 178: '' try{ $linesql = "SELECT * FROM castro_rentals.payment"; $lineresult = $pdo->query($linesql); if($lineresult->rowCount() > 0) { $amount = array(); $paymentdate = array(); '' – definitelynotaprogrammer Nov 20 '22 at 09:57
  • There are many good tutorials out there to learn about functions. For instance: https://www.tutorialspoint.com/php/php_functions.htm – KIKO Software Nov 20 '22 at 09:59
  • @KIKOSoftware I did what you told me, I removed all the unset($pdo); and the errors were removed, also removed the redundancies but we're back at square one where it still doesn't show the two charts. – definitelynotaprogrammer Nov 20 '22 at 10:04
  • @definitelynotaprogrammer that is because of my comment. You are creating JavaScript `const` variables. These can't be overwritten later. Try to rename your JS variables for each chart --- or use `var` instead of `const` --- or create a custom scope [see const MDN doc](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) – Uwe Nov 20 '22 at 10:14
  • @Uwe that worked! changing from const to var fixed it! thank you so much! – definitelynotaprogrammer Nov 20 '22 at 10:19
  • Nice. You should read the two links I wrote in my comments to understand why it happened. – Uwe Nov 20 '22 at 12:17

0 Answers0