From 4a9e836d681450681d1201cf1ec43e33ac6b127e Mon Sep 17 00:00:00 2001 From: Tanay Vardhan Date: Tue, 9 Jul 2019 01:11:29 +0530 Subject: [PATCH] Add limit props to set height and width of XLine and YLine respectively (#152) * implement xy limit props * update documentation * add test cases, better undefined check for XLine --- docs/src/docs/XLine/propDocs.json | 21 +++++++++++++++++++++ docs/src/docs/YLine/propDocs.json | 14 ++++++++++++++ src/XLine.js | 24 ++++++++++++++++++++++-- src/YLine.js | 11 ++++++++++- tests/jsdom/spec/XLine.spec.js | 16 ++++++++++++++++ tests/jsdom/spec/YLine.spec.js | 15 +++++++++++++++ 6 files changed, 98 insertions(+), 3 deletions(-) diff --git a/docs/src/docs/XLine/propDocs.json b/docs/src/docs/XLine/propDocs.json index 375b58b3..3d1b9cf4 100644 --- a/docs/src/docs/XLine/propDocs.json +++ b/docs/src/docs/XLine/propDocs.json @@ -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" diff --git a/docs/src/docs/YLine/propDocs.json b/docs/src/docs/YLine/propDocs.json index 6b7d5e49..cf270778 100644 --- a/docs/src/docs/YLine/propDocs.json +++ b/docs/src/docs/YLine/propDocs.json @@ -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" diff --git a/src/XLine.js b/src/XLine.js index aa9396c5..2ac4c890 100644 --- a/src/XLine.js +++ b/src/XLine.js @@ -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 */ @@ -39,6 +48,9 @@ export default class XLine extends React.Component { const { xScale, value, + yScale, + yLimit, + yDomain, height, style, spacingTop, @@ -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 ( { ); expect(findLine(wrapper).prop("style")).to.deep.equal(style); }); + + it("limits the line's height using yLimit", () => { + const limit = 2; + let wrapper = shallow( + + ); + expect(getLineHeight(findLine(wrapper))).to.equal(linearScale(limit)); + }); + }); diff --git a/tests/jsdom/spec/YLine.spec.js b/tests/jsdom/spec/YLine.spec.js index 78f21e51..ebcf37f2 100644 --- a/tests/jsdom/spec/YLine.spec.js +++ b/tests/jsdom/spec/YLine.spec.js @@ -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( + + ); + expect(getLineWidth(findLine(wrapper))).to.equal(linearScale(limit)); + }); + });