Skip to content

Commit

Permalink
Add limit props to set height and width of XLine and YLine respective…
Browse files Browse the repository at this point in the history
…ly (#152)

* implement xy limit props

* update documentation

* add test cases, better undefined check for XLine
  • Loading branch information
tanayv authored and krissalvador27 committed Jul 8, 2019
1 parent 6e50783 commit 5e449b7
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
21 changes: 21 additions & 0 deletions docs/src/docs/XLine/propDocs.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@
"required": true,
"description": ""
},
"yScale": {
"type": {
"name": "func"
},
"required": false,
"description": "D3 scale for Y axis - provided by XYPlot"
},
"yLimit": {
"type": {
"name": "any"
},
"required": false,
"description": "Limit on the height of the line"
},
"yDomain": {
"type": {
"name": "array"
},
"required": false,
"description": "Y domain of the data as an array - provided by XYPlot"
},
"spacingTop": {
"type": {
"name": "number"
Expand Down
14 changes: 14 additions & 0 deletions docs/src/docs/YLine/propDocs.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
"required": true,
"description": ""
},
"xScale": {
"type": {
"name": "func"
},
"required": false,
"description": "D3 scale for X axis - provided by XYPlot"
},
"xLimit": {
"type": {
"name": "any"
},
"required": false,
"description": "Limit on the width of the line"
},
"spacingLeft": {
"type": {
"name": "number"
Expand Down
24 changes: 22 additions & 2 deletions src/XLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ export default class XLine extends React.Component {
*/
xScale: PropTypes.func,
value: PropTypes.any.isRequired,
/**
* D3 scale for Y axis - provided by XYPlot
*/
yScale: PropTypes.func,
yLimit: PropTypes.any,
/**
* The Y domain of the data as an array - provided by XYPlot
*/
yDomain: PropTypes.array,
/**
* Spacing top - provided by XYPlot
*/
Expand Down Expand Up @@ -39,6 +48,9 @@ export default class XLine extends React.Component {
const {
xScale,
value,
yScale,
yLimit,
yDomain,
height,
style,
spacingTop,
Expand All @@ -47,13 +59,21 @@ export default class XLine extends React.Component {
const className = `rct-chart-line-x ${this.props.className}`;
const lineX = xScale(value);

let y1 = -spacingTop;
let y2 = height + spacingBottom;

if (typeof yLimit !== "undefined") {
y1 = yScale(yDomain[0]) + spacingBottom;
y2 = yScale(yLimit);
}

return (
<line
{...{
x1: lineX,
x2: lineX,
y1: -spacingTop,
y2: height + spacingBottom,
y1: y1,
y2: y2,
className,
style
}}
Expand Down
11 changes: 10 additions & 1 deletion src/YLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export default class YLine extends React.Component {
*/
yScale: PropTypes.func,
value: PropTypes.any.isRequired,
/**
* D3 scale for X axis - provided by XYPlot
*/
xScale: PropTypes.func,
xLimit: PropTypes.any,
/**
* Spacing left - provided by XYPlot
*/
Expand Down Expand Up @@ -39,19 +44,23 @@ export default class YLine extends React.Component {
const {
yScale,
value,
xScale,
xLimit,
width,
spacingLeft,
spacingRight,
style
} = this.props;
const className = `rct-chart-line-y ${this.props.className || ""}`;
const lineY = yScale(value);
const lineX =
typeof xLimit === "undefined" ? width + spacingRight : xScale(xLimit);

return (
<line
{...{
x1: -spacingLeft,
x2: width + spacingRight,
x2: lineX,
y1: lineY,
y2: lineY,
className,
Expand Down
16 changes: 16 additions & 0 deletions tests/jsdom/spec/XLine.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,20 @@ describe("XLine", () => {
);
expect(findLine(wrapper).prop("style")).to.deep.equal(style);
});

it("limits the line's height using yLimit", () => {
const limit = 2;
let wrapper = shallow(
<XLine
yScale={linearScale}
xScale={linearScale}
yDomain={linearScale.domain}
value={linearValue}
yLimit={limit}
{...commonProps}
/>
);
expect(getLineHeight(findLine(wrapper))).to.equal(linearScale(limit));
});

});
15 changes: 15 additions & 0 deletions tests/jsdom/spec/YLine.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,19 @@ describe("YLine", () => {
);
expect(findLine(wrapper).prop("style")).to.deep.equal(style);
});

it("limits the line's width using xLimit", () => {
const limit = 2;
let wrapper = shallow(
<YLine
yScale={linearScale}
xScale={linearScale}
value={linearValue}
xLimit={limit}
{...commonProps}
/>
);
expect(getLineWidth(findLine(wrapper))).to.equal(linearScale(limit));
});

});

0 comments on commit 5e449b7

Please sign in to comment.