Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brosenberger/module-chartee
Magento Base Module to create chart.js graphs in frontend and adminhtml
https://github.com/brosenberger/module-chartee
Last synced: 22 days ago
JSON representation
Magento Base Module to create chart.js graphs in frontend and adminhtml
- Host: GitHub
- URL: https://github.com/brosenberger/module-chartee
- Owner: brosenberger
- License: mit
- Created: 2023-08-04T10:35:31.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-13T12:10:56.000Z (11 months ago)
- Last Synced: 2024-11-22T17:51:46.450Z (about 1 month ago)
- Language: JavaScript
- Size: 1.34 MB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Chartee - a Magento 2 chart-js module
This module should ease the way of creating powerful charts in Magento 2. It is based on the chart.js library (which itself is already used by Magento2 for its dashboard charts) and various extensions for additional chart types.
**Goals of this module:**
* Be independent of the version management of Magento and updates of its library dependencies (only the current 2.4.7-beta2 uses chart.js v4.4.0!) and use newer chart.js versions in older Magento2 installations
* Provide a simple way to create charts in Magento2 without much knowledge of the chart.js library itself
* Provide boilerplate codes to easily integrate any type of chart in various places within Magento
* Encourage everyone to create meaningful representations of data already available in Magento2, no stackholder would see otherwise as it is buried in the databases[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/brosenberger)
## Installation
```
composer require brocode/module-chartee
bin/magento module:enable BroCode_Chartee
bin/magento setup:upgrade
```## Features
### Supported chart types
**Bar chart**
Based on the base implementation from https://www.chartjs3.com/docs/chart/getting-started/![Bar chart](/docs/demo_barchart.png)
**Stacked bar chart**
A bar chart variation with the help of the chart.js plugin https://github.com/y-takey/chartjs-plugin-stacked100/
![Stacked bar chart](/docs/demo_stackedbarchart.png)**Polar chart**
A polar chart based on the sample from https://codepen.io/FranciscusAgnew/pen/LRGPYX
![Polar chart](/docs/demo_polarchart.png)**Doughnut chart**
A variation of the base doughnut chart, based on https://www.youtube.com/watch?v=YcRj52VovYQ
![Doughnut chart](/docs/demo_doughnutchart.png)**Gauge chart**
A variation of the doughnut chart, based on https://www.youtube.com/watch?v=VF6jd2Fv4bs&list=RDCMUCojXvfr41NqDxaPb9amu8-A&index=1
![Gauge chart](/docs/demo_gaugechart.png)**Boxplot chart**
*What is a Boxplot chart? See [Wikipedia](https://en.wikipedia.org/wiki/Box_plot) for more information.*
A boxplot chart, based on https://www.youtube.com/watch?v=2zEbeX0bPS8
![Boxplot chart](/docs/demo_boxplotchart.png)### Ready to use charts
**Customer Group Distribution**
The charts shows the distribution of registered customers between all customer groups. It also provides the possibility to download the shown data as CSV.
It can be found in the page ```Customers -> Customer Groups```:
![Customer Group Distribution](/docs/customergroup_distribution_chart.png)*Permissions:* only admin users with the permission ```BroCode_Chartee::chart_customer_group_distribution``` can see the chart.
![Customer Group Distribution Permissions](/docs/acl_configuration.png)*Configuration:* ```Stores -> Configuration -> Services -> Chartee Reports Configuration -> Customer Report Settings```
* the chart can be disabled via configuration
* the download filename can be changed via configuration
![Customer Group Distribution Configuration](/docs/customer_chart_configuration.png)### Adminhtml menu items
All menus can be found under ```Content -> BroCode Chartee```
![Adminhtml menu items](/docs/menu_items.png)**Chartbuilder Listing**
Shows all registered chart builders.
![Chartbuilder Listing](/docs/chartbuilder_listing.png)
Currently no further features are implemented. Possible features could be:
* Preview of generated chart.js configurations (TBD)**Demo Charts**
Listing of all current demo charts and links how they have been implemented on js side.
### Boilerplate code snipptes
#### ColumnBaseChart
Simple base class to order all added subblocks in a column layout. It dynamically sets the according needed columns, based if a chart is shown or not (which can change based on configuration or permissions). Additionally a section title added.Sample of a row with 2 columns and a section title:
````xml
section_title
First Section Title
...
...````
#### BaseCompositeChart
Base chart display class. It can be used within the frontend and adminhtml (a specialized subclass exists with the extension of checking backend permissions: BroCode\Chartee\Block\Adminhtml\BaseCompositeChartsBlock). It provides the following features:
* checks the visibility based on a configuration value
* adds the possibility to prepare a download link of the shown data (or more) as CSV file
* the download file name can be configured (otherwise it will be autogenerated as {current-date}_{md5-hash}.csv)
* the key to the data from the chartbuilder that should be downloadable
* checks the visibility based on a ACL permission (only the adminhtml-class)````xml
visibilityConfigPath
brocode_chartee_reports/customer/customer_group_distribution
permission
BroCode_Chartee::chart_customer_group_distribution
chartDataBuilder
brocode-customer-stacked-group
downloadNameConfigPath
brocode_chartee_reports/customer/customer_group_distribution_downloadfile
downloadDataKey
customer_groups_data
````
#### Chart Data Builder
Every chart needs a datasource, which is an implementation of \BroCode\Chartee\Api\ChartDataBuilderInterface. For every base chart a separate Databuilder is already implemented with default configurations for that type of chart and possibly additional methods for creating the needed configuration.
Every base chart has its default DataBuilder and an implementation example within \BroCode\Chartee\Model\DataBuilder\Demo.
**Implementation of a new Databuilder**
Extend the needed specific ChartDataBuilder, here a Sample of the DemoPolarChartDataBuilder:
````php
class DemoPolarChartDataBuilder extends PolarChartDataBuilder
{
public function build()
{
$this->setDataLabels(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])
->createDataSet()
->setLabel('Weekly Sales')
->setDataValues(12, 19, 3, 17, 28, 24, 7)
->setBackgroundColors("#2ecc71", "#3498db", "#95a5a6", "#9b59b6", "#f1c40f", "#e74c3c", "#34495e")
->build();return parent::build();
}
}
````To add downloadable data to be used, any data must be set separatelly to the chart builder class:
````php
public function build()
{
...
$this->setData('customer_groups_data', $groupData);
...
return parent::build();
}
````**Registration within the databuilder service**
The databuilder needs to be registered within a di.xml in order to use it as named builder within the Template-block class:
````xml
\BroCode\Chartee\Model\DataBuilder\Customer\StackedCustomerGroupDataBuilder
````
**Datafiltering**
the \BroCode\Chartee\Model\ChartRequestParameterService provides methods to gather all set filtering parameter that should be considered when building up the needed data.
Current following parameters are supported:
* Scope (ChartRequestParameterService::getStoreIds(), based on the backend store switcher Magento\Backend\Block\Store\Switcher)
* DateRange (ChartRequestParameterService::getStartDate() & ChartRequestParameterService::getEndDate(), null if not set)Hint for adding a store filter to your backend page:
````xml
0
1
1
````
Hint for adding a date range filter to your backend page:
````xml
````
Hint for using a date range picker in a custom element:
````html````
Hint for configuration possibility (see https://www.daterangepicker.com/#config), e.g. hiding the week numbers:````html
````
#### BroCode\Chartee\Block\Widget\BaseChart
These charts are used by the BaseCompositeChart class and can also be used as standalone charts too. The according demo page charts are implemented that way.
Steps to add a new chart:
* create a databuilder (see above)
* create a virtual type and define a specific view model for that databuilder
````xml
BroCode\Chartee\Model\DataBuilder\Demo\DemoBoxplotDataBuilder
````
* create a layout update and set a the defined view model
````xml
BroCode\Chartee\Block\ViewModel\DemoBoxplotChartConfiguration
````
## JS-Libraries, Chart.js version and extensions
* chart.js v4.4.0
* chart.js helpers (parts of it) v4.3.2
* chart.js boxplot plugin v4.2.4
* chart.js stacked100 plugin v1.5.2
* chart.js autocolors plugin 0.2.2
* custom implementation (based on samples from the internet, see above for reference) within graph.js
* doughnut labels line plugin
* gauge chart
* JQuery daterangepicker extension https://github.com/dangrossman/daterangepicker