Back

Dot Matrix Sales Chart

Animated sales growth chart built with a dot-matrix grid. Perfect for SaaS dashboards and analytics interfaces.

Category
CardReact
CSS
Tailwind

Manual

Create a file and paste the following code into it.

src/components/dot-chart.tsx
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { type PointerEvent, useLayoutEffect, useRef, useState } from "react";
import { motion } from "motion/react";

import { cn } from "@/lib/cn";

const DOT_TRANSITION = {
  type: "spring",
  stiffness: 320,
  damping: 28,
  mass: 0.45,
} as const;

export interface DotChartDataPoint {
  value: number;
  label?: string;
}

export interface DotChartActivePoint<
  TData extends DotChartDataPoint = DotChartDataPoint,
> {
  dataPoint: TData;
  dataIndex: number;
  columnIndex: number;
  height: number;
  x: number;
}

export interface DotChartProps<
  TData extends DotChartDataPoint = DotChartDataPoint,
> {
  data: readonly TData[];
  rows?: number;
  dotSize?: number;
  gap?: number;
  activeColor?: string;
  filledColor?: string;
  idleColor?: string;
  hoverColor?: string;
  topDotColor?: string;
  activeColumnSpread?: number;
  className?: string;
  defaultActiveIndex?: number;
  onActivePointChange?: (point: DotChartActivePoint<TData>) => void;
  onPointerLeave?: () => void;
}

function clamp(value: number, min: number, max: number) {
  return Math.min(Math.max(value, min), max);
}

