Merge branch 'main' of https://git.csclub.uwaterloo.ca/www/cs-2022-class-profile into personal-page
This commit is contained in:
commit
b837032ca0
|
@ -6,23 +6,11 @@
|
||||||
fill: var(--primary-accent-light);
|
fill: var(--primary-accent-light);
|
||||||
}
|
}
|
||||||
|
|
||||||
.barText {
|
|
||||||
visibility: hidden;
|
|
||||||
|
|
||||||
font-family: "Inconsolata", monospace;
|
|
||||||
font-weight: 800;
|
|
||||||
fill: var(--label);
|
|
||||||
}
|
|
||||||
|
|
||||||
.barGroup:hover .bar {
|
.barGroup:hover .bar {
|
||||||
fill: var(--primary-accent);
|
fill: var(--primary-accent);
|
||||||
filter: drop-shadow(0 0 calc(4rem / 16) var(--primary-accent));
|
filter: drop-shadow(0 0 calc(4rem / 16) var(--primary-accent));
|
||||||
}
|
}
|
||||||
|
|
||||||
.barGroup:hover .barText {
|
|
||||||
visibility: visible;
|
|
||||||
}
|
|
||||||
|
|
||||||
.tickLabel {
|
.tickLabel {
|
||||||
font-family: "Inconsolata", monospace;
|
font-family: "Inconsolata", monospace;
|
||||||
font-weight: 800;
|
font-weight: 800;
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
import { AxisBottom, AxisLeft } from "@visx/axis";
|
import { AxisBottom, AxisLeft } from "@visx/axis";
|
||||||
import { bottomTickLabelProps } from "@visx/axis/lib/axis/AxisBottom";
|
import { bottomTickLabelProps } from "@visx/axis/lib/axis/AxisBottom";
|
||||||
import { leftTickLabelProps } from "@visx/axis/lib/axis/AxisLeft";
|
import { leftTickLabelProps } from "@visx/axis/lib/axis/AxisLeft";
|
||||||
|
import { localPoint } from "@visx/event";
|
||||||
import { GridColumns, GridRows } from "@visx/grid";
|
import { GridColumns, GridRows } from "@visx/grid";
|
||||||
import { Group } from "@visx/group";
|
import { Group } from "@visx/group";
|
||||||
|
import { Point } from "@visx/point";
|
||||||
import { scaleBand, scaleLinear } from "@visx/scale";
|
import { scaleBand, scaleLinear } from "@visx/scale";
|
||||||
import { Bar } from "@visx/shape";
|
import { Bar } from "@visx/shape";
|
||||||
import { Text } from "@visx/text";
|
import { withTooltip } from "@visx/tooltip";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { Color } from "utils/Color";
|
import { Color } from "utils/Color";
|
||||||
|
|
||||||
|
import { TooltipWrapper } from "./TooltipWrapper";
|
||||||
|
|
||||||
import styles from "./BarGraph.module.css";
|
import styles from "./BarGraph.module.css";
|
||||||
|
|
||||||
interface BarGraphProps {
|
interface BarGraphProps {
|
||||||
|
@ -29,8 +33,6 @@ interface BarGraphProps {
|
||||||
categoryTickLabelSize?: number;
|
categoryTickLabelSize?: number;
|
||||||
/** Font size of the value axis tick labels, in pixels. Default is 16px. */
|
/** Font size of the value axis tick labels, in pixels. Default is 16px. */
|
||||||
valueTickLabelSize?: number;
|
valueTickLabelSize?: number;
|
||||||
/** Font size of the value that appears when hovering over a bar, in pixels. */
|
|
||||||
hoverLabelSize?: number;
|
|
||||||
/** Label text for the category axis. */
|
/** Label text for the category axis. */
|
||||||
categoryAxisLabel?: string;
|
categoryAxisLabel?: string;
|
||||||
/** Font size of the label for the cateogry axis, in pixels. */
|
/** Font size of the label for the cateogry axis, in pixels. */
|
||||||
|
@ -62,8 +64,11 @@ interface BarGraphData {
|
||||||
|
|
||||||
const DEFAULT_LABEL_SIZE = 16;
|
const DEFAULT_LABEL_SIZE = 16;
|
||||||
|
|
||||||
export function BarGraphHorizontal(props: BarGraphProps) {
|
type TooltipData = string;
|
||||||
const {
|
|
||||||
|
export const BarGraphHorizontal = withTooltip<BarGraphProps, TooltipData>(
|
||||||
|
({
|
||||||
|
width,
|
||||||
height,
|
height,
|
||||||
margin,
|
margin,
|
||||||
data,
|
data,
|
||||||
|
@ -71,7 +76,6 @@ export function BarGraphHorizontal(props: BarGraphProps) {
|
||||||
minWidth = 500,
|
minWidth = 500,
|
||||||
categoryTickLabelSize = DEFAULT_LABEL_SIZE,
|
categoryTickLabelSize = DEFAULT_LABEL_SIZE,
|
||||||
valueTickLabelSize = DEFAULT_LABEL_SIZE,
|
valueTickLabelSize = DEFAULT_LABEL_SIZE,
|
||||||
hoverLabelSize,
|
|
||||||
categoryAxisLabel,
|
categoryAxisLabel,
|
||||||
categoryAxisLabelSize = DEFAULT_LABEL_SIZE,
|
categoryAxisLabelSize = DEFAULT_LABEL_SIZE,
|
||||||
categoryAxisLabelOffset = 0,
|
categoryAxisLabelOffset = 0,
|
||||||
|
@ -79,8 +83,14 @@ export function BarGraphHorizontal(props: BarGraphProps) {
|
||||||
valueAxisLabelSize = DEFAULT_LABEL_SIZE,
|
valueAxisLabelSize = DEFAULT_LABEL_SIZE,
|
||||||
valueAxisLabelOffset = 0,
|
valueAxisLabelOffset = 0,
|
||||||
defaultLabelDy = "0",
|
defaultLabelDy = "0",
|
||||||
} = props;
|
tooltipOpen,
|
||||||
const width = props.width < minWidth ? minWidth : props.width; // Ensuring graph's width >= minWidth
|
tooltipLeft,
|
||||||
|
tooltipTop,
|
||||||
|
tooltipData,
|
||||||
|
hideTooltip,
|
||||||
|
showTooltip,
|
||||||
|
}) => {
|
||||||
|
width = width < minWidth ? minWidth : width; // Ensuring graph's width >= minWidth
|
||||||
const barPadding = 0.4;
|
const barPadding = 0.4;
|
||||||
|
|
||||||
const categoryMax = height - margin.top - margin.bottom;
|
const categoryMax = height - margin.top - margin.bottom;
|
||||||
|
@ -105,6 +115,7 @@ export function BarGraphHorizontal(props: BarGraphProps) {
|
||||||
const valuePoint = (d: BarGraphData) => valueScale(getValue(d));
|
const valuePoint = (d: BarGraphData) => valueScale(getValue(d));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div>
|
||||||
<svg className={className} width={width} height={height}>
|
<svg className={className} width={width} height={height}>
|
||||||
<Group top={margin.top} left={margin.left}>
|
<Group top={margin.top} left={margin.left}>
|
||||||
<Group>
|
<Group>
|
||||||
|
@ -142,32 +153,33 @@ export function BarGraphHorizontal(props: BarGraphProps) {
|
||||||
return (
|
return (
|
||||||
<Group className={styles.barGroup} key={`bar-${barName}`}>
|
<Group className={styles.barGroup} key={`bar-${barName}`}>
|
||||||
<Bar
|
<Bar
|
||||||
|
onMouseMove={(e) => {
|
||||||
|
const eventSvgCoords = localPoint(
|
||||||
|
// ownerSVGElement is given by visx docs but not recognized by typescript
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
e.target.ownerSVGElement as Element,
|
||||||
|
e
|
||||||
|
) as Point;
|
||||||
|
showTooltip({
|
||||||
|
tooltipData: getValue(d).toString(),
|
||||||
|
tooltipTop: eventSvgCoords.y,
|
||||||
|
tooltipLeft: eventSvgCoords.x,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onMouseOut={hideTooltip}
|
||||||
className={styles.bar}
|
className={styles.bar}
|
||||||
x={0}
|
x={0}
|
||||||
y={categoryPoint(d)}
|
y={categoryPoint(d)}
|
||||||
width={barLength}
|
width={barLength}
|
||||||
height={barWidth}
|
height={barWidth}
|
||||||
/>
|
/>
|
||||||
<Text
|
|
||||||
className={styles.barText}
|
|
||||||
x={valuePoint(d) - 12}
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
||||||
y={categoryPoint(d)! + barWidth / 2}
|
|
||||||
fontSize={hoverLabelSize ?? barWidth * 0.75}
|
|
||||||
textAnchor="end"
|
|
||||||
verticalAnchor="middle"
|
|
||||||
>
|
|
||||||
{getValue(d)}
|
|
||||||
</Text>
|
|
||||||
</Group>
|
</Group>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</Group>
|
</Group>
|
||||||
</Group>
|
|
||||||
<AxisLeft
|
<AxisLeft
|
||||||
scale={categoryScale}
|
scale={categoryScale}
|
||||||
top={margin.top}
|
|
||||||
left={margin.left}
|
|
||||||
hideAxisLine
|
hideAxisLine
|
||||||
hideTicks
|
hideTicks
|
||||||
tickLabelProps={() => {
|
tickLabelProps={() => {
|
||||||
|
@ -186,8 +198,7 @@ export function BarGraphHorizontal(props: BarGraphProps) {
|
||||||
/>
|
/>
|
||||||
<AxisBottom
|
<AxisBottom
|
||||||
scale={valueScale}
|
scale={valueScale}
|
||||||
top={margin.top + categoryMax}
|
top={categoryMax}
|
||||||
left={margin.left}
|
|
||||||
hideAxisLine
|
hideAxisLine
|
||||||
hideTicks
|
hideTicks
|
||||||
numTicks={5}
|
numTicks={5}
|
||||||
|
@ -206,12 +217,24 @@ export function BarGraphHorizontal(props: BarGraphProps) {
|
||||||
fontSize: `${valueAxisLabelSize / 16}rem`,
|
fontSize: `${valueAxisLabelSize / 16}rem`,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
</Group>
|
||||||
</svg>
|
</svg>
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function BarGraphVertical(props: BarGraphProps) {
|
{tooltipOpen && (
|
||||||
const {
|
<TooltipWrapper
|
||||||
|
top={tooltipTop}
|
||||||
|
left={tooltipLeft}
|
||||||
|
header={tooltipData as string}
|
||||||
|
></TooltipWrapper>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export const BarGraphVertical = withTooltip<BarGraphProps, TooltipData>(
|
||||||
|
({
|
||||||
|
width,
|
||||||
height,
|
height,
|
||||||
margin,
|
margin,
|
||||||
data,
|
data,
|
||||||
|
@ -219,7 +242,6 @@ export function BarGraphVertical(props: BarGraphProps) {
|
||||||
minWidth = 500,
|
minWidth = 500,
|
||||||
categoryTickLabelSize = DEFAULT_LABEL_SIZE,
|
categoryTickLabelSize = DEFAULT_LABEL_SIZE,
|
||||||
valueTickLabelSize = DEFAULT_LABEL_SIZE,
|
valueTickLabelSize = DEFAULT_LABEL_SIZE,
|
||||||
hoverLabelSize,
|
|
||||||
categoryAxisLabel,
|
categoryAxisLabel,
|
||||||
categoryAxisLabelSize = DEFAULT_LABEL_SIZE,
|
categoryAxisLabelSize = DEFAULT_LABEL_SIZE,
|
||||||
categoryAxisLabelOffset = 0,
|
categoryAxisLabelOffset = 0,
|
||||||
|
@ -230,8 +252,14 @@ export function BarGraphVertical(props: BarGraphProps) {
|
||||||
alternatingLabelSpace = 80,
|
alternatingLabelSpace = 80,
|
||||||
defaultLabelDy = `0px`,
|
defaultLabelDy = `0px`,
|
||||||
lowerLabelDy = `30px`,
|
lowerLabelDy = `30px`,
|
||||||
} = props;
|
tooltipOpen,
|
||||||
const width = props.width < minWidth ? minWidth : props.width; // Ensuring graph's width >= minWidth
|
tooltipLeft,
|
||||||
|
tooltipTop,
|
||||||
|
tooltipData,
|
||||||
|
hideTooltip,
|
||||||
|
showTooltip,
|
||||||
|
}) => {
|
||||||
|
width = width < minWidth ? minWidth : width; // Ensuring graph's width >= minWidth
|
||||||
const barPadding = 0.4;
|
const barPadding = 0.4;
|
||||||
const alternatingLabel = width <= widthAlternatingLabel;
|
const alternatingLabel = width <= widthAlternatingLabel;
|
||||||
const final_margin_bottom = alternatingLabel
|
const final_margin_bottom = alternatingLabel
|
||||||
|
@ -260,6 +288,7 @@ export function BarGraphVertical(props: BarGraphProps) {
|
||||||
const valuePoint = (d: BarGraphData) => valueScale(getValue(d));
|
const valuePoint = (d: BarGraphData) => valueScale(getValue(d));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div>
|
||||||
<svg className={className} width={width} height={height}>
|
<svg className={className} width={width} height={height}>
|
||||||
<Group top={margin.top} left={margin.left}>
|
<Group top={margin.top} left={margin.left}>
|
||||||
<Group>
|
<Group>
|
||||||
|
@ -297,36 +326,39 @@ export function BarGraphVertical(props: BarGraphProps) {
|
||||||
return (
|
return (
|
||||||
<Group className={styles.barGroup} key={`bar-${barName}`}>
|
<Group className={styles.barGroup} key={`bar-${barName}`}>
|
||||||
<Bar
|
<Bar
|
||||||
|
onMouseMove={(e) => {
|
||||||
|
const eventSvgCoords = localPoint(
|
||||||
|
// ownerSVGElement is given by visx docs but not recognized by typescript
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
e.target.ownerSVGElement as Element,
|
||||||
|
e
|
||||||
|
) as Point;
|
||||||
|
showTooltip({
|
||||||
|
tooltipData: getValue(d).toString(),
|
||||||
|
tooltipTop: eventSvgCoords.y,
|
||||||
|
tooltipLeft: eventSvgCoords.x,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onMouseOut={hideTooltip}
|
||||||
className={styles.bar}
|
className={styles.bar}
|
||||||
x={categoryPoint(d)}
|
x={categoryPoint(d)}
|
||||||
y={valueMax - barHeight}
|
y={valueMax - barHeight}
|
||||||
width={barWidth}
|
width={barWidth}
|
||||||
height={barHeight}
|
height={barHeight}
|
||||||
/>
|
/>
|
||||||
<Text
|
|
||||||
className={styles.barText}
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
||||||
x={categoryPoint(d)! + barWidth / 2}
|
|
||||||
y={valueMax - barHeight + 12}
|
|
||||||
fontSize={hoverLabelSize ?? barWidth * 0.5}
|
|
||||||
textAnchor="middle"
|
|
||||||
verticalAnchor="start"
|
|
||||||
>
|
|
||||||
{getValue(d)}
|
|
||||||
</Text>
|
|
||||||
</Group>
|
</Group>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</Group>
|
</Group>
|
||||||
</Group>
|
|
||||||
<AxisBottom
|
<AxisBottom
|
||||||
scale={categoryScale}
|
scale={categoryScale}
|
||||||
top={valueMax + margin.top}
|
top={valueMax}
|
||||||
left={margin.left}
|
|
||||||
hideAxisLine
|
hideAxisLine
|
||||||
hideTicks
|
hideTicks
|
||||||
tickLabelProps={(value, index) => {
|
tickLabelProps={(value, index) => {
|
||||||
const alternatingDy = index % 2 == 0 ? defaultLabelDy : lowerLabelDy;
|
const alternatingDy =
|
||||||
|
index % 2 == 0 ? defaultLabelDy : lowerLabelDy;
|
||||||
return {
|
return {
|
||||||
...bottomTickLabelProps(),
|
...bottomTickLabelProps(),
|
||||||
className: styles.tickLabel,
|
className: styles.tickLabel,
|
||||||
|
@ -345,8 +377,6 @@ export function BarGraphVertical(props: BarGraphProps) {
|
||||||
/>
|
/>
|
||||||
<AxisLeft
|
<AxisLeft
|
||||||
scale={valueScale}
|
scale={valueScale}
|
||||||
top={margin.top}
|
|
||||||
left={margin.left}
|
|
||||||
hideAxisLine
|
hideAxisLine
|
||||||
hideTicks
|
hideTicks
|
||||||
numTicks={5}
|
numTicks={5}
|
||||||
|
@ -366,6 +396,17 @@ export function BarGraphVertical(props: BarGraphProps) {
|
||||||
fontSize: `${valueAxisLabelSize / 16}rem`,
|
fontSize: `${valueAxisLabelSize / 16}rem`,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
</Group>
|
||||||
</svg>
|
</svg>
|
||||||
|
|
||||||
|
{tooltipOpen && (
|
||||||
|
<TooltipWrapper
|
||||||
|
top={tooltipTop}
|
||||||
|
left={tooltipLeft}
|
||||||
|
header={tooltipData as string}
|
||||||
|
></TooltipWrapper>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
|
@ -9,18 +9,6 @@
|
||||||
fill: var(--card-background);
|
fill: var(--card-background);
|
||||||
}
|
}
|
||||||
|
|
||||||
.barText {
|
|
||||||
visibility: hidden;
|
|
||||||
|
|
||||||
font-family: "Inconsolata", monospace;
|
|
||||||
font-weight: 800;
|
|
||||||
fill: var(--label);
|
|
||||||
}
|
|
||||||
|
|
||||||
.singleBar:hover .barText {
|
|
||||||
visibility: visible;
|
|
||||||
}
|
|
||||||
|
|
||||||
.tickLabel {
|
.tickLabel {
|
||||||
font-family: "Inconsolata", monospace;
|
font-family: "Inconsolata", monospace;
|
||||||
font-weight: 800;
|
font-weight: 800;
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
import { AxisBottom, AxisLeft } from "@visx/axis";
|
import { AxisBottom, AxisLeft } from "@visx/axis";
|
||||||
import { bottomTickLabelProps } from "@visx/axis/lib/axis/AxisBottom";
|
import { bottomTickLabelProps } from "@visx/axis/lib/axis/AxisBottom";
|
||||||
import { leftTickLabelProps } from "@visx/axis/lib/axis/AxisLeft";
|
import { leftTickLabelProps } from "@visx/axis/lib/axis/AxisLeft";
|
||||||
|
import { localPoint } from "@visx/event";
|
||||||
import { GridColumns, GridRows } from "@visx/grid";
|
import { GridColumns, GridRows } from "@visx/grid";
|
||||||
import { Group } from "@visx/group";
|
import { Group } from "@visx/group";
|
||||||
import { LegendOrdinal } from "@visx/legend";
|
import { LegendOrdinal } from "@visx/legend";
|
||||||
|
import { Point } from "@visx/point";
|
||||||
import { scaleBand, scaleLinear, scaleOrdinal } from "@visx/scale";
|
import { scaleBand, scaleLinear, scaleOrdinal } from "@visx/scale";
|
||||||
import { Bar, BarGroup, BarGroupHorizontal } from "@visx/shape";
|
import { Bar, BarGroup, BarGroupHorizontal } from "@visx/shape";
|
||||||
import { BarGroupBar as BarGroupBarType } from "@visx/shape/lib/types";
|
import { BarGroupBar as BarGroupBarType } from "@visx/shape/lib/types";
|
||||||
import { Text } from "@visx/text";
|
import { withTooltip } from "@visx/tooltip";
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { Color } from "utils/Color";
|
import { Color } from "utils/Color";
|
||||||
|
|
||||||
|
import { TooltipWrapper } from "./TooltipWrapper";
|
||||||
|
|
||||||
import styles from "./GroupedBarGraph.module.css";
|
import styles from "./GroupedBarGraph.module.css";
|
||||||
|
|
||||||
interface GroupedBarGraphProps {
|
interface GroupedBarGraphProps {
|
||||||
|
@ -79,15 +83,20 @@ interface BarGroupData {
|
||||||
|
|
||||||
// BAR_PADDING must be in the range [0, 1)
|
// BAR_PADDING must be in the range [0, 1)
|
||||||
const BAR_PADDING = 0.2;
|
const BAR_PADDING = 0.2;
|
||||||
const BAR_TEXT_PADDING = 12;
|
|
||||||
|
|
||||||
const DEFAULT_LABEL_SIZE = 16;
|
const DEFAULT_LABEL_SIZE = 16;
|
||||||
|
|
||||||
export function GroupedBarGraphVertical(props: GroupedBarGraphProps) {
|
type TooltipData = string;
|
||||||
const {
|
|
||||||
|
export const GroupedBarGraphVertical = withTooltip<
|
||||||
|
GroupedBarGraphProps,
|
||||||
|
TooltipData
|
||||||
|
>(
|
||||||
|
({
|
||||||
data: propsData,
|
data: propsData,
|
||||||
barColors,
|
barColors,
|
||||||
barHoverColorsMap,
|
barHoverColorsMap,
|
||||||
|
width,
|
||||||
height,
|
height,
|
||||||
margin,
|
margin,
|
||||||
className,
|
className,
|
||||||
|
@ -106,8 +115,14 @@ export function GroupedBarGraphVertical(props: GroupedBarGraphProps) {
|
||||||
alternatingLabelSpace = 80,
|
alternatingLabelSpace = 80,
|
||||||
defaultLabelDy = `0px`,
|
defaultLabelDy = `0px`,
|
||||||
lowerLabelDy = `30px`,
|
lowerLabelDy = `30px`,
|
||||||
} = props;
|
tooltipOpen,
|
||||||
const width = props.width < minWidth ? minWidth : props.width; // Ensuring graph's width >= minWidth
|
tooltipLeft,
|
||||||
|
tooltipTop,
|
||||||
|
tooltipData,
|
||||||
|
hideTooltip,
|
||||||
|
showTooltip,
|
||||||
|
}) => {
|
||||||
|
width = width < minWidth ? minWidth : width; // Ensuring graph's width >= minWidth
|
||||||
const alternatingLabel = width <= widthAlternatingLabel;
|
const alternatingLabel = width <= widthAlternatingLabel;
|
||||||
const final_margin_bottom = alternatingLabel
|
const final_margin_bottom = alternatingLabel
|
||||||
? margin.bottom + alternatingLabelSpace
|
? margin.bottom + alternatingLabelSpace
|
||||||
|
@ -163,7 +178,9 @@ export function GroupedBarGraphVertical(props: GroupedBarGraphProps) {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={className ? `${className} ${styles.wrapper}` : styles.wrapper}
|
className={
|
||||||
|
className ? `${className} ${styles.wrapper}` : styles.wrapper
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<div className={styles.legend}>
|
<div className={styles.legend}>
|
||||||
<LegendOrdinal
|
<LegendOrdinal
|
||||||
|
@ -239,6 +256,21 @@ export function GroupedBarGraphVertical(props: GroupedBarGraphProps) {
|
||||||
>
|
>
|
||||||
{barGroup.bars.map((bar) => (
|
{barGroup.bars.map((bar) => (
|
||||||
<HoverableBar
|
<HoverableBar
|
||||||
|
onMouseMove={(e) => {
|
||||||
|
const eventSvgCoords = localPoint(
|
||||||
|
// ownerSVGElement is given by visx docs but not recognized by typescript
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
e.target.ownerSVGElement as Element,
|
||||||
|
e
|
||||||
|
) as Point;
|
||||||
|
showTooltip({
|
||||||
|
tooltipData: bar.value.toString(),
|
||||||
|
tooltipTop: eventSvgCoords.y,
|
||||||
|
tooltipLeft: eventSvgCoords.x,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onMouseOut={hideTooltip}
|
||||||
key={`bar-group-bar-${barGroup.x0}-${barGroup.index}-${bar.key}-${bar.index}`}
|
key={`bar-group-bar-${barGroup.x0}-${barGroup.index}-${bar.key}-${bar.index}`}
|
||||||
bar={bar}
|
bar={bar}
|
||||||
valueMax={valueMax}
|
valueMax={valueMax}
|
||||||
|
@ -250,11 +282,9 @@ export function GroupedBarGraphVertical(props: GroupedBarGraphProps) {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
</BarGroup>
|
</BarGroup>
|
||||||
</Group>
|
|
||||||
<AxisBottom
|
<AxisBottom
|
||||||
scale={categoryScale}
|
scale={categoryScale}
|
||||||
top={valueMax + margin.top}
|
top={valueMax}
|
||||||
left={margin.left}
|
|
||||||
hideAxisLine
|
hideAxisLine
|
||||||
hideTicks
|
hideTicks
|
||||||
tickLabelProps={(value, index) => {
|
tickLabelProps={(value, index) => {
|
||||||
|
@ -278,8 +308,6 @@ export function GroupedBarGraphVertical(props: GroupedBarGraphProps) {
|
||||||
/>
|
/>
|
||||||
<AxisLeft
|
<AxisLeft
|
||||||
scale={valueScale}
|
scale={valueScale}
|
||||||
top={margin.top}
|
|
||||||
left={margin.left}
|
|
||||||
hideAxisLine
|
hideAxisLine
|
||||||
hideTicks
|
hideTicks
|
||||||
numTicks={5}
|
numTicks={5}
|
||||||
|
@ -299,16 +327,30 @@ export function GroupedBarGraphVertical(props: GroupedBarGraphProps) {
|
||||||
fontSize: `${valueAxisLabelSize / 16}rem`,
|
fontSize: `${valueAxisLabelSize / 16}rem`,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
</Group>
|
||||||
</svg>
|
</svg>
|
||||||
|
|
||||||
|
{tooltipOpen && (
|
||||||
|
<TooltipWrapper
|
||||||
|
top={tooltipTop}
|
||||||
|
left={tooltipLeft}
|
||||||
|
header={tooltipData as string}
|
||||||
|
></TooltipWrapper>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
export function GroupedBarGraphHorizontal(props: GroupedBarGraphProps) {
|
export const GroupedBarGraphHorizontal = withTooltip<
|
||||||
const {
|
GroupedBarGraphProps,
|
||||||
|
TooltipData
|
||||||
|
>(
|
||||||
|
({
|
||||||
data: propsData,
|
data: propsData,
|
||||||
barColors,
|
barColors,
|
||||||
barHoverColorsMap,
|
barHoverColorsMap,
|
||||||
|
width,
|
||||||
height,
|
height,
|
||||||
margin,
|
margin,
|
||||||
className,
|
className,
|
||||||
|
@ -324,8 +366,14 @@ export function GroupedBarGraphHorizontal(props: GroupedBarGraphProps) {
|
||||||
valueAxisLabelOffset = 0,
|
valueAxisLabelOffset = 0,
|
||||||
itemMargin = "0 0 0 15px",
|
itemMargin = "0 0 0 15px",
|
||||||
defaultLabelDy = "0",
|
defaultLabelDy = "0",
|
||||||
} = props;
|
tooltipOpen,
|
||||||
const width = props.width < minWidth ? minWidth : props.width; // Ensuring graph's width >= minWidth
|
tooltipLeft,
|
||||||
|
tooltipTop,
|
||||||
|
tooltipData,
|
||||||
|
hideTooltip,
|
||||||
|
showTooltip,
|
||||||
|
}) => {
|
||||||
|
width = width < minWidth ? minWidth : width; // Ensuring graph's width >= minWidth
|
||||||
|
|
||||||
const data: BarGroupData[] = propsData.map((datum: GroupedBarGraphData) => {
|
const data: BarGroupData[] = propsData.map((datum: GroupedBarGraphData) => {
|
||||||
return { category: datum.category, ...datum.values };
|
return { category: datum.category, ...datum.values };
|
||||||
|
@ -377,7 +425,9 @@ export function GroupedBarGraphHorizontal(props: GroupedBarGraphProps) {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={className ? `${className} ${styles.wrapper}` : styles.wrapper}
|
className={
|
||||||
|
className ? `${className} ${styles.wrapper}` : styles.wrapper
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<div className={styles.legend}>
|
<div className={styles.legend}>
|
||||||
<LegendOrdinal
|
<LegendOrdinal
|
||||||
|
@ -453,6 +503,21 @@ export function GroupedBarGraphHorizontal(props: GroupedBarGraphProps) {
|
||||||
>
|
>
|
||||||
{barGroup.bars.map((bar) => (
|
{barGroup.bars.map((bar) => (
|
||||||
<HoverableBar
|
<HoverableBar
|
||||||
|
onMouseMove={(e) => {
|
||||||
|
const eventSvgCoords = localPoint(
|
||||||
|
// ownerSVGElement is given by visx docs but not recognized by typescript
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
e.target.ownerSVGElement as Element,
|
||||||
|
e
|
||||||
|
) as Point;
|
||||||
|
showTooltip({
|
||||||
|
tooltipData: bar.value.toString(),
|
||||||
|
tooltipTop: eventSvgCoords.y,
|
||||||
|
tooltipLeft: eventSvgCoords.x,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onMouseOut={hideTooltip}
|
||||||
key={`bar-group-bar-${barGroup.y0}-${barGroup.index}-${bar.key}-${bar.index}`}
|
key={`bar-group-bar-${barGroup.y0}-${barGroup.index}-${bar.key}-${bar.index}`}
|
||||||
bar={bar}
|
bar={bar}
|
||||||
valueMax={valueMax}
|
valueMax={valueMax}
|
||||||
|
@ -465,11 +530,8 @@ export function GroupedBarGraphHorizontal(props: GroupedBarGraphProps) {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
</BarGroupHorizontal>
|
</BarGroupHorizontal>
|
||||||
</Group>
|
|
||||||
<AxisLeft
|
<AxisLeft
|
||||||
scale={categoryScale}
|
scale={categoryScale}
|
||||||
top={margin.top}
|
|
||||||
left={margin.left}
|
|
||||||
hideAxisLine
|
hideAxisLine
|
||||||
hideTicks
|
hideTicks
|
||||||
tickLabelProps={() => {
|
tickLabelProps={() => {
|
||||||
|
@ -491,8 +553,7 @@ export function GroupedBarGraphHorizontal(props: GroupedBarGraphProps) {
|
||||||
/>
|
/>
|
||||||
<AxisBottom
|
<AxisBottom
|
||||||
scale={valueScale}
|
scale={valueScale}
|
||||||
top={categoryMax + margin.top}
|
top={categoryMax}
|
||||||
left={margin.left}
|
|
||||||
hideAxisLine
|
hideAxisLine
|
||||||
hideTicks
|
hideTicks
|
||||||
numTicks={5}
|
numTicks={5}
|
||||||
|
@ -512,10 +573,20 @@ export function GroupedBarGraphHorizontal(props: GroupedBarGraphProps) {
|
||||||
fontSize: `${valueAxisLabelSize / 16}rem`,
|
fontSize: `${valueAxisLabelSize / 16}rem`,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
</Group>
|
||||||
</svg>
|
</svg>
|
||||||
|
|
||||||
|
{tooltipOpen && (
|
||||||
|
<TooltipWrapper
|
||||||
|
top={tooltipTop}
|
||||||
|
left={tooltipLeft}
|
||||||
|
header={tooltipData as string}
|
||||||
|
></TooltipWrapper>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
interface HoverableBarProps {
|
interface HoverableBarProps {
|
||||||
bar: BarGroupBarType<string>;
|
bar: BarGroupBarType<string>;
|
||||||
|
@ -523,16 +594,12 @@ interface HoverableBarProps {
|
||||||
hoverFillColor?: string;
|
hoverFillColor?: string;
|
||||||
hoverLabelSize?: number;
|
hoverLabelSize?: number;
|
||||||
isHorizontal?: boolean;
|
isHorizontal?: boolean;
|
||||||
|
onMouseMove?: (e: React.MouseEvent<SVGPathElement, MouseEvent>) => void;
|
||||||
|
onMouseOut?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
function HoverableBar(props: HoverableBarProps) {
|
function HoverableBar(props: HoverableBarProps) {
|
||||||
const {
|
const { bar, hoverFillColor, onMouseMove, onMouseOut } = props;
|
||||||
bar,
|
|
||||||
valueMax,
|
|
||||||
hoverLabelSize,
|
|
||||||
hoverFillColor,
|
|
||||||
isHorizontal = false,
|
|
||||||
} = props;
|
|
||||||
|
|
||||||
const [isHovered, setIsHovered] = useState(false);
|
const [isHovered, setIsHovered] = useState(false);
|
||||||
|
|
||||||
|
@ -549,6 +616,8 @@ function HoverableBar(props: HoverableBarProps) {
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Bar
|
<Bar
|
||||||
|
onMouseMove={onMouseMove}
|
||||||
|
onMouseOut={onMouseOut}
|
||||||
x={bar.x}
|
x={bar.x}
|
||||||
y={bar.y}
|
y={bar.y}
|
||||||
width={bar.width}
|
width={bar.width}
|
||||||
|
@ -557,22 +626,6 @@ function HoverableBar(props: HoverableBarProps) {
|
||||||
// apply the glow effect when the bar is hovered
|
// apply the glow effect when the bar is hovered
|
||||||
filter={isHovered ? `url(#glow-${colorId})` : undefined}
|
filter={isHovered ? `url(#glow-${colorId})` : undefined}
|
||||||
/>
|
/>
|
||||||
<Text
|
|
||||||
className={styles.barText}
|
|
||||||
x={isHorizontal ? bar.width - BAR_TEXT_PADDING : bar.x + bar.width / 2}
|
|
||||||
y={
|
|
||||||
isHorizontal
|
|
||||||
? bar.y + bar.height / 2
|
|
||||||
: valueMax - bar.height + BAR_TEXT_PADDING
|
|
||||||
}
|
|
||||||
fontSize={
|
|
||||||
hoverLabelSize ?? (isHorizontal ? bar.height : bar.width) * 0.5
|
|
||||||
}
|
|
||||||
textAnchor={isHorizontal ? "end" : "middle"}
|
|
||||||
verticalAnchor={isHorizontal ? "middle" : "start"}
|
|
||||||
>
|
|
||||||
{bar.value}
|
|
||||||
</Text>
|
|
||||||
</Group>
|
</Group>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
|
import { localPoint } from "@visx/event";
|
||||||
import { Group } from "@visx/group";
|
import { Group } from "@visx/group";
|
||||||
|
import { Point } from "@visx/point";
|
||||||
import Pie, { ProvidedProps } from "@visx/shape/lib/shapes/Pie";
|
import Pie, { ProvidedProps } from "@visx/shape/lib/shapes/Pie";
|
||||||
import { Text } from "@visx/text";
|
import { Text } from "@visx/text";
|
||||||
|
import { withTooltip } from "@visx/tooltip";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
|
import { TooltipWrapper } from "./TooltipWrapper";
|
||||||
|
|
||||||
import styles from "./PieChart.module.css";
|
import styles from "./PieChart.module.css";
|
||||||
|
|
||||||
interface PieChartProps {
|
interface PieChartProps {
|
||||||
|
@ -39,7 +44,8 @@ interface PieChartData {
|
||||||
value: number;
|
value: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function PieChart({
|
export const PieChart = withTooltip<PieChartProps>(
|
||||||
|
({
|
||||||
data,
|
data,
|
||||||
width,
|
width,
|
||||||
labelWidth,
|
labelWidth,
|
||||||
|
@ -52,11 +58,19 @@ export function PieChart({
|
||||||
labelTextSize = 40,
|
labelTextSize = 40,
|
||||||
labelTextXOffset = 0,
|
labelTextXOffset = 0,
|
||||||
labelTextYOffset = 0,
|
labelTextYOffset = 0,
|
||||||
getLabelDisplayValueFromDatum = (datum: PieChartData) => `${datum.category}`,
|
getLabelDisplayValueFromDatum = (datum: PieChartData) =>
|
||||||
|
`${datum.category}`,
|
||||||
className,
|
className,
|
||||||
}: PieChartProps) {
|
tooltipOpen,
|
||||||
|
tooltipLeft,
|
||||||
|
tooltipTop,
|
||||||
|
tooltipData,
|
||||||
|
hideTooltip,
|
||||||
|
showTooltip,
|
||||||
|
}) => {
|
||||||
const pieWidth = width * 0.5 - labelWidth;
|
const pieWidth = width * 0.5 - labelWidth;
|
||||||
return (
|
return (
|
||||||
|
<div>
|
||||||
<svg className={className} width={width} height={width}>
|
<svg className={className} width={width} height={width}>
|
||||||
<Group top={width * 0.5} left={width * 0.5}>
|
<Group top={width * 0.5} left={width * 0.5}>
|
||||||
<Pie
|
<Pie
|
||||||
|
@ -68,15 +82,38 @@ export function PieChart({
|
||||||
innerRadius={innerRadius}
|
innerRadius={innerRadius}
|
||||||
outerRadius={pieWidth}
|
outerRadius={pieWidth}
|
||||||
>
|
>
|
||||||
{(pie) => (
|
{({ arcs, path }) => {
|
||||||
<PieSlice
|
return arcs.map((arc) => {
|
||||||
{...pie}
|
const [centroidX, centroidY] = path.centroid(arc);
|
||||||
pieTextSize={pieTextSize}
|
const pathArc = path(arc) as string;
|
||||||
pieTextXOffset={pieTextXOffset}
|
return (
|
||||||
pieTextYOffset={pieTextYOffset}
|
<Group
|
||||||
getPieDisplayValueFromDatum={getPieDisplayValueFromDatum}
|
className={styles.group}
|
||||||
|
key={`arc-${arc.data.category}`}
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
onMouseMove={(e) => {
|
||||||
|
const eventSvgCoords = localPoint(
|
||||||
|
// ownerSVGElement is given by visx docs but not recognized by typescript
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
e.target.ownerSVGElement as Element,
|
||||||
|
e
|
||||||
|
) as Point;
|
||||||
|
showTooltip({
|
||||||
|
tooltipData: `${arc.data.category}: ${arc.data.value}%`,
|
||||||
|
tooltipTop: eventSvgCoords.y,
|
||||||
|
tooltipLeft: eventSvgCoords.x,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onMouseOut={hideTooltip}
|
||||||
|
className={styles.piePath}
|
||||||
|
d={pathArc}
|
||||||
/>
|
/>
|
||||||
)}
|
</Group>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}}
|
||||||
</Pie>
|
</Pie>
|
||||||
<Pie
|
<Pie
|
||||||
data={data}
|
data={data}
|
||||||
|
@ -96,48 +133,18 @@ export function PieChart({
|
||||||
</Pie>
|
</Pie>
|
||||||
</Group>
|
</Group>
|
||||||
</svg>
|
</svg>
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
type PieSliceProps<PieChartData> = ProvidedProps<PieChartData> & {
|
{tooltipOpen && (
|
||||||
pieTextSize: number;
|
<TooltipWrapper
|
||||||
pieTextXOffset: number;
|
top={tooltipTop}
|
||||||
pieTextYOffset: number;
|
left={tooltipLeft}
|
||||||
getPieDisplayValueFromDatum: (datum: PieChartData) => string;
|
header={tooltipData as string}
|
||||||
};
|
></TooltipWrapper>
|
||||||
|
)}
|
||||||
export function PieSlice({
|
</div>
|
||||||
path,
|
|
||||||
arcs,
|
|
||||||
pieTextSize,
|
|
||||||
pieTextXOffset,
|
|
||||||
pieTextYOffset,
|
|
||||||
getPieDisplayValueFromDatum,
|
|
||||||
}: PieSliceProps<PieChartData>) {
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
{arcs.map((arc) => {
|
|
||||||
const [centroidX, centroidY] = path.centroid(arc);
|
|
||||||
const pathArc = path(arc) as string;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Group className={styles.group} key={`arc-${arc.data.category}`}>
|
|
||||||
<path className={styles.piePath} d={pathArc} />
|
|
||||||
<Text
|
|
||||||
className={styles.pieText}
|
|
||||||
x={centroidX + pieTextXOffset}
|
|
||||||
y={centroidY + pieTextYOffset}
|
|
||||||
textAnchor="middle"
|
|
||||||
fontSize={pieTextSize}
|
|
||||||
>
|
|
||||||
{`${getPieDisplayValueFromDatum(arc.data)}`}
|
|
||||||
</Text>
|
|
||||||
</Group>
|
|
||||||
);
|
);
|
||||||
})}
|
}
|
||||||
</>
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
type PieSliceLabelProps<PieChartData> = ProvidedProps<PieChartData> & {
|
type PieSliceLabelProps<PieChartData> = ProvidedProps<PieChartData> & {
|
||||||
labelTextSize: number;
|
labelTextSize: number;
|
||||||
|
|
Loading…
Reference in New Issue