Skip to content

Commit aa4c09c

Browse files
authoredAug 6, 2018
Merge pull request #1081 from FormidableLabs/feature/allow-empty-legends
allow legends to render title annd border when data is an empty array
2 parents 76df972 + b69f9a3 commit aa4c09c

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed
 

‎packages/victory-legend/src/helper-methods.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ const getTitleProps = (props, borderProps) => {
180180

181181
const getBorderProps = (props, contentHeight, contentWidth) => {
182182
const { x, y, borderPadding, style } = props;
183-
const height = contentHeight + borderPadding.top + borderPadding.bottom;
184-
const width = contentWidth + borderPadding.left + borderPadding.right;
183+
const height = (contentHeight || 0) + borderPadding.top + borderPadding.bottom;
184+
const width = (contentWidth || 0) + borderPadding.left + borderPadding.right;
185185
return { x, y, height, width, style: assign({ fill: "none" }, style.border) };
186186
};
187187

@@ -225,16 +225,17 @@ const getBaseProps = (props, fallbackProps) => {
225225
y: rowGutter && typeof rowGutter === "object" ? rowGutter.top || 0 : 0
226226
};
227227
const { height, width } = getDimensions(props, fallbackProps);
228+
const borderProps = getBorderProps(props, height, width);
229+
const titleProps = getTitleProps(props, borderProps);
228230
const initialProps = {
229231
parent: {
230232
data, standalone, theme, padding, name,
231233
height: props.height,
232234
width: props.width,
233235
style: style.parent
234-
}
236+
},
237+
all: { border: borderProps, title: titleProps }
235238
};
236-
const borderProps = getBorderProps(props, height, width);
237-
const titleProps = getTitleProps(props, borderProps);
238239
return groupedData.reduce((childProps, datum, i) => {
239240
const color = colorScale[i % colorScale.length];
240241
const dataStyle = defaults({}, datum.symbol, style.data, { fill: color });
@@ -259,9 +260,7 @@ const getBaseProps = (props, fallbackProps) => {
259260
y: dataProps.y,
260261
x: dataProps.x + datum.symbolSpacer + (datum.size / 2)
261262
};
262-
childProps[eventKey] = eventKey === 0 ?
263-
{ data: dataProps, labels: labelProps, border: borderProps, title: titleProps } :
264-
{ data: dataProps, labels: labelProps };
263+
childProps[eventKey] = { data: dataProps, labels: labelProps };
265264

266265
return childProps;
267266
}, initialProps);

‎packages/victory-legend/src/victory-legend.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,12 @@ class VictoryLegend extends React.Component {
156156
renderChildren(props) {
157157
const { dataComponent, labelComponent, title } = props;
158158
const dataComponents = this.dataKeys.map((_dataKey, index) => {
159+
if (_dataKey === "all") {
160+
return undefined;
161+
}
159162
const dataProps = this.getComponentProps(dataComponent, "data", index);
160163
return React.cloneElement(dataComponent, dataProps);
161-
});
164+
}).filter(Boolean);
162165

163166
const labelComponents = this.dataKeys.map((_dataKey, index) => {
164167
const labelProps = this.getComponentProps(labelComponent, "labels", index);
@@ -168,10 +171,10 @@ class VictoryLegend extends React.Component {
168171
return undefined;
169172
}).filter(Boolean);
170173

171-
const borderProps = this.getComponentProps(props.borderComponent, "border", 0);
174+
const borderProps = this.getComponentProps(props.borderComponent, "border", "all");
172175
const borderComponent = React.cloneElement(props.borderComponent, borderProps);
173176
if (title) {
174-
const titleProps = this.getComponentProps(props.title, "title", 0);
177+
const titleProps = this.getComponentProps(props.title, "title", "all");
175178
const titleComponent = React.cloneElement(props.titleComponent, titleProps);
176179
return [borderComponent, ...dataComponents, titleComponent, ...labelComponents];
177180
}

0 commit comments

Comments
 (0)
Failed to load comments.