2👍
First of all, lets make it clear a bit, i see $kazanc_v[0]
and $kazanc_v->sub_total
is this and object or array? What is the output of this variable?
Also working with such output will force you do a mistake. If you want to show total count of sales or sum price per date, you should get data from database as grouped by date.
your query in your model should be something like this;
//in your model
$this->db->select("count(quantity) as qty, DATE(order_date) as date")
->from("orders")
->where(order_date > "2021-01-01") //beginning of this month
->group_by('date')
->get()
->result_array()
;
Now, you need to create x-axis
first.
In your case total day in a month (we consider as 31 days)
//in your controller
$begin = new DateTime( date('Y-m-01') ); //or given date
$end = new DateTime( date('Y-m-t') );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$dateRange = new DatePeriod($begin, $interval ,$end);
you have an array with dates, now you can create your chart data with y and x axis with data coming from your model.
$chartData =[];
$kazanc = $this->some_model->some_method();
foreach($dateRange as $date){
$dataKey = array_search($date->format("Y-m-d"), array_column($kazanc, 'date'));
if ($dataKey !== false) { // if we have the data in given date
$chartData[$date->format("Y-m-d")] = $kazanc[$dataKey]['qty'];
}else {
//if there is no record, create default values
$chartData[$date->format("Y-m-d")] = 0;
}
}
//send data to view
$this->load->view('template', ["chartData" => $chartData]);
now, you have date (x-axis) and qty (y-axis) for 31 days data in $chartData
variable.
Finally, we can print our data in view. According to chartjs documentation, something like this.
// in your view
var options = {
type: 'line',
data: {
labels: <?php echo json_encode(array_keys($chartData)); ?>,
datasets: [
{
label: '# Total Quantity',
data: <?php echo json_encode(array_values($chartData)); ?>,
borderWidth: 1
}
]
},
sample working code (php) https://www.tehplayground.com/pmiPYk3yhIpExJqa
jsfiddle for chartjs. https://jsfiddle.net/jwf67voe/