storybook/stories/sunburst/15_single_sunburst.story.tsx (45 lines of code) (raw):
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React from 'react';
import type { Datum, PartialTheme } from '@elastic/charts';
import { Chart, Partition, PartitionLayout, Settings, defaultPartitionValueFormatter } from '@elastic/charts';
import { mocks } from '@elastic/charts/src/mocks/hierarchical';
import type { ChartsStory } from '../../types';
import { useBaseTheme } from '../../use_base_theme';
import {
discreteColor,
colorBrewerCategoricalStark9,
countryLookup,
productLookup,
regionLookup,
} from '../utils/utils';
const theme: PartialTheme = {
partition: {
linkLabel: {
maxCount: 0,
fontSize: 14,
},
fontFamily: 'Arial',
fillLabel: {
fontStyle: 'italic',
fontWeight: 900,
valueFont: {
fontFamily: 'Menlo',
fontStyle: 'normal',
fontWeight: 100,
},
},
minFontSize: 1,
idealFontSizeJump: 1.1,
outerSizeRatio: 1,
emptySizeRatio: 0,
circlePadding: 4,
},
};
export const Example: ChartsStory = (_, { title, description }) => (
<Chart title={title} description={description}>
<Settings showLegend theme={theme} baseTheme={useBaseTheme()} />
<Partition
id="spec_1"
data={mocks.miniSunburst.slice(0, 1)}
layout={PartitionLayout.sunburst}
valueAccessor={(d: Datum) => d.exportVal as number}
valueFormatter={(d: number) => `$${defaultPartitionValueFormatter(Math.round(d / 1000000000))}\u00A0Bn`}
layers={[
{
groupByRollup: (d: Datum) => d.sitc1,
nodeLabel: (d: any) => productLookup[d].name,
shape: {
fillColor: (key, sortIndex) => discreteColor(colorBrewerCategoricalStark9, 0.7)(sortIndex),
},
},
{
groupByRollup: (d: Datum) => countryLookup[d.dest].continentCountry.slice(0, 2),
nodeLabel: (d: any) => regionLookup[d].regionName.replaceAll(/\s/g, '\u00A0'),
shape: {
fillColor: (key, sortIndex, node) =>
discreteColor(colorBrewerCategoricalStark9, 0.5)(node.parent.sortIndex),
},
},
{
groupByRollup: (d: Datum) => d.dest,
nodeLabel: (d: any) => countryLookup[d].name.replaceAll(/\s/g, '\u00A0'),
shape: {
fillColor: (key, sortIndex, node) =>
discreteColor(colorBrewerCategoricalStark9, 0.3)(node.parent.parent.sortIndex),
},
},
]}
/>
</Chart>
);