-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchart.tsx
169 lines (157 loc) · 4.9 KB
/
chart.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Box } from '@mui/material';
import { ChartData, ChartOptions, DefaultDataPoint } from 'chart.js';
import { GeoChartOptions, GeoChartType, GeoChartData} from './chart-types';
import { ChartDoughnut } from './charts/chart-doughnut';
import { ChartBarsVertical } from './charts/chart-bars-vertical';
import { ChartPie } from './charts/chart-pie';
import { ChartLine } from './charts/chart-line';
import styles from './chart.module.css';
/**
* Main props for the Chart
*/
export interface TypeChartChartProps<TType extends GeoChartType> {
style?: any;
data?: GeoChartData<TType>;
options?: GeoChartOptions;
redraw?: boolean;
handleSliderXChanged?: (value: number | number[]) => void;
handleSliderYChanged?: (value: number | number[]) => void;
}
/**
* Create a customized Chart UI
*
* @param {TypeChartChartProps} props the properties passed to the Chart element
* @returns {JSX.Element} the created Chart element
*/
export function Chart(props: TypeChartChartProps<GeoChartType>): JSX.Element {
// Fetch cgpv
const w = window as any;
const { cgpv } = w;
const { Slider } = cgpv.ui.elements;
const { style, data, options, redraw } = props;
const _handleSliderXChange = (value: number | number[]) => {
// If callback set
if (props.handleSliderXChanged) {
props.handleSliderXChanged!(value);
}
};
const _handleSliderYChange = (value: number | number[]) => {
// If callback set
if (props.handleSliderYChanged) {
props.handleSliderYChanged!(value);
}
};
/**
* Renders the Chart JSX.Element itself using Line as default
* @returns The Chart JSX.Element itself using Line as default
*/
const renderChart = (): JSX.Element => {
// Depending on the type of chart
switch (options!.geochart.chart) {
case 'bar':
// Vertical Bars Chart
return (
<ChartBarsVertical
type="bar"
data={data as ChartData<'bar', DefaultDataPoint<'bar'>, string>}
options={options as ChartOptions<'bar'>}
redraw={redraw}
/>
);
case 'pie':
// Pie Chart
return (
<ChartPie
type="pie"
data={data as ChartData<'pie', DefaultDataPoint<'pie'>, string>}
options={options as ChartOptions<'pie'>}
redraw={redraw}
/>
);
case 'doughnut':
// Doughnut Chart
return (
<ChartDoughnut
type="doughnut"
data={data as ChartData<'doughnut', number[], string>}
options={options as ChartOptions<'doughnut'>}
redraw={redraw}
/>
);
default:
// Line Chart is default
return (
<ChartLine
type="line"
data={data as ChartData<'line', DefaultDataPoint<'line'>, string>}
options={options as ChartOptions<'line'>}
redraw={redraw}
/>
);
}
};
/**
* Renders the X Chart Slider JSX.Element or an empty div
* @returns The X Chart Slider JSX.Element or an empty div
*/
const renderXSlider = () : JSX.Element => {
const { xSlider } = options!.geochart;
if (xSlider?.display) {
return (
<Box sx={{ height: '100%' }}>
<Slider
sliderId="sliderHorizontal"
min={xSlider.min || 0}
max={xSlider.max || 100}
value={xSlider.value || 0}
track={xSlider.track || false}
customOnChange={_handleSliderXChange}
/>
</Box>
);
}
// None
return <div />;
}
/**
* Renders the Y Chart Slider JSX.Element or an empty div
* @returns The Y Chart Slider JSX.Element or an empty div
*/
const renderYSlider = (): JSX.Element => {
const { ySlider } = options!.geochart;
if (ySlider?.display) {
return (
<Box sx={{ height: '100%' }}>
<Slider
sliderId="sliderVertical"
min={ySlider.min || 0}
max={ySlider.max || 100}
value={ySlider.value || 0}
track={ySlider.track || false}
orientation="vertical"
customOnChange={_handleSliderYChange}
/>
</Box>
);
}
return <div />;
};
/**
* Renders the whole Chart container JSX.Element or an empty div
* @returns The whole Chart container JSX.Element or an empty div
*/
const renderChartContainer = (): JSX.Element => {
if (options && options.geochart && options.geochart.chart) {
return (
<div style={style} className={styles.chartContainer}>
<div className={styles.chartContainerGrid1}>{renderChart()}</div>
<div className={styles.chartContainerGrid2}>{renderYSlider()}</div>
<div className={styles.chartContainerGrid3}>{renderXSlider()}</div>
<div className={styles.chartContainerGrid4} />
</div>
);
}
return <div />;
}
return renderChartContainer();
}