This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SIP-5] Refactor and improve histogram (apache#5758)
* Extract slice and formData * indent * update data proptype * enable theme * remove legacy code * rename file * Add legend * Implement WithLegend * align legend items to the right for bottom position * add line at end of file * fix linting issues
- Loading branch information
1 parent
2811498
commit b461287
Showing
10 changed files
with
1,305 additions
and
832 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Histogram, BarSeries, XAxis, YAxis } from '@data-ui/histogram'; | ||
import { chartTheme } from '@data-ui/theme'; | ||
import { LegendOrdinal } from '@vx/legend'; | ||
import { scaleOrdinal } from '@vx/scale'; | ||
import WithLegend from './WithLegend'; | ||
import { getColorFromScheme } from '../modules/colors'; | ||
|
||
const propTypes = { | ||
className: PropTypes.string, | ||
data: PropTypes.arrayOf(PropTypes.shape({ | ||
key: PropTypes.string, | ||
values: PropTypes.arrayOf(PropTypes.number), | ||
})).isRequired, | ||
width: PropTypes.number.isRequired, | ||
height: PropTypes.number.isRequired, | ||
colorScheme: PropTypes.string, | ||
normalized: PropTypes.bool, | ||
binCount: PropTypes.number, | ||
opacity: PropTypes.number, | ||
xAxisLabel: PropTypes.string, | ||
yAxisLabel: PropTypes.string, | ||
}; | ||
const defaultProps = { | ||
className: '', | ||
colorScheme: '', | ||
normalized: false, | ||
binCount: 15, | ||
opacity: 1, | ||
xAxisLabel: '', | ||
yAxisLabel: '', | ||
}; | ||
|
||
class CustomHistogram extends React.PureComponent { | ||
render() { | ||
const { | ||
className, | ||
data, | ||
width, | ||
height, | ||
binCount, | ||
colorScheme, | ||
normalized, | ||
opacity, | ||
xAxisLabel, | ||
yAxisLabel, | ||
} = this.props; | ||
|
||
const keys = data.map(d => d.key); | ||
const colorScale = scaleOrdinal({ | ||
domain: keys, | ||
range: keys.map(key => getColorFromScheme(key, colorScheme)), | ||
}); | ||
|
||
return ( | ||
<WithLegend | ||
className={`histogram-chart ${className}`} | ||
width={width} | ||
height={height} | ||
position="top" | ||
renderLegend={({ direction }) => ( | ||
<LegendOrdinal | ||
scale={colorScale} | ||
direction={direction} | ||
shape="rect" | ||
labelMargin="0 15px 0 0" | ||
/> | ||
)} | ||
renderChart={parent => ( | ||
<Histogram | ||
width={parent.width} | ||
height={parent.height} | ||
ariaLabel="Histogram" | ||
normalized={normalized} | ||
binCount={binCount} | ||
binType="numeric" | ||
renderTooltip={({ datum, color }) => ( | ||
<div> | ||
<strong style={{ color }}>{datum.bin0} to {datum.bin1}</strong> | ||
<div><strong>count </strong>{datum.count}</div> | ||
<div><strong>cumulative </strong>{datum.cumulative}</div> | ||
</div> | ||
)} | ||
valueAccessor={datum => datum} | ||
theme={chartTheme} | ||
> | ||
{data.map(series => ( | ||
<BarSeries | ||
key={series.key} | ||
animated | ||
rawData={series.values} | ||
fill={colorScale(series.key)} | ||
fillOpacity={opacity} | ||
/> | ||
))} | ||
<XAxis label={xAxisLabel} /> | ||
<YAxis label={yAxisLabel} /> | ||
</Histogram> | ||
)} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
CustomHistogram.propTypes = propTypes; | ||
CustomHistogram.defaultProps = defaultProps; | ||
|
||
function adaptor(slice, payload) { | ||
const { selector, formData } = slice; | ||
const { | ||
color_scheme: colorScheme, | ||
link_length: binCount, | ||
normalized, | ||
global_opacity: opacity, | ||
x_axis_label: xAxisLabel, | ||
y_axis_label: yAxisLabel, | ||
} = formData; | ||
|
||
ReactDOM.render( | ||
<CustomHistogram | ||
data={payload.data} | ||
width={slice.width()} | ||
height={slice.height()} | ||
binCount={parseInt(binCount, 10)} | ||
colorScheme={colorScheme} | ||
normalized={normalized} | ||
opacity={opacity} | ||
xAxisLabel={xAxisLabel} | ||
yAxisLabel={yAxisLabel} | ||
/>, | ||
document.querySelector(selector), | ||
); | ||
} | ||
|
||
export default adaptor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.with-legend .legend-container { | ||
padding-top: 5px; | ||
font-size: 0.9em; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { ParentSize } from '@vx/responsive'; | ||
import './WithLegend.css'; | ||
|
||
const propTypes = { | ||
className: PropTypes.string, | ||
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), | ||
height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), | ||
renderChart: PropTypes.func.isRequired, | ||
renderLegend: PropTypes.func.isRequired, | ||
position: PropTypes.oneOf(['top', 'left', 'bottom', 'right']), | ||
legendJustifyContent: PropTypes.oneOf(['center', 'flex-start', 'flex-end']), | ||
}; | ||
const defaultProps = { | ||
className: '', | ||
width: 'auto', | ||
height: 'auto', | ||
position: 'top', | ||
legendJustifyContent: undefined, | ||
}; | ||
|
||
const LEGEND_STYLE_BASE = { | ||
display: 'flex', | ||
flexGrow: 0, | ||
flexShrink: 0, | ||
order: -1, | ||
}; | ||
|
||
const CHART_STYLE_BASE = { | ||
flexGrow: 1, | ||
flexShrink: 1, | ||
flexBasis: 'auto', | ||
position: 'relative', | ||
}; | ||
|
||
class WithLegend extends React.Component { | ||
getContainerDirection() { | ||
const { position } = this.props; | ||
switch (position) { | ||
case 'left': return 'row'; | ||
case 'right': return 'row-reverse'; | ||
case 'bottom': return 'column-reverse'; | ||
default: | ||
case 'top': return 'column'; | ||
} | ||
} | ||
|
||
getLegendJustifyContent() { | ||
const { legendJustifyContent, position } = this.props; | ||
if (legendJustifyContent) { | ||
return legendJustifyContent; | ||
} | ||
switch (position) { | ||
case 'left': return 'flex-start'; | ||
case 'right': return 'flex-start'; | ||
case 'bottom': return 'flex-end'; | ||
default: | ||
case 'top': return 'flex-end'; | ||
} | ||
} | ||
|
||
render() { | ||
const { | ||
className, | ||
width, | ||
height, | ||
position, | ||
renderChart, | ||
renderLegend, | ||
} = this.props; | ||
|
||
const isHorizontal = position === 'left' || position === 'right'; | ||
|
||
const style = { | ||
display: 'flex', | ||
flexDirection: this.getContainerDirection(), | ||
}; | ||
if (width) { | ||
style.width = width; | ||
} | ||
if (height) { | ||
style.height = height; | ||
} | ||
|
||
const chartStyle = { ...CHART_STYLE_BASE }; | ||
if (isHorizontal) { | ||
chartStyle.width = 0; | ||
} else { | ||
chartStyle.height = 0; | ||
} | ||
|
||
const legendDirection = isHorizontal ? 'column' : 'row'; | ||
const legendStyle = { | ||
...LEGEND_STYLE_BASE, | ||
flexDirection: legendDirection, | ||
justifyContent: this.getLegendJustifyContent(), | ||
}; | ||
|
||
return ( | ||
<div className={`with-legend ${className}`} style={style}> | ||
<div className="legend-container" style={legendStyle}> | ||
{renderLegend({ | ||
// Pass flexDirection for @vx/legend to arrange legend items | ||
direction: legendDirection, | ||
})} | ||
</div> | ||
<div className="main-container" style={chartStyle}> | ||
<ParentSize>{parent => (parent.width > 0 && parent.height > 0) | ||
// Only render when necessary | ||
? renderChart(parent) | ||
: null} | ||
</ParentSize> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
WithLegend.propTypes = propTypes; | ||
WithLegend.defaultProps = defaultProps; | ||
|
||
export default WithLegend; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.