-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
200 lines (166 loc) · 7.3 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Apple Health Data Visualizer</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/papaparse.min.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style>
body { font-family: Arial, sans-serif; margin: 20px; text-align: center; }
#chart { width: 90%; height: 500px; margin: auto; }
input, select, button { margin: 10px; padding: 8px; font-size: 16px; }
</style>
</head>
<body>
<h1>Apple Health Data Visualizer</h1>
<h2>Upload Apple Health XML</h2>
<input type="file" id="xmlFileInput" accept=".xml">
<button onclick="processXML()">Process File</button>
<h2>Download CSV</h2>
<button onclick="downloadCSV()">Download CSV</button>
<h2>Filter Data</h2>
<label>Type:</label>
<select id="typeSelect"></select>
<br>
<label>Start Date:</label>
<input type="date" id="startDate">
<label>End Date:</label>
<input type="date" id="endDate">
<div id="chart"></div>
<script>
let parsedData = [];
function processXML() {
const file = document.getElementById('xmlFileInput').files[0];
if (!file) { alert("Please select an XML file."); return; }
const reader = new FileReader();
reader.onload = (event) => {
const xmlString = event.target.result;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "text/xml");
parseHealthData(xmlDoc);
};
reader.readAsText(file);
}
function parseHealthData(xmlDoc) {
const records = xmlDoc.getElementsByTagName("Record");
parsedData = [];
for (let i = 0; i < records.length; i++) {
const record = records[i];
let dateString = record.getAttribute("creationDate") || record.getAttribute("startDate");
let dateObj = new Date(dateString);
let value = parseFloat(record.getAttribute("value"));
if (!isNaN(dateObj) && !isNaN(value) && value > 0) { // Exclude invalid, null, or zero values
parsedData.push({
type: record.getAttribute("type"),
timestamp: dateObj,
value: value
});
}
}
parsedData.sort((a, b) => a.timestamp - b.timestamp);
console.log("Parsed Data:", parsedData.length, "records");
populateTypeDropdown();
setupDateFilters();
updateChart();
}
function populateTypeDropdown() {
const select = document.getElementById("typeSelect");
select.innerHTML = "";
[...new Set(parsedData.map(d => d.type))].forEach(type => {
let option = document.createElement("option");
option.value = type;
option.textContent = type;
select.appendChild(option);
});
select.addEventListener("change", updateChart);
}
function setupDateFilters() {
const timestamps = parsedData.map(d => d.timestamp);
if (timestamps.length === 0) return;
const minDate = new Date(Math.min(...timestamps));
const maxDate = new Date(Math.max(...timestamps));
document.getElementById("startDate").value = minDate.toISOString().split("T")[0];
document.getElementById("endDate").value = maxDate.toISOString().split("T")[0];
document.getElementById("startDate").addEventListener("change", updateChart);
document.getElementById("endDate").addEventListener("change", updateChart);
}
function updateChart() {
const selectedType = document.getElementById("typeSelect").value;
const startDate = new Date(document.getElementById("startDate").value);
const endDate = new Date(document.getElementById("endDate").value);
const filteredData = parsedData.filter(d =>
d.type === selectedType && d.timestamp >= startDate && d.timestamp <= endDate
);
if (filteredData.length === 0) {
document.getElementById("chart").innerHTML = "<p>No data available for selected filters.</p>";
return;
}
const timestamps = filteredData.map(d => d.timestamp);
const values = filteredData.map(d => d.value);
// Ensure values exclude 0 and null before calculations
const cleanedValues = values.filter(v => v > 0 && v !== null);
if (cleanedValues.length === 0) {
document.getElementById("chart").innerHTML = "<p>No valid data available after filtering.</p>";
return;
}
// Calculate Mean & Standard Deviation
const mean = cleanedValues.reduce((a, b) => a + b, 0) / cleanedValues.length;
const std = Math.sqrt(cleanedValues.map(x => Math.pow(x - mean, 2)).reduce((a, b) => a + b, 0) / cleanedValues.length);
const upperBound = mean + 2 * std;
const lowerBound = mean - 2 * std;
const trace = {
x: timestamps,
y: values,
mode: "lines+markers",
type: "scatter",
name: selectedType,
line: { width: 2 }
};
const meanLine = {
x: timestamps,
y: Array(values.length).fill(mean),
mode: "lines",
type: "scatter",
name: "Mean",
line: { dash: "dash", color: "red", width: 2 }
};
const upperBoundLine = {
x: timestamps,
y: Array(values.length).fill(upperBound),
mode: "lines",
type: "scatter",
name: "+2 Std",
line: { dash: "dot", color: "blue", width: 1 }
};
const lowerBoundLine = {
x: timestamps,
y: Array(values.length).fill(lowerBound),
mode: "lines",
type: "scatter",
name: "-2 Std",
line: { dash: "dot", color: "blue", width: 1 }
};
const layout = {
title: `Health Data: ${selectedType}`,
xaxis: { title: "Timestamp" },
yaxis: { title: "Value" },
width: 900,
height: 500
};
Plotly.newPlot("chart", [trace, meanLine, upperBoundLine, lowerBoundLine], layout);
}
function downloadCSV() {
if (!parsedData.length) { alert("No data to download."); return; }
const csv = Papa.unparse(parsedData.map(row => ({
Type: row.type, Timestamp: row.timestamp.toISOString(), Value: row.value
})));
const blob = new Blob([csv], { type: "text/csv" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = "health_data.csv";
a.click();
}
</script>
</body>
</html>