-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Created ContentClipPath The ContentClipPath creates a new clipPath that is the size of the inner portion of the graph. Other series can use this with `style={{clipPath: 'url(#content-area)'}}
- Loading branch information
Chris Thomas
authored
Jun 7, 2020
1 parent
881dbe8
commit cce5980
Showing
18 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
## Clip | ||
|
||
Depending on the data and domain, sometimes the series in the plot will extend into the axis. This can either be solved with a [Border](border.md), or the elements can be clipped. | ||
|
||
To have the rendered series, clipped you will need to set up a `clipPath` (see [MDN](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/clipPath)) and tell the series to use it. | ||
|
||
As seen below, the `clipPath` can be created with the `ContentClipArea` component, and its `id` can be referenced by the components that want to be clipped. | ||
|
||
```jsx | ||
<XYPlot> | ||
<ContentClipArea id="clip" /> | ||
<LineSeries style={{clipPath: 'url(#clip)'}} /> | ||
</XYPlot> | ||
``` | ||
|
||
|
||
|
||
## API Reference | ||
|
||
#### id (optional) | ||
|
||
Type: `String` | ||
|
||
The id to assign to the `clipArea`. If not provided, this will default to `content-area` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
|
||
export default function ContentClipPath(props) { | ||
const {id = 'content-area', innerWidth, innerHeight} = props; | ||
return ( | ||
<defs> | ||
<clipPath id={id}> | ||
<rect x={0} y={0} width={innerWidth} height={innerHeight} /> | ||
</clipPath> | ||
</defs> | ||
); | ||
} | ||
|
||
ContentClipPath.requiresSVG = true; | ||
ContentClipPath.displayName = 'ContentClipPath'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"typeAcquisition": { | ||
"enable": true, | ||
"include": [ | ||
"jest" | ||
] | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/react-vis/tests/plot/__snapshots__/content-clip-path.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`content-clip-path should render 1`] = ` | ||
<defs> | ||
<clipPath | ||
id="foo" | ||
> | ||
<rect | ||
height={200} | ||
width={300} | ||
x={0} | ||
y={0} | ||
/> | ||
</clipPath> | ||
</defs> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import {shallow} from 'enzyme'; | ||
import ContentClipPath from '../../src/plot/content-clip-path'; | ||
|
||
describe('content-clip-path', () => { | ||
it('should render', () => { | ||
const wrapper = shallow( | ||
<ContentClipPath id="foo" innerWidth={300} innerHeight={200} /> | ||
); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
it('should default id to content-area', () => { | ||
const wrapper = shallow( | ||
<ContentClipPath innerWidth={300} innerHeight={200} /> | ||
); | ||
const clip = wrapper.find('clipPath'); | ||
expect(clip.prop('id')).toEqual('content-area'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React, {useState} from 'react'; | ||
|
||
import { | ||
XYPlot, | ||
XAxis, | ||
YAxis, | ||
VerticalGridLines, | ||
HorizontalGridLines, | ||
AreaSeries, | ||
ContentClipPath | ||
} from 'react-vis'; | ||
|
||
export default function ClipExample() { | ||
const [clip, setClip] = useState(true); | ||
|
||
return ( | ||
<> | ||
<label style={{display: 'block'}}> | ||
<input | ||
type="checkbox" | ||
checked={clip} | ||
onChange={e => setClip(e.currentTarget.checked)} | ||
/> | ||
Enable Clipping | ||
</label> | ||
<XYPlot xDomain={[1.2, 3]} yDomain={[11, 26]} width={300} height={300}> | ||
{clip && <ContentClipPath id="content" />} | ||
|
||
<VerticalGridLines /> | ||
<HorizontalGridLines /> | ||
|
||
<AreaSeries | ||
data={[ | ||
{x: 1, y: 10, y0: 1}, | ||
{x: 2, y: 25, y0: 5}, | ||
{x: 3, y: 15, y0: 3} | ||
]} | ||
style={{clipPath: 'url(#content)'}} | ||
/> | ||
<XAxis /> | ||
<YAxis /> | ||
<AreaSeries | ||
data={[ | ||
{x: 1, y: 5, y0: 6}, | ||
{x: 2, y: 20, y0: 11}, | ||
{x: 3, y: 10, y0: 9} | ||
]} | ||
style={{clipPath: 'url(#content)'}} | ||
/> | ||
</XYPlot> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,5 @@ import './radial-story'; | |
|
||
import './axis-story'; | ||
import './legend-story'; | ||
|
||
import './misc-story'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint-env node */ | ||
|
||
import React from 'react'; | ||
|
||
import {storiesOf} from '@storybook/react'; | ||
|
||
import {withKnobs, boolean} from '@storybook/addon-knobs/react'; | ||
import {SimpleChartWrapper} from './storybook-utils'; | ||
import {generateLinearData} from './storybook-data'; | ||
|
||
import {LineSeries, ContentClipPath} from 'react-vis'; | ||
|
||
const data = generateLinearData({randomFactor: 10}); | ||
|
||
storiesOf('Misc', module) | ||
.addDecorator(withKnobs) | ||
.addWithJSX('Clip Content', () => { | ||
const margin = {left: 40, top: 40, bottom: 40, right: 40}; | ||
const xDomain = [data[1].x, data[data.length - 2].x]; | ||
const shouldClip = boolean( | ||
'Enable Clipping', | ||
false, | ||
'General chart options' | ||
); | ||
return ( | ||
<SimpleChartWrapper xDomain={xDomain} yDomain={[0, 20]} margin={margin}> | ||
{shouldClip && <ContentClipPath id="clip" />} | ||
<LineSeries data={data} style={{clipPath: 'url(#clip)'}} /> | ||
</SimpleChartWrapper> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters