-
Notifications
You must be signed in to change notification settings - Fork 4
/
linechart.ts
112 lines (97 loc) · 3.35 KB
/
linechart.ts
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
/*
Implementation partially borrowed from https://github.com/tensorflow/playground/
Modification Copyright: (2016) Xin Rong. License: MIT.
Original file copyright:
------------------------
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
type DataPoint = {
x: number;
y: number[];
}
/**
* A multi-series line chart that allows you to append new data points
* as data becomes available.
*/
export class AppendingLineChart {
private numLines: number;
private data: DataPoint[] = [];
private svg: d3.Selection<any>;
private xScale: d3.scale.Linear<number, number>;
private yScale: d3.scale.Linear<number, number>;
private paths: d3.Selection<any>[];
private lineColors: string[];
private minY = Number.MAX_VALUE;
private maxY = Number.MIN_VALUE;
constructor(container: d3.Selection<any>, lineColors: string[]) {
this.lineColors = lineColors;
this.numLines = lineColors.length;
let node = <HTMLElement>container.node();
let totalWidth = node.offsetWidth;
let totalHeight = node.offsetHeight;
let margin = {top: 2, right: 0, bottom: 2, left: 2};
let width = totalWidth - margin.left - margin.right;
let height = totalHeight - margin.top - margin.bottom;
this.xScale = d3.scale.linear()
.domain([0, 0])
.range([0, width]);
this.yScale = d3.scale.linear()
.domain([0, 0])
.range([height, 0]);
this.svg = container.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
this.paths = new Array(this.numLines);
for (let i = 0; i < this.numLines; i++) {
this.paths[i] = this.svg.append("path")
.attr("class", "line")
.style({
"fill": "none",
"stroke": lineColors[i],
"stroke-width": "1.5px"
});
}
}
reset() {
this.data = [];
this.redraw();
this.minY = Number.MAX_VALUE;
this.maxY = Number.MIN_VALUE;
}
addDataPoint(dataPoint: number[]) {
if (dataPoint.length !== this.numLines) {
throw Error("Length of dataPoint must equal number of lines");
}
dataPoint.forEach(y => {
this.minY = Math.min(this.minY, y);
this.maxY = Math.max(this.maxY, y);
});
this.data.push({x: this.data.length + 1, y: dataPoint});
this.redraw();
}
private redraw() {
// Adjust the x and y domain.
this.xScale.domain([1, this.data.length]);
this.yScale.domain([this.minY, this.maxY]);
// Adjust all the <path> elements (lines).
let getPathMap = (lineIndex: number) => {
return d3.svg.line<DataPoint>()
.x(d => this.xScale(d.x))
.y(d => this.yScale(d.y[lineIndex]));
};
for (let i = 0; i < this.numLines; i++) {
this.paths[i].datum(this.data).attr("d", getPathMap(i));
}
}
}