aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/DataVizBox/ChartBox.tsx
blob: 42bd8a7f60d56eaacb2409b06b3a8096475e7f9a (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc } from '../../../../fields/Doc';
// import { Chart as ChartJS, CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend, InteractionItem, PointElement, LineElement } from 'chart.js';
// import { Bar, getDatasetAtEvent, getElementAtEvent, Line } from 'react-chartjs-2';
import { ChartJSOrUndefined } from 'react-chartjs-2/dist/types';
import { action, computed, observable } from 'mobx';
import { Cast, StrCast } from '../../../../fields/Types';
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts';
import { CategoricalChartState } from 'recharts/types/chart/generateCategoricalChart';

export interface ChartBoxProps {
    rootDoc: Doc;
    pairs: { x: number; y: number }[];
}

export interface ChartJsData {
    labels: number[];
    datasets: {
        label: string;
        data: number[];
        backgroundColor: string[];
    }[];
}

// ChartJS.register(CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend, PointElement, LineElement);

const primaryColor = 'rgba(53, 162, 235, 0.5)';
const selectedColor = 'rgba(255, 99, 132, 0.5)';

const data = [
    {
        name: 'Page A',
        uv: 4000,
        pv: 2400,
        amt: 2400,
    },
    {
        name: 'Page B',
        uv: 3000,
        pv: 1398,
        amt: 2210,
    },
    {
        name: 'Page C',
        uv: 2000,
        pv: 9800,
        amt: 2290,
    },
    {
        name: 'Page D',
        uv: 2780,
        pv: 3908,
        amt: 2000,
    },
    {
        name: 'Page E',
        uv: 1890,
        pv: 4800,
        amt: 2181,
    },
    {
        name: 'Page F',
        uv: 2390,
        pv: 3800,
        amt: 2500,
    },
    {
        name: 'Page G',
        uv: 3490,
        pv: 4300,
        amt: 2100,
    },
];

export interface RechartData {
    name: string | number;
    y: number;
}

@observer
export class ChartBox extends React.Component<ChartBoxProps> {
    @observable private _chartData: RechartData[] = [];

    @computed get currView() {
        if (this.props.rootDoc._dataVizView) {
            return StrCast(this.props.rootDoc._currChartView);
        } else {
            return 'table';
        }
    }

    constructor(props: any) {
        super(props);
        if (!this.props.rootDoc._currChartView) {
            this.props.rootDoc._currChartView = 'bar';
        }
    }

    @action
    generateChartData() {
        if (this.props.rootDoc._chartData) {
            this._chartData = JSON.parse(StrCast(this.props.rootDoc._chartData));
            return;
        }

        // generate the chart data according to the RechartData type from the pairs prop
        const chartData: RechartData[] = [];
        this.props.pairs.forEach(pair => {
            chartData.push({
                name: pair.x,
                y: pair.y,
            });
        });

        this._chartData = chartData;
    }

    // @action
    // generateChartJsData() {
    //     if (this.props.rootDoc._chartData) {
    //         // parse the string into a json object
    //         this._chartJsData = JSON.parse(StrCast(this.props.rootDoc._chartData));
    //         this._prevColor = StrCast(this.props.rootDoc._prevColor);
    //         this._prevIndex = JSON.parse(StrCast(this.props.rootDoc._prevIndex));
    //         return;
    //     }
    //     const labels = this.props.pairs.map(p => p.x);
    //     const dataset = {
    //         label: 'Dataset 1',
    //         data: this.props.pairs.map(p => p.y),
    //         backgroundColor: this.props.pairs.map(p => primaryColor),
    //     };
    //     const data = {
    //         labels,
    //         datasets: [dataset],
    //     };
    //     this._chartJsData = data;
    // }

    componentDidMount() {
        // this.generateChartJsData();
        this.generateChartData();
    }

