mirror of
https://github.com/apache/superset.git
synced 2026-05-12 11:25:56 +00:00
* Initial work * Working version * Specify legend position * Max height with scroll * Fix lint * Better compatibility with nvd3 * Fix object.keys polyfill version * Fix lint
59 lines
1.5 KiB
JavaScript
59 lines
1.5 KiB
JavaScript
import React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
|
|
import './Legend.css';
|
|
|
|
const propTypes = {
|
|
categories: PropTypes.object,
|
|
toggleCategory: PropTypes.func,
|
|
showSingleCategory: PropTypes.func,
|
|
position: PropTypes.oneOf(['tl', 'tr', 'bl', 'br']),
|
|
};
|
|
|
|
const defaultProps = {
|
|
categories: {},
|
|
toggleCategory: () => {},
|
|
showSingleCategory: () => {},
|
|
position: 'tr',
|
|
};
|
|
|
|
export default class Legend extends React.PureComponent {
|
|
render() {
|
|
if (Object.keys(this.props.categories).length === 0) {
|
|
return null;
|
|
}
|
|
|
|
const categories = Object.entries(this.props.categories).map(([k, v]) => {
|
|
const style = { color: 'rgba(' + v.color.join(', ') + ')' };
|
|
const icon = v.enabled ? '\u25CF' : '\u25CB';
|
|
return (
|
|
<li>
|
|
<a
|
|
href="#"
|
|
onClick={() => this.props.toggleCategory(k)}
|
|
onDoubleClick={() => this.props.showSingleCategory(k)}
|
|
>
|
|
<span style={style}>{icon}</span> {k}
|
|
</a>
|
|
</li>
|
|
);
|
|
});
|
|
|
|
const vertical = this.props.position.charAt(0) === 't' ? 'top' : 'bottom';
|
|
const horizontal = this.props.position.charAt(1) === 'r' ? 'right' : 'left';
|
|
const style = {
|
|
[vertical]: '0px',
|
|
[horizontal]: '10px',
|
|
};
|
|
|
|
return (
|
|
<div className={'legend'} style={style}>
|
|
<ul className={'categories'}>{categories}</ul>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
|
|
Legend.propTypes = propTypes;
|
|
Legend.defaultProps = defaultProps;
|