Files
superset2/caravel/assets/javascripts/explorev2/components/charts/TimeSeriesLineChart.jsx
Alanna Scott 8a5f050f6c [explore v2] fix explorev2 chart errors (#1277)
* fix prototypes and arrow function

* only show line chart if viz type is line

* split render lines function

* fix arrow-body linter
2016-10-06 13:07:27 -07:00

69 lines
1.7 KiB
JavaScript

import React, { PropTypes } from 'react';
import * as V from 'victory';
import theme from '../../../components/VictoryTheme';
import moment from 'moment';
import { schemeCategory20c } from 'd3-scale';
import Legend from './Legend';
const propTypes = {
data: PropTypes.array.isRequired,
label1: PropTypes.string.isRequired,
};
export default class TimeSeriesLineChart extends React.Component {
constructor(props) {
super(props);
this.keysToColorsMap = this.mapKeysToColors(props.data);
}
mapKeysToColors(data) {
// todo: what if there are more lines than colors in schemeCategory20c?
const keysToColorsMap = {};
data.forEach((d, i) => {
keysToColorsMap[d.key] = schemeCategory20c[i];
});
return keysToColorsMap;
}
renderLines() {
return this.props.data.map((d) => (
<V.VictoryLine
key={d.key}
data={d.values}
interpolation="cardinal"
style={{ data: { stroke: this.keysToColorsMap[d.key] } }}
/>
));
}
render() {
return (
<div style={{ height: '80%', width: '100%' }}>
<V.VictoryChart
theme={theme}
>
{this.renderLines()}
<V.VictoryAxis
label={this.props.label1}
orientation="left"
/>
<V.VictoryAxis
dependentAxis
label={'label needed'}
orientation="bottom"
tickValues={this.props.data[0].values.map((d) => d.x)}
tickFormat={(x) => moment(new Date(x)).format('YYYY')}
fixLabelOverlap
/>
</V.VictoryChart>
<Legend
data={this.props.data}
keysToColorsMap={this.keysToColorsMap}
/>
</div>
);
}
}
TimeSeriesLineChart.propTypes = propTypes;