aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/DataVizBox/LineChart.tsx
blob: 42e9da3d763bc35ddd934cf0d91fe222bbcbccab (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
import { action, computed, IReactionDisposer, observable, reaction } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { ChartData, DataPoint } from './ChartBox';
// import d3
import * as d3 from 'd3';
import { minMaxRange, createLineGenerator, xGrid, yGrid, drawLine, xAxisCreator, yAxisCreator, scaleCreatorNumerical, scaleCreatorCategorical } from './utils/D3Utils';

interface LineChartProps {
    data: ChartData;
    width: number;
    height: number;
}

interface SelectedDataPoint {
    x: number;
    y: number;
    elem: d3.Selection<d3.BaseType, unknown, SVGGElement, unknown>;
}

@observer
export class LineChart extends React.Component<LineChartProps> {
    private _dataReactionDisposer: IReactionDisposer | undefined = undefined;
    @observable private _x: number = 0;
    @observable private _y: number = 0;
    @observable private _currSelected: SelectedDataPoint | undefined = undefined;
    // create ref for the div
    private _chartRef: React.RefObject<HTMLDivElement> = React.createRef();

    componentDidMount() {
        console.log('Getting to line chart');
        this.drawChart();
        this._dataReactionDisposer = reaction(() => this.props.data, this.drawChart);
    }

    componentWillUnmount() {
        if (this._dataReactionDisposer) {
            this._dataReactionDisposer();
        }
    }

    tooltipContent(data: DataPoint) {
        return `<b>x: ${data.x} y: ${data.y}</b>`;
    }

    drawChart() {
        // clearing tooltip
        d3.select(this._chartRef.current).select('svg').remove();
        d3.select(this._chartRef.current).select('.tooltip').remove();

        const margin = { top: 50, right: 50, bottom: 50, left: 50 };
        const { data } = this.props;

        const { xMin, xMax, yMin, yMax } = minMaxRange(data.data);

        // adding svg
        const svg = d3.select(this._chartRef.current).append('svg').attr('width', '100%').attr('height', '100%').append('g').attr('transform', `translate(${margin.left}, ${margin.top})`);

        // adding tooltip
        // const tooltip = d3.select('#chart-container').append('div').attr('class', 'tooltip');

        if (!xMin || !xMax || !yMin || !yMax) {
            // TODO: nda - error handle
            return;
        }
        const xScale = scaleCreatorNumerical(xMin, xMax, 0, this.props.width);
        // adding x axis
        const yScale = scaleCreatorNumerical(0, yMax, this.props.height, 0);

        // create a line function that takes in the data.data.x and data.data.y
        // TODO: nda - fix the types for the d here
        const lineGen = createLineGenerator(xScale, yScale);

        // create x and y grids
        xGrid(svg.append('g'), this.props.height, xScale);
        yGrid(svg.append('g'), this.props.width, yScale);
        xAxisCreator(svg.append('g'), this.props.height, xScale);
        yAxisCreator(svg.append('g'), this.props.width, yScale);

        // draw the line
        drawLine(svg.append('path'), data.data, lineGen);

        // draw the datapoint circles
        svg.selectAll('.circle-d1')
            .data(data.data)
            .join('circle') // enter append
            .attr('class', 'circle-d1')
            .attr('r', '3') // radius
            .attr('cx', d => xScale(d.x))
            .attr('cy', d => yScale(d.y))
            .attr('data-x', d => d.x)
            .attr('data-y', d => d.y);

        // this.setupDatapointClickHandlers();

        const focus = svg.append('g').attr('class', 'focus').style('display', 'none');
        focus.append('circle').attr('r', 5).attr('class', 'circle');
        const tooltip = d3
            .select(this._chartRef.current)
            .append('div')
            .attr('class', 'tooltip')
            .style('opacity', 0)
            .style('position', 'absolute')
            // .style('z-index', '10')
            .style('background', '#fff')
            .style('border', '1px solid #ccc')
            .style('padding', '5px')
            .style('font-size', '12px');
        // add all the tooltipContent to the tooltip
        // @action
        const mousemove = action((e: any) => {
            const bisect = d3.bisector((d: DataPoint) => d.x).left;
            const xPos = d3.pointer(e)[0];
            const x0 = bisect(data.data, xScale.invert(xPos));
            const d0 = data.data[x0];
            this._x = d0.x;
            this._y = d0.y;
            focus.attr('transform', `translate(${xScale(d0.x)},${yScale(d0.y)})`);
            // TODO: nda - implement tooltips
            // tooltip.transition().duration(300).style('opacity', 0.9);
            // tooltip.html(() => this.tooltipContent(d0)).attr('transform', `translate(${xScale(d0.x) + 30}px,${yScale(d0.y) + 30}px)`);
        });

        const onPointClick = action((e: any) => {
            const bisect = d3.bisector((d: DataPoint) => d.x).left;
            const xPos = d3.pointer(e)[0];
            const x0 = bisect(data.data, xScale.invert(xPos));
            const d0 = data.data[x0];
            this._x = d0.x;
            this._y = d0.y;
            // find .circle-d1 with data-x = d0.x and data-y = d0.y
            const selected = svg.selectAll('.circle-d1').filter((d: any) => d['data-x'] === d0.x && d['data-y'] === d0.y);
            this._currSelected = { x: d0.x, y: d0.y, elem: selected };
            console.log('Getting here');
            console.log(this._currSelected);
        });

        svg.append('rect')
            .attr('class', 'overlay')
            .attr('width', this.props.width)
            .attr('height', this.props.height)
            .style('opacity', 0)
            .on('mouseover', () => {
                focus.style('display', null);
            })
            .on('mouseout', () => {
                tooltip.transition().duration(300).style('opacity', 0);
            })
            .on('mousemove', mousemove)
            .on('click', onPointClick);
    }

    render() {
        return (
            <div ref={this._chartRef} className="chart-container">
                <span>
                    x: {this._x} y: {this._y}
                    Curr Selected: {this._currSelected ? `x: ${this._currSelected.x} y: ${this._currSelected.y}` : 'none'}
                </span>
            </div>
        );
    }
}