-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[charts] Introduce plugins system #13367
Changes from 4 commits
cd76f19
b181b0c
e98da3d
beab537
e4da84c
7dd85de
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ChartsPluginType } from '../models/plugin'; | ||
import { getExtremumX, getExtremumY } from './extremums'; | ||
import formatter from './formatter'; | ||
import getColor from './getColor'; | ||
|
||
export const plugin: ChartsPluginType<'bar'> = { | ||
seriesType: 'bar', | ||
seriesFormatter: formatter, | ||
colorProcessor: getColor, | ||
xExtremumGetter: getExtremumX, | ||
yExtremumGetter: getExtremumY, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { plugin as barPlugin } from '../BarChart/plugin'; | ||
import { plugin as scatterPlugin } from '../ScatterChart/plugin'; | ||
import { plugin as linePlugin } from '../LineChart/plugin'; | ||
import { plugin as piePlugin } from '../PieChart/plugin'; | ||
|
||
export const defaultPlugins = [barPlugin, scatterPlugin, linePlugin, piePlugin]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as React from 'react'; | ||
import { ChartsPluginTypes, ColorProcessorsConfig } from '../models'; | ||
import { ChartSeriesType } from '../models/seriesType/config'; | ||
import { ExtremumGettersConfig } from '../context/CartesianContextProvider'; | ||
import { SeriesFormatterConfig } from '../context/SeriesContextProvider'; | ||
import { defaultPlugins } from './defaultPlugins'; | ||
|
||
export function usePluginsMerge<T extends ChartSeriesType>(plugins?: ChartsPluginTypes<T>[]) { | ||
const defaultizedPlugins = plugins ?? defaultPlugins; | ||
JCQuintas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return React.useMemo(() => { | ||
const seriesFormatters: SeriesFormatterConfig<ChartSeriesType> = {}; | ||
const colorProcessors: ColorProcessorsConfig<ChartSeriesType> = {}; | ||
const xExtremumGetters: ExtremumGettersConfig<ChartSeriesType> = {}; | ||
const yExtremumGetters: ExtremumGettersConfig<ChartSeriesType> = {}; | ||
|
||
for (let i = 0; i < defaultizedPlugins.length; i += 1) { | ||
const plugin = defaultizedPlugins[i]; | ||
|
||
// To remove those any we will need to solve this union discrimination issue: | ||
// https://www.typescriptlang.org/play/?#code/FDAuE8AcFMAIDkCuBbARtATgYQPYDsAzASwHNYBeWAb2FlgGsi8ATALlgHI8V0MOBuWrBwwMAQ1A4M7ABQAPdtzSYAlBQB8sJb0EBfEBBiwAyqAxMSuQqQrUhjFuw4BnMxYFCRmCVNkLYruZ4JGrkmoEWeiAAxviuWqhWxCTsSMrY+Mm2VAxMbLAARNqYBQA0wqI+0rByGrAATLAAVDWw+rF48YFJpOymQZaZNpQ5DvkFEcFlFd6S1bVhsAAG9S0AJFRyukttMXGgsB3JzrYA2niJQyTl3VcAugZQcADylXPOALJikJAW2ULFDAAflSPEwPRIpw4XnEcw4d1KQkmJBBJjcwQhUJhVXhiN0gmAHXi2LmXx+FnYr1mUk+31+wWy+JABCksBkABtoAcjjYcARDldnGoaCA6AB6MWwADqUnoJxw9FgRH5AHc4L9ooroGJogALQ5iZxwPJEABuRGYiDE7PASJVRFAerZPJIADoxsKhHRooa4FwwXxWF66DNYVIyfTIS73Xk7rZoySpIIQyHUBhtfRkyGfUbOMiOEGU3RExgIxZTtGxnHKAm3kng8xoAQxIh2aBC0W0xms-pvftqLkWOUS2141chBLYABJDimuB4HBKxtiWBiVA4RAHXU4FWwSSwTkHAAqxlgiBYmFcYhYAusbrGq5vtepGFX6YPTHo0GYnjrpbp5ZVrYJZ6EAA | ||
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. furthest I could get quickly is this, it solves the assignment type issue type OperatorsMapping<T extends NumberConfig | StringConfig= NumberConfig | StringConfig> = {
[k in T['kind']]?: T['operator']
} 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'm not sure it's feasible, but does not matter a lot. As long as the type constraint in the input/output of the hook are ok, it should not cause any issue |
||
seriesFormatters[plugin.seriesType] = plugin.seriesFormatter as any; | ||
|
||
colorProcessors[plugin.seriesType] = plugin.colorProcessor as any; | ||
|
||
if (plugin.xExtremumGetter) { | ||
xExtremumGetters[plugin.seriesType] = plugin.xExtremumGetter as any; | ||
} | ||
|
||
if (plugin.yExtremumGetter) { | ||
yExtremumGetters[plugin.seriesType] = plugin.yExtremumGetter as any; | ||
} | ||
} | ||
return { | ||
seriesFormatters, | ||
colorProcessors, | ||
xExtremumGetters, | ||
yExtremumGetters, | ||
}; | ||
}, [defaultizedPlugins]); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ChartsPluginType } from '../models/plugin'; | ||
import { getExtremumX, getExtremumY } from './extremums'; | ||
import formatter from './formatter'; | ||
import getColor from './getColor'; | ||
|
||
export const plugin: ChartsPluginType<'line'> = { | ||
seriesType: 'line', | ||
colorProcessor: getColor, | ||
seriesFormatter: formatter, | ||
xExtremumGetter: getExtremumX, | ||
yExtremumGetter: getExtremumY, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ChartsPluginType } from '../models/plugin'; | ||
import formatter from './formatter'; | ||
import getColor from './getColor'; | ||
|
||
export const plugin: ChartsPluginType<'pie'> = { | ||
seriesType: 'pie', | ||
colorProcessor: getColor, | ||
seriesFormatter: formatter, | ||
}; |
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 proptype seem overly verbose 😅
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.
Yes, could be removed :)
I think I did it in the main PR, but forgot to extract it 👍