Skip to content

Commit

Permalink
feat(website): add doc for bar label + fix stories links
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Mar 22, 2019
1 parent 16253fa commit 223c5e5
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 140 deletions.
16 changes: 9 additions & 7 deletions packages/circle-packing/src/BubbleCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ class BubbleCanvas extends Component {
this.ctx.font = `${theme.labels.text.fontSize}px ${theme.labels.text.fontFamily}`

// draw labels on top
nodes.filter(({ r }) => r > labelSkipRadius).forEach(node => {
const label = getLabel(node)
const labelTextColor = getLabelTextColor(node)

this.ctx.fillStyle = labelTextColor
this.ctx.fillText(label, node.x, node.y)
})
nodes
.filter(({ r }) => r > labelSkipRadius)
.forEach(node => {
const label = getLabel(node)
const labelTextColor = getLabelTextColor(node)

this.ctx.fillStyle = labelTextColor
this.ctx.fillText(label, node.x, node.y)
})
}
}

Expand Down
30 changes: 17 additions & 13 deletions packages/generators/src/sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ import { names } from './sets'
const availableNodes = names.map(name => ({ id: name }))

const getNodeTargets = (id, links, currentPath) => {
const targets = links.filter(({ source }) => source === id).map(({ target }) => {
if (target === id) {
throw new Error(`[sankey] a node cannot be linked on itself:\n link: ${id} —> ${id}`)
}
if (currentPath && currentPath.includes(target)) {
throw new Error(
`[sankey] found cyclic dependency:\n link: ${currentPath.join(
' —> '
)} —> ${target}`
)
}
return target
})
const targets = links
.filter(({ source }) => source === id)
.map(({ target }) => {
if (target === id) {
throw new Error(
`[sankey] a node cannot be linked on itself:\n link: ${id} —> ${id}`
)
}
if (currentPath && currentPath.includes(target)) {
throw new Error(
`[sankey] found cyclic dependency:\n link: ${currentPath.join(
' —> '
)} —> ${target}`
)
}
return target
})

return targets.reduce(
(acc, targetId) =>
Expand Down
42 changes: 22 additions & 20 deletions packages/pie/src/PieSlicesLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,30 @@ export default class PieSlicesLabels extends Component {

return (
<Fragment>
{arcs.filter(arc => skipAngle === 0 || arc.angleDeg > skipAngle).map(arc => {
const angle = midAngle(arc) - Math.PI / 2
const position = positionFromAngle(angle, centerRadius)
{arcs
.filter(arc => skipAngle === 0 || arc.angleDeg > skipAngle)
.map(arc => {
const angle = midAngle(arc) - Math.PI / 2
const position = positionFromAngle(angle, centerRadius)

return (
<g
key={arc.data.id}
transform={`translate(${position.x}, ${position.y})`}
style={sliceStyle}
>
<text
textAnchor="middle"
style={{
...theme.labels.text,
fill: textColor(arc.data, theme),
}}
return (
<g
key={arc.data.id}
transform={`translate(${position.x}, ${position.y})`}
style={sliceStyle}
>
{label(arc.data)}
</text>
</g>
)
})}
<text
textAnchor="middle"
style={{
...theme.labels.text,
fill: textColor(arc.data, theme),
}}
>
{label(arc.data)}
</text>
</g>
)
})}
</Fragment>
)
}
Expand Down
66 changes: 34 additions & 32 deletions packages/pie/src/compute.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,41 @@ export const computeRadialLabels = (
textXOffset,
}
) =>
arcs.filter(arc => skipAngle === 0 || arc.angleDeg > skipAngle).map(arc => {
const angle = absoluteAngleRadians(midAngle(arc) - Math.PI / 2)
const positionA = positionFromAngle(angle, radius + linkOffset)
const positionB = positionFromAngle(angle, radius + linkOffset + linkDiagonalLength)
arcs
.filter(arc => skipAngle === 0 || arc.angleDeg > skipAngle)
.map(arc => {
const angle = absoluteAngleRadians(midAngle(arc) - Math.PI / 2)
const positionA = positionFromAngle(angle, radius + linkOffset)
const positionB = positionFromAngle(angle, radius + linkOffset + linkDiagonalLength)

let positionC
let labelPosition
let textAlign
let positionC
let labelPosition
let textAlign

if (
absoluteAngleDegrees(radiansToDegrees(angle)) < 90 ||
absoluteAngleDegrees(radiansToDegrees(angle)) >= 270
) {
positionC = { x: positionB.x + linkHorizontalLength, y: positionB.y }
labelPosition = {
x: positionB.x + linkHorizontalLength + textXOffset,
y: positionB.y,
if (
absoluteAngleDegrees(radiansToDegrees(angle)) < 90 ||
absoluteAngleDegrees(radiansToDegrees(angle)) >= 270
) {
positionC = { x: positionB.x + linkHorizontalLength, y: positionB.y }
labelPosition = {
x: positionB.x + linkHorizontalLength + textXOffset,
y: positionB.y,
}
textAlign = 'left'
} else {
positionC = { x: positionB.x - linkHorizontalLength, y: positionB.y }
labelPosition = {
x: positionB.x - linkHorizontalLength - textXOffset,
y: positionB.y,
}
textAlign = 'right'
}
textAlign = 'left'
} else {
positionC = { x: positionB.x - linkHorizontalLength, y: positionB.y }
labelPosition = {
x: positionB.x - linkHorizontalLength - textXOffset,
y: positionB.y,
}
textAlign = 'right'
}

return {
arc,
text: getLabel(arc.data),
position: labelPosition,
align: textAlign,
line: [positionA, positionB, positionC],
}
})
return {
arc,
text: getLabel(arc.data),
position: labelPosition,
align: textAlign,
line: [positionA, positionB, positionC],
}
})
8 changes: 4 additions & 4 deletions packages/scales/src/compute.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ export const computeXYScalesForSeries = (_series, xScaleSpec, yScaleSpec, width,
? null
: xScale(d.data.xStacked)
: d.data.x === null
? null
: xScale(d.data.x),
? null
: xScale(d.data.x),
y:
yScale.stacked === true
? d.data.yStacked === null
? null
: yScale(d.data.yStacked)
: d.data.y === null
? null
: yScale(d.data.y),
? null
: yScale(d.data.y),
}
})
})
Expand Down
23 changes: 11 additions & 12 deletions packages/stream/src/Stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,17 @@ const Stream = ({
{...motionProps}
/>
))}
{isInteractive &&
enableStackTooltip && (
<StreamSlices
slices={slices}
height={height}
showTooltip={showTooltip}
hideTooltip={hideTooltip}
theme={theme}
getTooltipValue={getTooltipValue}
getTooltipLabel={getTooltipLabel}
/>
)}
{isInteractive && enableStackTooltip && (
<StreamSlices
slices={slices}
height={height}
showTooltip={showTooltip}
hideTooltip={hideTooltip}
theme={theme}
getTooltipValue={getTooltipValue}
getTooltipLabel={getTooltipLabel}
/>
)}
{legends.map((legend, i) => {
const legendData = enhancedLayers
.map(l => ({
Expand Down
26 changes: 14 additions & 12 deletions packages/sunburst/src/Sunburst.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,20 @@ const Sunburst = ({
{({ showTooltip, hideTooltip }) => (
<SvgWrapper width={outerWidth} height={outerHeight} margin={margin} theme={theme}>
<g transform={`translate(${centerX}, ${centerY})`}>
{nodes.filter(node => node.depth > 0).map((node, i) => (
<SunburstArc
key={i}
node={node}
arcGenerator={arcGenerator}
borderWidth={borderWidth}
borderColor={borderColor}
showTooltip={showTooltip}
hideTooltip={hideTooltip}
theme={theme}
/>
))}
{nodes
.filter(node => node.depth > 0)
.map((node, i) => (
<SunburstArc
key={i}
node={node}
arcGenerator={arcGenerator}
borderWidth={borderWidth}
borderColor={borderColor}
showTooltip={showTooltip}
hideTooltip={hideTooltip}
theme={theme}
/>
))}
</g>
</SvgWrapper>
)}
Expand Down
22 changes: 12 additions & 10 deletions packages/treemap/src/TreeMapCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,22 @@ class TreeMapCanvas extends Component {
this.ctx.font = `${theme.labels.text.fontSize}px ${theme.labels.text.fontFamily}`

// draw labels on top
nodes.filter(({ label }) => label !== undefined).forEach(node => {
const labelTextColor = getLabelTextColor(node)
nodes
.filter(({ label }) => label !== undefined)
.forEach(node => {
const labelTextColor = getLabelTextColor(node)

const rotate = orientLabel && node.height > node.width
const rotate = orientLabel && node.height > node.width

this.ctx.save()
this.ctx.translate(node.x + node.width / 2, node.y + node.height / 2)
this.ctx.rotate(degreesToRadians(rotate ? -90 : 0))
this.ctx.save()
this.ctx.translate(node.x + node.width / 2, node.y + node.height / 2)
this.ctx.rotate(degreesToRadians(rotate ? -90 : 0))

this.ctx.fillStyle = labelTextColor
this.ctx.fillText(node.label, 0, 0)
this.ctx.fillStyle = labelTextColor
this.ctx.fillText(node.label, 0, 0)

this.ctx.restore()
})
this.ctx.restore()
})
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/voronoi/src/Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ class Mesh extends Component {
return (
<g ref={this.setRectRef}>
{debug && <path d={voronoiPath} stroke="red" strokeWidth={0.5} opacity={0.75} />}
{index !== null &&
debug && <path fill="red" opacity={0.25} d={voronoi.renderCell(index)} />}
{index !== null && debug && (
<path fill="red" opacity={0.25} d={voronoi.renderCell(index)} />
)}
<rect
width={width}
height={height}
Expand Down
15 changes: 7 additions & 8 deletions website/src/components/ChartTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ export default class ChartTabs extends Component {
)}
</div>
{content}
{currentTab === 'chart' &&
nodeCount !== undefined && (
<span className="chart-tabs__node-count">
<strong>{nodeCount}</strong>
&nbsp;
{nodeCountWording}
</span>
)}
{currentTab === 'chart' && nodeCount !== undefined && (
<span className="chart-tabs__node-count">
<strong>{nodeCount}</strong>
&nbsp;
{nodeCountWording}
</span>
)}
</div>
)
}
Expand Down
7 changes: 4 additions & 3 deletions website/src/components/Stories.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { Component } from 'react'
import snakeCase from 'lodash/kebabCase'
import PropTypes from 'prop-types'
import VisitIcon from 'react-icons/lib/md/keyboard-arrow-right'
import config from '../config'
import CollapsibleCard from './CollapsibleCard'

const buildStoryLink = ({ kind, story }) =>
`${config.storybookUrl}?selectedKind=${encodeURIComponent(
kind
)}&selectedStory=${encodeURIComponent(story)}`
`${config.storybookUrl}?path=/story/${encodeURIComponent(
kind.toLowerCase()
)}--${encodeURIComponent(snakeCase(story))}`

export default class Stories extends Component {
static propTypes = {
Expand Down
20 changes: 20 additions & 0 deletions website/src/components/charts/bar/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,26 @@ export default [
controlType: 'switch',
controlGroup: 'Labels',
},
{
key: 'label',
scopes: '*',
description: (
<div>
Define how bar labels are computed. By default it will use the bar's value. It
accepts a string which will be used to access a specific bar data property, such as{' '}
<code className="code-string">'value'</code> or{' '}
<code className="code-string">'id'</code>. You can also use a funtion if you want to
add more logic, this function will receive the current bar's data and must return
the computed label which, depending on the context, should return a string or an svg
element (Bar) or a string (BarCanvas). For example let's say you want to use a label
with both the id and the value, you can achieve this with{' '}
<code>{`label={d => \`\${d.id}: \${d.value}\`}`}</code>.
</div>
),
type: '{string|Function}',
required: false,
default: defaults.label,
},
{
key: 'labelFormat',
scopes: '*',
Expand Down
Loading

0 comments on commit 223c5e5

Please sign in to comment.