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 scaling factor for timeline, custom render popup function, prop documentation #13

Open
wants to merge 3 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
18 changes: 13 additions & 5 deletions src/GanttPopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default class GanttPopup extends Component {
markerTime: PropTypes.object.isRequired,
activeStep: PropTypes.object.isRequired,
title: PropTypes.string.isRequired,
renderPopupDetails: PropTypes.func,
titleStyle: PropTypes.object
};
static contextTypes = {
Expand All @@ -19,19 +20,26 @@ export default class GanttPopup extends Component {
marginBottom: '10px',
fontWeight: 'bold',
borderBottom: '1px solid #cfcfcf'
},
renderPopupDetails: (stepName, markerTime) => {
return (
<span>
{ markerTime }
<br />
{ stepName }
</span>
);
}
};

render() {
const { title, style, markerTime, activeStep, titleStyle } = this.props;
const { title, style, markerTime, activeStep, titleStyle, renderPopupDetails } = this.props;
const { dateFormat } = this.context;
return (
<div style={style}>
<span style={titleStyle}>{title}</span>
{moment(markerTime).format(dateFormat)}
<br />
{activeStep.name}
{renderPopupDetails(activeStep.name, moment(markerTime).format(dateFormat)) }
</div>
);
}
}
}
13 changes: 8 additions & 5 deletions src/GanttRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ export default class GanttRow extends Component {
markerStyle: PropTypes.object,
steps: PropTypes.array.isRequired,
templateName: PropTypes.string,
title: PropTypes.string
title: PropTypes.string,
renderPopupDetails: PropTypes.func
};
static contextTypes = {
templates: PropTypes.object.isRequired,
dateFormat: PropTypes.string.isRequired,
leftBound: PropTypes.object.isRequired,
rightBound: PropTypes.object.isRequired,
timelineWidth: PropTypes.number.isRequired,
debug: PropTypes.bool.isRequired
debug: PropTypes.bool.isRequired,
renderPopupDetails: PropTypes.func
};
static defaultProps = {
barStyle: {
Expand All @@ -42,7 +44,8 @@ export default class GanttRow extends Component {
opacity: 0.5
},
templateName: 'default',
title: ''
title: '',
renderPopupDetails: () => {}
};

state = {
Expand Down Expand Up @@ -158,9 +161,8 @@ export default class GanttRow extends Component {
}

renderPopup() {
const { popupStyle, title } = this.props;
const { popupStyle, title, renderPopupDetails } = this.props;
const { activeStep, markerTime, active } = this.state;
if (_.isEmpty(activeStep)) return <div />;
return (
<div
style={{
Expand All @@ -174,6 +176,7 @@ export default class GanttRow extends Component {
title={title}
activeStep={activeStep}
markerTime={markerTime}
renderPopupDetails={renderPopupDetails}
/>
</div>
);
Expand Down
15 changes: 11 additions & 4 deletions src/GanttTimeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import PropTypes from 'prop-types';

export default class GanttTimeline extends Component {
static propTypes = {
style: PropTypes.object.isRequired
style: PropTypes.object.isRequired,
scalingFactor: PropTypes.number
};

static defaultProps = {
scalingFactor: 1.0
}

static contextTypes = {
dateFormat: PropTypes.string.isRequired,
timeFormat: PropTypes.string,
Expand All @@ -23,13 +29,13 @@ export default class GanttTimeline extends Component {
};

getTick(unit, timelineDuration) {
const { style } = this.props;
const { style, scalingFactor } = this.props;
const { leftBound, rightBound, timelineWidth } = this.context;
if (!unit) {
timelineDuration = moment(rightBound).diff(moment(leftBound), 'seconds');
unit = this.getTimespanUnit(timelineDuration);
}
const tickCount = Math.ceil(timelineDuration / this.units[unit]);
const tickCount = Math.ceil(timelineDuration / this.units[unit]) * scalingFactor;
const maxTicks = Math.ceil(timelineWidth / parseInt(style.minWidth, 10));
if (tickCount > maxTicks) {
const unitKeys = _.keys(this.units);
Expand Down Expand Up @@ -87,9 +93,10 @@ export default class GanttTimeline extends Component {

durationToWidth(duration) {
const { leftBound, rightBound, timelineWidth } = this.context;
const { scalingFactor } = this.props;
const timelineDuration = moment(rightBound).diff(leftBound, 'seconds');
const percentage = duration > 0 ? duration / timelineDuration : 0;
return timelineWidth * percentage;
return timelineWidth * percentage / scalingFactor;
}

widthToDuration(width) {
Expand Down
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export default class ReactGantt extends Component {
timeFormat: PropTypes.string,
timelineStyle: PropTypes.object,
weekFormat: PropTypes.string,
yearFormat: PropTypes.string
yearFormat: PropTypes.string,
scalingFactor: PropTypes.number
};
static childContextTypes = {
dateFormat: PropTypes.string.isRequired,
Expand Down Expand Up @@ -55,7 +56,8 @@ export default class ReactGantt extends Component {
timeFormat: 'YY-MM-DD HH:MM',
timelineStyle: { minWidth: '60px' },
weekFormat: 'YY-MM-DD',
yearFormat: 'YY-MM-DD'
yearFormat: 'YY-MM-DD',
scalingFactor: 1.0
};

state = {
Expand Down Expand Up @@ -96,8 +98,7 @@ export default class ReactGantt extends Component {
}

handleResize() {
this.setState({ timelineWidth: 0 });
this.setState({ timelineWidth: this.refs.timeline.offsetWidth });
this.setState({ timelineWidth: this.timeline.offsetWidth });
}

render() {
Expand All @@ -114,14 +115,15 @@ export default class ReactGantt extends Component {
}}
/>
<th
ref="timeline"
ref={c => { this.timeline = c }}
style={{
...thStyle,
width: '100%'
}}
>
<GanttTimeline
style={this.props.timelineStyle}
scalingFactor={this.props.scalingFactor}
rows={this.props.children}
/>
</th>
Expand Down