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

Add options to align points for area and line shapes #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/commonProps.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default {
xScale: 'linear',
yScale: 'linear',
showXGrid: true,
showYGrid: true
showYGrid: true,
pointAlign: 'left'
}

export const pieProps = {
Expand Down
15 changes: 14 additions & 1 deletion src/components/area.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import {
import D3Shape from 'd3-shape'
import CommonProps from '../commonProps';
import {series} from '../utils/series';
import {getTranslateXAmount} from '../utils/alignment';

export default class Area extends Component {
constructor (props) {
super(props);
}

static propTypes = {
pointAlign: PropTypes.oneOf(['left', 'center', 'right'])
};

static defaultProps = {
areaClassName: 'react-d3-basic__area',
...CommonProps
Expand Down Expand Up @@ -76,8 +81,16 @@ export default class Area extends Component {
render() {
var area = this._mkArea();

const translateXAmount = getTranslateXAmount(
this.props.pointAlign,
this.props.xScaleSet.bandwidth()
);
const style = {
transform: `translateX(${translateXAmount}px)`
};

return (
<g>
<g style={style}>
{area}
</g>
)
Expand Down
15 changes: 14 additions & 1 deletion src/components/line.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import {
import D3Shape from 'd3-shape'
import CommonProps from '../commonProps';
import {series} from '../utils/series';
import {getTranslateXAmount} from '../utils/alignment';

export default class Line extends Component {
constructor (props) {
super(props);
}

static propTypes = {
pointAlign: PropTypes.oneOf(['left', 'center', 'right'])
};

static defaultProps = {
interpolate: null,
lineClassName: 'react-d3-basic__line',
Expand Down Expand Up @@ -64,8 +69,16 @@ export default class Line extends Component {
render() {
var line = this._mkLine();

const translateXAmount = getTranslateXAmount(
this.props.pointAlign,
this.props.xScaleSet.bandwidth()
);
const style = {
transform: `translateX(${translateXAmount}px)`
};

return (
<g>
<g style={style}>
{line}
</g>
);
Expand Down
17 changes: 17 additions & 0 deletions src/utils/alignment.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

/**
* Get the horizontal translation amount based on point alignment.
* @param [pointAlign='left'] - How the points should be aligned, left, center,
* or right
* @param bandwidth - The width of a single band of the X scale
* @returns {number} - How many horizontal pixels to translate
*/
export function getTranslateXAmount (pointAlign, bandwidth) {
if ( pointAlign === 'center' ) {
return bandwidth / 2;
} else if ( pointAlign === 'right') {
return bandwidth;
}
return 0;
}