|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * |
| 8 | + * objectstack#27372 — a chart's data labels (`showDataLabels`) read in the |
| 9 | + * units of their value axis, and the tallest mark keeps its label on the |
| 10 | + * chart surface. |
| 11 | + * |
| 12 | + * Two defects, one label: |
| 13 | + * - The value axis falls back to the compact formatter (`400K`) when no |
| 14 | + * `yAxis.format` is authored; the data label had no fallback and printed |
| 15 | + * the raw number (`174000` beside a `400K` tick). |
| 16 | + * - The label sits above its mark. With recharts' default 5px top margin, a |
| 17 | + * mark on the plot's top edge (the domain's nice max) had its label drawn |
| 18 | + * wholly above the chart surface. |
| 19 | + * |
| 20 | + * happy-dom paints no text, but given a fixed box recharts computes every mark |
| 21 | + * and label position arithmetically (the same property |
| 22 | + * `AdvancedChartImpl.pointAxisEdgeLabels-13390.test.tsx` relies on), so these |
| 23 | + * pins read the label's text and its `y` from the DOM. |
| 24 | + */ |
| 25 | +import React from 'react'; |
| 26 | +import { describe, it, expect, afterEach, vi } from 'vitest'; |
| 27 | +import { render, cleanup } from '@testing-library/react'; |
| 28 | + |
| 29 | +const WIDTH = 520; |
| 30 | +const HEIGHT = 300; |
| 31 | + |
| 32 | +vi.mock('recharts', async () => { |
| 33 | + const actual = await vi.importActual<typeof import('recharts')>('recharts'); |
| 34 | + return { |
| 35 | + ...actual, |
| 36 | + ResponsiveContainer: ({ children }: { children: React.ReactElement<{ width: number; height: number }> }) => |
| 37 | + React.cloneElement(children, { width: WIDTH, height: HEIGHT }), |
| 38 | + }; |
| 39 | +}); |
| 40 | + |
| 41 | +import AdvancedChartImpl from './AdvancedChartImpl'; |
| 42 | + |
| 43 | +afterEach(cleanup); |
| 44 | + |
| 45 | +/** The card's shape: the tallest value lands on a nice top tick (400K). */ |
| 46 | +const PIPELINE = [ |
| 47 | + { stage: 'Prospecting', amount: 174000, won: 12 }, |
| 48 | + { stage: 'Qualification', amount: 400000, won: 30 }, |
| 49 | + { stage: 'Proposal', amount: 251000, won: 18 }, |
| 50 | + { stage: 'Negotiation', amount: 92500, won: 7 }, |
| 51 | +]; |
| 52 | + |
| 53 | +/** The font size the chart prints data labels in. */ |
| 54 | +const LABEL_FONT_PX = 11; |
| 55 | + |
| 56 | +type Family = NonNullable<React.ComponentProps<typeof AdvancedChartImpl>['chartType']>; |
| 57 | + |
| 58 | +function draw(chartType: Family, extra: Record<string, unknown> = {}) { |
| 59 | + const { container } = render( |
| 60 | + <AdvancedChartImpl |
| 61 | + chartType={chartType} |
| 62 | + data={PIPELINE} |
| 63 | + xAxisKey="stage" |
| 64 | + series={[{ dataKey: 'amount', label: 'Amount' }]} |
| 65 | + showLegend={false} |
| 66 | + showDataLabels |
| 67 | + isAnimationActive={false} |
| 68 | + {...extra} |
| 69 | + />, |
| 70 | + ); |
| 71 | + return container; |
| 72 | +} |
| 73 | + |
| 74 | +/** Each data label: its text and the `y` its baseline is drawn at. */ |
| 75 | +function labelsOf(container: HTMLElement) { |
| 76 | + const texts = [...container.querySelectorAll('.recharts-label-list text')]; |
| 77 | + expect(texts.length, 'the chart drew no data labels, so this measures nothing').toBeGreaterThan(0); |
| 78 | + return texts.map((t) => ({ text: (t.textContent ?? '').trim(), y: Number(t.getAttribute('y')) })); |
| 79 | +} |
| 80 | + |
| 81 | +/** The y-axis tick texts, so a label can be read against its own axis. */ |
| 82 | +function yTicksOf(container: HTMLElement, axis = '.recharts-yAxis') { |
| 83 | + return [...container.querySelectorAll(`${axis} .recharts-cartesian-axis-tick-value`)].map((t) => |
| 84 | + (t.textContent ?? '').trim(), |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +describe('objectstack#27372 — a data label reads in its value axis’s units', () => { |
| 89 | + for (const chartType of ['bar', 'line', 'area'] as const) { |
| 90 | + it(`prints the compact default beside a compact ${chartType} axis when no format is authored`, () => { |
| 91 | + const container = draw(chartType); |
| 92 | + // The axis is the reference: it must itself be compact, or the label |
| 93 | + // matching it would prove nothing about units. |
| 94 | + expect(yTicksOf(container)).toContain('400K'); |
| 95 | + expect(labelsOf(container).map((l) => l.text)).toEqual(['174K', '400K', '251K', '92.5K']); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + it('lets an authored axis format win over the compact default', () => { |
| 100 | + const container = draw('bar', { yAxes: [{ field: 'amount', format: '$0,0' }] }); |
| 101 | + expect(labelsOf(container).map((l) => l.text)).toEqual(['$174,000', '$400,000', '$251,000', '$92,500']); |
| 102 | + }); |
| 103 | + |
| 104 | + it('prints each series in the units of the axis it is bound to on a dual-axis chart', () => { |
| 105 | + const container = draw('bar', { |
| 106 | + series: [ |
| 107 | + { dataKey: 'amount', label: 'Amount' }, |
| 108 | + { dataKey: 'won', label: 'Won', yAxis: 'right' }, |
| 109 | + ], |
| 110 | + yAxes: [{ field: 'amount' }, { field: 'won', position: 'right', format: '0.0' }], |
| 111 | + }); |
| 112 | + expect(labelsOf(container).map((l) => l.text)).toEqual([ |
| 113 | + '174K', '400K', '251K', '92.5K', |
| 114 | + '12.0', '30.0', '18.0', '7.0', |
| 115 | + ]); |
| 116 | + }); |
| 117 | + |
| 118 | + it('prints a combo series in the units of the axis it is drawn against', () => { |
| 119 | + const container = draw('combo', { |
| 120 | + series: [ |
| 121 | + { dataKey: 'amount', label: 'Amount', chartType: 'bar' }, |
| 122 | + { dataKey: 'won', label: 'Won', chartType: 'line', yAxis: 'right' }, |
| 123 | + ], |
| 124 | + yAxes: [{ field: 'amount' }, { field: 'won', position: 'right', format: '0.0' }], |
| 125 | + }); |
| 126 | + expect(labelsOf(container).map((l) => l.text)).toEqual([ |
| 127 | + '174K', '400K', '251K', '92.5K', |
| 128 | + '12.0', '30.0', '18.0', '7.0', |
| 129 | + ]); |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('objectstack#27372 — the tallest mark keeps its data label on the chart surface', () => { |
| 134 | + for (const chartType of ['bar', 'line', 'area', 'combo'] as const) { |
| 135 | + it(`draws the top label of a ${chartType} chart inside the surface`, () => { |
| 136 | + const container = draw(chartType); |
| 137 | + const labels = labelsOf(container); |
| 138 | + const top = labels.reduce((a, b) => (b.y < a.y ? b : a)); |
| 139 | + expect(top.text).toBe('400K'); |
| 140 | + // The label's baseline is at `y`; its glyphs rise at most one font size |
| 141 | + // above it. Below that, the label is (partly) off the top of the svg. |
| 142 | + expect(top.y - LABEL_FONT_PX, `"${top.text}" is drawn above the chart surface`).toBeGreaterThanOrEqual(0); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + it('keeps recharts’ default top margin when data labels are off', () => { |
| 147 | + const { container } = render( |
| 148 | + <AdvancedChartImpl |
| 149 | + chartType="bar" |
| 150 | + data={PIPELINE} |
| 151 | + xAxisKey="stage" |
| 152 | + series={[{ dataKey: 'amount', label: 'Amount' }]} |
| 153 | + showLegend={false} |
| 154 | + isAnimationActive={false} |
| 155 | + />, |
| 156 | + ); |
| 157 | + const plotTop = Number(container.querySelector('defs clipPath rect')?.getAttribute('y')); |
| 158 | + expect(plotTop).toBe(5); |
| 159 | + }); |
| 160 | +}); |
0 commit comments