Fix Line Graph Legend Position (Close #100) (#107)
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Co-authored-by: Beihao Zhou <beihaozhou@Beihaos-MacBook-Air.local> Co-authored-by: e26chiu <e26chiu@csc.uwaterloo.ca> Reviewed-on: #107 Reviewed-by: Mark Chiu <e26chiu@csclub.uwaterloo.ca>
This commit is contained in:
parent
6ba124ee15
commit
2720728e46
|
@ -18,4 +18,5 @@
|
||||||
.legend {
|
.legend {
|
||||||
display: flex;
|
display: flex;
|
||||||
margin: calc(16rem / 8);
|
margin: calc(16rem / 8);
|
||||||
|
justify-content: center;
|
||||||
}
|
}
|
|
@ -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. */
|
||||||
|
@ -62,33 +46,15 @@ interface LineGraphProps {
|
||||||
};
|
};
|
||||||
/** List of hexademical colours for each line, length of colorRange should match the length of data. */
|
/** List of hexademical colours for each line, length of colorRange should match the length of data. */
|
||||||
colorRange: string[];
|
colorRange: string[];
|
||||||
className?: string;
|
|
||||||
/** Font size of the category tick labels, in pixels. Default is 16px. */
|
/** Font size of the category tick labels, in pixels. Default is 16px. */
|
||||||
xTickLabelSize?: number;
|
xTickLabelSize?: number;
|
||||||
/** Font size of the value tick labels, in pixels. Default is 16px. */
|
/** Font size of the value tick labels, in pixels. Default is 16px. */
|
||||||
yTickLabelSize?: number;
|
yTickLabelSize?: number;
|
||||||
/** Font size of the value that appears when hovering over a bar, in pixels. */
|
/** Margin for each item in the legend */
|
||||||
hoverLabelSize?: number;
|
itemMargin?: string;
|
||||||
/** Label text for the category axis. */
|
|
||||||
xAxisLabel?: string;
|
|
||||||
/** Font size of the label for the cateogry axis, in pixels. */
|
|
||||||
xAxisLabelSize?: number;
|
|
||||||
/** Controls the distance between the category axis label and the category axis. */
|
|
||||||
xAxisLabelOffset?: number;
|
|
||||||
/** Label text for the value axis. */
|
|
||||||
yAxisLabel?: string;
|
|
||||||
/** Font size of the label for the value axis, in pixels. */
|
|
||||||
yAxisLabelSize?: number;
|
|
||||||
/** Controls the distance between the value axis label and the value axis. */
|
|
||||||
yAxisLabelOffset?: number;
|
|
||||||
legendProps?: LegendProps;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_LABEL_SIZE = 16;
|
const DEFAULT_LABEL_SIZE = 16;
|
||||||
const DEFAULT_LEGEND_GAP = 16;
|
|
||||||
|
|
||||||
// TODO: Address unused props in this file
|
|
||||||
/* eslint-disable unused-imports/no-unused-vars*/
|
|
||||||
|
|
||||||
type TooltipData = string;
|
type TooltipData = string;
|
||||||
|
|
||||||
|
@ -99,31 +65,16 @@ 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,
|
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 +120,21 @@ 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={
|
<div className={styles.legend}>
|
||||||
className ? `${className} ${styles.wrapper}` : styles.wrapper
|
<LegendOrdinal
|
||||||
}
|
scale={colorScale}
|
||||||
style={{
|
direction="row"
|
||||||
flexDirection: legendPosition === "right" ? "row" : "column-reverse",
|
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 +225,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
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue