-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add support for stacked bar chart #17
Changes from all commits
9d402e2
aaad973
e5b6b8a
495b476
a9abea4
6e6609a
db6fc91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,10 @@ | |
use Atk4\Ui\JsExpression; | ||
use Atk4\Ui\View; | ||
|
||
/** | ||
* ChartJS 2.7.x documentation https://www.chartjs.org/docs/2.7.3/ | ||
* ChartJS 3.9.1 documentation https://www.chartjs.org/docs/3.9.1/. | ||
*/ | ||
class Chart extends View | ||
{ | ||
/** @var string HTML element type */ | ||
|
@@ -28,6 +32,7 @@ class Chart extends View | |
['rgba(75, 192, 192, 0.2)', 'rgba(75, 192, 192, 1)'], | ||
['rgba(153, 102, 255, 0.2)', 'rgba(153, 102, 255, 1)'], | ||
['rgba(255, 159, 64, 0.2)', 'rgba(255, 159, 64, 1)'], | ||
['rgba(20, 20, 20, 0.2)', 'rgba(20, 20, 20, 1)'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this come from some design guide? can this can abstracted for unlimited colors? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current Colour scheme looks nice and can well be used, I would not abstract or generalize it: devs should change if they prefer other colour coding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems the original colors come from https://www.chartjs.org/docs/3.9.1/#creating-a-chart example /wo any specific design guide/logic |
||
]; | ||
|
||
/** @var array<string, mixed> Options for chart.js widget */ | ||
|
@@ -115,9 +120,18 @@ public function setOptions(array $options) | |
* This component will automatically figure out name of the chart, | ||
* series titles based on column captions etc. | ||
* | ||
* Example for bar chart with two side-by side bars per category, and one of them stacked: | ||
* | ||
* $chart->setModel( | ||
* $model, | ||
* ['month', 'turnover_month_shoes', 'turnover_month_shirts', 'turnover_month_trousers', 'turnover_month_total_last_year'], | ||
* [1, 1, 1, 2] // 1 => shoes+shirts+trousers, 2 => total last year | ||
* ); | ||
* | ||
* @param array<int, string> $columns | ||
* @param array<int, mixed> $stacks | ||
*/ | ||
public function setModel(Model $model, array $columns = []): void | ||
public function setModel(Model $model, array $columns = [], array $stacks = []): void | ||
{ | ||
if ($columns === []) { | ||
throw new Exception('Second argument must be specified to Chart::setModel()'); | ||
|
@@ -130,10 +144,11 @@ public function setModel(Model $model, array $columns = []): void | |
if ($key === 0) { | ||
$titleColumn = $column; | ||
|
||
continue; // skipping labels | ||
continue; // skipping label column | ||
} | ||
|
||
$colors = array_shift($this->niceColors); | ||
$stack = array_shift($stacks); | ||
|
||
$this->datasets[$column] = [ | ||
'label' => $model->getField($column)->getCaption(), | ||
|
@@ -142,6 +157,14 @@ public function setModel(Model $model, array $columns = []): void | |
'borderWidth' => 1, | ||
'data' => [], | ||
]; | ||
|
||
if ($stack !== null) { | ||
$this->datasets[$column]['stack'] = $stack; | ||
} | ||
} | ||
|
||
if ($stacks !== []) { | ||
$this->setOptions(['scales' => ['yAxes' => [0 => ['stacked' => true]], 'xAxes' => [0 => ['stacked' => true]]]]); | ||
} | ||
|
||
// prepopulate data-sets | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Chart; | ||
|
||
class LineChart extends Chart | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This complicates any extension in user projects... Not sure if different chart type that does not need additional modifications needs a custom class. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There can be modifications in future is someone will be ready to contribute them :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am fine with this for now. |
||
{ | ||
public $type = 'line'; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to upgrade to the latest lib now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do