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

[Feature]: Heatmap & AreaGraph Legends #436

Merged
merged 7 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
49 changes: 31 additions & 18 deletions Graphs/AbstractGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ export default class AbstractGraph extends React.Component {
return this.yLabelWidth;
}

setDimensions(props, data = null, column = null, label = null, legendData = null) {
setDimensions(props, data = null, column = null, label = null, legendData = null, heatmapColor = {}) {
const {
graphWidth,
graphHeight
} = this.getGraphDimension(label, legendData);
} = this.getGraphDimension(label, legendData, heatmapColor);

this.setYlabelWidth(data ? data : props.data, column);
this.setLeftMargin();
Expand Down Expand Up @@ -483,7 +483,7 @@ export default class AbstractGraph extends React.Component {
return null;
}

getGraphDimension = (label, filterData = null) => {
getGraphDimension = (label, filterData = null, heatmapColor) => {
const {
height,
width,
Expand Down Expand Up @@ -521,13 +521,13 @@ export default class AbstractGraph extends React.Component {
} else {
const lineHeight = this.getLegendLineHeight(legend);
const legendData = filterData || data.filter((e, i) => data.findIndex(a => label(a) === label(e)) === i);
const legendCount = legendData.length;
const value = this.getLegendArea(filterData || data, label, { lineHeight, legendCount });
const legendCount = heatmapColor ? Object.keys(heatmapColor).length : legendData.length;
const value = this.getLegendArea(filterData || data, label, { lineHeight, legendCount, legendWidth: width });
dimensions = {
...dimensions,
graphHeight: height - value - margin.top,
legendHeight: value + margin.top,
legendWidth: width,
legendWidth: this.getAvailableWidth(),
labelWidth: labelTextWidth,
}
}
Expand All @@ -547,19 +547,29 @@ export default class AbstractGraph extends React.Component {
return legend.circleSize * circleToPixel;
}

renderLegend(data, legend, getColor, label, isVertical) {
renderLegend(data, legend, getColor, label, isVertical, heatmapColor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heatmapColor = {}
pls fix other such code as well, null heatmapColor should not result in exception

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@killanch

Changes have been made. Please check once.

if (!legend.show)
return;

// Getting unique labels
data = data.filter((e, i) => data.findIndex(a => label(a) === label(e)) === i);
const legendData = [];
for (const key in heatmapColor) {
const filterData = data.filter(element => element.key === key);
if (!!filterData.length && filterData[0].key === key) {
legendData.push(filterData[0]);
} else {
legendData.push({ key, values: [] });
}
}

const {
legendHeight,
legendWidth,
} = this.getGraphDimension(label, data);
} = this.getGraphDimension(label, legendData, heatmapColor);

const legendContainerStyle = {
marginLeft: '5px',
marginLeft: this.props.width - legendWidth,
marginTop: '5px',
width: legendWidth,
height: legendHeight,
Expand All @@ -572,31 +582,32 @@ export default class AbstractGraph extends React.Component {
// Place the legends in the bottom left corner
legendStyle = { ...legendStyle, alignSelf: 'flex-end', height: legendHeight }
} else {
// Place legends horizontally, Maximum 3 legend 1 row
// Place legends horizontally, 20 is margin between legends
const highestLabel = this.getLongestLabel(legendData, label);
legendStyle = {
...legendStyle,
display: 'grid',
gridTemplateColumns: `repeat(auto-fit, minmax(${legendWidth / 3 - 5}px, 1fr))`,
display: 'grid',
gridTemplateColumns: `repeat(auto-fit, ${highestLabel+20}px)`,
}
}

return (
<div className='legendContainer' style={legendContainerStyle}>
<div className='legend' style={legendStyle}>
{this.getLegendContent(data, legend, getColor, label)}
{this.getLegendContent(legendData, legend, getColor, label, heatmapColor)}
</div>
</div>
);
}

getLegendContent(data, legend, getColor, label) {
getLegendContent(data, legend, getColor, label, heatmapColor) {
const {
fontColor
} = this.getConfiguredProperties();

const {
labelWidth
} = this.getGraphDimension(label, data);
} = this.getGraphDimension(label, data, heatmapColor);

const lineHeight = this.getLegendLineHeight(legend);

Expand Down Expand Up @@ -682,9 +693,11 @@ export default class AbstractGraph extends React.Component {
const highestLabel = this.getLongestLabel(data, label);

if (legend) {
const { legendCount, lineHeight } = legend;
//Maximum 3 legend 1 row
const legendRow = Math.ceil(legendCount / 3);
const { legendCount, lineHeight, legendWidth } = legend;
//Caluting legendContainer height, 20 is margin between margin label.
let legendRow = Math.ceil( legendCount * (highestLabel + 20) );
legendRow = Math.ceil(legendRow / legendWidth)

return legendRow * lineHeight;
}

Expand Down
4 changes: 3 additions & 1 deletion Graphs/AreaGraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ const AreaGraph = (props) => {
renderLegend({
legend,
height,
customLegendLabel
customLegendLabel,
margin,
containerWidth: width
})
}
{
Expand Down
25 changes: 22 additions & 3 deletions Graphs/Components/utils/Legend/StandardLegend.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';
import { styled } from '@material-ui/core/styles';
import * as d3 from "d3";
import max from 'lodash/max';

const Container = styled('div')({
display: 'flex',
Expand All @@ -12,11 +14,20 @@ const Container = styled('div')({

const Item = styled('div')({
color: ({ color } = {}) => color,
flexBasis: '50%',
flexBasis: ({ legendWidth }) => legendWidth,
padding: '0.15rem 0',
});

export default ({ payload: legends, labelColumn, legend, customLegendLabel }) => {
const labelSize = (label, LabelFont = '0.6rem') => {
const container = d3.select('body').append('svg');
container.append('text').text(label).style("font-size", LabelFont);
const dimension = container.node().getBBox();
container.remove();

return dimension.width + 30;
}

export default ({ payload: legends, labelColumn, legend, customLegendLabel, containerWidth }) => {
if (legends && legends[0].value === undefined) {
return false;
}
Expand All @@ -35,11 +46,19 @@ export default ({ payload: legends, labelColumn, legend, customLegendLabel }) =>
});
}

const highestLabel = max(legends.map( ({payload, value}) => {
const label = labelColumn ? payload[labelColumn] : value;

return labelSize(label);
}));

const legendWidth = containerWidth ? `${highestLabel/containerWidth * 100}%` : '50%';

return (
<Container legend={legend}>
{
legends.map(({ color, payload, value, props: { fill, name: { xVal } = {} } = {} }, index) => (
<Item key={`legend-${index}`} color={color || fill}>
<Item key={`legend-${index}`} color={color || fill} legendWidth={legendWidth}>
<svg height='0.8rem' width='0.9rem'>
<circle cx="7" cy="9" r="3.5" fill={color || fill} />
</svg>
Expand Down
5 changes: 3 additions & 2 deletions Graphs/Components/utils/Legend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { LEGEND_SEPARATE } from '../../../../constants';
import * as customLegends from './export';
import StandardLegend from './StandardLegend';

export default ({ legend, height, type, ...rest }) => {
export default ({ legend, height, type, margin, ...rest }) => {
let Component = type ? customLegends[type] || StandardLegend : StandardLegend;
const legendHeight = legend.separate ? (legend.separate * height) / 100 : (LEGEND_SEPARATE * height) / 100;
const marginLeft = margin ? margin.left : 0;
if (legend && legend.show) {
return (
<LegendComponent
wrapperStyle={{ overflowY: 'auto', maxHeight: legendHeight, bottom: 5 }}
wrapperStyle={{ overflowY: 'auto', maxHeight: legendHeight, bottom: 5, marginLeft }}
content={(props) => (
<Component {...props} legend={legend} {...rest} />
)}
Expand Down
18 changes: 12 additions & 6 deletions Graphs/HeatmapGraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ class HeatmapGraph extends XYGraph {
} = props

const {
yColumn
yColumn,
heatmapColor,
} = this.getConfiguredProperties()

if (!data || !data.length)
return

this.parseData(props)
this.setDimensions(props, this.getFilterData(), yColumn, (d) => d["key"], this.getCellColumnData())
this.setDimensions(props, this.getFilterData(), yColumn, (d) => d["key"], this.getCellColumnData(), heatmapColor)
this.configureAxis({
data: this.getFilterData()
})
Expand Down Expand Up @@ -364,7 +365,8 @@ class HeatmapGraph extends XYGraph {
colorColumn,
colors,
legendColumn,
emptyBoxColor
emptyBoxColor,
heatmapColor,
} = this.getConfiguredProperties()

const colorScale = this.getMappedScaleColor(this.getFilterData(), legendColumn)
Expand All @@ -382,6 +384,9 @@ class HeatmapGraph extends XYGraph {
if (value === 'Empty') {
return emptyBoxColor
}
if (heatmapColor.hasOwnProperty(value)) {
return heatmapColor[value]
}
return colorScale ? colorScale(value) : stroke.color || colors[0]
}

Expand Down Expand Up @@ -574,13 +579,14 @@ class HeatmapGraph extends XYGraph {

const {
margin,
legend
legend,
heatmapColor
} = this.getConfiguredProperties();

const {
graphHeight,
graphWidth,
} = this.getGraphDimension(this.getLegendFn());
} = this.getGraphDimension(this.getLegendFn(), null, heatmapColor);

const graphStyle = {
width: graphWidth,
Expand All @@ -595,7 +601,7 @@ class HeatmapGraph extends XYGraph {
<div className='heatmap-graph'>
<div>{this.tooltip}</div>
<div style={{ height, width, display: this.checkIsVerticalLegend() ? 'flex' : 'inline-grid' }}>
{this.renderLegend(this.getCellColumnData(), legend, this.getColor(), (d) => d["key"], this.checkIsVerticalLegend())}
{this.renderLegend(this.getCellColumnData(), legend, this.getColor(), (d) => d["key"], this.checkIsVerticalLegend(), heatmapColor)}
<div className='graphContainer' style={graphStyle}>
<svg width={graphWidth} height={graphHeight}>
<g ref={node => this.node = node}>
Expand Down