:fix: legend
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Beihao Zhou 2022-12-11 19:16:31 -05:00
parent dbb1a5929f
commit 72ad473050
2 changed files with 23 additions and 6975 deletions

View File

@ -31,22 +31,6 @@ interface LineGraphData {
lines: LineData[]; lines: LineData[];
} }
interface LegendProps {
/** Position of the legend, relative to the graph. */
position?: "top" | "right";
/** Font size of the labels in the legend, in pixels. Default is 16px. */
itemLabelSize?: number;
/** Gap between items in the legend, in pixels. */
itemGap?: number;
/** Distance between the legend and other adjacent elements, in pixels. */
margin?: {
top?: number;
bottom?: number;
left?: number;
right?: number;
};
}
interface LineGraphProps { interface LineGraphProps {
data: LineGraphData; data: LineGraphData;
/** Width of the entire graph, in pixels. */ /** Width of the entire graph, in pixels. */
@ -81,11 +65,15 @@ interface LineGraphProps {
yAxisLabelSize?: number; yAxisLabelSize?: number;
/** Controls the distance between the value axis label and the value axis. */ /** Controls the distance between the value axis label and the value axis. */
yAxisLabelOffset?: number; yAxisLabelOffset?: number;
legendProps?: LegendProps; /** Distance between the right side of the graph and the legend, in px. */
legendLeftOffset?: number;
/** Distance between the top of the graph and the legend, in px. */
legendTopOffset?: number;
/** Margin for each item in the legend */
itemMargin?: string;
} }
const DEFAULT_LABEL_SIZE = 16; const DEFAULT_LABEL_SIZE = 16;
const DEFAULT_LEGEND_GAP = 16;
// TODO: Address unused props in this file // TODO: Address unused props in this file
/* eslint-disable unused-imports/no-unused-vars*/ /* eslint-disable unused-imports/no-unused-vars*/
@ -99,31 +87,18 @@ export const LineGraph = withTooltip<LineGraphProps, TooltipData>(
margin, margin,
data, data,
colorRange, colorRange,
className,
xTickLabelSize = DEFAULT_LABEL_SIZE, xTickLabelSize = DEFAULT_LABEL_SIZE,
yTickLabelSize = DEFAULT_LABEL_SIZE, yTickLabelSize = DEFAULT_LABEL_SIZE,
hoverLabelSize,
xAxisLabel,
xAxisLabelSize = DEFAULT_LABEL_SIZE,
xAxisLabelOffset = 0,
yAxisLabel,
yAxisLabelSize = DEFAULT_LABEL_SIZE,
yAxisLabelOffset = 0,
tooltipOpen, tooltipOpen,
tooltipLeft, tooltipLeft,
tooltipTop, tooltipTop,
tooltipData, tooltipData,
hideTooltip, hideTooltip,
showTooltip, showTooltip,
legendProps, legendLeftOffset = 40,
legendTopOffset = 40,
itemMargin = "0 0 0 15px",
}) => { }) => {
const {
position: legendPosition = "right",
itemLabelSize: legendLabelSize = DEFAULT_LABEL_SIZE,
itemGap: legendItemGap = DEFAULT_LEGEND_GAP,
margin: legendMargin = {},
} = legendProps ?? {};
const xLength = data.xValues.length; const xLength = data.xValues.length;
if (data.lines.length != colorRange.length) { if (data.lines.length != colorRange.length) {
@ -169,20 +144,24 @@ export const LineGraph = withTooltip<LineGraphProps, TooltipData>(
const keys = data.lines.map((line) => line.label); const keys = data.lines.map((line) => line.label);
const legendScale = scaleOrdinal<string, string>({ const colorScale = scaleOrdinal<string, string>({
domain: keys, domain: keys,
range: colorRange, range: colorRange,
}); });
return ( return (
<div <div className={styles.container}>
className={ <div
className ? `${className} ${styles.wrapper}` : styles.wrapper className={styles.legend}
} style={{ left: width + legendLeftOffset, top: legendTopOffset }}
style={{ >
flexDirection: legendPosition === "right" ? "row" : "column-reverse", <LegendOrdinal
}} scale={colorScale}
> direction="row"
itemMargin={itemMargin}
labelAlign="center"
/>
</div>
<svg width={width} height={height}> <svg width={width} height={height}>
<Group top={margin.top} left={margin.left}> <Group top={margin.top} left={margin.left}>
<GridColumns <GridColumns
@ -273,27 +252,6 @@ export const LineGraph = withTooltip<LineGraphProps, TooltipData>(
</Group> </Group>
</Group> </Group>
</svg> </svg>
<LegendOrdinal
className={styles.legend}
style={{
marginTop: legendMargin.top,
marginRight: legendMargin.right,
marginBottom: legendMargin.bottom,
marginLeft: legendMargin.left,
fontSize: legendLabelSize,
}}
scale={legendScale}
direction={legendPosition === "right" ? "column" : "row"}
itemMargin={
legendPosition === "right"
? `calc(${legendItemGap / 2}rem / 16) 0 calc(${
legendItemGap / 2
}rem / 16) 0`
: `0 calc(${legendItemGap / 2}rem / 16) 0 calc(${
legendItemGap / 2
}rem / 16)`
}
/>
{tooltipOpen && ( {tooltipOpen && (
<TooltipWrapper <TooltipWrapper

6912
package-lock.json generated

File diff suppressed because it is too large Load Diff