Color Bar
ColorBar with tick labels; compare tick_side:
Interactive example
svelte<script lang="ts">
import { ColorBar } from 'matterviz'
</script>
{#each [// [scale, tick_side, tick_labels, range, label_text]
[`interpolateViridis`, `primary`, [0, 0.25, 0.5, 0.75, 1], [0, 1]], [`interpolateMagma`, `secondary`, 10, [100, 1631]], [`interpolateCividis`, `primary`, 4, [-99.9812, -10]]] as [scale, tick_side, tick_labels, range]}
<ColorBar
title="scheme={scale}   tick_side={tick_side}   range={range}"
color_scale={{ scheme: scale, value_range: range }}
{tick_side}
{tick_labels}
tick_format=".4"
title_style="padding: 3pt;"
--cbar-padding="3em"
/>
{/each}You can make fat and skinny bars:
Interactive example
svelte<script lang="ts">
import { ColorBar } from 'matterviz'
</script>
<div style="--cbar-margin: auto">
<ColorBar bar_style="width: 10em; height: 8pt" />
<br />
<ColorBar title="Viridis" bar_style="width: 4em; height: 2em" tick_labels={2} />
<br />
<ColorBar --cbar-width="10em" --cbar-height="2em" tick_labels={3} />
</div>PeriodicTable shows a ColorBar when heatmap_values is provided.
Interactive example
svelte<script lang="ts">
import type { D3InterpolateName } from 'matterviz/colors'
import { element_data, type ChemicalElement } from 'matterviz/element'
import { ELEM_HEATMAP_LABELS } from 'matterviz/labels'
import { ColorScaleSelect } from 'matterviz/plot'
import { PeriodicTable, PropertySelect } from 'matterviz/periodic-table'
let color_scale = $state<D3InterpolateName>(`interpolateCividis`)
let heatmap_key = $state<keyof ChemicalElement | null>(`atomic_radius`)
let heat_label = $derived(
Object.keys(ELEM_HEATMAP_LABELS).find(
(label) => ELEM_HEATMAP_LABELS[label] === heatmap_key,
),
)
let heatmap_values = $derived.by(() => {
const key = heatmap_key
return key ? element_data.map((element) => element[key]) : []
})
</script>
<form>
<ColorScaleSelect bind:value={color_scale} min_select={1} />
<PropertySelect bind:key={heatmap_key} />
</form>
<PeriodicTable
{heatmap_values}
style="margin: 2em auto 4em"
{color_scale}
color_bar={{ title: heat_label }}
links="name"
/>For more control, you can also manually add a ColorBar inside a custom TableInset (which overrides the automatic color bar):
Interactive example
svelte<script lang="ts">
import { element_data } from 'matterviz/element'
import { ColorBar } from 'matterviz/plot'
import { PeriodicTable, TableInset } from 'matterviz/periodic-table'
const heatmap_values = element_data.map((element) => element.atomic_mass)
const heat_range = [Math.min(...heatmap_values), Math.max(...heatmap_values)]
</script>
<PeriodicTable
{heatmap_values}
style="margin: 2em auto"
color_scale={{ scheme: `interpolateInferno`, value_range: heat_range }}
>
{#snippet inset()}
<TableInset style="place-items: center; padding: 2em">
<ColorBar
color_scale={{ scheme: `interpolateInferno`, value_range: heat_range }}
title="Atomic Mass (u)"
tick_labels={5}
tick_side="primary"
--cbar-width="calc(100% - 2em)"
/>
</TableInset>
{/snippet}
</PeriodicTable>Example demonstrating title_side and tick_side interaction:
Interactive example
svelte<script lang="ts">
import { ColorBar } from 'matterviz'
const title_sides = [`top`, `bottom`, `left`, `right`]
const tick_sides = [`primary`, `secondary`, `inside`]
</script>
<section>
{#each title_sides as title_side, l_idx (title_side)}
{#each tick_sides as tick_side, t_idx (tick_side)}
{@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}
{bar_style}
title="Label"
tick_labels={num_ticks}
color_scale={{ value_range: current_range }}
/>
</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()).
Interactive example
svelte<script lang="ts">
import { ColorBar } from 'matterviz'
// 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>
<section>
<ColorBar
title="YYYY-MM-DD"
color_scale={{ value_range: date_range }}
tick_format="%Y-%m-%d"
bar_style="width: 200px"
tick_labels={2}
/>
<ColorBar
title="Month Day"
color_scale={{ value_range: date_range }}
bar_style="width: 500px"
tick_format="%b %d"
tick_labels={7}
/>
<ColorBar
title="Vertical - Mmm DD, YY"
color_scale={{ value_range: date_range }}
tick_format="%b %d, '%y"
tick_labels={4}
orientation="vertical"
bar_style="height: 200px;"
/>
</section>Interactive Property and Color Scale Selection
Use property_options, selected_property_key, and on_property_change for property selection. The caller owns the data: set loading while fetching, then update the selected key and range together when the request succeeds. Palette selection follows the same pattern: on_color_scale_change requests a key, and the caller commits it through selected_color_scale_key. The caller derives color_scale from that same key so the chart and legend share one color mapping. Palette options only provide keys and labels.
Interactive example
svelte<script lang="ts">
import { ColorBar } from 'matterviz'
// Property options with different data ranges
const property_options = [
{ key: `formation_energy`, label: `Formation Energy`, unit: `eV/atom` },
{ key: `band_gap`, label: `Band Gap`, unit: `eV` },
{ key: `volume`, label: `Volume`, unit: `ų/atom` },
{ key: `density`, label: `Density`, unit: `g/cm³` },
{ key: `bulk_modulus`, label: `Bulk Modulus`, unit: `GPa` },
]
// Simulated data ranges for each property
const property_ranges = {
formation_energy: [-2.5, 1.5],
band_gap: [0, 8],
volume: [8, 45],
density: [1.5, 22],
bulk_modulus: [5, 450],
}
const color_scales = {
viridis: `interpolateViridis`,
plasma: `interpolatePlasma`,
inferno: `interpolateInferno`,
magma: `interpolateMagma`,
cividis: `interpolateCividis`,
turbo: `interpolateTurbo`,
}
const color_scale_options = Object.keys(color_scales).map((key) => ({
key,
label: key[0].toUpperCase() + key.slice(1),
}))
// State
let selected_property = $state(`formation_energy`)
let selected_color_scale = $state(`viridis`)
let current_range = $derived(property_ranges[selected_property])
</script>
<ColorBar
title="Formation Energy (eV/atom)"
tick_labels={5}
{property_options}
selected_property_key={selected_property}
on_property_change={(key) => (selected_property = key)}
{color_scale_options}
color_scale={{ scheme: color_scales[selected_color_scale], value_range: current_range }}
selected_color_scale_key={selected_color_scale}
on_color_scale_change={(key) => (selected_color_scale = key)}
--cbar-width="600px"
--cbar-padding="2em"
/>Vertical orientation with the title on different sides:
Interactive example
svelte<script lang="ts">
import { ColorBar } from 'matterviz'
const property_options = [
{ key: `energy`, label: `Energy`, unit: `eV` },
{ key: `force`, label: `Force`, unit: `eV/Å` },
{ key: `stress`, label: `Stress`, unit: `GPa` },
]
const color_scales = {
blues: `interpolateBlues`,
reds: `interpolateReds`,
greens: `interpolateGreens`,
}
const color_scale_options = Object.keys(color_scales).map((key) => ({
key,
label: key[0].toUpperCase() + key.slice(1),
}))
const ranges = {
energy: [-5, 2],
force: [0, 15],
stress: [-100, 100],
}
let prop_left = $state(`energy`)
let prop_right = $state(`force`)
let scale_left = $state(`blues`)
let scale_right = $state(`blues`)
let range_left = $derived(ranges[prop_left])
let range_right = $derived(ranges[prop_right])
</script>
<section>
<ColorBar
title="Energy"
orientation="vertical"
title_side="left"
{property_options}
selected_property_key={prop_left}
on_property_change={(key) => (prop_left = key)}
{color_scale_options}
color_scale={{ scheme: color_scales[scale_left], value_range: range_left }}
selected_color_scale_key={scale_left}
on_color_scale_change={(key) => (scale_left = key)}
bar_style="height: 200px;"
/>
<ColorBar
title="Force"
orientation="vertical"
title_side="right"
{property_options}
selected_property_key={prop_right}
on_property_change={(key) => (prop_right = key)}
{color_scale_options}
color_scale={{ scheme: color_scales[scale_right], value_range: range_right }}
selected_color_scale_key={scale_right}
on_color_scale_change={(key) => (scale_right = key)}
bar_style="height: 200px;"
/>
</section>Large Value Ranges (Linear and Log)
Large numeric ranges on linear and log scales (color_scale={{ type: 'log' }}). Log needs a positive range (min and max > 0). The linear bar uses SI-prefixed ticks via tick_format='.1s'; bars without tick_format use the default tick formatter.
Interactive example
svelte<script lang="ts">
import { ColorBar } from 'matterviz'
</script>
<section>
<ColorBar
title="Large Linear Range (0 to 1e6)"
color_scale={{ value_range: [0, 1e6] }}
tick_labels={5}
tick_format=".1s"
/>
<ColorBar
title="Large Log Range (1 to 1e9)"
color_scale={{ value_range: [1, 1e9], type: `log` }}
bar_style="width: 400px"
tick_labels={10}
/>
<ColorBar
title="Vertical Log Range (10 to 1e7)"
color_scale={{ value_range: [10, 1e7], type: `log` }}
orientation="vertical"
/>
<ColorBar
title="Vertical Linear Range<br>(10 to 1e7) with line breaks"
title_style="margin: 1em;"
color_scale={{ value_range: [10, 1e7] }}
orientation="vertical"
/>
<ColorBar
title="Small Log Range (0.01 to 100)"
color_scale={{ value_range: [0.01, 100], type: `log` }}
tick_format=".3"
bar_style="width: 400px"
tick_labels={5}
/>
</section>Arcsinh Scale: Symmetric Ranges Including Negative Values
Interactive example
svelte<script lang="ts">
import { ColorBar } from 'matterviz'
const scale_types = [`linear`, `log`, `arcsinh`]
let scale_type = $state(`arcsinh`)
let threshold = $state(1)
let type = $derived(scale_type === `arcsinh` ? { type: `arcsinh`, threshold } : scale_type)
</script>
<div class="demo-controls">
<fieldset>
<legend>Scale Type</legend>
{#each scale_types as scale (scale)}
<label>
<input type="radio" bind:group={scale_type} value={scale} />
{scale}
</label>
{/each}
</fieldset>
{#if scale_type === `arcsinh`}
<label>
Threshold: {threshold}
<input type="range" bind:value={threshold} min="0.1" max="100" step="0.1" />
</label>
{/if}
</div>
<section>
<ColorBar
title="Symmetric Range (-1000 to 1000)"
color_scale={{ scheme: `interpolateRdBu`, value_range: [-1000, 1000], type }}
tick_labels={7}
bar_style="width: 350px"
/>
<ColorBar
title="Asymmetric Range (-100 to 1000)"
color_scale={{ scheme: `interpolatePuOr`, value_range: [-100, 1000], type }}
tick_labels={6}
bar_style="width: 350px"
/>
<ColorBar
title="Vertical Arcsinh (-500 to 500)"
orientation="vertical"
color_scale={{ scheme: `interpolateBrBG`, value_range: [-500, 500], type }}
bar_style="height: 200px"
/>
<ColorBar
title="Near-Zero Focus (-10 to 10)"
color_scale={{ scheme: `interpolatePiYG`, value_range: [-10, 10], type }}
tick_labels={5}
bar_style="width: 350px"
/>
</section>