如何在单个页面中有多个图表canvas.js php

时间:2019-06-12 07:21:17

标签: javascript php canvasjs

我正在使用canvasJS在一页中显示两个图表。

当我在页面中有两个图表时,仅显示第二个图表,第一个永远不会显示。

这是我的代码

主页

<?php include("top_airlines.php"); ?>
<?php include("genderrange.php"); ?>

这是第一个图表genderrange.php的代码

<?php 

$dataPoints1 = array( 
	array("label"=>"Male", "y"=>5),
	array("label"=>"Female", "y"=>1),
);



        
        
        
       ?> 
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function() {
 
 
var chart = new CanvasJS.Chart("chartContainer1", {
	animationEnabled: true,
	title: {
		text: "Gender Ratio of Applicants"
	},
	subtitles: [{
//		text: "November 2017"
	}],
	data: [{
		type: "pie",
		yValueFormatString: "#,##0.00\"\"",
		indexLabel: "{label} ({y})",
		dataPoints: <?php echo json_encode($dataPoints1, JSON_NUMERIC_CHECK); ?>
	}]
});
chart.render();
 
}
</script>
</head>
<body>
<div id="chartContainer1" style="height: 300px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>   

这是第二张图表的代码。变量是不同的,但它们仍将覆盖第一个变量。 top_airlines.php

<?php 

$malecount=5;
$femalecount=1;
$dataPoints2 = array( 
	array("label"=>"Male", "y"=>$malecount),
	array("label"=>"Female", "y"=>$femalecount),
);


?>


<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function() {
 
 
var chart2 = new CanvasJS.Chart("chartContainer2", {
	animationEnabled: true,
	title: {
		text: "Gender Ratio of Applicants"
	},
	subtitles: [{
//		text: "November 2017"
	}],
	data: [{
		type: "pie",
		yValueFormatString: "#,##0.00\"\"",
		indexLabel: "{label} ({y})",
		dataPoints: <?php echo json_encode($dataPoints2, JSON_NUMERIC_CHECK); ?>
	}]
});
chart2.render();
 
}
</script>
</head>
<body>
<div id="chartContainer2" style="height: 300px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>   

<br>
    

0 个答案:

没有答案