-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcomplexity-graph.js
More file actions
132 lines (111 loc) · 5.07 KB
/
Copy pathcomplexity-graph.js
File metadata and controls
132 lines (111 loc) · 5.07 KB
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
/*
* Copyright (C) 2015-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
class ComplexityGraph extends Graph {
constructor(run, settings, size, margins) {
super(run, settings);
let svg = this.appendGraph("complexity-graph", size, margins);
let xMin = d3.min(this.run.timeline.frames, (frame) => {
return frame.complexity;
});
let xMax = d3.max(this.run.timeline.frames, (frame) => {
return frame.complexity;
});
const yMin = Graph.msPerSecond / this.minFrameRate;
const yMax = Graph.msPerSecond / this.maxFrameRate;
const axisWidth = size.width - margins.left - margins.right;
const axisHeight = size.height - margins.top - margins.bottom;
let xScale = d3.scale.linear()
.range([0, axisWidth])
.domain([xMin, xMax]);
let yScale = d3.scale.linear()
.range([axisHeight, 0])
.domain([yMin, yMax]);
this.appendAxes(svg, xScale, yScale, axisHeight);
this.appendData(svg, xScale, yScale);
this.appendRegression(svg, xScale, yScale, xMin, xMax, this.run.statistics.regression);
}
appendAxes(svg, xScale, yScale, axisHeight) {
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yScale)
.tickValues(this.tickValuesForFrameRate(this.settings.targetFrameRate, this.minFrameRate, this.maxFrameRate))
.tickFormat((d) => {
return (Graph.msPerSecond / d).toFixed(0);
})
.orient("left");
// x-axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + axisHeight + ")")
.call(xAxis);
// y-axis
var yAxisGroup = svg.append("g")
.attr("class", "y axis")
.call(yAxis);
}
appendData(svg, xScale, yScale) {
// series
let group = svg.append("g")
.attr("class", "series raw")
.selectAll("line")
.data(this.run.timeline.frames)
.enter();
group.append("line")
.attr("x1", (frame) => { return xScale(frame.complexity) - 3; })
.attr("x2", (frame) => { return xScale(frame.complexity) + 3; })
.attr("y1", (frame) => { return yScale(frame.timespan.length) - 3; })
.attr("y2", (frame) => { return yScale(frame.timespan.length) + 3; });
group.append("line")
.attr("x1", (frame) => { return xScale(frame.complexity) - 3; })
.attr("x2", (frame) => { return xScale(frame.complexity) + 3; })
.attr("y1", (frame) => { return yScale(frame.timespan.length) + 3; })
.attr("y2", (frame) => { return yScale(frame.timespan.length) - 3; });
}
appendLine(svg, xScale, yScale, point1, point2) {
svg.append("line")
.style("stroke", "white")
.style("stroke-width", 2)
.attr("x1", xScale(point1.x))
.attr("y1", yScale(point1.y))
.attr("x2", xScale(point2.x))
.attr("y2", yScale(point2.y));
}
appendRegression(svg, xScale, yScale, xMin, xMax, regression) {
let segment1 = regression.segment1;
let segment2 = regression.segment2;
let score = regression.score;
let point1 = new Point(xMin, segment1.s + segment1.t * xMin);
let point2 = new Point(score, segment1.s + segment1.t * score);
let point3 = new Point(xMax, segment2.s + segment2.t * xMax);
svg.append("circle")
.attr("cx", xScale(point2.x))
.attr("cy", yScale(point2.y))
.attr("r", 3);
this.appendLine(svg, xScale, yScale, point1, point2);
this.appendLine(svg, xScale, yScale, point2, point3);
}
}