This commit is contained in:
Dobromir Popov
2025-10-23 19:43:41 +03:00
parent 0225f4df58
commit de2ad92602
5 changed files with 379 additions and 96 deletions

View File

@@ -14,14 +14,15 @@ class ChartManager {
}
/**
* Initialize charts for all timeframes
* Initialize charts for all timeframes with pivot bounds
*/
initializeCharts(chartData) {
initializeCharts(chartData, pivotBounds = null) {
console.log('Initializing charts with data:', chartData);
console.log('Pivot bounds:', pivotBounds);
this.timeframes.forEach(timeframe => {
if (chartData[timeframe]) {
this.createChart(timeframe, chartData[timeframe]);
this.createChart(timeframe, chartData[timeframe], pivotBounds);
}
});
@@ -32,7 +33,7 @@ class ChartManager {
/**
* Create a single chart for a timeframe
*/
createChart(timeframe, data) {
createChart(timeframe, data, pivotBounds = null) {
const plotId = `plot-${timeframe}`;
const plotElement = document.getElementById(plotId);
@@ -129,7 +130,49 @@ class ChartManager {
scrollZoom: true
};
Plotly.newPlot(plotId, [candlestickTrace, volumeTrace], layout, config);
// Prepare chart data with pivot bounds
const chartData = [candlestickTrace, volumeTrace];
// Add pivot levels if available
if (pivotBounds && pivotBounds.support_levels && pivotBounds.resistance_levels) {
// Add support levels
pivotBounds.support_levels.forEach((level, index) => {
chartData.push({
x: data.timestamps,
y: Array(data.timestamps.length).fill(level),
type: 'scatter',
mode: 'lines',
line: {
color: '#28a745',
width: 1,
dash: 'dash'
},
name: `Support ${index + 1}`,
showlegend: index === 0, // Only show legend for first support level
hovertemplate: `Support: $%{y:.2f}<extra></extra>`
});
});
// Add resistance levels
pivotBounds.resistance_levels.forEach((level, index) => {
chartData.push({
x: data.timestamps,
y: Array(data.timestamps.length).fill(level),
type: 'scatter',
mode: 'lines',
line: {
color: '#dc3545',
width: 1,
dash: 'dash'
},
name: `Resistance ${index + 1}`,
showlegend: index === 0, // Only show legend for first resistance level
hovertemplate: `Resistance: $%{y:.2f}<extra></extra>`
});
});
}
Plotly.newPlot(plotId, chartData, layout, config);
// Store chart reference
this.charts[timeframe] = {
@@ -204,29 +247,74 @@ class ChartManager {
}
/**
* Update charts with new data
* Update charts with new data including pivot levels
*/
updateCharts(newData) {
updateCharts(newData, pivotBounds = null) {
Object.keys(newData).forEach(timeframe => {
if (this.charts[timeframe]) {
const plotId = this.charts[timeframe].plotId;
Plotly.react(plotId, [
// Prepare chart data
const chartData = [
{
x: newData[timeframe].timestamps,
open: newData[timeframe].open,
high: newData[timeframe].high,
low: newData[timeframe].low,
close: newData[timeframe].close,
type: 'candlestick'
type: 'candlestick',
name: 'Price'
},
{
x: newData[timeframe].timestamps,
y: newData[timeframe].volume,
type: 'bar',
yaxis: 'y2'
yaxis: 'y2',
name: 'Volume',
marker: { color: 'rgba(0, 123, 255, 0.3)' }
}
]);
];
// Add pivot levels if available
if (pivotBounds && pivotBounds.support_levels && pivotBounds.resistance_levels) {
// Add support levels
pivotBounds.support_levels.forEach((level, index) => {
chartData.push({
x: newData[timeframe].timestamps,
y: Array(newData[timeframe].timestamps.length).fill(level),
type: 'scatter',
mode: 'lines',
line: {
color: '#28a745',
width: 1,
dash: 'dash'
},
name: `Support ${index + 1}`,
showlegend: index === 0, // Only show legend for first support level
hovertemplate: `Support: $%{y:.2f}<extra></extra>`
});
});
// Add resistance levels
pivotBounds.resistance_levels.forEach((level, index) => {
chartData.push({
x: newData[timeframe].timestamps,
y: Array(newData[timeframe].timestamps.length).fill(level),
type: 'scatter',
mode: 'lines',
line: {
color: '#dc3545',
width: 1,
dash: 'dash'
},
name: `Resistance ${index + 1}`,
showlegend: index === 0, // Only show legend for first resistance level
hovertemplate: `Resistance: $%{y:.2f}<extra></extra>`
});
});
}
Plotly.react(plotId, chartData);
}
});
}