KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

ChartCard - count instead of sum #3153

Closed John opened this topic on on Sep 24, 2023 - 8 comments

John commented on Sep 24, 2023

I try to show the count of records (instead of the sum) in the chart area of ChartCard but i can not find the way to do it. I use ChartCard in dashboard, can you help?

KoolReport commented on Sep 25, 2023

Can you show me some code that you implement the ChartCard in Dashboard.

John commented on Sep 25, 2023

Thanks, please see code example below:

use \koolreport\dashboard\Dashboard; use \koolreport\dashboard\widgets\KWidget;

class MyClass extends KWidget {

protected function onInit()
{
    $this->use(\koolreport\amazing\ChartCard::class)
            ->settings(array(
                "title"=>"MY TITLE",
                "value"=>MyDB::table("table_call")->count()
				->innerjoin...
                                   .....other joint code here... 
				->run()->getScalar(),
			    "chart"=>array(
			        "type"=>"area",
			        "dataSource"=>MyDB::table("table_call")
					->innerjoin...
                                            .....other joint code here...
					->groupBy("date")
					->orderBy("date")
					->run(), 	
			        "columns"=>array(
			             "date",
			             "call_id"=>array(
							 "type"=>"number"
John commented on Sep 25, 2023

This part of the above code: "value"=> works fine, i mean it shows the count This part: "chart"=> has the problem

KoolReport commented on Sep 25, 2023

In order to show the AreaChart, you will need to 2 axis, a category column + a value column. So in your query, you need to group and count. For example:

MyDB::table("table1")->groupBy("category column")->select("category column")->count()->run()

The category column may be a product category or a date for example. So that the data input to AreaChart will be like this:

categorycount
A123
B422
C222

Let me know if you need further assistance.

John commented on Sep 25, 2023

You asked for a part of my code and i didn't give all the code. Yes i have groupBy etc. (Please take a look at the updated code above). As category column i have date. I want to count the number of calls, so i use the id of a call. But i don't want to sum id...

John commented on Sep 25, 2023

Also to mention that based on your code:

MyDB::table("table1")->groupBy("category column")->select("category column")->count()->run()

... if i add to my code this part ->select or/and ->count() it shows error.

KoolReport commented on Sep 25, 2023

You can try

MyDB::table("table1")->groupBy("category column")->select("category column")->count("id of table 1")->run()

It could be the name of column provided in "columns" is not correct yet. May be your should try:

MyDB::table("table1")->groupBy("date")->select("date")->count("call_id")->alias("count_call_id")->run()

later in the "columns" you put

"columns"=>[
    "date",
    "count_call_id"
]
John commented on Sep 25, 2023

It works perfect with this! Thank you!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

Dashboard