DEV Community

Cover image for Adapting CanvasJS Charts to System Default Themes for Enhanced User Experience
Manoj Mohan
Manoj Mohan

Posted on

Adapting CanvasJS Charts to System Default Themes for Enhanced User Experience

In today's digital landscape, users expect applications to seamlessly adapt to their system preferences, including dark and light modes. For data visualization libraries like CanvasJS, dynamically adjusting chart themes based on system settings can significantly improve user experience. This article demonstrates how to detect system theme preferences and apply corresponding styles to CanvasJS charts.

Why Theme Adaptation Matters

Modern operating systems and browsers allow users to specify preferred color schemes. By respecting these preferences, developers can:

  • Create visually consistent experiences
  • Reduce eye strain in low-light environments
  • Improve accessibility
  • Increase user engagement

Understanding CanvasJS Themes

CanvasJS comes with built-in themes like "light1", "light2", "dark1", and "dark2", which you can apply directly to your charts:

  • light1 and light2: These themes provide a light background with different color palettes for data points.
  • dark1 and dark2: These offer dark backgrounds, which are especially useful for applications with a dark mode.

By default, if no theme is specified, CanvasJS will use "light1". However, for a more dynamic user interface, adjusting the chart theme based on the system's theme (light or dark mode) can make your application appear more cohesive and user-friendly.

Implementing System Theme Detection

  1. Detecting Color Scheme Preferences The CSS Media Query prefers-color-scheme allows JavaScript to detect system theme preferences:
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
let selectedTheme = isDarkMode  ? "dark2" : "light2";
Enter fullscreen mode Exit fullscreen mode

Applying the Theme to CanvasJS

Once you've determined the appropriate theme, applying it to your CanvasJS chart is straightforward:

var chart = new CanvasJS.Chart("chartContainer", {
  theme: selectedTheme, // selected theme from above match media query
  // Other chart options...
});
chart.render();
Enter fullscreen mode Exit fullscreen mode

Dynamic Theme Switching

For applications with light/dark mode toggling, ensure charts update dynamically by monitoring system preferences with a change event listener, as shown below.

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
  selectedTheme = e.matches ? "dark2" : "light2";
  chart.options.theme = newTheme;
  chart.render();
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

By dynamically adjusting CanvasJS charts to reflect system color schemes, you're not just customizing the look but enhancing usability and comfort. This approach respects user preferences, potentially increases engagement, and ensures your charts are accessible and aesthetically pleasing in any environment. Remember, a good user experience is about making technology adapt to the user, not the other way around.

This article provides both the conceptual understanding and the practical steps needed to integrate CanvasJS with system color schemes, ensuring that your charts are not only functional but also visually harmonious with the user's environment.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay