Stabilize chart axis
This commit is contained in:
parent
0a030e88dc
commit
14451a8d7b
|
|
@ -10,7 +10,7 @@ import {
|
||||||
ResponsiveContainer,
|
ResponsiveContainer,
|
||||||
Tooltip
|
Tooltip
|
||||||
} from "recharts";
|
} from "recharts";
|
||||||
import { linspace } from "./utils";
|
import { getLinspaceTicks } from "./utils";
|
||||||
import { useTheme } from "@material-ui/styles";
|
import { useTheme } from "@material-ui/styles";
|
||||||
|
|
||||||
function SupplyVsDemandChart({
|
function SupplyVsDemandChart({
|
||||||
|
|
@ -112,7 +112,7 @@ function SupplyVsDemandChart({
|
||||||
<CartesianGrid strokeDasharray="3 3" />
|
<CartesianGrid strokeDasharray="3 3" />
|
||||||
<XAxis
|
<XAxis
|
||||||
interval={"preserveStartEnd"}
|
interval={"preserveStartEnd"}
|
||||||
ticks={linspace({ to: to, steps: 4 })}
|
ticks={getLinspaceTicks(data.map(d => d[keyHorizontal]), 4)}
|
||||||
dataKey={keyHorizontal}
|
dataKey={keyHorizontal}
|
||||||
tickFormatter={formatter}
|
tickFormatter={formatter}
|
||||||
unit={unit}
|
unit={unit}
|
||||||
|
|
@ -121,7 +121,7 @@ function SupplyVsDemandChart({
|
||||||
/>
|
/>
|
||||||
<YAxis
|
<YAxis
|
||||||
interval={"preserveStartEnd"}
|
interval={"preserveStartEnd"}
|
||||||
ticks={linspace({ to: f(to), steps: 3 })}
|
ticks={getLinspaceTicks(data.map(d => d[keyVertical]), 3)}
|
||||||
tickFormatter={formatter}
|
tickFormatter={formatter}
|
||||||
unit={unit}
|
unit={unit}
|
||||||
tick={{ fill: theme.palette.text.secondary }}
|
tick={{ fill: theme.palette.text.secondary }}
|
||||||
|
|
@ -139,7 +139,7 @@ function SupplyVsDemandChart({
|
||||||
<ReferenceLine
|
<ReferenceLine
|
||||||
x={R0}
|
x={R0}
|
||||||
stroke="#90a4ae"
|
stroke="#90a4ae"
|
||||||
strokeDasharray="6 3"
|
strokeDasharray="9 0"
|
||||||
label={<ReferenceLabel />}
|
label={<ReferenceLabel />}
|
||||||
/>
|
/>
|
||||||
<Legend formatter={renderColorfulLegendText} />
|
<Legend formatter={renderColorfulLegendText} />
|
||||||
|
|
|
||||||
14
src/utils.ts
14
src/utils.ts
|
|
@ -15,6 +15,20 @@ export function linspace({
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a uniform distribution of `num` ticks
|
||||||
|
*/
|
||||||
|
export function getLinspaceTicks(data: number[], num: number) {
|
||||||
|
const desiredPoints = [];
|
||||||
|
const step = (data[data.length - 1] - data[0]) / num;
|
||||||
|
for (let x = data[0]; x < data[data.length - 1]; x += step) {
|
||||||
|
desiredPoints.push(x);
|
||||||
|
}
|
||||||
|
if (desiredPoints.length < num + 1) desiredPoints.push(data[data.length - 1]);
|
||||||
|
|
||||||
|
return desiredPoints;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the last element of an array
|
* Returns the last element of an array
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue