Skip to content

Commit

Permalink
(feat)Line: add support for hiding line charts by default (#2540)
Browse files Browse the repository at this point in the history
* (feat)Line: add support for hiding line charts by default

* add unit test

* add unit test for testing hiding multiple series by default

* add to documentation

* add to types and update props

* use make fmt

---------

Co-authored-by: Carlos Catalan <[email protected]>
  • Loading branch information
clcatalan and ccatalan6 authored Jul 23, 2024
1 parent 620b099 commit 0f5ac2f
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/line/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export interface LineProps {
debugMesh?: boolean

enableSlices?: 'x' | 'y' | false
initialHiddenIds?: string[]
debugSlices?: boolean
sliceTooltip?: SliceTooltip

Expand Down
2 changes: 2 additions & 0 deletions packages/line/src/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const Line = props => {
enableTouchCrosshair = false,

role = 'img',
initialHiddenIds = [],
} = props

const { margin, innerWidth, innerHeight, outerWidth, outerHeight } = useDimensions(
Expand Down Expand Up @@ -139,6 +140,7 @@ const Line = props => {
pointColor,
pointBorderColor,
enableSlices,
initialHiddenIds,
})

const theme = useTheme()
Expand Down
3 changes: 2 additions & 1 deletion packages/line/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const useLine = ({
pointColor = LineDefaultProps.pointColor,
pointBorderColor = LineDefaultProps.pointBorderColor,
enableSlices = LineDefaultProps.enableSlicesTooltip,
initialHiddenIds = LineDefaultProps.initialHiddenIds,
}) => {
const [componentId] = useState(uniqueId(LINE_UNIQUE_ID_PREFIX))
const formatX = useValueFormatter(xFormat)
Expand All @@ -156,7 +157,7 @@ export const useLine = ({
const theme = useTheme()
const getPointColor = useInheritedColor(pointColor, theme)
const getPointBorderColor = useInheritedColor(pointBorderColor, theme)
const [hiddenIds, setHiddenIds] = useState([])
const [hiddenIds, setHiddenIds] = useState(initialHiddenIds ?? [])

const {
xScale,
Expand Down
1 change: 1 addition & 0 deletions packages/line/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const LineDefaultProps = {
defs: [],
fill: [],
role: 'img',
initialHiddenIds: [],
}

export const LineCanvasDefaultProps = {
Expand Down
97 changes: 97 additions & 0 deletions packages/line/tests/Line.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mount } from 'enzyme'
import { Axis } from '@nivo/axes'
import Line from '../src/Line'
import Lines from '../src/Lines'
import { LINE_UNIQUE_ID_PREFIX } from '../src/hooks'
import SlicesItem from '../src/SlicesItem'
import renderer from 'react-test-renderer'
Expand Down Expand Up @@ -89,6 +90,102 @@ it('should create slice for each x value', () => {
expect(slices.at(4).prop('slice').x).toBe(500)
})

it('should hide single line charts by default given their id', () => {
const data = [
{
id: 'A',
data: [
{ x: 0, y: 3 },
{ x: 1, y: 7 },
{ x: 2, y: 11 },
{ x: 3, y: 9 },
{ x: 4, y: 8 },
],
},
{
id: 'B',
data: [
{ x: 0, y: 4 },
{ x: 2, y: 8 },
{ x: 3, y: 12 },
{ x: 4, y: 10 },
{ x: 5, y: 9 },
],
},
{
id: 'C',
data: [
{ x: 0, y: 5 },
{ x: 2, y: 9 },
{ x: 3, y: 13 },
{ x: 4, y: 11 },
{ x: 5, y: 10 },
],
},
]
const wrapper = mount(
<Line
width={500}
height={300}
data={data}
enableSlices="x"
animate={false}
initialHiddenIds={['B']}
/>
)

const lines = wrapper.find(Lines)
expect(lines).toHaveLength(1)
})

it('should hide multiple line charts by default given their ids', () => {
const data = [
{
id: 'A',
data: [
{ x: 0, y: 3 },
{ x: 1, y: 7 },
{ x: 2, y: 11 },
{ x: 3, y: 9 },
{ x: 4, y: 8 },
],
},
{
id: 'B',
data: [
{ x: 0, y: 4 },
{ x: 2, y: 8 },
{ x: 3, y: 12 },
{ x: 4, y: 10 },
{ x: 5, y: 9 },
],
},
{
id: 'C',
data: [
{ x: 0, y: 5 },
{ x: 2, y: 9 },
{ x: 3, y: 13 },
{ x: 4, y: 11 },
{ x: 5, y: 10 },
],
},
]
const wrapper = mount(
<Line
width={500}
height={300}
data={data}
enableSlices="x"
animate={false}
initialHiddenIds={['B', 'C']}
/>
)

const lines = wrapper.find(Lines)
expect(lines).toHaveLength(1)
})

it('should have left and bottom axis by default', () => {
const data = [
{
Expand Down
11 changes: 11 additions & 0 deletions storybook/stories/line/Line.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const commonProperties = {
animate: true,
enableTouchCrosshair: true,
enableSlices: 'x',
initialHiddenIds: ['cognac'],
}

const CustomSymbol = ({ size, color, borderWidth, borderColor }) => (
Expand Down Expand Up @@ -64,6 +65,16 @@ export const StackedLines: Story = {
stacked: true,
}}
curve={args.curve}
legends={[
{
anchor: 'bottom',
direction: 'row',
itemHeight: 20,
itemWidth: 80,
toggleSerie: true,
translateY: 50,
},
]}
/>
),
}
Expand Down
8 changes: 8 additions & 0 deletions website/src/data/components/line/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,14 @@ const props: ChartProperty[] = [
defaultValue: defaults.enableTouchCrosshair,
control: { type: 'switch' },
},
{
key: 'initialHiddenIds',
flavors: allFlavors,
group: 'Interactivity',
help: `Hides certain series by default given their ids`,
type: 'string[]',
defaultValue: defaults.initialHiddenIds,
},
{
key: 'crosshairType',
flavors: ['svg'],
Expand Down

0 comments on commit 0f5ac2f

Please sign in to comment.