File size: 14,118 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | import { defaultProps } from '@nivo/swarmplot'
import { OrdinalColorScaleConfig } from '@nivo/colors'
import { themeProperty, motionProperties, groupProperties } from '../../../lib/componentProperties'
import {
chartDimensions,
chartRef,
ordinalColors,
chartGrid,
axes,
isInteractive,
} from '../../../lib/chart-properties'
import { ChartProperty, Flavor } from '../../../types'
const allFlavors: Flavor[] = ['svg', 'canvas']
const props: ChartProperty[] = [
{
key: 'data',
help: 'Chart data.',
flavors: allFlavors,
description: `
This Chart's doesn't have a predefined structure,
you must use a schema which match \`groupBy\`,
\`identity\` and \`value\` properties.
`,
type: 'object[]',
group: 'Base',
required: true,
},
{
key: 'id',
group: 'Base',
flavors: allFlavors,
type: 'string | Function',
required: false,
help: `Property used to retrieve the node's unique identifier.`,
description: `
This property will determine the identifier of a datum
amongst the whole data set, thus, it's really important
that it's unique.
It is especially important to have proper identifier
when enabling animations, as it will be used to determine
if a node is a new one or should transition from previous
to next state.
`,
defaultValue: defaultProps.id,
},
{
key: 'value',
group: 'Base',
flavors: allFlavors,
type: 'string | Function',
required: false,
help: `Property used to retrieve the node's value.`,
defaultValue: defaultProps.value,
},
{
key: 'valueFormat',
group: 'Base',
flavors: allFlavors,
help: 'Optional formatter for values.',
description: `
The formatted value can then be used for labels & tooltips.
Under the hood, nivo uses [d3-format](https://github.com/d3/d3-format),
please have a look at it for available formats, you can also pass a function
which will receive the raw value and should return the formatted one.
`,
required: false,
type: 'string | (value: number) => string | number',
control: { type: 'valueFormat' },
},
{
key: 'groups',
group: 'Base',
flavors: allFlavors,
type: 'string[]',
required: true,
help: 'Available groups.',
},
{
key: 'groupBy',
group: 'Base',
flavors: allFlavors,
type: 'string | Function',
required: false,
help: 'Propety used to group nodes, must return a group which is available in the groups property.',
defaultValue: defaultProps.groupBy,
},
{
key: 'size',
group: 'Base',
flavors: allFlavors,
type: 'number | object | Function',
required: false,
help: `How to compute node size, static or dynamic.`,
description: `
If you provide a **number**, all nodes will have the same
**fixed size**.
You can also use an object to define a varying size,
it must conform to the following interface:
\`\`\`
{
key: string
values: [min: number, max: number]
sizes: [min: number, max: number]
}
\`\`\`
Then the size of each node will **depend on the value
of \`key\` and \`sizes\`**.
If you use a **custom function**, it will receive the current
node and must **return a number**.
`,
defaultValue: defaultProps.size,
},
{
key: 'spacing',
help: 'Spacing between nodes.',
flavors: allFlavors,
type: 'number',
required: false,
defaultValue: defaultProps.spacing,
group: 'Base',
control: {
type: 'range',
unit: 'px',
min: 0,
max: 20,
},
},
...chartDimensions(allFlavors),
chartRef(['svg', 'canvas']),
{
key: 'forceStrength',
help: 'Force strength.',
flavors: allFlavors,
description: `
This value determine the strength applied on the value
axis, using lower value will result in a **more linear
arrangement**, however nodes won't be placed accurately
according to their values.
On the other hand, if you increase this value, the
simulation will try to **align the nodes with their
corresponding values** on the value axis, resulting
in a narrower chart.
Please note that increasing this value will sometimes
require to **increase the quality of the simulation**
via the \`simulationIterations\` property.
`,
type: 'number',
required: false,
defaultValue: defaultProps.forceStrength,
group: 'Simulation',
control: {
type: 'range',
step: 0.2,
min: 0.2,
max: 9,
},
},
{
key: 'simulationIterations',
help: 'Adjust the simulation quality.',
flavors: allFlavors,
description: `
Increasing this number will result in a **more accurate simulation**,
however it will also involve more computing.
`,
type: 'number',
required: false,
defaultValue: defaultProps.simulationIterations,
group: 'Simulation',
control: {
type: 'range',
min: 60,
max: 260,
},
},
{
key: 'layout',
help: `Chart layout.`,
flavors: allFlavors,
type: 'string',
required: false,
defaultValue: defaultProps.layout,
group: 'Layout',
control: {
type: 'radio',
choices: [
{ label: 'horizontal', value: 'horizontal' },
{ label: 'vertical', value: 'vertical' },
],
},
},
{
key: 'gap',
help: 'Gap between each serie.',
flavors: allFlavors,
type: 'number',
required: false,
defaultValue: defaultProps.gap,
group: 'Layout',
control: {
type: 'range',
unit: 'px',
min: 0,
max: 100,
},
},
themeProperty(allFlavors),
ordinalColors({
flavors: allFlavors,
defaultValue: defaultProps.colors as OrdinalColorScaleConfig,
description: `
The colors property is used to determine the **ordinal color scale**
to use to compute nodes' color, it use the serie id as a discriminator.
You can use a **predefined color scheme**, using the following form:
\`\`\`
colors={{ scheme: 'category10' }}
\`\`\`
Where \`category10\` is the identifier of the color scheme.
Please have a look at [the dedicated guide](self:/guides/colors)
for available schemes.
If you wish to use **color already defined on the data**
you passed to the chart, you can also use this form:
\`\`\`
colors={{ datum: 'color' }}
\`\`\`
Where \`color\` is the path to the property containing the color
to apply in the current datum.
You can also use a **custom function** which will receive the current
datum and must **return a valid color code**.
If you pass a string, it will be **used as a color for each and every nodes**.
`,
}),
{
key: 'colorBy',
group: 'Style',
help: 'Property or accessor function to be used with colors.',
flavors: allFlavors,
description: `
When using a color scheme or an array of colors,
you'll generate a color scale, this scale will
receive a value which will be translated to a color.
This property define the way we get this value,
it can be either a \`string\` or a custom function.
Please have a look at [the colors guide](self:/guides/colors)
for further information.
`,
type: `Function | string`,
required: false,
defaultValue: defaultProps.colorBy,
control: {
type: 'choices',
choices: ['group', 'id'].map(key => ({
label: key,
value: key,
})),
},
},
{
key: 'borderWidth',
help: 'Control node border width.',
flavors: allFlavors,
type: 'number | Function',
required: false,
defaultValue: defaultProps.borderWidth,
control: { type: 'lineWidth' },
group: 'Style',
},
{
key: 'borderColor',
help: 'Control node border color.',
flavors: allFlavors,
type: 'string | object | Function',
required: false,
defaultValue: defaultProps.borderColor,
control: { type: 'inheritedColor' },
group: 'Style',
},
{
key: 'layers',
group: 'Customization',
flavors: allFlavors,
help: 'Defines the order of layers and add custom layers.',
description: `
Defines the order of layers, available layers are:
\`grid\`, \`axes\`, \`nodes\`, \`mesh\`.
You can also use this to insert extra layers
to the chart, the extra layer must be a function.
The layer function which will receive the chart's
context & computed data and must return a valid SVG element
for the \`SwarmPlot\` component.
When using the canvas implementation, the function
will receive the canvas 2d context as first argument
and the chart's context and computed data as second.
Please make sure to use \`context.save()\` and
\`context.restore()\` if you make some global
modifications to the 2d context inside this function
to avoid side effects.
You can see a live example of custom layers
[here](storybook:/swarmplot--extra-layers).
`,
required: false,
type: 'Array<string | Function>',
defaultValue: defaultProps.layers,
},
{
key: 'circleComponent',
group: 'Customization',
flavors: ['svg'],
help: 'Override default circle component for the SVG implementation.',
description: `
This property can be used to completely
customize the way nodes are rendered.
You should return a valid SVG node.
You can see a live example of custom circle component
[here](storybook:/swarmplot--custom-circle-component).
`,
required: false,
type: 'Component',
},
{
key: 'renderCircle',
group: 'Customization',
flavors: ['canvas'],
help: 'Override default circle rendering for the canvas implementation.',
description: `
This property can be used to completely
customize the way nodes are rendered.
The rendering function will receive the canvas 2d
context as first argument.
Please make sure to use \`context.save()\` and
\`context.restore()\` if you make some global
modifications to the 2d context inside this function
to avoid side effects.
`,
required: false,
type: 'Function',
},
...chartGrid({
flavors: allFlavors,
values: true,
xDefault: defaultProps.enableGridX,
yDefault: defaultProps.enableGridY,
}),
...axes({ flavors: allFlavors }),
isInteractive({
flavors: ['svg', 'canvas'],
defaultValue: defaultProps.isInteractive,
}),
{
key: 'useMesh',
help: 'Use a mesh to detect mouse interactions, always true for canvas.',
flavors: ['svg'],
type: 'boolean',
required: false,
defaultValue: defaultProps.useMesh,
control: { type: 'switch' },
group: 'Interactivity',
},
{
key: 'debugMesh',
help: 'Display the mesh used to detect mouse interactions (voronoi cells), depends on useMesh.',
flavors: allFlavors,
type: 'boolean',
required: false,
defaultValue: defaultProps.debugMesh,
control: { type: 'switch' },
group: 'Interactivity',
},
{
key: 'onMouseEnter',
group: 'Interactivity',
flavors: allFlavors,
help: 'onMouseEnter handler.',
type: '(node, event) => void',
required: false,
},
{
key: 'onMouseMove',
group: 'Interactivity',
flavors: allFlavors,
help: 'onMouseMove handler.',
type: '(node, event) => void',
required: false,
},
{
key: 'onMouseLeave',
group: 'Interactivity',
flavors: allFlavors,
help: 'onMouseLeave handler.',
type: '(node, event) => void',
required: false,
},
{
key: 'onClick',
group: 'Interactivity',
flavors: allFlavors,
help: 'onClick handler.',
type: '(node, event) => void',
required: false,
},
{
key: 'tooltip',
group: 'Interactivity',
flavors: allFlavors,
type: 'Function',
required: false,
help: 'Custom tooltip component.',
description: `
A function allowing complete tooltip customisation,
it must return a valid HTML
element and will receive the node's data.
`,
},
...motionProperties(['svg'], defaultProps),
]
export const groups = groupProperties(props)
|