Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JS expression for chart label typo #30

Merged
merged 21 commits into from
Nov 23, 2023
2 changes: 1 addition & 1 deletion demos/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

$m = new Model(new Persistence\Array_($t));
$m->addFields(['name', 'sales_cash', 'sales_bank', 'sales', 'purchases', 'profit']);
$m->onHook(Model::HOOK_AFTER_LOAD, function (Model $m) {
$m->onHook(Model::HOOK_AFTER_LOAD, static function (Model $m) {
$m->set('sales', $m->get('sales_cash') + $m->get('sales_bank'));
$m->set('profit', $m->get('sales') - $m->get('purchases'));
});
Expand Down
21 changes: 12 additions & 9 deletions src/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,18 @@ public function setCurrencyLabel(string $char = '€', string $axis = 'y', int $
'mode' => 'point',
'callbacks' => [
'label' => new JsFunction(['context'], [
new JsExpression(<<<'EOF'
let label = context.dataset.label || "";
// let value = context.parsed.y; // or x (horizontal) or r (radar) etc
let value = context.formattedValue.replace(/,/, "");
if (label) {
label += ": ";
}
return label + (value ? "' . $char . ' " + Number(value).toLocaleString(undefined, {minimumFractionDigits: ' . $digits . ', maximumFractionDigits: ' . $digits . '}) : "No Data");
EOF),
new JsExpression(
<<<'EOF'
let label = context.dataset.label || "";
// let value = context.parsed.y; // or x (horizontal) or r (radar) etc
let value = context.formattedValue.replace(/,/, "");
if (label) {
label += ": ";
}
return label + (value ? [] + Number(value).toLocaleString(undefined, {minimumFractionDigits: [], maximumFractionDigits: []}) : "No Data")
EOF,
[$char, $digits, $digits]
),
]),
],
],
Expand Down
21 changes: 12 additions & 9 deletions src/PieChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,18 @@ public function setCurrencyLabel(string $char = '€', string $axis = 'y', int $
'mode' => 'point',
'callbacks' => [
'label' => new JsFunction(['context'], [
new JsExpression(<<<'EOF'
let label = context.dataset.label || "";
// let value = context.parsed; // y or x (horizontal) or r (radar) etc
let value = context.formattedValue.replace(/,/, "");
if (label) {
label += ": ";
}
return label + (value ? "' . $char . ' " + Number(value).toLocaleString(undefined, {minimumFractionDigits: ' . $digits . ', maximumFractionDigits: ' . $digits . '}) : "No Data");
EOF),
new JsExpression(
<<<'EOF'
let label = context.dataset.label || "";
// let value = context.parsed; // y or x (horizontal) or r (radar) etc
let value = context.formattedValue.replace(/,/, "");
if (label) {
label += ": ";
}
return label + (value ? [] + Number(value).toLocaleString(undefined, {minimumFractionDigits: [], maximumFractionDigits: []}) : "No Data");
EOF,
[$char, $digits, $digits]
),
]),
],
],
Expand Down
19 changes: 11 additions & 8 deletions src/ScatterChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ public function setAxisTitles(string $xTitle = null, string $yTitle = null): voi
'mode' => 'point',
'callbacks' => [
'label' => new JsFunction(['context'], [
new JsExpression(<<<'EOF'
let label = context.dataset.label || "";
let value = context.parsed.y;
if (label) {
label += ": ";
}
return label + (value ? Number(value).toLocaleString(undefined, {minimumFractionDigits: ' . $digits . ', maximumFractionDigits: ' . $digits . '}) : "No Data");
EOF),
new JsExpression(
<<<'EOF'
let label = context.dataset.label || "";
let value = context.parsed.y;
if (label) {
label += ": ";
}
return label + (value ? Number(value).toLocaleString(undefined, {minimumFractionDigits: [], maximumFractionDigits: []}) : "No Data");
EOF.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dot? do not fix it, I am on this PR now...

[$digits, $digits]
),
]),
],
],
Expand Down