Skip to content
Snippets Groups Projects
Unverified Commit e5dbebd0 authored by Alexander Polyankin's avatar Alexander Polyankin Committed by GitHub
Browse files

Add vertical labels for bar chart (#17664)

parent b0a9dbee
No related merge requests found
...@@ -3,15 +3,21 @@ import React from "react"; ...@@ -3,15 +3,21 @@ import React from "react";
import { t } from "ttag"; import { t } from "ttag";
import { Bar } from "@visx/shape"; import { Bar } from "@visx/shape";
import { AxisLeft, AxisBottom } from "@visx/axis"; import { AxisLeft, AxisBottom } from "@visx/axis";
import { GridRows } from "@visx/grid";
import { scaleBand, scaleLinear } from "@visx/scale"; import { scaleBand, scaleLinear } from "@visx/scale";
import { Text } from "@visx/text";
import { bottomAxisTickStyles, leftAxisTickStyles } from "../utils.js"; import { bottomAxisTickStyles, leftAxisTickStyles } from "../utils.js";
import { GridRows } from "@visx/grid";
export default function CategoricalBar( export default function CategoricalBar(
{ data, yScaleType = scaleLinear, accessors, labels }, { data, yScaleType = scaleLinear, accessors, labels },
layout, layout,
) { ) {
const leftMargin = 55; const leftMargin = 55;
const isVertical = data.length > 10;
const leftLabel = labels.left || t`Count`;
const bottomLabel = !isVertical ? labels.bottom || t`Category` : undefined;
const tickMargin = 5;
const xAxisScale = scaleBand({ const xAxisScale = scaleBand({
domain: data.map(accessors.x), domain: data.map(accessors.x),
range: [leftMargin, layout.xMax], range: [leftMargin, layout.xMax],
...@@ -33,14 +39,15 @@ export default function CategoricalBar( ...@@ -33,14 +39,15 @@ export default function CategoricalBar(
left={leftMargin} left={leftMargin}
strokeDasharray="4" strokeDasharray="4"
/> />
{data.map(d => { {data.map((d, index) => {
const barWidth = xAxisScale.bandwidth(); const barWidth = xAxisScale.bandwidth();
const barHeight = layout.yMax - yAxisScale(accessors.y(d)); const barHeight = layout.yMax - yAxisScale(accessors.y(d));
const x = xAxisScale(accessors.x(d)); const x = xAxisScale(accessors.x(d));
const y = layout.yMax - barHeight; const y = layout.yMax - barHeight;
return ( return (
<Bar <Bar
key={`bar-${x}`} key={index}
width={barWidth} width={barWidth}
height={barHeight} height={barHeight}
x={x} x={x}
...@@ -52,22 +59,31 @@ export default function CategoricalBar( ...@@ -52,22 +59,31 @@ export default function CategoricalBar(
<AxisLeft <AxisLeft
hideTicks hideTicks
hideAxisLine hideAxisLine
tickFormat={d => {
return String(d);
}}
scale={yAxisScale} scale={yAxisScale}
label={labels.left || t`Count`} label={leftLabel}
left={leftMargin} left={leftMargin}
tickLabelProps={() => leftAxisTickStyles(layout)} tickLabelProps={() => leftAxisTickStyles(layout)}
/> />
<AxisBottom <AxisBottom
hideTicks={false} hideTicks={false}
tickStroke={layout.colors.axis.stroke} tickStroke={layout.colors.axis.stroke}
numTicks={5} numTicks={data.length}
top={layout.yMax} top={layout.yMax}
scale={xAxisScale} scale={xAxisScale}
stroke={layout.colors.axis.stroke} stroke={layout.colors.axis.stroke}
label={labels.bottom || t`Category`} label={bottomLabel}
tickComponent={props => {
const transform = isVertical
? `rotate(45, ${props.x} ${props.y}) translate(${-tickMargin} 0)`
: undefined;
const textAnchor = isVertical ? "start" : "middle";
return (
<Text {...props} transform={transform} textAnchor={textAnchor}>
{props.formattedValue}
</Text>
);
}}
tickLabelProps={() => bottomAxisTickStyles(layout)} tickLabelProps={() => bottomAxisTickStyles(layout)}
/> />
</svg> </svg>
......
export function leftAxisTickStyles(layout) { export function leftAxisTickStyles(layout) {
return { return {
fontSize: 11,
fontFamily: "Lato, sans-serif", fontFamily: "Lato, sans-serif",
fill: layout.colors.axis.label.fill, fill: layout.colors.axis.label.fill,
fontSize: 11,
textAnchor: "end", textAnchor: "end",
}; };
} }
export function bottomAxisTickStyles(layout) { export function bottomAxisTickStyles(layout) {
return { return {
fontSize: 11,
fontFamily: "Lato, sans-serif", fontFamily: "Lato, sans-serif",
fill: layout.colors.axis.label.fill, fill: layout.colors.axis.label.fill,
fontSize: 11,
textAnchor: "middle", textAnchor: "middle",
}; };
} }
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
"@visx/group": "1.7.0", "@visx/group": "1.7.0",
"@visx/scale": "1.7.0", "@visx/scale": "1.7.0",
"@visx/shape": "1.8.0", "@visx/shape": "1.8.0",
"@visx/text": "1.7.0",
"ace-builds": "^1.4.7", "ace-builds": "^1.4.7",
"arg": "^5.0.0", "arg": "^5.0.0",
"chevrotain": "^6.5.0", "chevrotain": "^6.5.0",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment