|
1 | 1 | // @flow |
2 | 2 |
|
3 | 3 | import React from "react"; |
| 4 | +import { connect } from "unistore/react"; |
| 5 | +import gradstop from "gradstop"; |
4 | 6 | import DataViewer from "./DataViewer"; |
5 | 7 | import type { Props as DataViewerProps } from "./DataViewer"; |
6 | | -import { connect } from "unistore/react"; |
7 | 8 |
|
8 | 9 | type ColorScalePoint = |
9 | 10 | | { color: string, type: "number", value: number } |
10 | 11 | | { color: string, type: "percent", value: number } |
11 | 12 | | { color: string, type: "percentile", value: number }; |
12 | 13 |
|
| 14 | +type MinPoint = ColorScalePoint | { type: "minimum", color: string }; |
| 15 | +type MidPoint = ColorScalePoint | null; |
| 16 | +type MaxPoint = ColorScalePoint | { type: "maximum", color: string }; |
| 17 | + |
13 | 18 | type Props = DataViewerProps & { |
14 | | - columnMaxValue: ColorScalePoint | { type: "minimum", color: string }, |
15 | | - columnMinValue: ColorScalePoint | { type: "maximum", color: string }, |
16 | | - maxPoint: {} |
| 19 | + columnMaxValue: number, |
| 20 | + columnMinValue: number, |
| 21 | + columnSize: number, |
| 22 | + minPoint: ColorScalePoint | { type: "minimum", color: string }, |
| 23 | + midPoint: ColorScalePoint, |
| 24 | + maxPoint: ColorScalePoint | { type: "maximum", color: string } |
| 25 | +}; |
| 26 | + |
| 27 | +const resolveColor = (props: Props): ?string => { |
| 28 | + const { |
| 29 | + columnMaxValue, |
| 30 | + columnMinValue, |
| 31 | + minPoint, |
| 32 | + midPoint, |
| 33 | + maxPoint, |
| 34 | + columnSize, |
| 35 | + cell |
| 36 | + } = props; |
| 37 | + if (!cell || !cell.value) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + const { value } = cell; |
| 41 | + /** @todo handle midPoint */ |
| 42 | + const gradient = gradstop({ |
| 43 | + stops: columnSize, |
| 44 | + inputFormat: "hex", |
| 45 | + colorArray: midPoint |
| 46 | + ? [minPoint.color, midPoint.color, maxPoint.color] |
| 47 | + : [minPoint.color, maxPoint.color] |
| 48 | + }); |
| 49 | + const relativeValue = |
| 50 | + (value - columnMinValue) / (columnMaxValue - columnMinValue); |
| 51 | + const index = Math.floor(relativeValue * (columnSize - 1)); |
| 52 | + return gradient[index]; |
17 | 53 | }; |
18 | 54 |
|
19 | 55 | const ColorScaleDataViewer = (props: Props) => { |
20 | | - console.log(props.cell.value, props.columnMaxValue, props.columnMinValue); |
21 | | - return <DataViewer {...props} />; |
| 56 | + const color = resolveColor(props); |
| 57 | + return ( |
| 58 | + <div style={{ backgroundColor: color }}> |
| 59 | + <DataViewer {...props} /> |
| 60 | + </div> |
| 61 | + ); |
22 | 62 | }; |
23 | 63 |
|
24 | 64 | const mapStateToProps = (state, props) => { |
25 | | - let columnMaxValue: number = 0; |
26 | | - let columnMinValue: number = 0; |
| 65 | + let columnMaxValue: number; |
| 66 | + let columnMinValue: number; |
27 | 67 | for (const row of state.data) { |
28 | 68 | const cell = row[props.column]; |
29 | | - columnMaxValue = Math.max(cell.value, columnMaxValue); |
30 | | - columnMinValue = Math.min(cell.value, columnMinValue); |
| 69 | + const value = cell && cell.value; |
| 70 | + if (!value) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + columnMaxValue = columnMaxValue ? Math.max(value, columnMaxValue) : value; |
| 74 | + columnMinValue = columnMinValue ? Math.min(value, columnMinValue) : value; |
31 | 75 | } |
32 | | - return { columnMaxValue, columnMinValue }; |
| 76 | + return { columnMaxValue, columnMinValue, columnSize: state.data.length }; |
| 77 | +}; |
| 78 | + |
| 79 | +const createColorScaleDataViewer = ({ |
| 80 | + minPoint, |
| 81 | + maxPoint, |
| 82 | + midPoint = null |
| 83 | +}: { |
| 84 | + minPoint: MinPoint, |
| 85 | + maxPoint: MaxPoint, |
| 86 | + midPoint: MidPoint |
| 87 | +}) => { |
| 88 | + const BoundScaleDataViewer = props => { |
| 89 | + return ( |
| 90 | + <ColorScaleDataViewer |
| 91 | + {...props} |
| 92 | + minPoint={minPoint} |
| 93 | + midPoint={midPoint} |
| 94 | + maxPoint={maxPoint} |
| 95 | + /> |
| 96 | + ); |
| 97 | + }; |
| 98 | + return connect(mapStateToProps)(BoundScaleDataViewer); |
33 | 99 | }; |
34 | 100 |
|
35 | | -export default connect(mapStateToProps)(ColorScaleDataViewer); |
| 101 | +export default createColorScaleDataViewer; |
0 commit comments