-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acea8ee
commit 7bf37e8
Showing
10 changed files
with
406 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { AxisDefaultized } from '../models/axis'; | ||
import { DefaultizedBarSeriesType } from '../models/seriesType/bar'; | ||
|
||
export default function getColor( | ||
series: DefaultizedBarSeriesType, | ||
xAxis: AxisDefaultized, | ||
yAxis: AxisDefaultized, | ||
) { | ||
const verticalLayout = series.layout === 'vertical'; | ||
|
||
const bandColorScale = verticalLayout ? xAxis.colorScale : yAxis.colorScale; | ||
const valueColorScale = verticalLayout ? yAxis.colorScale : xAxis.colorScale; | ||
const bandValues = verticalLayout ? xAxis.data! : yAxis.data!; | ||
|
||
if (valueColorScale) { | ||
return (dataIndex: number) => { | ||
const value = series.data[dataIndex]; | ||
return value === null ? series.color : valueColorScale(value); | ||
}; | ||
} | ||
if (bandColorScale) { | ||
return (dataIndex: number) => { | ||
const value = bandValues[dataIndex]; | ||
return value === null ? series.color : bandColorScale(value); | ||
}; | ||
} | ||
return () => series.color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as React from 'react'; | ||
import { useTheme } from '@mui/material/styles'; | ||
import barGetColor from '../BarChart/getColor'; | ||
import scatterSeriesFormatter from '../ScatterChart/formatter'; | ||
import lineSeriesFormatter from '../LineChart/formatter'; | ||
import pieSeriesFormatter from '../PieChart/formatter'; | ||
import { AllSeriesType, DefaultizedSeriesType } from '../models/seriesType'; | ||
import { defaultizeColor } from '../internals/defaultizeColor'; | ||
import { | ||
ChartSeriesDefaultized, | ||
ChartSeriesType, | ||
DatasetType, | ||
FormatterParams, | ||
FormatterResult, | ||
} from '../models/seriesType/config'; | ||
import { ChartsColorPalette, blueberryTwilightPalette } from '../colorPalettes'; | ||
import { SeriesId } from '../models/seriesType/common'; | ||
import { CartesianContext } from './CartesianContextProvider'; | ||
|
||
export type ColorProviderProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export type ColorGetter = { series: Record<SeriesId, (itemIndex: number) => string> }; | ||
|
||
export const ColorsContext = React.createContext<ColorGetter>({ series: {} }); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
ColorsContext.displayName = 'ColorsContext'; | ||
} | ||
|
||
const seriesColorGetter: { | ||
[type in ChartSeriesType]?: ( | ||
series: DefaultizedSeriesType<type>, | ||
xAxis: AxisDefaultized, | ||
yAxis: AxisDefaultized, | ||
) => (itemIndex: number) => string; | ||
} = { | ||
bar: barGetColor, | ||
// scatter: scatterSeriesFormatter, | ||
// line: lineSeriesFormatter, | ||
// pie: pieSeriesFormatter, | ||
}; | ||
|
||
function ColorProvider(props: ColorProviderProps) { | ||
|
||
const series = React.useContext(SeriesContext)[itemData.type]!.series[ | ||
itemData.seriesId | ||
] as ChartSeriesDefaultized<T>; | ||
|
||
const axisData = React.useContext(CartesianContext); | ||
|
||
const { xAxis, yAxis, xAxisIds, yAxisIds } = axisData; | ||
const defaultXAxisId = xAxisIds[0]; | ||
const defaultYAxisId = yAxisIds[0]; | ||
|
||
const color = getColor( | ||
series, | ||
xAxis[series.xAxisKey ?? defaultXAxisId], | ||
yAxis[series.yAxisKey ?? defaultYAxisId], | ||
); | ||
|
||
const { series, dataset, colors = blueberryTwilightPalette, children } = props; | ||
|
||
const theme = useTheme(); | ||
|
||
const formattedSeries = React.useMemo( | ||
() => | ||
formatSeries( | ||
series, | ||
typeof colors === 'function' ? colors(theme.palette.mode) : colors, | ||
dataset as DatasetType<number>, | ||
), | ||
[series, colors, theme.palette.mode, dataset], | ||
); | ||
|
||
return <SeriesContext.Provider value={formattedSeries}>{children}</SeriesContext.Provider>; | ||
} | ||
|
||
export { ColorProvider }; |
Oops, something went wrong.