Components
Scatter Chart
A scatter chart visualizes relationships between two or three variables.
Import ScatterChart
Tremor exports one component to create a scatter chart.
import { ScatterChart } from "@tremor/react";
Usage example
The example below shows a chart composition combining a Card
with Title
and ScatterChart
components. Note: For the height, a number value has to be set
(e.g, h-72, or h-[500px]. Keep in mind that h-full won't work.)
Life expectancy vs. GDP per capita
As of 2015. Source: Our World in Data
import { Card, Title, Text, ScatterChart } from "@tremor/react"; const chartdata = [ { Country: "Argentina", Life_expectancy: 76.3, GDP: 13467.1236, Population: 43417765, }, { Country: "Australia", Life_expectancy: 82.8, GDP: 56554.3876, Population: 23789338, }, { Country: "Austria", Life_expectancy: 81.5, GDP: 43665.947, Population: 8633169, }, // ... ]; export default ScatterChartExample = () => ( <Card> <Title>Life expectancy vs. GDP per capita</Title> <Text>As of 2015. Source: Our World in Data </Text> <ScatterChart className="h-80 mt-6 -ml-2" yAxisWidth={50} data={chartdata} category="Country" x="GDP" y="Life_expectancy" size="Population" showOpacity={true} minYValue={60} valueFormatter={{ x: (amount) => `$${(amount / 1000).toFixed(1)}K`, y: (lifeExp) => `${lifeExp} yrs`, size: (population) => `${(population / 1000000).toFixed(1)}M people`, }} showLegend={false} /> </Card> );
Usage example with click event
The example below shows an interacive chart using the onValueChange
prop.
Life expectancy vs. GDP per capita
As of 2015. Source: Our World in Data
null
Returned upon onValueChange
import { Card, Title, Text, ScatterChart } from "@tremor/react"; import React from "react"; const chartdata = [ { Country: "Argentina", Life_expectancy: 76.3, GDP: 13467.1236, Population: 43417765, }, { Country: "Australia", Life_expectancy: 82.8, GDP: 56554.3876, Population: 23789338, }, { Country: "Austria", Life_expectancy: 81.5, GDP: 43665.947, Population: 8633169, }, // ... ]; export const ScatterChartExample2 = () => { const [value, setValue] = React.useState(null); return ( <> <Card> <Title>Life expectancy vs. GDP per capita</Title> <Text>As of 2015. Source: Our World in Data </Text> <ScatterChart className="h-80 mt-6 -ml-2" yAxisWidth={50} data={chartdata} category="Country" x="GDP" y="Life_expectancy" size="Population" showOpacity={true} minYValue={60} valueFormatter={{ x: (amount) => `$${(amount / 1000).toFixed(1)}K`, y: (lifeExp) => `${lifeExp} yrs`, size: (population) => `${(population / 1000000).toFixed(1)}M people`, }} onValueChange={(v) => setValue(v)} /> </Card> <pre>{JSON.stringify(value)}</pre> </> ); };
Theming
Scatter Chart
Element | Theme Token |
---|---|
Gridline color colorstremor-content-subtle | |
X and Y Axis labels color colorstremor-content-DEFAULT | |
X and Y Axis labels size fontSizetremor-label | |
Text color legend colorstremor-content-DEFAULT | |
Text color legend (hover) colorstremor-content-emphasis | |
Font size legend colorstremor-default | |
Background color legend (hover) colorstremor-background-subtle |
Chart Tooltip
Element | Theme Token |
---|---|
Border color colorstremor-border-DEFAULT | |
Text color head and numbers colorstremor-content-emphasis | |
Color ring around bulletpoint colorstremor-background-DEFAULT | |
Background color colorstremor-background-default | |
Text color label colorstremor-content-DEFAULT | |
Shadow boxShadowtremor-dropdown | |
Roundness borderRadiustremor-default | |
Font size head and numbers fontSizetremor-default |
API Reference: ScatterChart
client component
- data
- Description
- The source data, in which each entry is a dictionary.
- Type
- any[]
- Default
- Values
- x
- Description
- Select the series to populate the x axis.
- Type
- String
- Default
- Values
- y
- Description
- Select the series to populate the y axis.
- Type
- String
- Default
- Values
- category
- Description
- Used to assign different values for coloring data points according to your data.
- Type
- string
- Default
- Values
- E.g. 'continent'
- sizeoptional
- Description
- Select the series to set bubble size representing a third data dimension.
- Type
- string
- Default
- Values
- sizeRangeoptional
- Description
- Set the range of the size.
- Type
- number[]
- Default
- [1, 1000]
- Values
- E.g [1, 500]
- colorsoptional
- Description
- Change the default colors. When using Typescript, import the Color type provided by Tremor.
- Type
- Color[]
- Default
- Values
- E.g. ['blue', 'indigo'].
- valueFormatteroptional
- Description
- Controls the text formatting for the x, y, and z values. When using Typescript, import the ScatterChartValueFormatter type provided by Tremor.
- Type
- ScatterChartValueFormatter
- Default
- Values
- E.g. valueFormatter: {x: (x) => `${x} m`,y: (y) => `${y} cm`,size: (z) => `${z} kg`,}
- showOpacityoptional
- Description
- Add an opacity to the data points. Helpful when lot's of them overlap
- Type
- boolean
- Default
- false
- Values
- true, false
- startEndOnlyoptional
- Description
- Show only the first and last elements in the x-axis. Great for smaller charts or sparklines.
- Type
- boolean
- Default
- false
- Values
- true, false
- showXAxisoptional
- Description
- Controls the visibility of the X axis.
- Type
- boolean
- Default
- true
- Values
- true, false
- showYAxisoptional
- Description
- Controls the visibility of the Y axis.
- Type
- boolean
- Default
- true
- Values
- true, false
- yAxisWidthoptional
- Description
- Controls width of the vertical axis.
- Type
- number
- Default
- 56
- Values
- showAnimationoptional
- Description
- Sets an animation to the chart when it is loaded.
- Type
- boolean
- Default
- false
- Values
- true, false
- animationDurationoptional
- Description
- The animation duration in ms.
- Type
- number
- Default
- 900
- Values
- showTooltipoptional
- Description
- Controls the visibility of the tooltip.
- Type
- boolean
- Default
- true
- Values
- true, false
- showLegendoptional
- Description
- Controls the visibility of the legend.
- Type
- boolean
- Default
- true
- Values
- true, false
- showGridLinesoptional
- Description
- Controls the visibility of the gridlines within the plotted area.
- Type
- boolean
- Default
- true
- Values
- true, false
- autoMinXValueoptional
- Description
- Adjusts the minimum value in relation to the magnitude of the data for the X axis.
- Type
- boolean
- Default
- false
- Values
- true, false
- minXValueoptional
- Description
- Sets the minimum value of the shown chart data for the X axis.
- Type
- number
- Default
- Values
- maxXValueoptional
- Description
- Sets the maximum value of the shown chart data for the X axis.
- Type
- number
- Default
- Values
- autoMinYValueoptional
- Description
- Adjusts the minimum value in relation to the magnitude of the data for the Y axis.
- Type
- boolean
- Default
- false
- Values
- true, false
- minYValueoptional
- Description
- Sets the minimum value of the shown chart data for the Y axis.
- Type
- number
- Default
- Values
- maxYValueoptional
- Description
- Sets the maximum value of the shown chart data for the Y axis.
- Type
- number
- Default
- Values
- connectNullsoptional
- Description
- Connects datapoints that have null values between them
- Type
- boolean
- Default
- false
- Values
- true, false
- allowDecimalsoptional
- Description
- Controls if the ticks of a numeric axis are displayed as decimals or not.
- Type
- boolean
- Default
- true
- Values
- true, false
- noDataTextoptional
- Description
- The displayed text when the data is empty.
- Type
- string
- Default
- No Data
- Values
- onValueChangeoptional
- Description
- Callback function for when the value of the component changes.
- Type
- (value: T) => void
- Default
- Values
Need help? You can also ask the community on GitHub Discussions or Slack.