Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines
Here it is:
<?php
use \koolreport\dashboard\metrics\Category;
use \koolreport\dashboard\fields\Date;
use \koolreport\dashboard\fields\Number;
use \koolreport\dashboard\fields\Text;
use Mydb;
class QuoteMetric extends Category
{
protected function onCreated()
{
$this
->type("primary")
->title("Quotes")
->lazyLoading(false);
}
protected function dataSource()
{
return Mydb::rawSQL("
SELECT quotestage, subtotal
FROM ...");
}
protected function fields()
{
return [
$this->group(Text::create("quotestage"))->showTop(5)->andShowOthers(),
// $this->count(Number::create("quotestage"))->thousandSeparator('.')->decimalPoint(','),
$this->sum(Number::create("subtotal"))->showRawValue(true)->decimals(0)->thousandSeparator('.')->decimalPoint(',')
// $this->count(Text::create("quotestage"))
];
}
}
This error will happen when all your "subtotal" values sum to zero. We will fix it in the next version of Dashboard. Meanwhile you can apply the following fix:
1 . Open the files koolreport/amazing/dashboard/metrics/Category.view.php
and koolreport/appstack/dashboard/metrics/Category.view.php
, go to the 62nd line and replace this statement:
$row[$this->measuredField()->colName()]*100/$total
with this one:
($total == 0 ? 0 : ($row[$this->measuredField()->colName()]*100/$total))
Thanks but i don't have any zero 'subtotal' value. I made the changes to the files you proposed and now when i add $this->count(Number::create("quotestage"))->thousandSeparator('.')->decimalPoint(',') or $this->sum(Number::create("quotestage"))->thousandSeparator('.')->decimalPoint(',') i get zero values on my widget (which is not correct). My initial (see above) image has now become like this:
They are the defaults, only your change added and nothing else. Here is the koolreport/appstack/dashboard/metrics/Category.view.php
<?php
use \koolreport\dashboard\Lang;
$total = $this->data()->sum($this->measuredField()->colName());
?>
<div id="<?php echo $this->master()->name(); ?>" class="card card-accent-<?php echo $this->master()->type(); ?> card-value">
<div class="card-body pb-0" style="min-height:142px">
<div class="row">
<div class="col">
<div class="text-muted text-uppercase font-weight-bold font-xs"><?php echo $this->master()->title(); ?></div>
</div>
<div class="col text-right">
<div class="h5 text-secondary"><?php echo $this->measuredField()->field()->formatValue($total); ?></div>
</div>
</div>
<div>
<div style="display:inline-block;float:right;position:relative;top:-5px;left:10px;">
<?php
\koolreport\d3\DonutChart::create([
"dataSource"=>$this->donutData(),
"colorScheme"=>$this->colors(),
"height"=>$this->master()->pieSize(),
"width"=>$this->master()->pieSize(),
"columns"=>[
$this->categoryField()->field()->colName(),
$this->measuredField()->colName(),
],
"options"=>[
"legend"=>[
"show"=>false,
],
"tooltip"=>[
"format"=>[
"title"=>"function(){
return '".$this->master()->title()."';
}",
]
],
"donut"=>[
"label"=>[
"show"=>false
]
]
]
]);
?>
</div>
<small>
<ul class="list-inline">
<?php foreach($this->data() as $i=>$row): ?>
<li>
<i class="fa fa-circle" style="color:<?php echo $this->colors()[$i]; ?>"></i>
<?php
$field = $this->categoryField()->field();
echo $field->row($row)->formattedValue();
?>
-
<?php
echo $this->measuredField()->formatValue(
($this->measuredField()->_showRawValue()===true)?
$row[$this->measuredField()->colName()]:
// $row[$this->measuredField()->colName()]*100/$total,
($total == 0 ? 0 : ($row[$this->measuredField()->colName()]*100/$total)), //changed
$row);
?>
</li>
<?php endforeach; ?>
</ul>
</small>
<div class="row">
<div class="col-8">
</div>
<div class="col-4 text-right">
</div>
</div>
</div>
</div>
</div>
The change is to fix the division by zero issue. In case you might have zero data row then all totals and results are zero as well. Would you pls add the following lines at the beginning of the Category.view.php and see the dashboard:
<?php
use \koolreport\dashboard\Lang;
print_r($this->data()); //add this line
$total = $this->data()->sum($this->measuredField()->colName());
print_r($total); //add this line
...
Thanks, i get something like this:
Array ( [0] => Array ( [quotestage] => Delivered [subtotal] => 780.00000000 ) [1] => ... A LONG ARRAY HERE ... Array ( [quotestage] => Accepted [subtotal] => 19000.00000000 ) ) {"panels":{"QuoteMetric":[" \n \n [Widget] QuoteMetric\n\n \n \n
I saw that "quotestage" field's values are all null or empty here. That might be why your sum based on "quotestage" all return zero:
$this->sum(Number::create("quotestage"))->thousandSeparator('.')->decimalPoint(',')
Did you mean to count or sum based on "subtotal" field instead?
Hmm.... quotestage = Delivered, quotestage = Accepted etc. I don't know what you mean exactly... With my query i estimate the subtotal per quotestage. If you see on my image above the sum of quotes in quotestage Delivered is 6.835.562, with quotestage Created sum is 1.743.253 etc. Total sum of all quotes (in all quotestages) is 8.979.992
You can count data fields of any type but can only sum numeric fields. If quotestage = Delivered, Accepted, etc you can not sum it (the total result would be zero):
$this->sum(Number::create("quotestage"))... //this would return zero
You can use either:
$this->count(Number::create("quotestage"))...
or:
$this->sum(Number::create("subtotal"))...
If either of these return wrong results let us know.
I tried both and it shows this error:
Message: Call undefined showTop() method Line: 23 File: /vendor/koolreport/dashboard/TMagicMethod.php
The point is that - as you can see at my first image above - i can sum non numerical field ( by $this->group(Text::create("quotestage")) ). All numbers are correct, i just want to settle the thousand separator.
I think I made a mistake reading your description from the first post. You meant to format the upper right side total value, not the left side individual values.
Ok, so the total value is formatted according to the field setting, not the sum's or count's one. Thus, in your case you can try to format both the numeric field and its grouping one like this:
protected function fields()
{
return [
$this->group(Text::create("quotestage"))->showTop(5)->andShowOthers(),
$this->sum(Number::create("subtotal")->decimals(0)->thousandSeparator('.')->decimalPoint(','))
->showRawValue(true)->decimals(0)->thousandSeparator('.')->decimalPoint(',')
];
}
Let KoolReport help you to make great reports. It's free & open-source released under MIT license.
Download KoolReport View demo