generated from jtr13/EDAVtemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinteractive.Rmd
226 lines (199 loc) · 8.55 KB
/
interactive.Rmd
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
# Interactive component
## New Interactive Features:
In order to have a better understanding of overall long-term trend of Average Part Weight among products inspected by Machine I4, our team will use d3.js to add a interactive feature, showing the Date and Average Part Weight Difference Ratio from Target for each point along the line. For d3.js part, we firstly create a tooltip and attach the appropriate Mouseover, Mousemove, and Mouseout event functions to dynamically move and change the visibility of a tooltip and help display the data values as the text. That newly added feature will help the user obtain the exact value of each data point without going back to check the data table anymore, and it could draw user's attention while providing additional information.
## Instructions for users:
When the user moves the mouse pointer over the point along the line, the Date and Average Part Weight Difference Ratio from Target values will be displayed near the point in a black box. In addition, nothing will show on the graph when the mouse is away from the any point on the line.
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://d3js.org/d3.v7.js"></script>
<style>
.tooltip {
position: absolute;
pointer-events: none;
background: #000;
color: #fff;
padding: 10px;
border-radius: 10px;
font-size: 12px;
}
</style>
</head>
<body>
<h3>Average Part Weight Difference Ratio from Target of Each Product
Inspected by Machine I4</h3>
<svg id="withaxes" width="600" height="400"></svg>
<script>
const svg2 = d3.select("svg#withaxes")
const margin = {top: 20, right: 0, bottom: 40, left: 70}
const width = +svg2.attr("width") - margin.left - margin.right
const height = +svg2.attr("height") - margin.top - margin.bottom
const g = svg2.append("g").attr("transform", `translate(${margin.left}, ${margin.top})`);
g.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", width-260)
.attr("y", height +35)
.text("Date");
g.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", -60)
.attr("dy", ".95em")
.attr("x", -50)
.attr("transform", "rotate(-90)")
.text("Average Part Weight Difference Ratio");
const parseTime = d3.timeParse("%y-%m-%d");
const formatTime = d3.timeFormat("%B %d, %Y")
xScale = d3.scaleTime().range([0, width]);
yScale = d3.scaleLinear()
.domain([-0.17, 0.15])
.range([height, 0])
const xAxis = d3.axisBottom()
.scale(xScale)
.tickFormat(d3.timeFormat("%b"));
const line = d3.line()
.x(d => xScale(d.date))
.y(d => yScale(d.Part_Weight));
var tooltip = d3.select("body")
.append("div")
.style("z-index", "10")
.style("opacity", "0")
.attr("class", "tooltip");
const data =
[{'date': '22-09-01', 'Part_Weight': -0.0356},
{'date': '22-09-02', 'Part_Weight': 0.0437},
{'date': '22-09-03', 'Part_Weight': 0.0834},
{'date': '22-09-04', 'Part_Weight': 0.0773},
{'date': '22-09-05', 'Part_Weight': 0.0702},
{'date': '22-09-06', 'Part_Weight': 0.027},
{'date': '22-09-07', 'Part_Weight': -0.0079},
{'date': '22-09-08', 'Part_Weight': -0.0286},
{'date': '22-09-09', 'Part_Weight': 0.0297},
{'date': '22-09-10', 'Part_Weight': 0.0559},
{'date': '22-09-11', 'Part_Weight': -0.0038},
{'date': '22-09-12', 'Part_Weight': 0.0675},
{'date': '22-09-13', 'Part_Weight': 0.0498},
{'date': '22-09-14', 'Part_Weight': -0.0727},
{'date': '22-09-15', 'Part_Weight': -0.0418},
{'date': '22-09-16', 'Part_Weight': -0.085},
{'date': '22-09-17', 'Part_Weight': -0.0595},
{'date': '22-09-18', 'Part_Weight': -0.0509},
{'date': '22-09-19', 'Part_Weight': -0.0108},
{'date': '22-09-20', 'Part_Weight': 0.0491},
{'date': '22-09-21', 'Part_Weight': 0.0446},
{'date': '22-09-22', 'Part_Weight': -0.0449},
{'date': '22-09-23', 'Part_Weight': -0.0794},
{'date': '22-09-24', 'Part_Weight': 0.0104},
{'date': '22-09-26', 'Part_Weight': 0.0509},
{'date': '22-09-27', 'Part_Weight': -0.0333},
{'date': '22-09-28', 'Part_Weight': 0.0278},
{'date': '22-09-29', 'Part_Weight': -0.0437},
{'date': '22-09-30', 'Part_Weight': -0.0795},
{'date': '22-10-01', 'Part_Weight': -0.0676},
{'date': '22-10-02', 'Part_Weight': -0.0601},
{'date': '22-10-03', 'Part_Weight': -0.0546},
{'date': '22-10-04', 'Part_Weight': 0.1219},
{'date': '22-10-05', 'Part_Weight': 0.0803},
{'date': '22-10-06', 'Part_Weight': 0.0883},
{'date': '22-10-07', 'Part_Weight': 0.125},
{'date': '22-10-08', 'Part_Weight': 0.0842},
{'date': '22-10-09', 'Part_Weight': 0.0816},
{'date': '22-10-10', 'Part_Weight': 0.1065},
{'date': '22-10-11', 'Part_Weight': 0.0707},
{'date': '22-10-12', 'Part_Weight': 0.0595},
{'date': '22-10-13', 'Part_Weight': 0.1385},
{'date': '22-10-14', 'Part_Weight': 0.1606},
{'date': '22-10-15', 'Part_Weight': 0.1462},
{'date': '22-10-16', 'Part_Weight': 0.1333},
{'date': '22-10-17', 'Part_Weight': 0.1338},
{'date': '22-10-18', 'Part_Weight': 0.0892},
{'date': '22-10-19', 'Part_Weight': 0.0812},
{'date': '22-10-20', 'Part_Weight': 0.08},
{'date': '22-10-21', 'Part_Weight': 0.0738},
{'date': '22-10-22', 'Part_Weight': 0.0922},
{'date': '22-10-23', 'Part_Weight': 0.1076},
{'date': '22-10-24', 'Part_Weight': 0.0689},
{'date': '22-10-25', 'Part_Weight': 0.0169},
{'date': '22-10-26', 'Part_Weight': -0.0904},
{'date': '22-10-27', 'Part_Weight': 0.0404},
{'date': '22-10-28', 'Part_Weight': 0.0574},
{'date': '22-10-29', 'Part_Weight': 0.0443},
{'date': '22-10-30', 'Part_Weight': 0.0523},
{'date': '22-11-01', 'Part_Weight': 0.0275},
{'date': '22-11-02', 'Part_Weight': 0.0375},
{'date': '22-11-03', 'Part_Weight': 0.0501},
{'date': '22-11-04', 'Part_Weight': 0.0277},
{'date': '22-11-05', 'Part_Weight': -0.0423},
{'date': '22-11-06', 'Part_Weight': 0.0274},
{'date': '22-11-12', 'Part_Weight': -0.1308},
{'date': '22-11-13', 'Part_Weight': 0.1005},
{'date': '22-11-14', 'Part_Weight': 0.1088},
{'date': '22-11-15', 'Part_Weight': -0.0995},
{'date': '22-11-16', 'Part_Weight': 0.011},
{'date': '22-11-17', 'Part_Weight': 0.0394},
{'date': '22-11-18', 'Part_Weight': 0.0382},
{'date': '22-11-22', 'Part_Weight': 0.0391},
{'date': '22-11-23', 'Part_Weight': 0.0621},
{'date': '22-11-24', 'Part_Weight': -0.0633},
{'date': '22-11-28', 'Part_Weight': -0.0721},
{'date': '22-11-29', 'Part_Weight': -0.0348},
{'date': '22-11-30', 'Part_Weight': 0.0023},
{'date': '22-12-01', 'Part_Weight': 0.053}
];
data.forEach(function(d) {
d.date = parseTime(d.date);
});
xScale
.domain(d3.extent(data, d => d.date))
g.append("g")
.attr("transform", `translate(0, ${height})`)
.call(xAxis);
g.append("g")
.call(d3.axisLeft(yScale))
function goyellow() {
d3.select(this)
.append(text)
.attr("x", "100")
.attr("y", "40")
.text("date");
};
g.append("path")
.datum(data)
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("d", line)
.on("mouseover", mouseoverFunc)
.on("mousemove", mousemoveFunc)
.on("mouseout", mouseoutFunc);
const [min, max] = d3.extent(data, d=>d.date);
function mouseoverFunc(mouse, d) {
//coordinates
const [x_cord,y_cord] = d3.pointer(mouse);
const current_date = xScale.invert(x_cord);
const current_Part_Weight = yScale.invert(y_cord);
return tooltip
.style("opacity", "1")
.html("Date: " + formatTime(current_date) + "<br/> Part_Weight: " + current_Part_Weight)
.style("left", (x_cord+150) + "px")
.style("top", (y_cord+500) + "px")
}
function mousemoveFunc(mouse, d) {
const [x_cord,y_cord] = d3.pointer(mouse);
const current_date = xScale.invert(x_cord);
const current_Part_Weight = yScale.invert(y_cord);
return tooltip
.style("opacity", "1")
.html("Date: " + formatTime(current_date) + "<br/> Part_Weight: " + current_Part_Weight)
.style("left", (x_cord+450) + "px")
.style("top", (y_cord+200) + "px")
}
function mouseoutFunc(d) {
return tooltip.style("opacity", "0")
}
</script>
</body>
</html>