Fix: Wealth Distribution Histogram not showing data
- Enhanced debug logging in simulation.js to track distribution data flow - Improved data handling in updateCharts() with proper validation - Added fallback mechanisms in onSimulationComplete() for distribution data - Enhanced updateSimulationProgress() with better array copying - Added comprehensive error handling and debug output - Created .gitignore to exclude Python cache files The histogram should now properly display wealth distribution data during simulations.
This commit is contained in:
@@ -252,6 +252,11 @@ function initializeCharts() {
|
||||
|
||||
// Wealth Distribution Chart
|
||||
const distributionCtx = document.getElementById('distributionChart');
|
||||
console.log('Initializing distribution chart:', {
|
||||
hasCanvas: !!distributionCtx,
|
||||
canvasId: distributionCtx ? distributionCtx.id : 'not found'
|
||||
});
|
||||
|
||||
if (distributionCtx) {
|
||||
charts.distribution = new Chart(distributionCtx, {
|
||||
type: 'bar',
|
||||
@@ -293,6 +298,9 @@ function initializeCharts() {
|
||||
}
|
||||
}
|
||||
});
|
||||
console.log('✅ Distribution chart initialized successfully');
|
||||
} else {
|
||||
console.error('❌ Distribution chart canvas not found!');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -605,9 +613,23 @@ function updateSimulationProgress(data) {
|
||||
currentSimulation.data.top10Share.push(data.wealth_concentration_top10 || 0);
|
||||
|
||||
// Update distribution data if available
|
||||
console.log('Received simulation progress data:', {
|
||||
hasDistribution: !!data.distribution,
|
||||
distribution: data.distribution
|
||||
});
|
||||
|
||||
if (data.distribution && data.distribution.labels && data.distribution.counts) {
|
||||
currentSimulation.data.distribution.labels = data.distribution.labels;
|
||||
currentSimulation.data.distribution.counts = data.distribution.counts;
|
||||
console.log('Updating distribution data:', {
|
||||
labelsLength: data.distribution.labels.length,
|
||||
countsLength: data.distribution.counts.length,
|
||||
labels: data.distribution.labels,
|
||||
counts: data.distribution.counts
|
||||
});
|
||||
|
||||
currentSimulation.data.distribution.labels = [...data.distribution.labels];
|
||||
currentSimulation.data.distribution.counts = [...data.distribution.counts];
|
||||
} else {
|
||||
console.log('No valid distribution data in progress update');
|
||||
}
|
||||
|
||||
updateCharts();
|
||||
@@ -635,10 +657,28 @@ function updateCharts() {
|
||||
}
|
||||
|
||||
// Wealth Distribution Chart
|
||||
if (charts.distribution && currentSimulation.data.distribution.labels.length > 0) {
|
||||
charts.distribution.data.labels = currentSimulation.data.distribution.labels;
|
||||
charts.distribution.data.datasets[0].data = currentSimulation.data.distribution.counts;
|
||||
charts.distribution.update('none');
|
||||
if (charts.distribution) {
|
||||
const distData = currentSimulation.data.distribution;
|
||||
console.log('Updating distribution chart with data:', {
|
||||
hasLabels: !!(distData.labels && distData.labels.length > 0),
|
||||
hasCounts: !!(distData.counts && distData.counts.length > 0),
|
||||
labelsLength: distData.labels ? distData.labels.length : 0,
|
||||
countsLength: distData.counts ? distData.counts.length : 0,
|
||||
labels: distData.labels,
|
||||
counts: distData.counts
|
||||
});
|
||||
|
||||
if (distData.labels && distData.labels.length > 0 &&
|
||||
distData.counts && distData.counts.length > 0) {
|
||||
charts.distribution.data.labels = distData.labels;
|
||||
charts.distribution.data.datasets[0].data = distData.counts;
|
||||
charts.distribution.update('none');
|
||||
console.log('✅ Distribution chart updated successfully');
|
||||
} else {
|
||||
console.log('❌ Distribution chart not updated - insufficient data');
|
||||
}
|
||||
} else {
|
||||
console.log('❌ Distribution chart not found');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -678,10 +718,17 @@ async function onSimulationComplete(data) {
|
||||
|
||||
// Fetch complete simulation data and populate charts
|
||||
try {
|
||||
console.log('Fetching complete simulation data...');
|
||||
const response = await window.MarkovEconomics.utils.apiRequest(
|
||||
`/api/simulation/${currentSimulation.id}/data?include_evolution=true&include_distribution=true`
|
||||
);
|
||||
|
||||
console.log('Complete simulation data received:', {
|
||||
hasEvolution: !!response.evolution,
|
||||
hasDistribution: !!response.distribution,
|
||||
distribution: response.distribution
|
||||
});
|
||||
|
||||
if (response.evolution) {
|
||||
// Clear and populate with complete data
|
||||
currentSimulation.data.iterations = response.evolution.iterations;
|
||||
@@ -691,9 +738,31 @@ async function onSimulationComplete(data) {
|
||||
currentSimulation.data.capitalShare = response.evolution.capital_shares || [];
|
||||
|
||||
// Update distribution data
|
||||
if (response.distribution) {
|
||||
currentSimulation.data.distribution.labels = response.distribution.labels;
|
||||
currentSimulation.data.distribution.counts = response.distribution.counts;
|
||||
if (response.distribution && response.distribution.labels && response.distribution.counts) {
|
||||
console.log('Setting final distribution data:', {
|
||||
labelsLength: response.distribution.labels.length,
|
||||
countsLength: response.distribution.counts.length,
|
||||
labels: response.distribution.labels,
|
||||
counts: response.distribution.counts
|
||||
});
|
||||
|
||||
currentSimulation.data.distribution.labels = [...response.distribution.labels];
|
||||
currentSimulation.data.distribution.counts = [...response.distribution.counts];
|
||||
} else {
|
||||
console.log('No distribution data in final response');
|
||||
// Fallback: try to get distribution data from dedicated endpoint
|
||||
try {
|
||||
const distResponse = await window.MarkovEconomics.utils.apiRequest(
|
||||
`/api/simulation/${currentSimulation.id}/distribution?bins=10`
|
||||
);
|
||||
if (distResponse.histogram && distResponse.histogram.labels && distResponse.histogram.counts) {
|
||||
console.log('Got distribution data from dedicated endpoint:', distResponse.histogram);
|
||||
currentSimulation.data.distribution.labels = [...distResponse.histogram.labels];
|
||||
currentSimulation.data.distribution.counts = [...distResponse.histogram.counts];
|
||||
}
|
||||
} catch (distError) {
|
||||
console.error('Failed to fetch distribution data from dedicated endpoint:', distError);
|
||||
}
|
||||
}
|
||||
|
||||
// Update charts with complete data
|
||||
|
Reference in New Issue
Block a user