If I call my controller method through base_url()
or site_url()
in script my bargraph does not work with Chrome or Internet Explorer. However, it does work with Firefox.
When I call the URL through an HTTP request with either Chrome or Internet Explorer, the chart is works but it doesn't work with Firefox.
My controller method is:
public function get_bargraph()
{
$this->load->model('reports_model');
$result = $this->reports_model-> get_graph_report();
echo json_encode($result);
}
My model method:
public function get_graph_report()
{
$array = array('user'=>1);
$this->db->select('DATE_FORMAT(date, "%m-%Y") as date , SUM(total_sale) as sale');
$this->db->group_by('DATE_FORMAT(date, "%m-%Y")');
$this->db->from('file');
$this->db->where($array);
$query = $this->db->get();
return $query->result_array();
}
The script used:
<canvas id="myChart" width="400" height="100"></canvas>
<script type="text/javascript" src = "<?php echo base_url('assets/js/jquery.min.js');?>"></script>
<script type="text/javascript" src = "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<script type = "text/javascript">
var dates = [];
var sales = [];
$(document).ready(function(){
$.post("http://localhost/test/Reports/get_bargraph",
function(data){
var obj = JSON.parse(data);
$.each(obj,function(i,item){
dates.push(item.date);
sales.push(item.sale);
});
var ctx = $("#myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels:dates ,
datasets: [
{
label: "Sales Report",
fill: true,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: sales,
spanGaps: false,
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
});
});
</script>