|
200 | 200 | width: 100%; |
201 | 201 | height: 500px; |
202 | 202 | } |
| 203 | + .reset-buttons { |
| 204 | + display: flex; |
| 205 | + justify-content: center; |
| 206 | + gap: 6px; |
| 207 | + margin-top: 16px; |
| 208 | + } |
| 209 | + .reset-buttons button { |
| 210 | + padding: 6px; |
| 211 | + background-color: rgb(58, 58, 58); |
| 212 | + color: white; |
| 213 | + border-radius: 6px; |
| 214 | + border: none; |
| 215 | + } |
203 | 216 | </style> |
204 | 217 | </head> |
205 | 218 | <body> |
|
220 | 233 | </div> |
221 | 234 | </div> |
222 | 235 |
|
| 236 | + <div class="reset-buttons"> |
| 237 | + <button id="resetOffset">Reset Diffs</button> |
| 238 | + <button id="resetGraph">Reset Graph</button> |
| 239 | + </div> |
223 | 240 | <div id="sections"></div> |
224 | 241 | <div id="cells"></div> |
225 | 242 | <div id="charts"> |
|
398 | 415 | }), |
399 | 416 | ); |
400 | 417 | } |
| 418 | + resetValues = () => { |
| 419 | + this.labels = []; |
| 420 | + this.data = { |
| 421 | + capacity: [], |
| 422 | + voltage: [], |
| 423 | + current: [], |
| 424 | + cellDiff: [], |
| 425 | + cells: [], |
| 426 | + }; |
| 427 | + this.lastPlottedAt = -1; |
| 428 | + this.onAdd(); |
| 429 | + this.saveToStorage(); |
| 430 | + }; |
401 | 431 | } |
402 | 432 |
|
403 | 433 | const plotter = new Plotter(); |
| 434 | + const [initialValues, setInitialValues] = cachedValue("initialValues", {}); |
| 435 | + const [cellStartValues, setCellStartValues] = cachedValue('cellStartValues', []); |
| 436 | + |
| 437 | + const resetGraphBtn = document.getElementById('resetGraph'); |
| 438 | + resetGraphBtn.addEventListener('click', () => { |
| 439 | + plotter.resetValues(); |
| 440 | + }) |
| 441 | + const resetOffsetBtn = document.getElementById('resetOffset'); |
| 442 | + resetOffsetBtn.addEventListener('click', () => { |
| 443 | + setCellStartValues([]); |
| 444 | + setInitialValues({}); |
| 445 | + }) |
| 446 | + |
404 | 447 |
|
405 | 448 | function getIndicesOfLargestNumbers(arr) { |
406 | 449 | if (!arr || arr.length === 0) { |
|
501 | 544 | } |
502 | 545 | } |
503 | 546 |
|
504 | | - const initialValues = {}; |
| 547 | + |
| 548 | + |
505 | 549 |
|
506 | 550 | const createOrUpdateSection = (id, title, description, value, rawValue) => { |
507 | | - let initVal = initialValues[id]; |
| 551 | + let initVal = initialValues()[id]; |
508 | 552 | if (initVal === undefined) { |
509 | | - initialValues[id] = rawValue; |
| 553 | + setInitialValues({...initialValues(), [id]: rawValue}); |
510 | 554 | initVal = rawValue; |
511 | 555 | } |
512 | 556 |
|
|
558 | 602 | sections.innerHTML += element; |
559 | 603 | }; |
560 | 604 |
|
561 | | - let cellStartValues = []; |
562 | 605 |
|
563 | 606 | const estimateBalanceCells = []; |
564 | 607 | function createCellsSection(balanceStatus, cellVolts) { |
|
588 | 631 | {id: 'cell-avg', name: 'Avg', value: averageVoltage.toFixed(3)}, |
589 | 632 | ]; |
590 | 633 |
|
591 | | - if (!cellStartValues.length) { |
592 | | - cellStartValues = cellVolts; |
| 634 | + if (!cellStartValues().length) { |
| 635 | + setCellStartValues(cellVolts); |
593 | 636 | } |
594 | 637 |
|
595 | | - const cellDiffs = cellStartValues.map((v, i) => { |
| 638 | + const cellDiffs = cellStartValues().map((v, i) => { |
596 | 639 | const diff = cellVolts[i] - v; |
597 | 640 | return diff.toFixed(3); |
598 | 641 | }); |
|
1019 | 1062 | window.addEventListener('resize', () => { |
1020 | 1063 | myChart?.resize(); |
1021 | 1064 | }); |
| 1065 | + |
| 1066 | + function cachedValue(key, initialValue) { |
| 1067 | + let value = JSON.parse(localStorage.getItem(key) || JSON.stringify(initialValue)); |
| 1068 | + |
| 1069 | + const set = (val) => { |
| 1070 | + value = val; |
| 1071 | + localStorage.setItem(key, JSON.stringify(val)); |
| 1072 | + }; |
| 1073 | + const get = () => value; |
| 1074 | + |
| 1075 | + if (value === null) { |
| 1076 | + set(initialValue); |
| 1077 | + } |
| 1078 | + return [get, set]; |
| 1079 | + } |
1022 | 1080 | </script> |
1023 | 1081 | </html> |
0 commit comments