All files / apps/developer-portal/layers/telemetry/app/components TelemetryQuadrantChart.vue

100% Statements 33/33
100% Branches 9/9
100% Functions 16/16
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151  7x 2x 10x           2x   2x 15x             2x         11x             2x                             12x                                                     2x                                           7x     2x     7x 124x   7x             31x           7x 10x           7x 15x     7x 11x     7x         7x 21x                        
<template>
    <figure class="flex flex-col gap-3">
        <ul class="flex flex-wrap gap-4 text-xs text-muted">
            <li v-for="item in legendItems" :key="item.label" class="flex items-center gap-1.5">
                <span class="size-2.5 rounded-full" :style="item.style" />
                {{ item.label }}
            </li>
        </ul>
 
        <p class="text-xs text-dimmed">↑ {{ yLabel }}</p>
 
        <div class="relative ml-8 h-72 border-b border-l border-accented">
            <div
                v-for="tick in yTicks"
                :key="`y-${tick.label}`"
                class="absolute inset-x-0 border-t border-default"
                :style="tick.style"
            >
                <span
                    class="absolute right-full mr-1.5 -translate-y-1/2 text-xs text-dimmed tabular-nums"
                >
                    {{ tick.label }}
                </span>
            </div>
            <div
                v-for="tick in xTicks"
                :key="`x-${tick.label}`"
                class="absolute inset-y-0 border-l border-default"
                :style="tick.style"
            >
                <span
                    class="absolute top-full mt-1 -translate-x-1/2 text-xs text-dimmed tabular-nums"
                >
                    {{ tick.label }}
                </span>
            </div>
 
            <div
                class="absolute inset-y-0 border-l border-dashed border-inverted/40"
                :style="thresholds.x"
            />
            <div
                class="absolute inset-x-0 border-t border-dashed border-inverted/40"
                :style="thresholds.y"
            />
 
            <span
                v-for="quadrant in quadrants"
                :key="quadrant.label"
                class="absolute max-w-2/5 truncate rounded-xs bg-default/85 px-1 text-xs font-semibold tracking-wide uppercase"
                :title="quadrant.label"
                :class="[CORNERS[quadrant.corner], quadrant.bad ? 'tone-bad' : 'text-muted']"
            >
                {{ quadrant.label }}
            </span>
 
            <template v-for="point in dots" :key="point.name">
                <span
                    class="absolute z-10 size-3 -translate-x-1/2 translate-y-1/2 rounded-full"
                    :style="point.dot"
                    :title="point.title"
                />
                <span
                    v-if="point.labelled"
                    class="absolute z-10 max-w-40 translate-y-1/2 truncate rounded-xs bg-default/85 px-0.5 font-mono text-xs whitespace-nowrap"
                    :style="point.label"
                    :title="point.title"
                >
                    {{ point.name }}
                </span>
            </template>
        </div>
 
        <figcaption class="mt-3 text-center text-xs text-dimmed">{{ xLabel }} →</figcaption>
    </figure>
</template>
 
<script setup lang="ts" generic="Kind extends string">
import type { Quadrants } from "#layers/telemetry/shared/usageView.ts";
 
type Corner = "top-left" | "top-right" | "bottom-left" | "bottom-right";
 
interface Props {
    chart: Quadrants<Kind>;
    /** How each kind of point is drawn and named in the legend. */
    kinds: Record<Kind, { label: string; color: string; hollow?: boolean }>;
    quadrants: readonly { corner: Corner; label: string; bad?: boolean }[];
    xLabel: string;
    yLabel: string;
    /** The copy key a point's tooltip is worded with, given its `name` and `detail`. */
    pointKey: string;
}
 
const { chart, kinds, quadrants, xLabel, yLabel, pointKey } = defineProps<Props>();
 
const { t } = useI18n();
 
/** Past this share of the width a point's name goes on its left, so it never runs off the plot. */
const LABEL_FLIP = 60;
 
/** Clamped to the plot, and rounded so the server and the browser print the same style. */
const pct = (value: number): string =>
    `${Math.round(Math.min(100, Math.max(0, value)) * 1000) / 1000}%`;
 
const CORNERS = {
    "top-left": "top-2 left-2",
    "top-right": "top-2 right-2 text-right",
    "bottom-left": "bottom-2 left-2",
    "bottom-right": "bottom-2 right-2 text-right",
} as const satisfies Record<Corner, string>;
 
const fill = ({ color, hollow }: { color: string; hollow?: boolean }) => ({
    background: hollow ? "var(--ui-bg)" : color,
    border: hollow ? `2px solid ${color}` : undefined,
    boxShadow: "0 0 0 2px var(--ui-bg)",
});
 
const legendItems = computed(() =>
    (Object.values(kinds) as Props["kinds"][Kind][]).map((kind) => ({
        label: kind.label,
        style: fill(kind),
    })),
);
 
const yTicks = computed(() =>
    chart.y.ticks.map((tick) => ({ label: tick.label, style: { bottom: pct(tick.at) } })),
);
 
const xTicks = computed(() =>
    chart.x.ticks.map((tick) => ({ label: tick.label, style: { left: pct(tick.at) } })),
);
 
const thresholds = computed(() => ({
    x: { left: pct(chart.x.threshold) },
    y: { bottom: pct(chart.y.threshold) },
}));
 
const dots = computed(() =>
    chart.points.map((point) => ({
        name: point.name,
        title: t(pointKey, { name: point.name, ...point.detail }),
        labelled: point.labelled,
        dot: { left: pct(point.x), bottom: pct(point.y), ...fill(kinds[point.kind]) },
        label:
            point.x > LABEL_FLIP
                ? { right: `calc(${pct(100 - point.x)} + 0.625rem)`, bottom: pct(point.y) }
                : { left: `calc(${pct(point.x)} + 0.625rem)`, bottom: pct(point.y) },
    })),
);
</script>