Plot
import { Plot, toPlotData } from '@dxos/react-ui-plot';npm install @dxos/react-ui-plotPlot draws a table of values as a chart. It fills its container and redraws when the container changes size. A
chart with more than one series has a legend.
| Prop | Type | Effect |
|---|---|---|
data |
PlotData |
Sets the columns and rows. The first column is the category or the x axis |
options.type |
'bar' | 'pie' | 'xy' | 'scatter' | 'heatmap' | 'radar' | 'map' |
Sets the kind of chart. 'xy' is a line chart |
options.grid |
boolean |
Draws grid lines, on a bar or line chart |
options.stacked |
boolean |
Stacks bars instead of standing them side by side |
options.horizontal |
boolean |
Runs bars across |
options.regression |
boolean |
Draws a trend line on a scatter chart |
Chart a table
Section titled “Chart a table”toPlotData turns rows into the chart’s data. With header, the first row names the columns. The same data works for
every type, so you switch charts by changing one option.
Example (Drawing one table as three kinds of chart)
import { function useState<S>(initialState: S | (() => S)): [S, React.Dispatch<React.SetStateAction<S>>] (+1 overload)
Returns a stateful value, and a function to update it.
useState } from 'react';import { function Plot({ classNames, data, options }: PlotProps): React.JSX.Element
Plot, type type PlotType = "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"const PlotType: Literals<readonly ["bar", "pie", "xy", "scatter", "heatmap", "radar", "map"]>
PlotType, const toPlotData: (values: PlotValue[][], { header }?: ToPlotDataOptions) => PlotData
Converts a 2D block of cell values (rows of columns) into PlotData.
toPlotData } from '@dxos/react-ui-plot';import * as import Button
Button from '@dxos/react-ui/Button';import * as import Toolbar
Toolbar from '@dxos/react-ui/Toolbar';
/** Rows of a table: the header names each column, and the first column labels the x axis. One series draws no legend. */const const SALES: PlotData
Rows of a table: the header names each column, and the first column labels the x axis. One series draws no legend.
SALES = function toPlotData(values: PlotValue[][], { header }?: ToPlotDataOptions): PlotData
Converts a 2D block of cell values (rows of columns) into PlotData.
toPlotData( [ ['Month', 'Sales'], ['Jan', 12], ['Feb', 18], ['Mar', 15], ['Apr', 22], ['May', 26], ['Jun', 24], ], { header?: boolean | undefined
The first row holds the series names.
header: true },);
const const TYPES: Record<string, "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map">
TYPES: type Record<K extends keyof any, T> = { [P in K]: T; }
Construct a type with a set of properties K of type T
Record<string, type PlotType = "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"
PlotType> = { type Bar: "bar"
Bar: 'bar', type Radar: "radar"
Radar: 'radar', type Pie: "pie"
Pie: 'pie',};
/** The same data as a bar, radar or pie chart. */export const const SalesChart: () => React.JSX.Element
The same data as a bar, radar or pie chart.
SalesChart = () => { const [const type: "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"
type, const setType: React.Dispatch<React.SetStateAction<"bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map">>
setType] = useState<"bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map">(initialState: "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map" | (() => "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map")): ["bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map", React.Dispatch<React.SetStateAction<"bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map">>] (+1 overload)
Returns a stateful value, and a function to update it.
useState<type PlotType = "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"
PlotType>('bar');
return ( <React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div React.HTMLAttributes<HTMLDivElement>.className?: string | undefined
className='grid h-full grid-rows-[min-content_1fr]'> <import Toolbar
Toolbar.const Root: React.ForwardRefExoticComponent<Omit<Toolbar.RootProps & { asChild?: boolean;}, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>export Root
role=toolbar with the roving focus that role promises (decision 9); items that overflow scroll along its axis in a
thin ScrollArea whose bar shows on hover, the toolbar itself being the viewport.
Root> {var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.ObjectConstructor.entries<"bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map">(o: { [s: string]: "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map";} | ArrayLike<"bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map">): [string, "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"][] (+1 overload)
Returns an array of key/values of the enumerable own properties of an object
entries(const TYPES: Record<string, "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map">
TYPES).Array<[string, "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"]>.map<React.JSX.Element>(callbackfn: (value: [string, "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"], index: number, array: [string, "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"][]) => React.JSX.Element, thisArg?: any): React.JSX.Element[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.
map(([label: string
label, value: "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"
value]) => ( <import Button
Button.const Root: React.ForwardRefExoticComponent<Omit<Button.RootProps, "className"> & { classNames?: ClassNameValue;} & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLButtonElement>>export Root
Root React.Attributes.key?: React.Key | null | undefined
key={value: "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"
value} variant?: Button.Variant | undefined
variant={value: "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"
value === const type: "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"
type ? 'primary' : 'ghost'} onClick?: React.MouseEventHandler<HTMLButtonElement> | undefined
onClick={() => const setType: (value: React.SetStateAction<"bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map">) => void
setType(value: "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"
value)} > {label: string
label} </import Button
Button.const Root: React.ForwardRefExoticComponent<Omit<Button.RootProps, "className"> & { classNames?: ClassNameValue;} & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLButtonElement>>export Root
Root> ))} </import Toolbar
Toolbar.const Root: React.ForwardRefExoticComponent<Omit<Toolbar.RootProps & { asChild?: boolean;}, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>export Root
role=toolbar with the roving focus that role promises (decision 9); items that overflow scroll along its axis in a
thin ScrollArea whose bar shows on hover, the toolbar itself being the viewport.
Root> <function Plot({ classNames, data, options }: PlotProps): React.JSX.Element
Plot data: PlotData
data={const SALES: PlotData
Rows of a table: the header names each column, and the first column labels the x axis. One series draws no legend.
SALES} options: { readonly type: "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"; readonly header?: boolean | undefined; readonly grid?: boolean | undefined; readonly horizontal?: boolean | undefined; readonly stacked?: boolean | undefined; readonly regression?: boolean | undefined;}
options={{ type: "bar" | "pie" | "xy" | "scatter" | "heatmap" | "radar" | "map"
type, grid?: boolean | undefined
Draw grid lines (bar and xy).
grid: true }} /> </React.JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div> );};
type is the only option the buttons change. grid stays on, and shows only on the bar chart, because grid lines
are for bar and line charts.
The story at the top of this page runs this example.