merge
This commit is contained in:
commit
ed31c4f1fc
|
@ -17,8 +17,7 @@ module.exports = {
|
||||||
],
|
],
|
||||||
plugins: ["@typescript-eslint", "react", "react-hooks", "prettier"],
|
plugins: ["@typescript-eslint", "react", "react-hooks", "prettier"],
|
||||||
rules: {
|
rules: {
|
||||||
"prettier/prettier": "error",
|
"prettier/prettier": ["error", { "endOfLine": "auto" }],
|
||||||
|
|
||||||
"import/first": "error",
|
"import/first": "error",
|
||||||
"import/order": [
|
"import/order": [
|
||||||
"error",
|
"error",
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
.tickLabel {
|
||||||
|
font-family: "Inconsolata", monospace;
|
||||||
|
font-weight: 800;
|
||||||
|
fill: var(--label);
|
||||||
|
}
|
||||||
|
|
||||||
|
.line:hover {
|
||||||
|
filter: drop-shadow(0 0 calc(4rem / 16) var(--primary-accent));
|
||||||
|
}
|
||||||
|
|
||||||
|
.tooltip {
|
||||||
|
font-family: "Inconsolata", monospace;
|
||||||
|
font-weight: bold;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
position: absolute;
|
||||||
|
background-color: var(--label);
|
||||||
|
color: var(--primary-background);
|
||||||
|
box-shadow: 0px calc(1rem / 16) calc(2rem / 16) var(--card-background);
|
||||||
|
pointer-events: none;
|
||||||
|
padding: calc(10rem / 16);
|
||||||
|
font-size: calc(18rem / 16);
|
||||||
|
border-radius: calc(10rem / 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
.wrapper {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
width: min-content;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legend {
|
||||||
|
display: flex;
|
||||||
|
margin: calc(16rem / 8);
|
||||||
|
}
|
|
@ -1,22 +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 * as allCurves from "@visx/curve";
|
import { leftTickLabelProps } from "@visx/axis/lib/axis/AxisLeft";
|
||||||
|
import { localPoint } from "@visx/event";
|
||||||
|
import { GridColumns, GridRows } from "@visx/grid";
|
||||||
import { Group } from "@visx/group";
|
import { Group } from "@visx/group";
|
||||||
import { scaleBand, scaleLinear } from "@visx/scale";
|
import { LegendOrdinal } from "@visx/legend";
|
||||||
|
import { Point } from "@visx/point";
|
||||||
|
import { scaleBand, scaleLinear, scaleOrdinal } from "@visx/scale";
|
||||||
import { LinePath } from "@visx/shape";
|
import { LinePath } from "@visx/shape";
|
||||||
import { extent, max } from "d3-array";
|
import { useTooltip, useTooltipInPortal } from "@visx/tooltip";
|
||||||
|
import React from "react";
|
||||||
import { Color } from "utils/Color";
|
import { Color } from "utils/Color";
|
||||||
|
|
||||||
type CurveType = keyof typeof allCurves;
|
import styles from "./LineGraph.module.css";
|
||||||
|
|
||||||
const curveTypes = Object.keys(allCurves);
|
|
||||||
const lineCount = 2;
|
|
||||||
// const series = new Array(lineCount).fill(null).map((_, i) =>
|
|
||||||
// // vary each series value deterministically
|
|
||||||
// generateDateValue(25, /* seed= */ i / 72).sort(
|
|
||||||
// (a: DateValue, b: DateValue) => a.date.getTime() - b.date.getTime()
|
|
||||||
// )
|
|
||||||
// );
|
|
||||||
|
|
||||||
interface LineData {
|
interface LineData {
|
||||||
label: string;
|
label: string;
|
||||||
|
@ -33,6 +29,22 @@ 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. */
|
||||||
|
@ -65,9 +77,11 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_LABEL_SIZE = 16;
|
const DEFAULT_LABEL_SIZE = 16;
|
||||||
|
const DEFAULT_LEGEND_GAP = 16;
|
||||||
|
|
||||||
export function LineGraph(props: LineGraphProps) {
|
export function LineGraph(props: LineGraphProps) {
|
||||||
const {
|
const {
|
||||||
|
@ -85,14 +99,39 @@ export function LineGraph(props: LineGraphProps) {
|
||||||
yAxisLabel,
|
yAxisLabel,
|
||||||
yAxisLabelSize = DEFAULT_LABEL_SIZE,
|
yAxisLabelSize = DEFAULT_LABEL_SIZE,
|
||||||
yAxisLabelOffset = 0,
|
yAxisLabelOffset = 0,
|
||||||
|
legendProps,
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
const curveType = "curveLinear";
|
const {
|
||||||
const svgHeight = height - 40;
|
position: legendPosition = "right",
|
||||||
//const allData = data.reduce((rec, d) => rec.concat(d), []);
|
itemLabelSize: legendLabelSize = DEFAULT_LABEL_SIZE,
|
||||||
//console.log(allData);
|
itemGap: legendItemGap = DEFAULT_LEGEND_GAP,
|
||||||
|
margin: legendMargin = {},
|
||||||
|
} = legendProps ?? {};
|
||||||
|
|
||||||
// update scale output ranges
|
const xLength = data.xValues.length;
|
||||||
|
|
||||||
|
data.lines.forEach((line) => {
|
||||||
|
if (line.yValues.length != xLength) {
|
||||||
|
throw new Error("Invalid data with wrong length.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const {
|
||||||
|
tooltipData,
|
||||||
|
tooltipLeft,
|
||||||
|
tooltipTop,
|
||||||
|
tooltipOpen,
|
||||||
|
showTooltip,
|
||||||
|
hideTooltip,
|
||||||
|
} = useTooltip();
|
||||||
|
|
||||||
|
const { containerRef, TooltipInPortal } = useTooltipInPortal({
|
||||||
|
// use TooltipWithBounds
|
||||||
|
detectBounds: true,
|
||||||
|
// when tooltip containers are scrolled, this will correctly update the Tooltip position
|
||||||
|
scroll: true,
|
||||||
|
});
|
||||||
|
|
||||||
const yMax = height - margin.top - margin.bottom;
|
const yMax = height - margin.top - margin.bottom;
|
||||||
const xMax = width - margin.left - margin.right;
|
const xMax = width - margin.left - margin.right;
|
||||||
|
@ -103,6 +142,12 @@ export function LineGraph(props: LineGraphProps) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const yMaxValue = Math.max(
|
||||||
|
...data.lines.map((line) => {
|
||||||
|
return Math.max(...line.yValues);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
// data accessors
|
// data accessors
|
||||||
const getX = (d: PointData) => d.x;
|
const getX = (d: PointData) => d.x;
|
||||||
const getY = (d: PointData) => d.y;
|
const getY = (d: PointData) => d.y;
|
||||||
|
@ -112,33 +157,49 @@ export function LineGraph(props: LineGraphProps) {
|
||||||
range: [0, xMax],
|
range: [0, xMax],
|
||||||
domain: data.xValues,
|
domain: data.xValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
const yScale = scaleLinear<number>({
|
const yScale = scaleLinear<number>({
|
||||||
range: [0, yMax],
|
range: [0, yMax],
|
||||||
nice: true,
|
nice: true,
|
||||||
domain: [0, 100],
|
domain: [yMaxValue, 0],
|
||||||
|
});
|
||||||
|
|
||||||
|
const keys = data.lines.map((line) => line.label);
|
||||||
|
|
||||||
|
const legendScale = scaleOrdinal<string, string>({
|
||||||
|
domain: keys,
|
||||||
|
range: [Color.primaryAccent, Color.secondaryAccent],
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<svg width={width} height={svgHeight}>
|
<div
|
||||||
|
className={className ? `${className} ${styles.wrapper}` : styles.wrapper}
|
||||||
|
style={{
|
||||||
|
flexDirection: legendPosition === "right" ? "row" : "column-reverse",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<svg ref={containerRef} width={width} height={height}>
|
||||||
<Group top={margin.top} left={margin.left}>
|
<Group top={margin.top} left={margin.left}>
|
||||||
{actualData.map((lineData, i) => {
|
<GridColumns
|
||||||
console.log(lineData);
|
scale={xScale}
|
||||||
const even = i % 2 === 0;
|
height={yMax}
|
||||||
return (
|
left={margin.left}
|
||||||
<Group key={`line-${i}`}>
|
numTicks={5}
|
||||||
<LinePath
|
stroke={Color.tertiaryBackground}
|
||||||
curve={allCurves[curveType]}
|
strokeWidth={4}
|
||||||
data={lineData}
|
strokeDasharray="10"
|
||||||
x={(d) => xScale(getX(d)) ?? 0}
|
strokeLinecap="round"
|
||||||
y={(d) => yScale(getY(d)) ?? 0}
|
/>
|
||||||
stroke={even ? Color.primaryAccent : Color.secondaryAccent}
|
<GridRows
|
||||||
strokeWidth={2}
|
scale={yScale}
|
||||||
strokeOpacity={2}
|
width={xMax}
|
||||||
|
left={margin.left * 2.3}
|
||||||
|
numTicks={data.xValues.length}
|
||||||
|
stroke={Color.tertiaryBackground}
|
||||||
|
strokeWidth={4}
|
||||||
|
strokeDasharray="10"
|
||||||
|
strokeLinecap="round"
|
||||||
/>
|
/>
|
||||||
</Group>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</Group>
|
|
||||||
<AxisBottom
|
<AxisBottom
|
||||||
scale={xScale}
|
scale={xScale}
|
||||||
top={margin.top + yMax}
|
top={margin.top + yMax}
|
||||||
|
@ -148,20 +209,101 @@ export function LineGraph(props: LineGraphProps) {
|
||||||
tickLabelProps={() => {
|
tickLabelProps={() => {
|
||||||
return {
|
return {
|
||||||
...bottomTickLabelProps(),
|
...bottomTickLabelProps(),
|
||||||
//className: styles.tickLabel,
|
className: styles.tickLabel,
|
||||||
dy: "-0.25rem",
|
dy: "-0.25rem",
|
||||||
fontSize: `${xTickLabelSize / 16}rem`,
|
fontSize: `${xTickLabelSize / 16}rem`,
|
||||||
//width: categoryScale.bandwidth(),
|
width: xScale.bandwidth(),
|
||||||
verticalAnchor: "start",
|
|
||||||
};
|
};
|
||||||
}}
|
}}
|
||||||
label={xScale}
|
/>
|
||||||
// labelClassName={styles.axisLabel}
|
<AxisLeft
|
||||||
labelOffset={xAxisLabelOffset}
|
scale={yScale}
|
||||||
labelProps={{
|
left={margin.left}
|
||||||
fontSize: `${xAxisLabelSize / 16}rem`,
|
hideAxisLine
|
||||||
|
hideTicks
|
||||||
|
numTicks={5}
|
||||||
|
tickLabelProps={() => {
|
||||||
|
return {
|
||||||
|
...leftTickLabelProps(),
|
||||||
|
className: styles.tickLabel,
|
||||||
|
dx: "1.25rem",
|
||||||
|
dy: "0.25rem",
|
||||||
|
fontSize: `${yTickLabelSize / 16}rem`,
|
||||||
|
};
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
<Group left={margin.left + xMax / (data.xValues.length * 2)}>
|
||||||
|
{actualData.map((lineData, i) => {
|
||||||
|
const isEven = i % 2 === 0;
|
||||||
|
return (
|
||||||
|
<Group key={`line-${i}`}>
|
||||||
|
<LinePath
|
||||||
|
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: data.lines[i].label,
|
||||||
|
tooltipTop: eventSvgCoords.y,
|
||||||
|
tooltipLeft: eventSvgCoords.x,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onMouseOut={hideTooltip}
|
||||||
|
data={lineData}
|
||||||
|
className={styles.line}
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
|
x={(d) => xScale(getX(d))!}
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
|
y={(d) => yScale(getY(d))!}
|
||||||
|
stroke={
|
||||||
|
isEven ? Color.primaryAccent : Color.secondaryAccent
|
||||||
|
}
|
||||||
|
strokeWidth={4}
|
||||||
|
strokeOpacity={2}
|
||||||
|
/>
|
||||||
|
</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 && (
|
||||||
|
<TooltipInPortal
|
||||||
|
top={tooltipTop}
|
||||||
|
left={tooltipLeft}
|
||||||
|
className={styles.tooltip}
|
||||||
|
unstyled
|
||||||
|
applyPositionStyle
|
||||||
|
>
|
||||||
|
<>{tooltipData}</>
|
||||||
|
</TooltipInPortal>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,6 +119,20 @@ export const mockStackedBarKeys = [
|
||||||
"geese catchers",
|
"geese catchers",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export const mockLineData = {
|
||||||
|
xValues: ["1A", "1B", "2A", "2B", "3A", "3B"],
|
||||||
|
lines: [
|
||||||
|
{
|
||||||
|
label: "Java",
|
||||||
|
yValues: [54, 88, 22, 66, 77, 88],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: "C++",
|
||||||
|
yValues: [45, 22, 83, 98, 24, 33],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
export const mockTimelineData = [
|
export const mockTimelineData = [
|
||||||
{
|
{
|
||||||
time: "Fall 2020",
|
time: "Fall 2020",
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -6,6 +6,7 @@ import {
|
||||||
mockStackedBarKeys,
|
mockStackedBarKeys,
|
||||||
mockStackedBarGraphData,
|
mockStackedBarGraphData,
|
||||||
mockBoxPlotData,
|
mockBoxPlotData,
|
||||||
|
mockLineData,
|
||||||
mockQuoteData,
|
mockQuoteData,
|
||||||
mockQuoteDataLong,
|
mockQuoteDataLong,
|
||||||
mockPieData,
|
mockPieData,
|
||||||
|
@ -187,6 +188,22 @@ export default function Home() {
|
||||||
</p>
|
</p>
|
||||||
</CenterWrapper>
|
</CenterWrapper>
|
||||||
|
|
||||||
|
<h2>
|
||||||
|
<code>{"<LineGraph />"}</code>
|
||||||
|
</h2>
|
||||||
|
<LineGraph
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||||
|
data={mockLineData}
|
||||||
|
width={600}
|
||||||
|
height={400}
|
||||||
|
margin={{
|
||||||
|
top: 20,
|
||||||
|
bottom: 80,
|
||||||
|
left: 30,
|
||||||
|
right: 20,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
<h2>
|
<h2>
|
||||||
<code>{"<Sections />"}</code>
|
<code>{"<Sections />"}</code>
|
||||||
</h2>
|
</h2>
|
||||||
|
|
Loading…
Reference in New Issue