fixed chart destruction and recreation
This commit is contained in:
@@ -281,6 +281,9 @@
|
|||||||
let currentDaysRange =
|
let currentDaysRange =
|
||||||
parseInt(localStorage.getItem("lastDiaperTimeRange")) || 7;
|
parseInt(localStorage.getItem("lastDiaperTimeRange")) || 7;
|
||||||
let selectedChildId = null; // null means show first/only child
|
let selectedChildId = null; // null means show first/only child
|
||||||
|
let barChartInstance = null; // Track chart instance for proper cleanup
|
||||||
|
let poopPieChartInstance = null;
|
||||||
|
let peePieChartInstance = null;
|
||||||
|
|
||||||
// Restore last selected child from localStorage
|
// Restore last selected child from localStorage
|
||||||
const lastChildId = localStorage.getItem("lastDiaperChildFilter");
|
const lastChildId = localStorage.getItem("lastDiaperChildFilter");
|
||||||
@@ -782,7 +785,12 @@
|
|||||||
function renderBarChart(chartData) {
|
function renderBarChart(chartData) {
|
||||||
const ctx = document.getElementById("diaperChart").getContext("2d");
|
const ctx = document.getElementById("diaperChart").getContext("2d");
|
||||||
|
|
||||||
new Chart(ctx, {
|
// Destroy existing chart instance if it exists
|
||||||
|
if (barChartInstance) {
|
||||||
|
barChartInstance.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
barChartInstance = new Chart(ctx, {
|
||||||
type: "bar",
|
type: "bar",
|
||||||
data: {
|
data: {
|
||||||
labels: chartData.labels,
|
labels: chartData.labels,
|
||||||
@@ -875,6 +883,11 @@
|
|||||||
function renderPoopPieChart(chartData) {
|
function renderPoopPieChart(chartData) {
|
||||||
const ctx = document.getElementById("poopPieChart").getContext("2d");
|
const ctx = document.getElementById("poopPieChart").getContext("2d");
|
||||||
|
|
||||||
|
// Destroy existing chart instance if it exists
|
||||||
|
if (poopPieChartInstance) {
|
||||||
|
poopPieChartInstance.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
const colors = {
|
const colors = {
|
||||||
Light: {
|
Light: {
|
||||||
bg: "rgba(139, 69, 19, 0.5)",
|
bg: "rgba(139, 69, 19, 0.5)",
|
||||||
@@ -901,7 +914,7 @@
|
|||||||
(label) => colors[label]?.border || "rgba(200, 200, 200, 1)",
|
(label) => colors[label]?.border || "rgba(200, 200, 200, 1)",
|
||||||
);
|
);
|
||||||
|
|
||||||
new Chart(ctx, {
|
poopPieChartInstance = new Chart(ctx, {
|
||||||
type: "pie",
|
type: "pie",
|
||||||
data: {
|
data: {
|
||||||
labels: chartData.labels,
|
labels: chartData.labels,
|
||||||
@@ -957,6 +970,11 @@
|
|||||||
function renderPeePieChart(chartData) {
|
function renderPeePieChart(chartData) {
|
||||||
const ctx = document.getElementById("peePieChart").getContext("2d");
|
const ctx = document.getElementById("peePieChart").getContext("2d");
|
||||||
|
|
||||||
|
// Destroy existing chart instance if it exists
|
||||||
|
if (peePieChartInstance) {
|
||||||
|
peePieChartInstance.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
const colors = {
|
const colors = {
|
||||||
Light: {
|
Light: {
|
||||||
bg: "rgba(255, 215, 0, 0.5)",
|
bg: "rgba(255, 215, 0, 0.5)",
|
||||||
@@ -983,7 +1001,7 @@
|
|||||||
(label) => colors[label]?.border || "rgba(200, 200, 200, 1)",
|
(label) => colors[label]?.border || "rgba(200, 200, 200, 1)",
|
||||||
);
|
);
|
||||||
|
|
||||||
new Chart(ctx, {
|
peePieChartInstance = new Chart(ctx, {
|
||||||
type: "pie",
|
type: "pie",
|
||||||
data: {
|
data: {
|
||||||
labels: chartData.labels,
|
labels: chartData.labels,
|
||||||
|
|||||||
@@ -319,6 +319,8 @@
|
|||||||
parseInt(localStorage.getItem("lastFeedingTimeRange")) || 7;
|
parseInt(localStorage.getItem("lastFeedingTimeRange")) || 7;
|
||||||
let selectedChildId = null; // null means show first/only child
|
let selectedChildId = null; // null means show first/only child
|
||||||
let barChartInstance = null;
|
let barChartInstance = null;
|
||||||
|
let pieChartInstance = null;
|
||||||
|
let durationChartInstance = null;
|
||||||
|
|
||||||
// Restore last selected child from localStorage
|
// Restore last selected child from localStorage
|
||||||
const lastChildId = localStorage.getItem("lastFeedingChildFilter");
|
const lastChildId = localStorage.getItem("lastFeedingChildFilter");
|
||||||
@@ -979,6 +981,11 @@
|
|||||||
function renderPieChart(chartData) {
|
function renderPieChart(chartData) {
|
||||||
const ctx = document.getElementById("pieChart").getContext("2d");
|
const ctx = document.getElementById("pieChart").getContext("2d");
|
||||||
|
|
||||||
|
// Destroy existing chart instance if it exists
|
||||||
|
if (pieChartInstance) {
|
||||||
|
pieChartInstance.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
// Color mapping for each type
|
// Color mapping for each type
|
||||||
const colorMap = {
|
const colorMap = {
|
||||||
"Left Breast": {
|
"Left Breast": {
|
||||||
@@ -1003,7 +1010,7 @@
|
|||||||
(label) => colorMap[label]?.border || "rgba(200, 200, 200, 1)",
|
(label) => colorMap[label]?.border || "rgba(200, 200, 200, 1)",
|
||||||
);
|
);
|
||||||
|
|
||||||
new Chart(ctx, {
|
pieChartInstance = new Chart(ctx, {
|
||||||
type: "pie",
|
type: "pie",
|
||||||
data: {
|
data: {
|
||||||
labels: chartData.labels,
|
labels: chartData.labels,
|
||||||
@@ -1059,7 +1066,12 @@
|
|||||||
function renderDurationChart(chartData) {
|
function renderDurationChart(chartData) {
|
||||||
const ctx = document.getElementById("durationChart").getContext("2d");
|
const ctx = document.getElementById("durationChart").getContext("2d");
|
||||||
|
|
||||||
new Chart(ctx, {
|
// Destroy existing chart instance if it exists
|
||||||
|
if (durationChartInstance) {
|
||||||
|
durationChartInstance.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
durationChartInstance = new Chart(ctx, {
|
||||||
type: "bar",
|
type: "bar",
|
||||||
data: {
|
data: {
|
||||||
labels: chartData.labels,
|
labels: chartData.labels,
|
||||||
|
|||||||
Reference in New Issue
Block a user