I have created a php page where the graph works fine if loaded in the browser.
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="css/layout.css" />
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
<script src="https://www.chartjs.org/dist/2.9.3/Chart.min.js"></script>
</head>
<body>
<div id="dashboard_pie_pop_cont"><canvas id="dashboard_graph_quantity_enquiries_salespeople"</canvas</div>
<script>
var enquiriesquantitysalespeople = {
type: 'doughnut',
data: {
datasets: [{
data: [6,1,],
backgroundColor: ["#2585fe", "#71b0ff", "#29bb52", "#497956", "#fcb858", "#f8cd90"],
borderWidth: 0,
}],
labels: ['Lee Davies','Lee Davies']
},
options: {
responsive: true,
legend: {
position: 'right',
textDirection: 'rtl',
labels: {
boxWidth: 14,
fontSize: 14,
fontColor: "#000",
fontFamily: 'proxima-nova',
}
},
animation: {
animateScale: true,
animateRotate: true
}
}
};
window.onload = function() {
var enquiriesquantitysalespeople_chart = document.getElementById('dashboard_graph_quantity_enquiries_salespeople').getContext('2d');
window.myDoughnut = new Chart(enquiriesquantitysalespeople_chart, enquiriesquantitysalespeople);};
</script>
<body></html>
So when this page is loaded it works fine. But if I call it through ajax. Then its just blank, and graph doesnt show.
Here is the ajax code
$.ajax({
type : 'POST',
//dataType : 'json',
url : '//'+base_url+'/ajax2/dashboard-pie.php',
success : function(data) {
$('#dashboard_graph_pop_cont').html(data);
}
});
And there is the container
<div id="dashboard_graph_pop_cont">
</div>
The container loads just fine if I replace the data variable some test text, e.g. $('#dashboard_graph_pop_cont').html('test'); then 'test' would appear in the container. Meaning there is some reason the javascript wont run in ajax.
What am I doing wrong?
The console log retunrs
<html lang="en">
<head>
<title></title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="css/layout.css" />
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
<script src="https://www.chartjs.org/dist/2.9.3/Chart.min.js"></script>
</head>
<body>
<div id="dashboard_pie_pop_cont"><canvas id="dashboard_graph_quantity_enquiries_salespeople"></canvas></div>
<script>
var enquiriesquantitysalespeople = {
type: 'doughnut',
data: {
datasets: [{
data: [6,1,],
backgroundColor: ["#2585fe", "#71b0ff", "#29bb52", "#497956", "#fcb858", "#f8cd90"],
borderWidth: 0,
}],
labels: ['Lee Davies','Lee Davies']
},
options: {
responsive: true,
legend: {
position: 'right',
textDirection: 'rtl',
labels: {
boxWidth: 14,
fontSize: 14,
fontColor: "#000",
fontFamily: 'proxima-nova',
}
},
animation: {
animateScale: true,
animateRotate: true
}
}
};window.onload = function() {
var enquiriesquantitysalespeople_chart = document.getElementById('dashboard_graph_quantity_enquiries_salespeople').getContext('2d');
window.myDoughnut = new Chart(enquiriesquantitysalespeople_chart, enquiriesquantitysalespeople);};</script><body></html> scripts.js:4054:12
ā
EDIT
Here's how we are calling AJAX:
$.ajax({
type : 'POST',
dataType : 'json',
url : '//'+base_url+'/ajax2/dashboard-pie.php',
data : 'col_action='+col_action+'&type='+type+'&chart='+chart,
success : function(data) {
console.log("data", data);
$('#dashboard_graph_pop_cont').append('<div id="dashboard_pie_pop_cont" style="width: 100%; height: 100px"></div>');
$('#dashboard_pie_pop_cont')
.empty()
.append(
$('<canvas/>', {
id: 'dashboard_graph_quantity_enquiries_salespeople'
})
);
var enquiriesquantitysalespeople_chart = document.getElementById('dashboard_graph_quantity_enquiries_salespeople').getContext('2d');
window.myDoughnut = new Chart(enquiriesquantitysalespeople_chart, data);
}
});
And dashboard-pie.php has:
header('Content-type: application/json');
$data = [
'type' => 'doughnut',
'data' => [
'datasets'=> [[
'data'=> [6, 1],
'backgroundColor' => ["#2585fe", "#71b0ff", "#29bb52", "#497956", "#fcb858", "#f8cd90"],
'borderWidth'=> 0,
]],
'labels'=> ['Lee Davies','Lee Davies']
],
'options'=> [
'responsive'=> true,
'legend'=> [
'position'=> 'right',
'textDirection'=> 'rtl',
'labels'=> [
'boxWidth'=> 14,
'fontSize'=> 14,
'fontColor'=> "#000",
'fontFamily'=> 'proxima-nova',
]
],
'animation'=> [
'animateScale'=> true,
'animateRotate'=> true
]
]
];
echo json_encode($data);