While gradients rarely add meaning to your data, it's an easy way to enhance the look of your charts.
Defining gradients in nivo is a 2 steps process, first you'll have to declare available definitions (the same goes for patterns) using dedicated helpers or providing plain objects.
Then you must define the rules to apply those definitions using the fill
property.
Separating gradient definitions from application allows us to reuse those in various places, like fills and borders, and while maintaining a direct mapping for a bar chart with 5 items can be simple enough, when you're dealing with a complex heatmap with tens of nodes it can quickly become cumbersome. Doing so also provides the ability to use a gradient depending on chart element value. Last but not least, gradient colors can be inherited from current node ones.
import { linearGradientDef } from '@nivo/core'import { Stream } from '@nivo/stream'const MyChart = () => (<Streamdata={[/*…*/]}keys={['react', 'vue', 'elm']}// 1. defining gradientsdefs={[// using helpers// will inherit colors from current elementlinearGradientDef('gradientA', [{ offset: 0, color: 'inherit' },{ offset: 100, color: 'inherit', opacity: 0 },]),linearGradientDef('gradientB', [{ offset: 0, color: '#000' },{ offset: 100, color: 'inherit' },],// you may specify transforms for your gradients, e.g. rotations and skews,// following the transform attribute format.// For instance here we rotate 90 degrees relative to the center of the object.{gradientTransform: 'rotate(90 0.5 0.5)'}),// using plain object{id: 'gradientC',type: 'linearGradient',colors: [{ offset: 0, color: '#faf047' },{ offset: 100, color: '#e4b400' },],},]}// 2. defining rules to apply those gradientsfill={[// match using object query{ match: { id: 'react' }, id: 'gradientA' },// match using function{ match: d => d.id === 'vue', id: 'gradientB' },// match all, will only affect 'elm', because once a rule match,// others are skipped, so now it acts as a fallback{ match: '*', id: 'gradientC' },]}/>)
Please be aware that gradient usage has some limitations, it's not supported for canvas chart implementations for now, and tooltips involving colored chips will use plain element color.