Charts can be rendered by populating data from database. Given example shows populating data from database and render the chart. It also includes PHP source code that you can try running locally.
<?php $dataPoints = array(); //Best practice is to create a separate file for handling connection to database try{ // Creating a new connection. // Replace your-hostname, your-db, your-username, your-password according to your database $link = new \PDO( 'mysql:host=your-hostname;dbname=your-db;charset=utf8mb4', //'mysql:host=localhost;dbname=canvasjs_db;charset=utf8mb4', 'your-username', //'root', 'your-password', //'', array( \PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, \PDO::ATTR_PERSISTENT => false ) ); $handle = $link->prepare('select x, y from datapoints'); $handle->execute(); $result = $handle->fetchAll(\PDO::FETCH_OBJ); foreach($result as $row){ array_push($dataPoints, array("x"=> $row->x, "y"=> $row->y)); } $link = null; } catch(\PDOException $ex){ print($ex->getMessage()); } ?> <!DOCTYPE HTML> <html> <head> <script> window.onload = function () { var chart = new CanvasJS.Chart("chartContainer", { animationEnabled: true, exportEnabled: true, theme: "light1", // "light1", "light2", "dark1", "dark2" title:{ text: "PHP Column Chart from Database" }, data: [{ type: "column", //change type to bar, line, area, pie, etc dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?> }] }); chart.render(); } </script> </head> <body> <div id="chartContainer" style="height: 370px; width: 100%;"></div> <script src="https://cdn.canvasjs.com/canvasjs.min.js"></script> </body> </html>
Setting indexLabel property shows index / data labels all data-points. The positioning of indexLabels can be changed to either "inside" or "outside" using indexLabelPlacement. Some other commonly used customization options are indexLabelFontSize, indexLabelOrientaion, etc.