« home

Color Bar

Here’s a ColorBar with tick labels, using the new tick_side prop:

color_scale=Viridis   tick_side=primary   range=0,1
00.250.50.751
color_scale=Magma   tick_side=secondary   range=100,1631
020040060080010001200140016001800
color_scale=Cividis   tick_side=primary   range=-99.9812,-10
−100−80−60−40−200
<script>
  import { ColorBar } from '$lib'
</script>

{#each [
    // [color_scale, tick_side, tick_labels, range, label_text]
    [`Viridis`, `primary`, [0, 0.25, 0.5, 0.75, 1], [0, 1]],
    [`Magma`, `secondary`, 10, [100, 1631]],
    [`Cividis`, `primary`, 4, [-99.9812, -10]],
  ] as
  [color_scale, tick_side, tick_labels, range]
}
  <ColorBar
    title="color_scale={color_scale} &emsp; tick_side={tick_side} &emsp; range={range}"
    {color_scale}
    {tick_side}
    {tick_labels}
    {range}
    tick_format=".4"
    title_style="padding: 3pt;"
    --cbar-padding="3em"
  />
{/each}

You can make fat and skinny bars:

00.20.40.60.81

Viridis
00.51

00.51

<script>
  import { ColorBar } from '$lib'

  const wrapper_style = 'margin: auto;'
</script>

<ColorBar {wrapper_style} style="width: 10em; height: 8pt" />
<br />
<ColorBar
  title="Viridis"
  {wrapper_style}
  style="width: 4em; height: 2em"
  tick_labels={2}
/>
<br />
<ColorBar {wrapper_style} --cbar-width="10em" --cbar-height="2em" tick_labels={3} />
<br />

PeriodicTable.svelte heatmap example with ColorBar inside TableInset

<script>
  import { element_data } from '$lib'
  import { ColorBar, ColorScaleSelect } from '$lib/plot'
  import { PeriodicTable, PropertySelect, TableInset } from '$lib/periodic-table'

  let color_scale = $state(`interpolateCividis`)
  let heatmap_key = $state(``)
  let heat_label = $state(``)
  let heatmap_values = $derived(
    heatmap_key ? element_data.map((el) => el[heatmap_key]) : [],
  )
  let heat_range = $derived(
    heatmap_key ? [Math.min(...heatmap_values), Math.max(...heatmap_values)] : [0, 1],
  )
</script>

<form>
  <ColorScaleSelect bind:value={color_scale} minSelect={1} selected={[color_scale]} />
  <PropertySelect bind:key={heatmap_key} bind:value={heat_label} />
</form>

<PeriodicTable
  {heatmap_values}
  style="margin: 2em auto 4em"
  bind:color_scale
  color_scale_range={heat_range}
  links="name"
