Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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

```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
Expand Down
29 changes: 29 additions & 0 deletions app/assets/javascripts/echarts.themeloader.js
Original file line number Diff line number Diff line change
@@ -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);
});
};
})();