diff --git a/examples/topics/aggregations/power_plant_bar_chart.php b/examples/topics/aggregations/power_plant_bar_chart.php index b2430bf90..dab1d4815 100644 --- a/examples/topics/aggregations/power_plant_bar_chart.php +++ b/examples/topics/aggregations/power_plant_bar_chart.php @@ -46,7 +46,8 @@ ->withEntry('consumption', concat(ref('consumption'), lit('%'))) ->write( ChartJS::chart( - ChartJS::bar(label: ref('date'), datasets: [ref('production_kwh_avg'), ref('consumption_kwh_avg')]), + ChartJS::bar(label: ref('date'), datasets: [ref('production_kwh_avg'), ref('consumption_kwh_avg')]) + ->setOptions(['indexAxis' => 'y']), output: __FLOW_OUTPUT__ . '/power_plant_bar_chart.html' ) ); diff --git a/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/BarChart.php b/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/BarChart.php index 83b04f4a1..e1f51126f 100644 --- a/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/BarChart.php +++ b/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/BarChart.php @@ -6,7 +6,6 @@ use Flow\ETL\Adapter\ChartJS\Chart; use Flow\ETL\Exception\InvalidArgumentException; -use Flow\ETL\Exception\RuntimeException; use Flow\ETL\Row\EntryReference; use Flow\ETL\Rows; @@ -23,6 +22,8 @@ final class BarChart implements Chart 'datasets' => [], ]; + private array $datasetOptions = []; + private array $options = []; /** @@ -40,7 +41,7 @@ public function __construct( } foreach ($this->datasets as $dataset) { - $this->options[$dataset->name()] = []; + $this->datasetOptions[$dataset->name()] = []; } } @@ -64,14 +65,14 @@ public function collect(Rows $rows) : void public function data() : array { - return [ + $data = [ 'type' => 'bar', 'data' => [ 'labels' => $this->data['labels'], 'datasets' => \array_values(\array_map( function (array $dataset) : array { /** @var array $options */ - $options = $this->options[$dataset['label']] ?? []; + $options = $this->datasetOptions[$dataset['label']] ?? []; return \array_merge($dataset, $options); }, @@ -79,6 +80,12 @@ function (array $dataset) : array { )), ], ]; + + if ($this->options) { + $data['options'] = $this->options; + } + + return $data; } /** @@ -86,17 +93,19 @@ function (array $dataset) : array { */ public function setDatasetOptions(EntryReference $dataset, array $options) : self { - if (!\array_key_exists($dataset->name(), $this->options)) { + if (!\array_key_exists($dataset->name(), $this->datasetOptions)) { throw new InvalidArgumentException(\sprintf('Dataset "%s" does not exists', $dataset->name())); } - $this->options[$dataset->name()] = $options; + $this->datasetOptions[$dataset->name()] = $options; return $this; } public function setOptions(array $options) : self { - throw new RuntimeException('Please use setDatasetOptions while using BarChart'); + $this->options = $options; + + return $this; } } diff --git a/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/LineChart.php b/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/LineChart.php index ffcf40689..a66b0ddd5 100644 --- a/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/LineChart.php +++ b/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/LineChart.php @@ -6,7 +6,6 @@ use Flow\ETL\Adapter\ChartJS\Chart; use Flow\ETL\Exception\InvalidArgumentException; -use Flow\ETL\Exception\RuntimeException; use Flow\ETL\Row\EntryReference; use Flow\ETL\Rows; @@ -23,6 +22,8 @@ final class LineChart implements Chart 'datasets' => [], ]; + private array $datasetOptions = []; + private array $options = []; /** @@ -40,7 +41,7 @@ public function __construct( } foreach ($this->datasets as $dataset) { - $this->options[$dataset->name()] = []; + $this->datasetOptions[$dataset->name()] = []; } } @@ -64,14 +65,14 @@ public function collect(Rows $rows) : void public function data() : array { - return [ + $data = [ 'type' => 'line', 'data' => [ 'labels' => $this->data['labels'], 'datasets' => \array_values(\array_map( function (array $dataset) : array { /** @var array $options */ - $options = $this->options[$dataset['label']] ?? []; + $options = $this->datasetOptions[$dataset['label']] ?? []; return \array_merge($dataset, $options); }, @@ -79,6 +80,12 @@ function (array $dataset) : array { )), ], ]; + + if ($this->options) { + $data['options'] = $this->options; + } + + return $data; } /** @@ -86,17 +93,19 @@ function (array $dataset) : array { */ public function setDatasetOptions(EntryReference $dataset, array $options) : self { - if (!\array_key_exists($dataset->name(), $this->options)) { + if (!\array_key_exists($dataset->name(), $this->datasetOptions)) { throw new InvalidArgumentException(\sprintf('Dataset "%s" does not exists', $dataset->name())); } - $this->options[$dataset->name()] = $options; + $this->datasetOptions[$dataset->name()] = $options; return $this; } public function setOptions(array $options) : self { - throw new RuntimeException('Please use setDatasetOptions while using LineChart'); + $this->options = $options; + + return $this; } } diff --git a/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/PieChart.php b/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/PieChart.php index 38fbe8714..dcf5ef5ef 100644 --- a/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/PieChart.php +++ b/src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/PieChart.php @@ -6,7 +6,6 @@ use Flow\ETL\Adapter\ChartJS\Chart; use Flow\ETL\Exception\InvalidArgumentException; -use Flow\ETL\Exception\RuntimeException; use Flow\ETL\Row\EntryReference; use Flow\ETL\Rows; @@ -15,7 +14,7 @@ final class PieChart implements Chart /** * @var array{ * labels: array, - * datasets: array + * datasets: array * } */ private array $data = [ @@ -26,6 +25,8 @@ final class PieChart implements Chart /** * @var array */ + private array $datasetOptions = []; + private array $options = []; /** @@ -34,6 +35,7 @@ final class PieChart implements Chart * @throws InvalidArgumentException */ public function __construct( + private readonly EntryReference $label, private readonly array $datasets, ) { if (!\count($this->datasets)) { @@ -41,7 +43,7 @@ public function __construct( } foreach ($this->datasets as $dataset) { - $this->options[$dataset->name()] = []; + $this->datasetOptions[$dataset->name()] = []; $this->data['labels'][] = $dataset->name(); } } @@ -53,9 +55,11 @@ public function collect(Rows $rows) : void if (!\array_key_exists('pie', $this->data['datasets'])) { $this->data['datasets']['pie'] = [ 'data' => [$row->valueOf($dataset)], + 'label' => (string) $row->valueOf($this->label), ]; } else { $this->data['datasets']['pie']['data'][] = $row->valueOf($dataset); + $this->data['datasets']['pie']['label'] = (string) $row->valueOf($this->label); } } } @@ -64,9 +68,9 @@ public function collect(Rows $rows) : void public function data() : array { /** @var array $options */ - $options = $this->options['pie'] ?? []; + $options = $this->datasetOptions['pie'] ?? []; - return [ + $data = [ 'type' => 'pie', 'data' => [ 'labels' => $this->data['labels'], @@ -76,6 +80,12 @@ public function data() : array )), ], ]; + + if ($this->options) { + $data['options'] = $this->options; + } + + return $data; } /** @@ -83,12 +93,18 @@ public function data() : array */ public function setDatasetOptions(EntryReference $dataset, array $options) : self { - throw new RuntimeException('Please use setOptions while using PieChart'); + if (!\array_key_exists($dataset->name(), $this->datasetOptions)) { + throw new InvalidArgumentException(\sprintf('Dataset "%s" does not exists', $dataset->name())); + } + + $this->datasetOptions[$dataset->name()] = $options; + + return $this; } public function setOptions(array $options) : self { - $this->options['pie'] = $options; + $this->options = $options; return $this; } diff --git a/src/adapter/etl-adapter-chartjs/src/Flow/ETL/DSL/ChartJS.php b/src/adapter/etl-adapter-chartjs/src/Flow/ETL/DSL/ChartJS.php index c29019e21..e6c71b08e 100644 --- a/src/adapter/etl-adapter-chartjs/src/Flow/ETL/DSL/ChartJS.php +++ b/src/adapter/etl-adapter-chartjs/src/Flow/ETL/DSL/ChartJS.php @@ -55,8 +55,8 @@ final public static function line(EntryReference $label, array $datasets) : Char * * @throws InvalidArgumentException */ - final public static function pie(array $datasets) : Chart + final public static function pie(EntryReference $label, array $datasets) : Chart { - return new Chart\PieChart($datasets); + return new Chart\PieChart($label, $datasets); } } diff --git a/src/adapter/etl-adapter-chartjs/tests/Flow/ETL/Adapter/ChartJS/Tests/Integration/ChartJSLoaderTest.php b/src/adapter/etl-adapter-chartjs/tests/Flow/ETL/Adapter/ChartJS/Tests/Integration/ChartJSLoaderTest.php index d1888118a..ca7bc1f18 100644 --- a/src/adapter/etl-adapter-chartjs/tests/Flow/ETL/Adapter/ChartJS/Tests/Integration/ChartJSLoaderTest.php +++ b/src/adapter/etl-adapter-chartjs/tests/Flow/ETL/Adapter/ChartJS/Tests/Integration/ChartJSLoaderTest.php @@ -4,6 +4,7 @@ namespace Flow\ETL\Adapter\ChartJS\Tests\Integration; +use function Flow\ETL\DSL\first; use function Flow\ETL\DSL\lit; use function Flow\ETL\DSL\ref; use function Flow\ETL\DSL\sum; @@ -167,6 +168,7 @@ public function test_loading_data_to_pie_chart() : void ]; $chart = ChartJS::pie( + ref('Date'), [ ref('Revenue'), ref('CM'), @@ -182,6 +184,7 @@ public function test_loading_data_to_pie_chart() : void ->read(From::memory(new ArrayMemory($data))) ->withEntry('Profit', ref('Revenue')->minus(ref('CM'))->minus(ref('Ads Spends'))->minus(ref('Storage Costs'))->minus(ref('Shipping Costs'))->round(lit(2))) ->aggregate( + first(ref('Date')->as('Date')), sum(ref('Revenue')->as('Revenue')), sum(ref('CM')->as('CM')), sum(ref('Ads Spends')->as('Ads Spends')), @@ -212,10 +215,13 @@ public function test_loading_data_to_pie_chart() : void 10853, 4760.3099999999995, ], - 'label' => 'PnL', + 'label' => '2023-01-01', ], ], ], + 'options' => [ + 'label' => 'PnL', + ], ], $chart->data(), ); diff --git a/src/adapter/etl-adapter-chartjs/tests/Flow/ETL/Adapter/ChartJS/Tests/Integration/Output/pie_chart.html b/src/adapter/etl-adapter-chartjs/tests/Flow/ETL/Adapter/ChartJS/Tests/Integration/Output/pie_chart.html index d189bb541..30510b499 100644 --- a/src/adapter/etl-adapter-chartjs/tests/Flow/ETL/Adapter/ChartJS/Tests/Integration/Output/pie_chart.html +++ b/src/adapter/etl-adapter-chartjs/tests/Flow/ETL/Adapter/ChartJS/Tests/Integration/Output/pie_chart.html @@ -5,7 +5,7 @@