Home Forums Chart Support SQLServer + PHP + Canvasjs Reply To: SQLServer + PHP + Canvasjs

#25291

@manoj-mohan

Thanks a lot for your help, I used MySQL and my chart worked fine, but when I changed to SQLSrv, didn’t work. The connection to DB is working and information is the same than MySQL, the code is next:

Techinal Requirements:
– XAMPP for Windows 5.6.39
– PHP Version 5.6.39
php.ini file (I downloaded the SQLSrv drivers to establish the connection between my develop environment with my DB in another sever)
extension=php_pdo_sqlsrv_56_ts.dll
extension=php_sqlsrv_56_ts.dll

Thanks again and let me know if you need more detail please.

CODE:

<?php
$serverName = "192.2.1.21, 1433"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"Legal", "UID"=>"sa", "PWD"=>"T07Pass");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}

$queryG = "SELECT count(A.CveAsunto) as y, TL.Nombre as label 
		   FROM GeniusLegal.dbo.TipoLey TL INNER JOIN GeniusLegal.dbo.Asunto A ON  TL.CveTipoLey=A.CveTipoLey 
		   WHERE YEAR(A.FechaApertura)=2019 and A.CveAsuntoEstatus=1 GROUP BY TL.Nombre";
$dataG = sqlsrv_query($conn, $queryG);
$dataPoints = array();
while( $row = sqlsrv_fetch_array($dataG, SQLSRV_FETCH_ASSOC) ) {
	  array_push($dataPoints, $row);
}
?>
<!DOCTYPE HTML>
<html>
<head>
	<meta charset="UTF-8">
	<title> Dashboard - © 2019 Powered by Jauregui y Del Valle, S.C. </title>
	<meta name="description" content="JDV">
	<meta name="author" content="JDV">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" type="text/css" href="source/bootstrap-3.3.6-dist/css/bootstrap.css">
	<link rel="stylesheet" type="text/css" href="source/font-awesome-4.5.0/css/font-awesome.css">
	<link rel="stylesheet" type="text/css" href="style/slider.css">
	<link rel="stylesheet" type="text/css" href="style/mystyle.css">
<script type="text/javascript">
window.onload = function () {
        var chart = new CanvasJS.Chart("chartContainer", {
            title: {
                text: "Asuntos \"Activos\" por tipo"
            },
            animationEnabled: true,
            legend: {
                fontSize: 12,
                fontFamily: "Helvetica"
            },
            theme: "light2",
            data: [
            {
                type: "doughnut",
                indexLabelFontFamily: "Garamond",
                indexLabelFontSize: 13,
                indexLabel: "{label}: {y}",
                startAngle: -20,
                showInLegend: false,
				dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
            }
            ]
        });
        chart.render();
}
</script>
</head>
<body>
<div id="chartContainer" style="width: 50%; height: 300px;display: inline-block;"></div>
<script src="../canvasjs-2.2/canvasjs.min.js"></script>
</body>
</html>
<?php 
	sqlsrv_close($conn);
?>