Skip to content

Commit

Permalink
feat(admin-ui): make confusion matrix X labels diagonal and ellipse t…
Browse files Browse the repository at this point in the history
…hem when too long. (#364)
  • Loading branch information
mgs95 authored Oct 7, 2020
1 parent 28d1ac2 commit 24b546c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ type Props = {
width: number;
height: number;
margin: Margin;
ellipseLabels: boolean;
};

function ConfusionMatrixChart({ data, width, height, margin }: Props) {
function ConfusionMatrixChart({
data,
width,
height,
margin,
ellipseLabels
}: Props) {
const viz = useRef<ConfusionMatrixViz | null>(null);
const svg = useRef<SVGSVGElement>(null);
const tooltip = useRef<HTMLDivElement>(null);
Expand All @@ -29,7 +36,8 @@ function ConfusionMatrixChart({ data, width, height, margin }: Props) {
width,
height,
data,
margin
margin,
ellipseLabels
};
viz.current = new ConfusionMatrixViz(
svg.current,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const TEXT_COLOR = {
DARK: '#00252E',
LIGHT: '#CCF5FF'
};
const X_AXIS_MAX_LABEL_LENGTH = 20;

function getTooltipContent(d: D) {
return (
Expand All @@ -57,6 +58,7 @@ type Props = {
height: number;
margin: Margin;
data: D[];
ellipseLabels: boolean;
};

class ConfusionMatrixViz {
Expand Down Expand Up @@ -157,9 +159,11 @@ class ConfusionMatrixViz {
};

createAxes = () => {
const { xScale, yScale, g, innerHeight, axisBoxSide } = this;
const { xScale, yScale, g, innerHeight, axisBoxSide, ellipseLabel } = this;

const xAxis = axisBottom(xScale).tickSize(0);
const xAxis = axisBottom(xScale)
.tickSize(0)
.tickFormat(ellipseLabel);
const yAxis = axisLeft(yScale)
.ticks(0)
.tickSize(0);
Expand All @@ -179,11 +183,19 @@ class ConfusionMatrixViz {
.attr('transform', `translate(${-axisBoxSide - AXIS_PADDING},0)`)
.call(yAxis);

rotateAxis(xAxisG, -90);
rotateAxis(xAxisG, -45);

// Remove unwanted axes lines
yAxisG.select('.domain').remove();
xAxisG.select('.domain').remove();

// Better aligns the X axis labels and adds a title
xAxisG
.selectAll('text')
.attr('dx', 0)
.attr('dy', 0)
.append('svg:title')
.text(d => `${d}`);
};

createAxisBoxes = () => {
Expand Down Expand Up @@ -340,16 +352,25 @@ class ConfusionMatrixViz {
}
};

ellipseLabel = (label: string) => {
if (!this.props.ellipseLabels) return label;

return label.length > X_AXIS_MAX_LABEL_LENGTH
? `${label.slice(0, X_AXIS_MAX_LABEL_LENGTH)}...`
: label;
};

getPaddings = () => {
const {
svg,
xDomain,
yDomain,
axisBoxSide,
ellipseLabel,
props: { width }
} = this;

let xAxisHeight = getAxisHeight(svg, xDomain, -90);
let xAxisHeight = getAxisHeight(svg, xDomain.map(ellipseLabel), -45);
let yAxisWidth = getAxisWidth(svg, yDomain);

const leftAxisPadding = axisBoxSide + yAxisWidth + AXIS_PADDING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ type Props = {
nodeId?: string;
data: GetMetrics_metrics_charts_confusionMatrix[];
info: string;
expanded: boolean;
};
function ConfusionMatrixBox({ toggleExpanded, nodeId, data, info }: Props) {
function ConfusionMatrixBox({ toggleExpanded, nodeId, data, info, expanded }: Props) {
const container = useRef(null);
const { width, height } = useRenderOnResize({ container });
return (
Expand All @@ -47,6 +48,7 @@ function ConfusionMatrixBox({ toggleExpanded, nodeId, data, info }: Props) {
left: 14
}}
data={formatData(data)}
ellipseLabels={!expanded}
/>
</div>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function Charts({ data, expanded, toggleExpanded, viewAllData }: Props) {
toggleExpanded={toggleExpanded}
nodeId={'r2'}
data={data.metrics.charts.confusionMatrix}
expanded={nodesToExpand.includes('r2')}
info={INFO.CONFUSION_MATRIX}
/>
</Row>
Expand Down

0 comments on commit 24b546c

Please sign in to comment.