You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Complete documentation is **coming soon**. For now, please refer to the examples for any missing documentation.
6
+
> 🚨 This documentation is for **Version 3.0.0, which is currently in beta and is a work-in-progress**. For now, please refer to the examples for any missing documentation.
7
7
8
8
## Memoize your Props!
9
9
10
-
As you'll see in every example, the React Charts `<Chart>` component expects all props and options to be memoized using either `React.useMemo` or `React.useCallback`. While passing an unmemoized option/prop to the `<Chart>` component won't severly break any visible functionality, your charts will be severly non-performant. Internally, React Charts uses the immutable nature of these options/props to detect changes to the configuration and update accordingly.
11
-
12
-
While this may feel heavy at first, it gives you, the dev, full control over when you want to update your charts. To trigger and update, simply trigger one of your `React.useMemo` or `React.useCallback` hooks on the part of the config that you would like to update!
10
+
The React Charts `<Chart>` component has a few options that need to be **stable** or **memoized using either `React.useMemo` or `React.useCallback`**. Using an unstable option incorrectly shouldn't severly break any basic functionality, but could results in infinite change-detection loops in your app or at the very least, your charts will be severly non-performant. If an option says it needs to be stable, it's not kidding around!
13
11
14
12
## Data Model
15
13
16
14
React Charts uses a data shape based on **arrays of series and nested arrays of datums in those series**.
17
15
18
16
```js
19
-
// series array
20
17
constdata= [
21
18
{
22
-
// individual series
23
19
label:'Purchases',
24
-
// datum array
25
20
data: [
26
21
{
27
-
// individual datum
28
-
primary:'Apples', // primary value
29
-
secondary:20, // secondary value
22
+
date:newDate(),
23
+
stars:299320,
30
24
},
25
+
// ...etc
31
26
],
32
27
},
33
28
]
34
29
```
35
30
36
31
Visualization data can come in practically any shape and size, so **memoization of data into this shape is almost always necessary**.
The individual datums in a series' `data` array can be anything you want! Just remember that most of the types for React Query will require you to pass the type of your `Datum`s as the first generic type to work correctly.
60
+
91
61
## Axes
92
62
93
-
React Charts supports an `axes` prop that handles both the underlying scale and visual rendering of all axes. Multiple axes can be combined and configured to plot data however you'd like.
63
+
React Charts uses axes to configure a fair amount of the chart. Axes handle many things like:
64
+
65
+
- Accessing chart values from your series' `Datum`s
66
+
- Positioning your axis on the grid
67
+
- Configuring the scale type for your axis
68
+
- Configuring the element type for series that are tied to your axis
94
69
95
70
To date, we have the following scale types available:
96
71
97
72
-`linear` - A continuous axis used for plotting numerical data on an evenly distributed scale. Works well both as a **primary and secondary** axis.
98
-
-`ordinal` - A banded axis commonly used to plot categories or ordinal information. Works well as the **primary** axis for bar charts.
99
-
-`time` - A continuous axis used for plotting localized times and dates on an evenly distributed scale. Works well as a **primary** axis.
100
-
-`utc` - Similar to the `time` scale, but supports UTC datetimes instead of localized datetimes. Works well as a **primary** axis.
73
+
-`band` - A banded axis commonly used to plot categories or ordinal information. Works well as the **primary** axis for bar charts with ordinal domains.
74
+
-`time` - A continuous axis used for plotting UTC `Date`s on an evenly distributed scale. Works well as a **primary** axis.
75
+
-`timeLocal` - Similar to the `time` scale, but uses localized `Date` objects instead of UTC. Works well as a **primary** axis.
101
76
-`log` - A continuous axis used for plotting numerical data on a logarithmically distributed scale. Works well as a **secondary** axis
102
77
103
-
Axes are a required component of a React Chartand can used like so:
78
+
Axes are a required component of a React Chart. Both a `primaryAxis`and at least one axis vis `secondaryAxes` must be configured.
104
79
105
80
```javascript
106
81
import { Chart } from'react-charts'
107
82
83
+
type MyDatum = { date:Date, stars: number }
84
+
108
85
functionMyChart() {
109
-
constaxes=React.useMemo(
110
-
() => [
111
-
{ primary:true, type:'time', position:'bottom' },
112
-
{ type:'linear', position:'left' },
86
+
constdata= [
87
+
{
88
+
label:'React Charts',
89
+
data: [
90
+
{
91
+
date:newDate(),
92
+
stars:23467238,
93
+
},
94
+
],
95
+
},
96
+
]
97
+
98
+
constprimaryAxis=React.useMemo(
99
+
(): AxisOptions<MyDatum>=> ({
100
+
isPrimary:true,
101
+
scaleType:'time',
102
+
position:'bottom',
103
+
getValue:datum=>datum.date,
104
+
}),
105
+
[]
106
+
)
107
+
108
+
constsecondaryAxes=React.useMemo(
109
+
(): AxisOptions<MyDatum>[] => [
110
+
{
111
+
scaleType:'linear',
112
+
position:'left',
113
+
getValue:datum=>datum.stars,
114
+
},
113
115
],
114
116
[]
115
117
)
116
118
117
119
return (
118
-
<div
119
-
style={{
120
-
width:'400px',
121
-
height:'300px',
120
+
<Chart
121
+
options={{
122
+
data,
123
+
primaryAxis,
124
+
secondaryAxes,
122
125
}}
123
-
>
124
-
<Chart axes={axes} />
125
-
</div>
126
+
/>
126
127
)
127
128
}
128
129
```
129
130
130
-
## Series Types
131
+
## Secondary Axis Element Types
132
+
133
+
Secondary axes can be configured to render their respective series using 3 different element types using the `AxisOptions<TDatum>['scaleType']` property:
131
134
132
-
- Cartesian
133
-
-`line`
134
-
-`area`
135
-
-`bar`
136
-
-`bubble`
137
-
- Radial
138
-
-`pie`
135
+
-`line`
136
+
- 1 Line per series (optional, eg. for Bubble/Scatter charts)
-`series({} or function)` - A `memoized` object (or function that returns an object) of series options that correspond to all or each of the series in the dataset.
155
-
`type` - **string** - The series type (Line, Bar, Bubble, Area, etc)
156
-
`showPoints` - **bool** - If true, will show points for datums where applicable
157
-
`showOrphans` - **bool** - If true, will show orphan datums where applicable
158
-
`curve` - **func** - The curve function to use for this series where applicable (see [Curve Types](#curve-types))
159
-
-`axes[]` - An array of `memoized` axes
160
-
-`axis{}` - An axis object
161
-
-`primary`**bool** - Denotes whether this axis is the primary axis
162
-
-`type`**oneOf, required** - The type of scale for this axis
163
-
-`ordinal`
164
-
-`time`
165
-
-`utc`
166
-
-`linear`
167
-
-`log`
168
-
-`position`**oneOf, required** - The cartesian position of this axis
169
-
-`top`
170
-
-`right`
171
-
-`bottom`
172
-
-`left`
173
-
-`invert`**bool** - Whether this axis's scale should be inverted
174
-
-`primaryAxisID`**string** - If multiple primary axes are used, which primary axis ID does this axis refer to?
175
-
-`min`**number** - The suggested minimum for this axis
176
-
-`max`**number** - The suggested maximum for this axis
177
-
-`hardMin`**number** - The hard/forced minimum for this axis
178
-
-`hardMax`**number** - The hard/forced maximum for this axis
179
-
-`base`**number** - The base value for this axis. Defaults to `0`
180
-
-`ticks`**function** - The function used to generate ticks for the axis
181
-
-`format`**func** - The function used to format values on this axis for display
182
-
-`tickValues`**any** - The optional override for the tick values of the axis
183
-
-`tickSizeInner`**number** - The size of inner tick lines for the axis
184
-
-`tickSizeOuter`**number** - The size of the outer tick lines for the axis
185
-
-`tickPadding`**number** - The padding amount between tick labels
186
-
-`maxLabelRotation`**number** - The max label rotation angle in degrees. Defaults to 50
187
-
-`innerPadding`**number** - The inner padding for the axis
188
-
-`outerPadding`**number** - The outer padding for the axis
189
-
-`showGrid`**bool** - Whether or not the axis grid lines should be visible
190
-
-`showTicks`**bool** - Whether or not the tick and tick labels should be visible
191
-
-`show`**bool** - Whether or not the axis and scale are visible
192
-
-`stacked`**bool** - If true, will use stacked mode
193
-
-`id`**any** - An optional ID to identify this axis
194
-
-`primaryCursor{}` - An object of options for the primary cursor. If falsey, the cursor is disabled
195
-
-`render` - **func** - The render function for this cursor. Returns JSX
196
-
-`snap` - **bool** - If true, the cursor will snap to nearest values on the axis
197
-
-`showLine` - **bool** - If true, will show the grid line for this cursor
198
-
-`showLabel` - **bool** - If true, will show the label for this cursor
199
-
-`axisID` - **any** - The ID of the axis that this cursor corresponds to
200
-
-`onChange` - **func** - When the cursor is updated, this function will be called with relevant information
All element types that support lines or curves can be configured by passing any `curve` generator function as the `AxisOptions<TDatum>['curve']` option. By default, horizontal and vertical series default to using `monotoneX` and `monotoneY` curves, respectively. More information can be found at [`d3-shape curves`](https://github.com/d3/d3-shape#curves)
204
164
205
-
### Curve Types
165
+
#API
206
166
207
-
All series types that support lines or curves can be configured to use any [curve function from `d3-shape`](https://github.com/d3/d3-shape#curves) by passing one of the following strings as the `curve` prop to a series component. You may also pass your own curve function directly from d3 or if you're feeling powerful, even create your own!
208
-
209
-
Note the following string correspond to their respective d3 curve functions but with the `curve` prefix removed.
210
-
211
-
-`basisClosed`
212
-
-`basisOpen`
213
-
-`basis`
214
-
-`bundle`
215
-
-`cardinalClosed`
216
-
-`cardinalOpen`
217
-
-`cardinal`
218
-
-`catmullRomClosed`
219
-
-`catmullRomOpen`
220
-
-`catmullRom`
221
-
-`linearClosed`
222
-
-`linear`
223
-
-`monotoneX` (default)
224
-
-`monotoneY`
225
-
-`natural`
226
-
-`step`
227
-
-`stepAfter`
228
-
-`stepBefore`
167
+
> Coming Soon! Feel free to consult the **[examples](/examples/simple)** or refer to the exported types in your favorite IDE for now.
0 commit comments