From 814c0653bdc5940ed63ae790030e31334e13b7d5 Mon Sep 17 00:00:00 2001 From: Umair Abid Date: Sun, 7 Sep 2025 16:17:28 -0700 Subject: [PATCH 1/4] Add load theme function to load themes with window context --- app/assets/javascripts/echarts.themeloader.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 app/assets/javascripts/echarts.themeloader.js diff --git a/app/assets/javascripts/echarts.themeloader.js b/app/assets/javascripts/echarts.themeloader.js new file mode 100644 index 0000000..546bbe7 --- /dev/null +++ b/app/assets/javascripts/echarts.themeloader.js @@ -0,0 +1,29 @@ +(function() { + window.RailsCharts = window.RailsCharts || {}; + window.RailsCharts.loadedThemes = window.RailsCharts.loadedThemes || []; + + window.RailsCharts.loadTheme = function(themeName) { + document.addEventListener('DOMContentLoaded', () => { + if (typeof echarts === 'undefined') { + console.error('ECharts is not loaded. Please ensure echarts.js is included.'); + return; + } + + if (window.RailsCharts.loadedThemes.includes(themeName)) { + console.warn(`Theme '${themeName}' is already loaded.`); + return; + } + + const script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = `/assets/echarts/theme/${themeName}.js`; + script.onload = () => { + console.log(`Theme '${themeName}' loaded successfully.`); + }; + script.onerror = () => { + console.error(`Failed to load theme: /assets/echarts/theme/${themeName}.js`); + }; + document.head.appendChild(script); + }); + }; +})(); From 085ef26347835010ff22ff8b70780b1229191709 Mon Sep 17 00:00:00 2001 From: Umair Abid Date: Tue, 9 Sep 2025 18:41:34 -0700 Subject: [PATCH 2/4] Update readme --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 45466f3..37d21ee 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,27 @@ import "echarts/theme/dark" 4) customize charts if needed. See available options or [official documentation](https://echarts.apache.org/examples/en/index.html). +### Loading Themes + +Themes can be loaded as shown in example enough. However, in some cases where +themes are included in environment where `this` does not point to window, you +might get errors. In that case, you can use loadTheme helper to load themes by +name. For example, instead of + +```javascript +import 'echarts/theme/dark' +``` +you can do + +```javascript +// application.js +import "echarts" +import "echarts.themeloader" + +// Load the desired theme dynamically +RailsCharts.loadTheme('dark'); +``` + ## Options ```ruby From a5c309d57fd3821c17b17132ba91a37f805907ee Mon Sep 17 00:00:00 2001 From: Umair Abid Date: Tue, 9 Sep 2025 18:43:19 -0700 Subject: [PATCH 3/4] Fix typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 37d21ee..4467066 100644 --- a/README.md +++ b/README.md @@ -131,8 +131,8 @@ import "echarts/theme/dark" ### Loading Themes -Themes can be loaded as shown in example enough. However, in some cases where -themes are included in environment where `this` does not point to window, you +Themes can be loaded as shown in example above. However, in some cases where +themes are included in environment where `this` does not point to `window`, you might get errors. In that case, you can use loadTheme helper to load themes by name. For example, instead of From 950a8f6c1a905ec831293b138c3e124ccb53c2b1 Mon Sep 17 00:00:00 2001 From: Umair Abid Date: Tue, 9 Sep 2025 18:44:06 -0700 Subject: [PATCH 4/4] Fix Grammar --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4467066..1a80a04 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ import "echarts/theme/dark" ### Loading Themes -Themes can be loaded as shown in example above. However, in some cases where +Themes can be loaded as shown in examples above. However, in some cases where themes are included in environment where `this` does not point to `window`, you might get errors. In that case, you can use loadTheme helper to load themes by name. For example, instead of