Skip to content

Styling & Theming

NewChart JS has a three-layer styling system. Each layer overrides the previous:

Defaults  <  JS Config  <  CSS Custom Properties

This means you can theme charts entirely from CSS without touching JavaScript, or override everything from JS, or use a combination of both.


1. JS Config (style.*)

Pass visual settings in the style object when creating a chart:

js
NewChart.create('#chart', {
  type: 'bar',
  data: { ... },
  style: {
    background: '#1a1a2e',
    fontFamily: "'Roboto', sans-serif",
    fontSize: 13,
    fontColor: '#e0e0e0',
    grid: {
      color: '#333',
      width: 1
    },
    axis: {
      color: '#666',
      width: 1,
      fontSize: 11
    },
    animation: {
      duration: 800,
      easing: 'easeOutQuart'
    },
    tooltip: {
      background: '#2a2a3e',
      color: '#ffffff',
      fontSize: 11,
      padding: 10,
      borderRadius: 6,
      shadow: '0 8px 24px rgba(0,0,0,0.35)'
    },
    legend: {
      fontSize: 11,
      color: '#374151',
      marker: { size: 10 }
    },
    bar: {
      borderRadius: 4,
      gap: 0.2,
      groupGap: 0.5
    }
  }
});

Global style properties

PropertyTypeDefaultDescription
style.backgroundstring'#ffffff'Chart background color
style.fontFamilystring'Inter', system stackPrimary font family
style.monoFamilystring'JetBrains Mono', system monoMonospace font for values
style.fontSizenumber12Base font size (px)
style.fontColorstring'#374151'Base text color

Grid

PropertyTypeDefaultDescription
style.grid.colorstring'#E5E7EB'Grid line color
style.grid.widthnumber1Grid line width

Axis

PropertyTypeDefaultDescription
style.axis.colorstring'#374151'Axis line and label color
style.axis.widthnumber1Axis line width
style.axis.fontSizenumber12Axis label font size

Animation

PropertyTypeDefaultDescription
style.animation.durationnumber600Animation duration (ms). Set 0 to disable
style.animation.easingstring'easeOutCubic'Easing function name

Available easing functions:

NameDescription
linearConstant speed
easeInQuadAccelerating
easeOutQuadDecelerating
easeInOutQuadAccelerate then decelerate
easeInCubicAccelerating (steeper)
easeOutCubicDecelerating (steeper) - default
easeInOutCubicSmooth S-curve
easeInQuartAccelerating (steep)
easeOutQuartDecelerating (steep)
easeInOutQuartSharp S-curve
easeInQuintAccelerating (steepest)
easeOutQuintDecelerating (steepest)
easeInOutQuintSharpest S-curve

Spring physics animation is also available via the springAnimate() API for custom use.

Tooltip

PropertyTypeDefaultDescription
style.tooltip.backgroundstring'#1F2937'Background
style.tooltip.colorstring'#FFFFFF'Text color
style.tooltip.fontSizenumber12Font size
style.tooltip.paddingnumber8Inner padding
style.tooltip.borderRadiusnumber4Corner radius
style.tooltip.shadowstring'0 10px 15px...'Box shadow

Legend

PropertyTypeDefaultDescription
style.legend.fontSizenumber12Label font size
style.legend.colorstring'#374151'Label text color
style.legend.marker.sizenumber8Color marker width

2. CSS Custom Properties (--nc-* tokens)

Theme charts using standard CSS custom properties on the container element. This is the recommended approach for theming because it:

  • Works with CSS classes, media queries, and :root
  • Enables dark mode with zero JS
  • Lets designers style charts without touching application code

Enabling/Disabling

CSS token resolution is enabled by default. To disable:

js
{ options: { cssTokens: false } }

Token Reference

All tokens follow the pattern --nc-{category}-{property}:

Global

CSS TokenMaps toType
--nc-backgroundstyle.backgroundcolor
--nc-font-familystyle.fontFamilystring
--nc-font-sizestyle.fontSizenumber
--nc-font-colorstyle.fontColorcolor

Grid

CSS TokenMaps toType
--nc-grid-colorstyle.grid.colorcolor
--nc-grid-widthstyle.grid.widthnumber

Axis

CSS TokenMaps toType
--nc-axis-colorstyle.axis.colorcolor
--nc-axis-widthstyle.axis.widthnumber
--nc-axis-font-sizestyle.axis.fontSizenumber

Animation

CSS TokenMaps toType
--nc-animation-durationstyle.animation.durationnumber (ms)

Tooltip

CSS TokenMaps toType
--nc-tooltip-backgroundstyle.tooltip.backgroundcolor
--nc-tooltip-colorstyle.tooltip.colorcolor
--nc-tooltip-font-sizestyle.tooltip.fontSizenumber
--nc-tooltip-paddingstyle.tooltip.paddingnumber
--nc-tooltip-border-radiusstyle.tooltip.borderRadiusnumber
--nc-tooltip-shadowstyle.tooltip.shadowstring

Legend

CSS TokenMaps toType
--nc-legend-font-sizestyle.legend.fontSizenumber
--nc-legend-colorstyle.legend.colorcolor
--nc-legend-marker-sizestyle.legend.marker.sizenumber

Bar

CSS TokenMaps toType
--nc-bar-border-radiusstyle.bar.borderRadiusnumber
--nc-bar-gapstyle.bar.gapnumber
--nc-bar-group-gapstyle.bar.groupGapnumber