export function DotChart<TData extends DotChartDataPoint = DotChartDataPoint>({
  data,
  rows = 11,
  dotSize = 4,
  gap = 5,
  activeColor = "#34d399",
  filledColor = "rgba(255,255,255,0.35)",
  idleColor = "rgba(255,255,255,0.12)",
  hoverColor = "rgba(255,255,255,0.08)",
  topDotColor = "#ffffff",
  activeColumnSpread = 4,
  className,
  defaultActiveIndex,
  onActivePointChange,
  onPointerLeave,
}: DotChartProps<TData>) {
  const containerRef = useRef<HTMLDivElement>(null);
  const [containerWidth, setContainerWidth] = useState(0);
  const [activeColumn, setActiveColumn] = useState<number | null>(null);
  const resolvedRows = Math.max(1, Math.round(rows));
  const resolvedDotSize = Math.max(1, dotSize);
  const resolvedGap = Math.max(0, gap);
  const resolvedActiveColumnSpread = Math.max(
    0,
    Math.round(activeColumnSpread),
  );

  useLayoutEffect(() => {
    const element = containerRef.current;

    if (!element) {
      return;
    }

    const observer = new ResizeObserver(([entry]) => {
      setContainerWidth(entry.contentRect.width);
    });

    observer.observe(element);

    return () => observer.disconnect();
  }, []);

  const cellSize = resolvedDotSize + resolvedGap;
  const columnCount =
    containerWidth > 0 ? Math.max(1, Math.floor(containerWidth / cellSize)) : 0;
  const svgWidth = Math.max(columnCount * cellSize, resolvedDotSize);
  const svgHeight = Math.max(resolvedRows * cellSize, resolvedDotSize);
  const hasData = data.length > 0;
  const maxValue = data.reduce(
    (largest, point) => Math.max(largest, point.value),
    0,
  );
  const normalizedValues = data.map((point) =>
    maxValue === 0 ? 0 : Math.round((point.value / maxValue) * resolvedRows),
  );

  const resolveColumnIndex = (dataIndex: number) => {
    if (columnCount <= 1 || data.length <= 1) {
      return 0;
    }

    return Math.round(
      (clamp(dataIndex, 0, data.length - 1) / (data.length - 1)) *
        (columnCount - 1),
    );
  };

  const resolveDataIndex = (columnIndex: number) => {
    if (columnCount <= 1 || data.length <= 1) {
      return 0;
    }

    return Math.round(
      (clamp(columnIndex, 0, columnCount - 1) / (columnCount - 1)) *
        (data.length - 1),
    );
  };

  const getColumnHeight = (columnIndex: number) => {
    if (!hasData) {
      return 0;
    }

    if (columnCount <= 1 || data.length <= 1) {
      return normalizedValues[0] ?? 0;
    }

    const rawIndex = (columnIndex / (columnCount - 1)) * (data.length - 1);
    const lowerIndex = Math.floor(rawIndex);
    const upperIndex = Math.min(data.length - 1, Math.ceil(rawIndex));
    const interpolation = rawIndex - lowerIndex;

    return Math.round(
      (normalizedValues[lowerIndex] ?? 0) * (1 - interpolation) +
        (normalizedValues[upperIndex] ?? 0) * interpolation,
    );
  };

  const getActiveInfluence = (columnIndex: number) => {
    if (activeColumn === null) {
      return 0;
    }

    const distance = Math.abs(activeColumn - columnIndex);

    if (distance > resolvedActiveColumnSpread) {
      return 0;
    }

    return 1 - distance / (resolvedActiveColumnSpread + 1);
  };

  useLayoutEffect(() => {
    if (!hasData || columnCount === 0) {
      return;
    }

    setActiveColumn((currentColumn) => {
      if (currentColumn !== null) {
        return clamp(currentColumn, 0, columnCount - 1);
      }

      return resolveColumnIndex(defaultActiveIndex ?? data.length - 1);
    });
  }, [columnCount, defaultActiveIndex, hasData, data.length]);

  useLayoutEffect(() => {
    if (
      !hasData ||
      columnCount === 0 ||
      activeColumn === null ||
      !onActivePointChange
    ) {
      return;
    }

    const dataIndex = resolveDataIndex(activeColumn);
    const dataPoint = data[dataIndex];

    if (!dataPoint) {
      return;
    }

    onActivePointChange({
      dataPoint,
      dataIndex,
      columnIndex: activeColumn,
      height: getColumnHeight(activeColumn),
      x: activeColumn * cellSize + cellSize / 2,
    });
  }, [activeColumn, cellSize, columnCount, data, hasData, onActivePointChange]);

  const handlePointerMove = (event: PointerEvent<SVGSVGElement>) => {
    if (!hasData || columnCount === 0) {
      return;
    }

    const bounds = event.currentTarget.getBoundingClientRect();
    const nextColumn = clamp(
      Math.floor((event.clientX - bounds.left) / cellSize),
      0,
      columnCount - 1,
    );

    setActiveColumn(nextColumn);
  };

  return (
    <div ref={containerRef} className={cn("w-full", className)}>
      {hasData && columnCount > 0 ? (
        <svg
          width={svgWidth}
          height={svgHeight}
          viewBox={`0 0 ${svgWidth} ${svgHeight}`}
          onPointerMove={handlePointerMove}
          onPointerLeave={onPointerLeave}
          style={{ cursor: "crosshair", display: "block", width: "100%" }}
        >
          {Array.from({ length: columnCount }, (_, columnIndex) => {
            const columnHeight = getColumnHeight(columnIndex);
            const isActiveColumn = activeColumn === columnIndex;
            const activeInfluence = getActiveInfluence(columnIndex);

            return (
              <g key={columnIndex}>
                {Array.from({ length: resolvedRows }, (_, rowIndex) => {
                  const dotRow = resolvedRows - 1 - rowIndex;
                  const isFilled = dotRow < columnHeight;
                  const isTopDot =
                    isActiveColumn &&
                    isFilled &&
                    dotRow === Math.max(columnHeight - 1, 0);
                  const useActiveColor = isFilled && activeInfluence > 0;

                  return (
                    <motion.circle
                      key={`${columnIndex}-${rowIndex}`}
                      cx={columnIndex * cellSize + resolvedDotSize / 2}
                      cy={rowIndex * cellSize + resolvedDotSize / 2}
                      r={resolvedDotSize / 2}
                      initial={false}
                      animate={{
                        fill: isTopDot
                          ? topDotColor
                          : useActiveColor
                            ? activeColor
                            : isActiveColumn
                              ? hoverColor
                              : isFilled
                                ? filledColor
                                : idleColor,
                        opacity: isTopDot
                          ? 1
                          : useActiveColor
                            ? 0.2 + activeInfluence * 0.8
                            : isActiveColumn
                              ? isFilled
                                ? 1
                                : 0.45
                              : 1,
                        scale: isTopDot ? 1.45 : 1,
                      }}
                      transition={DOT_TRANSITION}
                    />
                  );
                })}
              </g>
            );
          })}
        </svg>
      ) : null}
    </div>
  );
}

Update the import paths to match your project setup.

Similar screens