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

feat: allow add plugins to the Chart and Dataset components #1638

Conversation

rgah2107
Copy link
Collaborator

fix: #1626

Changes proposed in this PR:

@nexxtway/react-rainbow

@commit-lint
Copy link

commit-lint bot commented Jun 17, 2020

Features

  • allow add plugins to the Chart and Dataset components (a7845c6)

Bug Fixes

  • modify component according to the last comments (2b9d212)
  • modify solution to unregister global plugins (b885ae8)
  • modify test (971daca)
  • modify component according to the established api (47e5961)
  • improve helper (6112680)

Contributors

@rgah2107

Commit-Lint commands

You can trigger Commit-Lint actions by commenting on this PR:

  • @Commit-Lint merge patch will merge dependabot PR on "patch" versions (X.X.Y - Y change)
  • @Commit-Lint merge minor will merge dependabot PR on "minor" versions (X.Y.Y - Y change)
  • @Commit-Lint merge major will merge dependabot PR on "major" versions (Y.Y.Y - Y change)
  • @Commit-Lint merge disable will desactivate merge dependabot PR
  • @Commit-Lint review will approve dependabot PR
  • @Commit-Lint stop review will stop approve dependabot PR

package.json Outdated
@@ -33,6 +33,7 @@
"dependencies": {
"autosize": "^4.0.2",
"chart.js": "2.7.3",
"chartjs-plugin-datalabels": "^0.7.0",
Copy link
Collaborator

Choose a reason for hiding this comment

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

this dependency should be only for examples purposes, move it to devDependency

@@ -17,6 +17,8 @@ export interface ChartProps extends BaseProps {
disableXAxisTickLabels?: boolean;
disableYAxisTickLabels?: boolean;
maintainAspectRatio?: boolean;
plugins?: Array<any>;
datalabels?: object;
Copy link
Collaborator

Choose a reason for hiding this comment

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

the plugins feature is for any possible plugin, right? which means there will be more props depending on the plugins passed in plugins prop, I would say we need to find some dynamic manner of defining those props.

i.e. a plugin named foo will receive it's config data through foo prop

import foo from 'chart-plugin-foo';

<Chart plugins={{ foo }} foo={fooConfigAtChartLevel}>
    <Dataset foo={fooConfigAtDatasetLevel}>
</Chart>

const { type, labels, ...conditions } = this.props;
const { type, labels, plugins, ...conditions } = this.props;
if (plugins) {
ChartJS.plugins.unregister(plugins);
Copy link
Collaborator

Choose a reason for hiding this comment

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

why we have to unregister the plugins?? I know the labels one needs to be unregistered ... but I guess that's not the case for all the plugins, right?

Again this feature should a mechanism to inject any possible chart plugin

Copy link
Contributor

Choose a reason for hiding this comment

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

I was looking at this.

The problem of globally registered plugins is solved like this:

ChartJS.plugins.clear();

this must be done in everything always, because when it is done

import datalabels from 'chartjs-plugin-datalabels';
import deferred from 'chartjs-plugin-deferred';

All ChartJS already have that plugin registered.

Copy link
Contributor

@yvmunayev yvmunayev Jul 4, 2020

Choose a reason for hiding this comment

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

all global plugins can be used inline

{
     plugins: [ datalabels, deferred ],
}

ChartJS.plugins.clear();

is the way to keep ChartJS plugins isolated

@rgah2107 rgah2107 requested a review from reiniergs June 18, 2020 11:48
@@ -55,6 +59,7 @@ export class Chart extends Component {
labels,
datasets: this.datasets,
},
plugins: plugins || null,
options: resolveOptions({ type, ...conditions }),
Copy link
Contributor

Choose a reason for hiding this comment

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

you should add the plugins variable here, because with the line

const { type, labels, plugins, ...conditions } = this.props;

conditions no longer contains plugins

Comment on lines 903 to 916

const datalabelsLevelChart = {color: '#fe4849', anchor: 'end', align: 'top'};
const deferredChart = {delay: 500};
const pluginsChartConf = {datalabels: datalabelsLevelChart, deferred: deferredChart};

const datalabelsLebelDataSet_0 = {color: '#ff6837'};
const datalabelsLebelDataSet_2 = {color: '#01b6f5'};
const pluginsDatasetConf_0 = {datalabels: datalabelsLebelDataSet_0};
const pluginsDatasetConf_2 = {datalabels: datalabelsLebelDataSet_2};

const containerStyles = {
maxWidth: 600,
};
Copy link
Contributor

@yvmunayev yvmunayev Jul 4, 2020

Choose a reason for hiding this comment

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

check lint rules

@rgah2107 rgah2107 requested a review from yvmunayev July 5, 2020 03:48
@@ -49,7 +48,7 @@ export class Chart extends Component {
renderChart() {
const { type, labels, plugins, ...conditions } = this.props;
if (plugins) {
ChartJS.plugins.unregister(getGlobalsPlugins(plugins));
ChartJS.plugins.clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

this must always be done in order to isolate the component so that the global plugins do not affect it.

Comment on lines 21 to 22
pluginsChartConf?: object;
pluginsDatasetConf?: object;
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this props from the API. the component has to be like this:

#1626

Comment on lines 3 to 8
const keys = plugins.map(plugin => plugin.id);
keys.forEach(key => {
if (rest[key]) {
pluginsConf[key] = rest[key];
}
});
Copy link
Contributor

Choose a reason for hiding this comment

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

that's easier

const pluginsConf = {};
    plugins.forEach(plugin => {
        const { id } = plugin;
        if (rest[id]) {
            pluginsConf[id] = rest[id];
        }
    });

@@ -46,7 +47,8 @@ export class Chart extends Component {
}

renderChart() {
const { type, labels, ...conditions } = this.props;
unregisterGlobalPLugins(ChartJS);
Copy link
Contributor

Choose a reason for hiding this comment

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

unregisterGlobalP(L)ugins => unregisterGlobalPlugins

@maxxgreene
Copy link
Contributor

@rgah2107 @yvmunayev it looks good to me, please change the typo in other PR, I will merge it

@maxxgreene maxxgreene merged commit 58b660c into nexxtway:master Jul 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: allow add plugins to the Chart and Dataset components
4 participants