    @action
    onClickChangeChart = (e: React.MouseEvent<HTMLButtonElement>) => {
        e.preventDefault();
        e.stopPropagation();
        console.log(e.currentTarget.value);
        this.props.rootDoc._currChartView = e.currentTarget.value.toLowerCase();
    };

    // @action
    // onClick = (e: React.MouseEvent<HTMLCanvasElement, MouseEvent>) => {
    //     e.preventDefault();
    //     console.log(e);

    //     if (getDatasetAtEvent(this._chartRef.current, e).length == 0) return;
    //     if (!this._chartJsData) return;
    //     if (this._prevIndex && this._prevColor) {
    //         this._chartJsData.datasets[this._prevIndex.dIndex].backgroundColor[this._prevIndex.index] = this._prevColor;
    //     }

    //     const currSelected = getElementAtEvent(this._chartRef.current, e);
    //     // TODO - nda: the currSelected might not have the updated color variables so look into that
    //     this._currSelected = currSelected;
    //     const index = { datasetIndex: currSelected[0].datasetIndex, index: currSelected[0].index };
    //     this._prevIndex = { dIndex: index.datasetIndex, index: index.index };
    //     this._prevColor = this._chartJsData.datasets[index.datasetIndex].backgroundColor[index.index];
    //     this._chartJsData.datasets[index.datasetIndex].backgroundColor[index.index] = selectedColor;
    //     this._chartRef.current.update();
    //     // stringify this._chartJsData
    //     const strData = JSON.stringify(this._chartJsData);
    //     this.props.rootDoc._chartData = strData;
    //     this.props.rootDoc._prevColor = this._prevColor;
    //     this.props.rootDoc._prevIndex = JSON.stringify(this._prevIndex);
    // };

    onMouseMove = (e: CategoricalChartState) => {
        console.log(e);

        // pops up at 526 when it should be at 263
        // at 100%scale it pops up at 526 but mouse shows 263 at 50% scale
        if (e.chartX && e.chartY) {
            e.chartX += 263;
            e.chartY += 263;
        }
        // if (e.chartX && e.chartY) {
        //     e.chartX -= 200;
        //     e.chartY -= 200;
        // }
    };

    handleDotClick = (e: any) => {
        console.log(e);
    };

    // handleTextClick = (e: any) => {
    //     console.log(e);
    // }

    @computed get reLineChart() {
        return (
            <ResponsiveContainer width="80%" height="80%">
                <LineChart
                    width={500}
                    height={300}
                    data={this._chartData}
                    margin={{
                        top: 5,
                        right: 30,
                        left: 20,
                        bottom: 5,
                    }}
                    onMouseDown={e => console.log(e)}
                    onMouseMove={e => this.onMouseMove(e)}>
                    <CartesianGrid strokeDasharray="3 3" />
                    <XAxis dataKey="name" onMouseDown={e => console.log(e)} />
                    <YAxis />
                    <Tooltip />
                    <Legend />
                    <Line type="monotone" dataKey="y" stroke="#8884d8" activeDot={{ r: 8, onClick: this.handleDotClick }} />
                    {/* <Line type="monotone" dataKey="uv" stroke="#82ca9d" /> */}
                </LineChart>
            </ResponsiveContainer>
        );
    }

    render() {
        if (this.props.pairs && this._chartData.length > 0) {
            return (
                <div>
                    <div>
                        {/*{this.props.rootDoc._currChartView == 'line' ? (
                            <Line ref={this._chartRef} options={this.options} data={this._chartJsData} onClick={e => this.onClick(e)} />
                        ) : (
                            <Bar ref={this._chartRef} options={this.options} data={this._chartJsData} onClick={e => this.onClick(e)} />
                        )} */}
                        {this.reLineChart}
                    </div>
                    <button onClick={e => this.onClickChangeChart(e)} value="line">
                        Line
                    </button>
                    <button onClick={e => this.onClickChangeChart(e)} value="bar">
                        Bar
                    </button>
                </div>
            );
        } else {
            return <div></div>;
        }
    }
}