>
  {#snippet inset()}
    <TableInset style="place-items: center; padding: 2em">
      <ColorBar
        {color_scale}
        title={heat_label}
        range={heat_range}
        tick_labels={5}
        tick_side="primary"
        --cbar-width="calc(100% - 2em)"
      />
    </TableInset>
  {/snippet}
</PeriodicTable>
1 H Hydrogen 2 He Helium 3 Li Lithium 4 Be Beryllium 5 B Boron 6 C Carbon 7 N Nitrogen 8 O Oxygen 9 F Fluorine 10 Ne Neon 11 Na Sodium 12 Mg Magnesium 13 Al Aluminium 14 Si Silicon 15 P Phosphorus 16 S Sulfur 17 Cl Chlorine 18 Ar Argon 19 K Potassium 20 Ca Calcium 21 Sc Scandium 22 Ti Titanium 23 V Vanadium 24 Cr Chromium 25 Mn Manganese 26 Fe Iron 27 Co Cobalt 28 Ni Nickel 29 Cu Copper 30 Zn Zinc 31 Ga Gallium 32 Ge Germanium 33 As Arsenic 34 Se Selenium 35 Br Bromine 36 Kr Krypton 37 Rb Rubidium 38 Sr Strontium 39 Y Yttrium 40 Zr Zirconium 41 Nb Niobium 42 Mo Molybdenum 43 Tc Technetium 44 Ru Ruthenium 45 Rh Rhodium 46 Pd Palladium 47 Ag Silver 48 Cd Cadmium 49 In Indium 50 Sn Tin 51 Sb Antimony 52 Te Tellurium 53 I Iodine 54 Xe Xenon 55 Cs Cesium 56 Ba Barium 57 La Lanthanum 58 Ce Cerium 59 Pr Praseodymium 60 Nd Neodymium 61 Pm Promethium 62 Sm Samarium 63 Eu Europium 64 Gd Gadolinium 65 Tb Terbium 66 Dy Dysprosium 67 Ho Holmium 68 Er Erbium 69 Tm Thulium 70 Yb Ytterbium 71 Lu Lutetium 72 Hf Hafnium 73 Ta Tantalum 74 W Tungsten 75 Re Rhenium 76 Os Osmium 77 Ir Iridium 78 Pt Platinum 79 Au Gold 80 Hg Mercury 81 Tl Thallium 82 Pb Lead 83 Bi Bismuth 84 Po Polonium 85 At Astatine 86 Rn Radon 87 Fr Francium 88 Ra Radium 89 Ac Actinium 90 Th Thorium 91 Pa Protactinium 92 U Uranium 93 Np Neptunium 94 Pu Plutonium 95 Am Americium 96 Cm Curium 97 Bk Berkelium 98 Cf Californium 99 Es Einsteinium 100 Fm Fermium 101 Md Mendelevium 102 No Nobelium 103 Lr Lawrencium 104 Rf Rutherfordium 105 Db Dubnium 106 Sg Seaborgium 107 Bh Bohrium 108 Hs Hassium 109 Mt Meitnerium 110 Ds Darmstadtium 111 Rg Roentgenium 112 Cn Copernicum 113 Nh Nihonium 114 Fl Flerovium 115 Mc Moscovium 116 Lv Livermorium 117 Ts Tennessine 118 Og Oganesson
57-71 La-Lu Lanthanides
89-103 Ac-Lr Actinides

Example demonstrating title_side and tick_side interaction:

title=top
tick=primary
Label
0510
title=top
tick=secondary
Label
0102030
title=top
tick=inside
Label
10203040
title=bottom
tick=primary
Label
101520
title=bottom
tick=secondary
Label
10203040
title=bottom
tick=inside
Label
20304050
title=left
tick=primary
Label
202224262830
title=left
tick=secondary
Label
20253035404550
title=left
tick=inside
Label
30405060
title=right
tick=primary
Label
303234363840
title=right
tick=secondary
Label
30354045505560
title=right
tick=inside
Label
40506070
<script>
  import { ColorBar } from '$lib'

  const title_sides = [`top`, `bottom`, `left`, `right`]
  const tick_sides = [`primary`, `secondary`, `inside`]
</script>

<section>
  {#each title_sides as title_side, l_idx}
    {#each tick_sides as tick_side, t_idx}
      {@const orientation = title_side === `top` || title_side === `bottom`
      ? `horizontal`
      : `vertical`}
      {@const bar_style = orientation === `horizontal`
      ? `width: 150px; height: 20px;`
      : `width: 20px; height: 150px;`}
      {@const num_ticks = l_idx + t_idx + 2}
      {@const current_range = [l_idx * 10, (l_idx + 1) * 10 + t_idx * 20]}
      <div>
        <code>title={title_side}<br />tick={tick_side}</code>
        <ColorBar
          {title_side}
          {tick_side}
          {orientation}
          style={bar_style}
          title="Label"
          tick_labels={num_ticks}
          range={current_range}
          --cbar-tick-overlap-offset="10px"
          --cbar-tick-label-color={tick_side === `inside` ? `white` : `currentColor`}
        />
      </div>
    {/each}
  {/each}
</section>

Date/Time Ranges

You can format tick labels for date/time ranges by providing a D3 format string via the tick_format prop. The color bar accepts ranges as milliseconds since the epoch (standard JavaScript Date.getTime()).

YYYY-MM-DD
2023-11-142024-07-032025-02-19
Month Day
Nov 14Jan 11Mar 09May 06Jul 03Aug 30Oct 27Dec 24Feb 19
Vertical - Mmm DD, YY
Nov 14, '23Mar 09, '24Jul 03, '24Oct 27, '24Feb 19, '25
<script>
  import { ColorBar } from '$lib'

  // Example date range (e.g., start and end of 2024)
  const date_range = [
    new Date(2024, 0, 1).getTime(), // Jan 1, 2024
    new Date(2024, 11, 31).getTime(), // Dec 31, 2024
  ]
</script>

<div style="display: flex; column; gap: 2em; align-items: center;">
  <ColorBar
    title="YYYY-MM-DD"
    range={date_range}
    tick_format="%Y-%m-%d"
    style="width: 200px; margin-left: 3em;"
    tick_labels={2}
  />

  <ColorBar
    title="Month Day"
    range={date_range}
    style="width: 500px; margin-left: 3em;"
    tick_format="%b %d"
    tick_labels={7}
  />

  <ColorBar
    title="Vertical - Mmm DD, YY"
    range={date_range}
    tick_format="%b %d, '%y"
    tick_labels={4}
    orientation="vertical"
    style="height: 200px;"
  />
</div>

Large Value Ranges (Linear and Log)

Demonstrating the color bar with large numeric ranges, using both linear and logarithmic scales (scale_type='log'). Log scales require a positive range (both min and max > 0). Scientific notation is used for tick labels via tick_format='.0e'.

Large Linear Range (0 to 1e6)
0200k400k600k800k1M
Large Log Range (1 to 1e9)
1101001k10k100k1M10M100M1G
Vertical Log Range (10 to 1e7)
101001k10k100k1M10M
Vertical Linear Range
(10 to 1e7) with line breaks
02M4M6M8M10M
Small Log Range (0.01 to 100)
0.010.1110100
<script>
  import { ColorBar } from '$lib'
</script>

<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 4em; place-items: center; margin: 2em 0">
  <ColorBar
    title="Large Linear Range (0 to 1e6)"
    range={[0, 1e6]}
    tick_labels={5}
    tick_format=".1s"
  />

  <ColorBar
    title="Large Log Range (1 to 1e9)"
    range={[1, 1e9]}
    scale_type="log"
    style="width: 400px"
    tick_labels={10}
  />

  <ColorBar
    title="Vertical Log Range (10 to 1e7)"
    range={[10, 1e7]}
    scale_type="log"
    orientation="vertical"
  />

  <ColorBar
    title="Vertical Linear Range<br>(10 to 1e7) with line breaks"
    title_style="margin: 1em;"
    range={[10, 1e7]}
    orientation="vertical"
  />

  <ColorBar
    title="Small Log Range (0.01 to 100)"
    range={[0.01, 100]}
    scale_type="log"
    tick_format=".3"
    style="width: 400px"
    tick_labels={5}
  />
</div>