-
-
Notifications
You must be signed in to change notification settings - Fork 112
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
feat: allow add plugins to the Chart and Dataset components #1638
Conversation
Features
Bug Fixes
ContributorsCommit-Lint commandsYou can trigger Commit-Lint actions by commenting on this PR:
|
package.json
Outdated
@@ -33,6 +33,7 @@ | |||
"dependencies": { | |||
"autosize": "^4.0.2", | |||
"chart.js": "2.7.3", | |||
"chartjs-plugin-datalabels": "^0.7.0", |
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.
this dependency should be only for examples purposes, move it to devDependency
src/components/Chart/index.d.ts
Outdated
@@ -17,6 +17,8 @@ export interface ChartProps extends BaseProps { | |||
disableXAxisTickLabels?: boolean; | |||
disableYAxisTickLabels?: boolean; | |||
maintainAspectRatio?: boolean; | |||
plugins?: Array<any>; | |||
datalabels?: object; |
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.
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>
src/components/Chart/index.js
Outdated
const { type, labels, ...conditions } = this.props; | ||
const { type, labels, plugins, ...conditions } = this.props; | ||
if (plugins) { | ||
ChartJS.plugins.unregister(plugins); |
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.
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
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.
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.
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.
all global plugins can be used inline
{
plugins: [ datalabels, deferred ],
}
ChartJS.plugins.clear();
is the way to keep ChartJS plugins isolated
src/components/Chart/index.js
Outdated
@@ -55,6 +59,7 @@ export class Chart extends Component { | |||
labels, | |||
datasets: this.datasets, | |||
}, | |||
plugins: plugins || null, | |||
options: resolveOptions({ type, ...conditions }), |
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.
you should add the plugins variable here, because with the line
const { type, labels, plugins, ...conditions } = this.props;
conditions no longer contains plugins
src/components/Chart/readme.md
Outdated
|
||
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, | ||
}; |
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.
check lint rules
src/components/Chart/index.js
Outdated
@@ -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(); |
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.
this must always be done in order to isolate the component so that the global plugins do not affect it.
src/components/Chart/index.d.ts
Outdated
pluginsChartConf?: object; | ||
pluginsDatasetConf?: object; |
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.
remove this props from the API. the component has to be like this:
const keys = plugins.map(plugin => plugin.id); | ||
keys.forEach(key => { | ||
if (rest[key]) { | ||
pluginsConf[key] = rest[key]; | ||
} | ||
}); |
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.
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); |
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.
unregisterGlobalP(L)ugins
=> unregisterGlobalPlugins
@rgah2107 @yvmunayev it looks good to me, please change the typo in other PR, I will merge it |
fix: #1626
Changes proposed in this PR:
allow add plugins to the Chart and Dataset components
I have followed (at least) the PR section of the contributing guide.
@nexxtway/react-rainbow