PPolaris ComponentsConnect an agent

Stat Box

A compact KPI tile pairing a headline figure and its rate of change with a polaris-viz sparkline of the trailing period.

Preview

Code

Example.jsx
import { BlockStack, Box, Card, Grid, Text } from '@shopify/polaris';
import { ArrowDownIcon, ArrowUpIcon } from '@shopify/polaris-icons';
import { PolarisVizProvider, SparkLineChart } from '@shopify/polaris-viz';

const STATS = [
  { title: 'Orders', data: [13, 20, 18, 5, 8, 15, 23] },
  { title: 'Reviews', data: [3, 3, 5, 6, 5, 2, 8] },
  { title: 'Returns', data: [5, 6, 5, 8, 4, 3, 1] },
];

const toChartSeries = (values) => [
  { data: values.map((value, index) => ({ key: index, value })) },
];

const rateOfChange = (values) => {
  const start = Number(values[0]);
  const end = Number(values[values.length - 1]);
  if (!Number.isFinite(start) || !Number.isFinite(end) || start === 0) return null;
  const percentage = Math.round(((end - start) / start) * 100);
  return Math.max(-999, Math.min(999, percentage));
};

const StatTile = ({ title, data }) => {
  const change = rateOfChange(data);
  const value = data[data.length - 1];

  return (
    <Card padding="0">
      <Box paddingBlock="400" paddingInlineStart="400">
        <div
          style={{
            height: 65,
            position: 'relative',
            width: '100%',
            display: 'flex',
            justifyContent: 'space-between',
          }}
        >
          <div
            style={{
              display: 'flex',
              flexDirection: 'column',
              justifyContent: 'end',
              minWidth: 30,
            }}
          >
            <div style={{ position: 'absolute', top: -8, left: -2, zIndex: 20 }}>
              <Text as="p" variant="headingSm">
                {title}
              </Text>
            </div>
            <Text as="p" variant="headingLg" fontWeight="bold">
              {value}
            </Text>
            <div style={{ display: 'flex', alignItems: 'center', marginBottom: -4 }}>
              {change ? (
                change > 0 ? (
                  <ArrowUpIcon style={{ height: 12, width: 12 }} fill="green" />
                ) : (
                  <ArrowDownIcon style={{ height: 12, width: 12 }} fill="red" />
                )
              ) : null}
              <Text as="span" variant="bodySm" tone="subdued">
                <span style={change ? { color: change > 0 ? 'green' : 'red' } : undefined}>
                  {change ? `${Math.abs(change)}%` : '-'}
                </span>
              </Text>
            </div>
          </div>
          <div style={{ flex: 1, minWidth: 0, height: 48, alignSelf: 'end' }}>
            <SparkLineChart offsetLeft={4} offsetRight={0} data={toChartSeries(data)} />
          </div>
        </div>
      </Box>
    </Card>
  );
};

export const Example = () => {
  return (
    <PolarisVizProvider>
      <BlockStack gap="400">
        <BlockStack gap="100">
          <Text as="h2" variant="headingMd">
            Daily stats
          </Text>
          <Text as="p" variant="bodySm" tone="subdued">
            Shows rate of change from the first entry of the chart data to today
          </Text>
        </BlockStack>
        <Grid columns={{ xs: 6, sm: 6, md: 6, lg: 12, xl: 12 }}>
          {STATS.map((stat) => (
            <Grid.Cell key={stat.title} columnSpan={{ xs: 6, sm: 6, md: 2, lg: 4, xl: 4 }}>
              <StatTile {...stat} />
            </Grid.Cell>
          ))}
        </Grid>
      </BlockStack>
    </PolarisVizProvider>
  );
};