Fix wealth distribution histogram issue by correcting wealth calculation and improving data handling
This commit is contained in:
@@ -21,6 +21,59 @@ window.testWebSocket = async function() {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Test distribution data generation
|
||||
* Call from browser console: testDistributionData()
|
||||
*/
|
||||
window.testDistributionData = async function() {
|
||||
debugLog('=== DISTRIBUTION DATA TEST ===');
|
||||
|
||||
try {
|
||||
// First check if we have a simulation ID
|
||||
const simulationId = currentSimulation.id;
|
||||
if (!simulationId) {
|
||||
debugLog('❌ No active simulation found');
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch distribution data directly from API
|
||||
debugLog('Fetching distribution data from API...');
|
||||
const response = await fetch(`/api/simulation/${simulationId}/distribution?bins=5`);
|
||||
const data = await response.json();
|
||||
|
||||
debugLog('Distribution API response', data);
|
||||
|
||||
if (data.histogram) {
|
||||
debugLog('Histogram data', {
|
||||
labels: data.histogram.labels,
|
||||
counts: data.histogram.counts,
|
||||
total_agents: data.histogram.total_agents
|
||||
});
|
||||
|
||||
// Try to update chart with this data
|
||||
if (charts.distribution && data.histogram.labels && data.histogram.counts) {
|
||||
debugLog('Attempting to update chart with API data...');
|
||||
try {
|
||||
charts.distribution.data.labels = data.histogram.labels;
|
||||
charts.distribution.data.datasets[0].data = data.histogram.counts;
|
||||
charts.distribution.update();
|
||||
debugLog('✅ Chart updated successfully with API data');
|
||||
} catch (error) {
|
||||
debugLog('❌ Error updating chart with API data', error);
|
||||
}
|
||||
} else {
|
||||
debugLog('❌ Cannot update chart - missing chart instance or data');
|
||||
}
|
||||
} else {
|
||||
debugLog('❌ No histogram data in response');
|
||||
}
|
||||
} catch (error) {
|
||||
debugLog('❌ Error fetching distribution data', error);
|
||||
}
|
||||
|
||||
debugLog('=== DISTRIBUTION DATA TEST COMPLETE ===');
|
||||
};
|
||||
|
||||
/**
|
||||
* Enhanced distribution update test function
|
||||
* Call from browser console: testDistributionUpdate()
|
||||
@@ -483,10 +536,10 @@ function initializeCharts() {
|
||||
charts.distribution = new Chart(distributionCtx, {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: [],
|
||||
labels: [], // Initialize with empty arrays
|
||||
datasets: [{
|
||||
label: 'Number of Agents',
|
||||
data: [],
|
||||
data: [], // Initialize with empty array
|
||||
backgroundColor: 'rgba(40, 167, 69, 0.7)',
|
||||
borderColor: '#28a745',
|
||||
borderWidth: 1
|
||||
@@ -515,7 +568,8 @@ function initializeCharts() {
|
||||
title: {
|
||||
display: true,
|
||||
text: 'Number of Agents'
|
||||
}
|
||||
},
|
||||
beginAtZero: true // Ensure y-axis starts at zero
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -963,7 +1017,13 @@ function updateSimulationProgress(data) {
|
||||
socketConnected: window.MarkovEconomics ? window.MarkovEconomics.isConnected : 'unknown'
|
||||
});
|
||||
|
||||
if (data.distribution && data.distribution.labels && data.distribution.counts) {
|
||||
// More robust handling of distribution data
|
||||
if (data.distribution &&
|
||||
Array.isArray(data.distribution.labels) &&
|
||||
Array.isArray(data.distribution.counts) &&
|
||||
data.distribution.labels.length > 0 &&
|
||||
data.distribution.counts.length > 0) {
|
||||
|
||||
debugLog('Updating distribution data', {
|
||||
labelsLength: data.distribution.labels.length,
|
||||
countsLength: data.distribution.counts.length,
|
||||
@@ -971,6 +1031,7 @@ function updateSimulationProgress(data) {
|
||||
counts: data.distribution.counts
|
||||
});
|
||||
|
||||
// Store the distribution data properly
|
||||
currentSimulation.data.distribution.labels = [...data.distribution.labels];
|
||||
currentSimulation.data.distribution.counts = [...data.distribution.counts];
|
||||
} else {
|
||||
@@ -1015,11 +1076,16 @@ function updateCharts() {
|
||||
canvasElement: document.getElementById('distributionChart')
|
||||
});
|
||||
|
||||
// Ensure we have valid data before updating
|
||||
if (distData.labels && distData.labels.length > 0 &&
|
||||
distData.counts && distData.counts.length > 0) {
|
||||
try {
|
||||
charts.distribution.data.labels = distData.labels;
|
||||
charts.distribution.data.datasets[0].data = distData.counts;
|
||||
// Make sure we're working with arrays
|
||||
const labels = Array.isArray(distData.labels) ? distData.labels : [];
|
||||
const counts = Array.isArray(distData.counts) ? distData.counts : [];
|
||||
|
||||
charts.distribution.data.labels = labels;
|
||||
charts.distribution.data.datasets[0].data = counts;
|
||||
charts.distribution.update('none');
|
||||
debugLog('✅ Distribution chart updated successfully');
|
||||
} catch (error) {
|
||||
|
Reference in New Issue
Block a user