|
17 | 17 |
|
18 | 18 | const BAR_HEIGHT = 40; |
19 | 19 |
|
20 | | - // Account for legend bar and vertical padding in chart height |
21 | | - const chartHeight = tests.length * BAR_HEIGHT + BAR_HEIGHT + 16; |
| 20 | + const BAR_MARGIN_X = 0.1; |
| 21 | + const BAR_MARGIN_Y = 2; |
22 | 22 |
|
23 | 23 | const testSpaces = ['A', 'B', 'C']; |
24 | 24 |
|
| 25 | + const chartHeight = testSpaces.length * BAR_HEIGHT + BAR_HEIGHT + 16; |
| 26 | +
|
25 | 27 | const testsBySpace = testSpaces.map((space) => { |
26 | 28 | if (space === 'A') { |
27 | 29 | return tests.filter( |
|
33 | 35 | }); |
34 | 36 |
|
35 | 37 | function getBars(testList: ABTest[], rowPosition: number) { |
36 | | - return testList.reduce<Array<ABTestBarData>>( |
37 | | - (barsList, test, index) => { |
38 | | - const previousBar = barsList.slice(-1)[0]; |
39 | | - const offset: number = Number(previousBar?.width ?? 0); |
40 | | - const rowYLevel = index + rowPosition; |
41 | | - const testSize = getSize(test); |
42 | | -
|
43 | | - return [ |
44 | | - ...barsList, |
45 | | - { |
46 | | - x: offset, |
47 | | - y: rowYLevel * BAR_HEIGHT + BAR_HEIGHT, |
48 | | - width: testSize, |
49 | | - name: test.name, |
50 | | - segments: `${offset}% to ${offset + testSize}%`, |
51 | | - }, |
52 | | - ]; |
53 | | - }, |
54 | | - [], |
55 | | - ); |
| 38 | + return testList.reduce<Array<ABTestBarData>>((barsList, test) => { |
| 39 | + const previousBarsWidth = barsList.reduce( |
| 40 | + (acc, bar) => acc + bar.width, |
| 41 | + 0, |
| 42 | + ); |
| 43 | + const offset: number = Number(previousBarsWidth); |
| 44 | + const rowYLevel = rowPosition; |
| 45 | + const testSize = getSize(test); |
| 46 | +
|
| 47 | + return [ |
| 48 | + ...barsList, |
| 49 | + { |
| 50 | + x: offset, |
| 51 | + y: rowYLevel * BAR_HEIGHT + BAR_HEIGHT, |
| 52 | + width: testSize, |
| 53 | + name: test.name, |
| 54 | + segments: `${offset}% to ${offset + testSize}%`, |
| 55 | + }, |
| 56 | + ]; |
| 57 | + }, []); |
56 | 58 | } |
57 | 59 |
|
58 | 60 | function getAllRows(testsBySpace: ABTest[][]) { |
59 | 61 | return testsBySpace.reduce<Array<ABTestBarData>>( |
60 | | - (barsList, testsInSpace) => { |
61 | | - return [...barsList, ...getBars(testsInSpace, barsList.length)]; |
| 62 | + (barsList, testsInSpace, i) => { |
| 63 | + return [...barsList, ...getBars(testsInSpace, i)]; |
62 | 64 | }, |
63 | 65 | [], |
64 | 66 | ); |
|
86 | 88 | </svg> |
87 | 89 | {#each getAllRows(testsBySpace) as bar} |
88 | 90 | <svg |
89 | | - x={`${bar.x}%`} |
| 91 | + x={`${bar.x + BAR_MARGIN_X}%`} |
90 | 92 | y={bar.y} |
91 | | - width={`${bar.width}%`} |
| 93 | + width={`${bar.width - BAR_MARGIN_X * 2}%`} |
92 | 94 | height={BAR_HEIGHT} |
93 | 95 | > |
94 | 96 | <g class="bar"> |
95 | | - <rect height={BAR_HEIGHT} width="100%" rx="4" /> |
| 97 | + <rect height={BAR_HEIGHT - BAR_MARGIN_Y} width="100%" rx="4" /> |
96 | 98 | <text class="name" x="50%" y="50%">{bar.name}</text> |
97 | 99 | <text class="segments" x="50%" y="50%">{bar.segments}</text> |
98 | 100 | </g> |
|
0 commit comments