Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat)Line: add support for hiding line charts by default #2540

Merged
merged 7 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = [],
plouc marked this conversation as resolved.
Show resolved Hide resolved
} = 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 @@
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

Check failure on line 127 in packages/line/tests/Line.test.js

View workflow job for this annotation

GitHub Actions / build

Delete `·`
width={500}

Check failure on line 128 in packages/line/tests/Line.test.js

View workflow job for this annotation

GitHub Actions / build

Delete `·`
height={300}

Check failure on line 129 in packages/line/tests/Line.test.js

View workflow job for this annotation

GitHub Actions / build

Delete `·`
data={data}

Check failure on line 130 in packages/line/tests/Line.test.js

View workflow job for this annotation

GitHub Actions / build

Delete `··`
enableSlices="x"

Check failure on line 131 in packages/line/tests/Line.test.js

View workflow job for this annotation

GitHub Actions / build

Delete `·`
animate={false}

Check failure on line 132 in packages/line/tests/Line.test.js

View workflow job for this annotation

GitHub Actions / build

Delete `·`
initialHiddenIds={['B']}
plouc marked this conversation as resolved.
Show resolved Hide resolved
/>
)

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

Check failure on line 175 in packages/line/tests/Line.test.js

View workflow job for this annotation

GitHub Actions / build

Delete `·`
width={500}

Check failure on line 176 in packages/line/tests/Line.test.js

View workflow job for this annotation

GitHub Actions / build

Delete `·`
height={300}

Check failure on line 177 in packages/line/tests/Line.test.js

View workflow job for this annotation

GitHub Actions / build

Delete `·`
data={data}

Check failure on line 178 in packages/line/tests/Line.test.js

View workflow job for this annotation

GitHub Actions / build

Delete `··`
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
9 changes: 9 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,15 @@ 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: 'boolean',
plouc marked this conversation as resolved.
Show resolved Hide resolved
defaultValue: defaults.initialHiddenIds,
control: { type: 'switch' },
plouc marked this conversation as resolved.
Show resolved Hide resolved
},
{
key: 'crosshairType',
flavors: ['svg'],
Expand Down
Loading