Line

CSS TokenMaps toType
--nc-line-widthstyle.line.widthnumber
--nc-line-tensionstyle.line.tensionnumber
--nc-line-point-radiusstyle.line.pointRadiusnumber
--nc-line-point-border-widthstyle.line.pointBorderWidthnumber
--nc-line-point-border-colorstyle.line.pointBorderColorcolor

Pie / Donut

CSS TokenMaps toType
--nc-pie-border-widthstyle.pie.borderWidthnumber
--nc-pie-border-colorstyle.pie.borderColorcolor
--nc-pie-inner-radiusstyle.pie.innerRadiusnumber

Palette

CSS TokenMaps toType
--nc-palette-1 through --nc-palette-10Color palette slotscolor

Example: Dark Theme

css
.dark-theme {
  --nc-background: #1a1a2e;
  --nc-font-color: #e0e0e0;
  --nc-grid-color: #2a2a4a;
  --nc-axis-color: #8888aa;
  --nc-tooltip-background: #16162a;
  --nc-tooltip-color: #e0e0e0;
  --nc-tooltip-shadow: 0 8px 24px rgba(0,0,0,0.5);
  --nc-palette-1: #7c3aed;
  --nc-palette-2: #06b6d4;
  --nc-palette-3: #f59e0b;
  --nc-palette-4: #ef4444;
  --nc-palette-5: #10b981;
}
html
<div id="chart" class="dark-theme" style="width: 600px; height: 400px;"></div>

Example: Brand Colors

css
.brand-theme {
  --nc-palette-1: #0052CC;
  --nc-palette-2: #00B8D9;
  --nc-palette-3: #36B37E;
  --nc-palette-4: #FF5630;
  --nc-palette-5: #6554C0;
  --nc-font-family: 'Company Sans', sans-serif;
}

Example: Dark Mode Toggle

css
@media (prefers-color-scheme: dark) {
  .chart-container {
    --nc-background: #0d1117;
    --nc-font-color: #c9d1d9;
    --nc-grid-color: #21262d;
    --nc-axis-color: #8b949e;
    --nc-tooltip-background: #161b22;
    --nc-tooltip-color: #c9d1d9;
  }
}

Programmatic Token List

Get all supported token names at runtime:

js
const tokens = NewChart.getSupportedTokens();
// ['--nc-background', '--nc-font-family', '--nc-font-size', ...]

3. Color Palette

The default palette contains 10 colors:

IndexColorHexUsage
0Blue#4c6ef5Primary
1Green#0ca678Success
2Orange#f08c00Warning
3Red#e03131Danger
4Purple#7048e8
5Teal#1098ad
6Pink#d6336c
7Light blue#5c7cfa
8Mint#20c997
9Yellow#fcc419

The compare/previous-period color is #b3bac5 (available as NewChart.COMPARE_COLOR).

Override palette

Per dataset:

js
datasets: [{ label: 'A', values: [...], color: '#ff6b6b' }]

Per slice (PieChart):

js
datasets: [{ values: [...], colors: ['#ff6b6b', '#51cf66', '#339af0'] }]

Entire palette via JS:

js
{ palette: ['#264653', '#2a9d8f', '#e9c46a', '#f4a261', '#e76f51'] }

Entire palette via CSS:

css
.my-chart {
  --nc-palette-1: #264653;
  --nc-palette-2: #2a9d8f;
  --nc-palette-3: #e9c46a;
  --nc-palette-4: #f4a261;
  --nc-palette-5: #e76f51;
}

When more datasets exist than palette colors, colors cycle from the beginning.


4. Typography

NewChart uses two font stacks:

PropertyDefaultUsed for
style.fontFamily'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serifLabels, legend, axis text
style.monoFamily'JetBrains Mono', 'SF Mono', 'Fira Code', monospaceValues in tooltips, data table, gauge center

Override via JS config:

js
{
  style: {
    fontFamily: "'Roboto', sans-serif",
    monoFamily: "'Roboto Mono', monospace"
  }
}

Or via CSS:

css
.my-chart { --nc-font-family: 'Roboto', sans-serif; }

5. Responsive Behavior

Charts are responsive by default via ResizeObserver:

js
{
  options: {
    responsive: true,         // Enable resize observer (default: true)
    maintainAspectRatio: true  // Preserve aspect ratio on resize
  }
}

The chart container must have a defined width and height (via CSS or inline style). Minimum rendered size is 300x300 px.


6. Disabling Animations

js
// Per chart
{ style: { animation: { duration: 0 } } }

// Via CSS
.no-animation { --nc-animation-duration: 0; }

// Respect user preference
@media (prefers-reduced-motion: reduce) {
  .chart-container { --nc-animation-duration: 0; }
}

Priority & Merge Order

When multiple sources define the same property, the highest-priority source wins:

1. CSS Custom Properties (--nc-*)     ← highest priority
2. JS config passed to create()
3. Chart-type defaults (BAR_DEFAULTS, LINE_DEFAULTS, etc.)
4. Global defaults (DEFAULT_CONFIG)   ← lowest priority

Example: if you set style.background: '#fff' in JS but also define --nc-background: #000 in CSS on the container, the chart background will be black (CSS wins).

The update() method merges with the current config (which already includes CSS overrides). To force a JS value over CSS, either remove the CSS property or disable CSS tokens:

js
{ options: { cssTokens: false } }

Released under the MIT License.