maher-tradingview-api
Version:
Tradingview instant stocks API, indicator alerts, trading bot, and more !
197 lines (171 loc) • 8.3 kB
JavaScript
/* eslint-disable linebreak-style */
/* eslint-disable no-restricted-properties */
/* eslint-disable linebreak-style */
/* eslint-disable no-unused-vars */
/* eslint-disable prefer-const */
/* eslint-disable linebreak-style */
/* eslint-disable space-before-function-paren */
/* eslint-disable no-const-assign */
/* eslint-disable arrow-parens */
/* eslint-disable no-use-before-define */
/* eslint-disable comma-dangle */
/* eslint-disable no-plusplus */
/* eslint-disable no-trailing-spaces */
/* eslint-disable object-shorthand */
/* eslint-disable func-names */
/* eslint-disable linebreak-style */
/* eslint-disable no-undef */
/* eslint-disable linebreak-style */
/**
* chart-fixer.js
* أداة لإصلاح مشاكل الرسوم البيانية وضمان أنها تعمل بشكل صحيح
*/
// عند تحميل النافذة بالكامل
window.addEventListener('load', function() {
console.log('تم تحميل الصفحة بالكامل، بدء فحص وإصلاح الرسوم البيانية...');
// التحقق من وجود الرسم البياني الرئيسي
const priceChart = document.getElementById('priceChart');
if (priceChart) {
fixChart(priceChart, 'الرسم البياني الرئيسي');
}
// التحقق من المؤشرات الفنية
const technicalCharts = ['maChart', 'emaChart', 'bbChart', 'rsiChart', 'macdChart', 'volumeChart', 'stochasticChart', 'obvChart', 'adxChart', 'trendChart'];
technicalCharts.forEach(chartId => {
const chartElement = document.getElementById(chartId);
if (chartElement) {
fixChart(chartElement, `مؤشر ${chartId.replace('Chart', '')}`);
}
});
});
// وظيفة لإصلاح الرسم البياني
function fixChart(canvas, chartName) {
try {
console.log(`فحص الرسم البياني: ${chartName}...`);
// التحقق مما إذا كان الرسم البياني تم تهيئته بالفعل
const chart = Chart.getChart(canvas);
if (!chart) {
console.warn(`لم يتم العثور على رسم بياني لـ ${chartName}، محاولة إعادة الإنشاء...`);
recreateChart(canvas.id, chartName);
} else {
// التحقق مما إذا كان الرسم البياني يحتوي على بيانات
if (!chart.data || !chart.data.datasets || chart.data.datasets.length === 0 ||
chart.data.datasets.every(ds => !ds.data || ds.data.length === 0)) {
console.warn(`الرسم البياني ${chartName} فارغ، محاولة إعادة الإنشاء...`);
chart.destroy();
recreateChart(canvas.id, chartName);
} else {
console.log(`✓ الرسم البياني ${chartName} يعمل بشكل صحيح`);
}
}
} catch (error) {
console.error(`خطأ أثناء فحص الرسم البياني ${chartName}:`, error);
showChartError(canvas, `حدث خطأ في الرسم البياني: ${error.message}`);
}
}
// إعادة إنشاء رسم بياني
function recreateChart(chartId, chartName) {
try {
if (!window.candleData || !Array.isArray(window.candleData) || window.candleData.length === 0) {
console.error('لا توجد بيانات متاحة لإعادة إنشاء الرسم البياني');
showChartError(document.getElementById(chartId), 'لا توجد بيانات متاحة');
return;
}
if (chartId === 'priceChart' && window.ChartRenderer) {
// إعادة إنشاء الرسم البياني الرئيسي
const data = window.candleData;
const labels = data.map(candle => typeof candle.time === 'string' ? candle.time.split(',')[0] : candle.time);
const values = data.map(candle => parseFloat(candle.close));
window.ChartRenderer.renderPriceChart(chartId, labels, values, '');
console.log('تم إعادة إنشاء الرسم البياني الرئيسي');
} else if (window.TechnicalIndicators) {
// إعادة إنشاء المؤشرات الفنية
const type = chartId.replace('Chart', '').toLowerCase();
switch (type) {
case 'ma':
window.TechnicalIndicators.renderMAChart(chartId, window.candleData, [20, 50, 200]);
break;
case 'bb':
window.TechnicalIndicators.renderBollingerBandsChart(chartId, window.candleData, 20, 2);
break;
case 'rsi':
window.TechnicalIndicators.renderRSIChart(chartId, window.candleData, 14);
break;
case 'macd':
window.TechnicalIndicators.renderMACDChart(chartId, window.candleData, 12, 26, 9);
break;
default:
console.warn(`نوع الرسم البياني غير معروف: ${type}`);
return;
}
console.log(`تم إعادة إنشاء مؤشر ${type}`);
} else {
console.error('مكتبات الرسم البياني غير متوفرة');
}
} catch (error) {
console.error(`خطأ أثناء إعادة إنشاء الرسم البياني ${chartName}:`, error);
showChartError(document.getElementById(chartId), 'فشلت محاولة إعادة الإنشاء');
}
}
// عرض رسالة خطأ على الرسم البياني
function showChartError(canvas, message) {
if (!canvas) return;
try {
const ctx = canvas.getContext('2d');
// تنظيف الـ canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// رسم خلفية
ctx.fillStyle = '#f9f9f9';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// رسم حدود حمراء
ctx.strokeStyle = '#e74c3c';
ctx.lineWidth = 2;
ctx.strokeRect(2, 2, canvas.width - 4, canvas.height - 4);
// رسم رسالة الخطأ
ctx.fillStyle = '#e74c3c';
ctx.font = 'bold 14px Arial';
ctx.textAlign = 'center';
ctx.fillText('خطأ في الرسم البياني', canvas.width / 2, canvas.height / 2 - 15);
// رسم تفاصيل الخطأ
ctx.fillStyle = '#666';
ctx.font = '12px Arial';
ctx.fillText(message, canvas.width / 2, canvas.height / 2 + 15);
// إضافة تعليمات
ctx.fillText('انقر هنا لإعادة المحاولة', canvas.width / 2, canvas.height / 2 + 40);
// إضافة مستمع نقر لإعادة المحاولة
canvas.onclick = function() {
location.reload();
};
// جعل الـ canvas قابل للنقر
canvas.style.cursor = 'pointer';
} catch (error) {
console.error('خطأ أثناء عرض رسالة الخطأ على الرسم البياني:', error);
}
}
// إضافة دعم للنافذة المنبثقة لإصلاح الرسوم البيانية
function createFixButton() {
const button = document.createElement('button');
button.textContent = 'إصلاح الرسوم البيانية';
button.style.cssText = 'position: fixed; bottom: 20px; right: 20px; background-color: #0088cc; color: white; border: none; padding: 10px 15px; border-radius: 4px; cursor: pointer; font-weight: bold; z-index: 9999; box-shadow: 0 2px 5px rgba(0,0,0,0.2);';
button.onclick = function() {
if (confirm('هل ترغب في محاولة إصلاح جميع الرسوم البيانية؟')) {
location.reload();
}
};
document.body.appendChild(button);
}
// تشغيل بعد ثواني قليلة للتأكد من تحميل كل شيء
setTimeout(function() {
try {
// التحقق من حالة الرسوم البيانية
const priceChart = Chart.getChart(document.getElementById('priceChart'));
// إذا لم يكن هناك رسم بياني رئيسي، أضف زر الإصلاح
if (!priceChart) {
createFixButton();
} else if (priceChart && (!priceChart.data || !priceChart.data.datasets || priceChart.data.datasets.length === 0)) {
createFixButton();
}
} catch (error) {
console.error('خطأ أثناء فحص حالة الرسوم البيانية:', error);
createFixButton();
}
}, 3000);