Chart Switcher
<zn-chart-switcher>
ZnChartSwitcher
Shows one chart at a time from a set, picked by metric and by what the metric is broken down by, or the chart’s data as a table.
<zn-chart-switcher caption="Trends" description="Last 5 days"> <zn-chart metric="Revenue" by="Project" type="area" smooth categories="["Mon","Tue","Wed","Thu","Fri"]" data="[{"name":"Checkout","data":[120,180,150,210,240]},{"name":"Billing","data":[80,60,90,110,95]}]"></zn-chart> <zn-chart metric="Revenue" by="Connector" type="area" smooth categories="["Mon","Tue","Wed","Thu","Fri"]" data="[{"name":"Stripe","data":[150,170,160,230,250]},{"name":"PayPal","data":[50,70,80,90,85]}]"></zn-chart> <zn-chart metric="Refunds" by="Project" type="area" smooth categories="["Mon","Tue","Wed","Thu","Fri"]" data="[{"name":"Checkout","data":[12,8,15,9,11]},{"name":"Billing","data":[4,6,3,5,7]}]"></zn-chart> <zn-chart metric="Refunds" by="Connector" type="area" smooth categories="["Mon","Tue","Wed","Thu","Fri"]" data="[{"name":"Stripe","data":[10,9,12,10,13]},{"name":"PayPal","data":[6,5,6,4,5]}]"></zn-chart> <zn-chart metric="Errors" type="bar" categories="["Mon","Tue","Wed","Thu","Fri"]" data="[{"name":"Declined","data":[5,3,7,4,6]}]"></zn-chart> </zn-chart-switcher>
Examples
Metrics and Breakdowns
Give each chart a metric. Charts that share a metric but split it differently also set
by, and a By select appears beside the metric pills for metrics with more than
one breakdown. The chosen breakdown is kept when switching to another metric that offers it.
The caption, an optional muted description, and the controls sit in the card’s header, since
they only change this card.
Every chart keeps its size while hidden, so the panel’s height never changes as you switch.
Remembering the Choice
Set store-key to remember the chosen metric and breakdown for the session, or add
local-storage to keep it across sessions. This keeps the choice when the switcher is replaced,
for example when a time range reloads the content around it.
<zn-chart-switcher caption="Trends" store-key="transactions-trends"> ... </zn-chart-switcher>
Default Choice
Set metric and by to choose what shows before anything is stored.
<zn-chart-switcher caption="Trends" metric="Refunds" by="Connector"> ... </zn-chart-switcher>
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component from the CDN using a script tag:
<script type="module" src="https://cdn.jsdelivr.net/npm/@kubex/zinc@2.0.37/dist/components/chart-switcher/chart-switcher.js"></script>
To import this component from the CDN using a JavaScript import:
import 'https://cdn.jsdelivr.net/npm/@kubex/zinc@2.0.37/dist/components/chart-switcher/chart-switcher.js';
To import this component using a bundler:
import '@kubex/zinc/dist/components/chart-switcher/chart-switcher.js';
Slots
| Name | Description |
|---|---|
| (default) |
The charts. Each sets a metric, plus by when its metric is offered with
more than one breakdown.
|
Learn more about using slots.
Properties
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
description
|
Muted text beside the caption, such as the period the charts cover. |
string
|
''
|
|
metric
|
The metric shown, and the default before a stored choice exists. Falls back to the first chart’s metric. |
|
string
|
''
|
by
|
The breakdown shown, kept across metrics that offer it. Falls back to the metric’s first breakdown. |
|
string
|
''
|
storeKey
store-key
|
Remembers the chosen metric and breakdown under this key, so they survive the switcher being re-rendered. |
string
|
''
|
|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Parts
| Name | Description |
|---|---|
base |
The panel holding the controls and charts. |
Learn more about customizing CSS parts.
Dependencies
This component automatically imports the following dependencies.
<zn-button><zn-dropdown><zn-example><zn-menu><zn-menu-item><zn-navbar><zn-panel>