Skip to content
On this page

Charts > Scatter plot ​

A scatter plot is a graphical representation of individual data points on a two-dimensional plane, where each point represents the values of two variables, displaying the relationship or distribution between them.

The Chart ​

IMDb Scores vs. Episode Viewership in The Simpsons' "Treehouse of Horror" Halloween episodes. Source: Data World

Download example CSV
Copy HTML

Playground ​

Data

Allow zooming and panning
Allow brushing to select dots
Show ref lines for median
Show regression line
Show confidence band
Show axis from value 0

Animation

Duration of animation in ms
Easing curve (D3)

Style

Color of dots
Show grid lines in background

Usage Example (with tooltip) ​

vue
<template>
  <ScatterPlot
    :data="data"
    class="h-[500px]"
    @hover="setTooltip"
    @zoom="setZoom"
  />

  <OTTooltip :info="tooltip">
    <p class="font-semibold">{{ tooltip.name }}</p>
  </OTTooltip>
</template>

<script setup>
import useTooltip from "@/functions/useTooltip.js";

const [tooltip, setTooltip] = useTooltip();
</script>

Component API ​

Data ​

js
[
        { id: "1", name: "Name 1", x: 10, y: 20, value: 1 },
        { id: "2", name: "Name 2", x: 20, y: 30, value: 1 },
        { id: "3", name: "Name 3", x: 5, y: 10, value: 1 },
        { id: "4", name: "Name 4", x: 20, y: 15, value: 1 },
        { id: "5", name: "Name 5", x: 30, y: 30, value: 1 },
        { id: "6", name: "Name 6", x: 30, y: 20, value: 1 },
        { id: "7", name: "Name 7", x: 40, y: 35, value: 1 },
...
      ],

Props ​

NameDefaultDescriptionType
data[]The datasetArray
margin{ top: 8, right: 8, bottom: 12, left: 12 }The margin on each side of the chart in pxObject
dotSize[5, 5]The min and max size of dots (to encode value as size)Array
dotOpacity0.8The transparency of dotsNumber
color#6B7280The color of dotsString
strokeColornullThe stroke around dotsString
strokeOpacity0.5The transparency of stroke around dotsNumber
gridColor#E5E7EBThe color of the grid linesString
showGridtrueWhether or not to show grid linesBoolean
refLineColor#6B7280The color of the reference linesString
strokeDasharray3,2The dash pattern for ref linesString
easeeaseCubicThe easing function for animationString
duration300The total time of the animation in msNumber
xDomainnullThe min and max values for x axis (computed from data if not given)Array
yDomainnullThe min and max values for y axis (computed from data if not given)Array
xTickFormatnullThe format function for x axis valuesFunction
yTickFormatnullThe format function for y axis valuesFunction
xTickFormatHovernullThe format function for hovered value on x axisFunction
yTickFormatHovernullThe format function for hovered value on y axisFunction
xTicksnullThe number of ticks on the x axisNumber
yTicksnullThe number of ticks on the y axisNumber
showMedianLinestrueWhether or not to show reference lines for the medianBoolean
showRegressionLinefalseWhether or not to show a regression lineBoolean
xScaleTypescaleLinearThe type of scale to use for the x axisString
highlightednullThe id of the highlighted dotString
transformValues{ k: 1, x: 0, y: 0 }The scale and translate values for programmatic zoomingObject
allowBrushfalseWhether or not to allowing brushing interactionsBoolean
allowZoomfalseWhether or not to allow zoom and panning interactionsBoolean
sizeByValuefalseWhether or not to size dots based on their valueBoolean
isMiniMapfalseWhether or not to use as mini mapBoolean
showFlagsOnHoverfalseWhether or not to show a flag when hovering a dotBoolean
selectednullThe id of the selected dotString | Number
refLinesnullThe coords of reference linesArray
axisNicefalseWhether to round axes domains to nice valuesBoolean

Events ​

NameArgumentDescription
@select{ target, data }Emitted when dot is clicked
@hover{ target, data } \nullEmitted when dot is hovered
@zoom{ transform, xDomain, yDomain }Emitted when chart is zoomed or panned
@brush{ data, target } \ nullEmitted when chart is brushed
@hoverRegressionLine{ data: r-squared } \nullEmitted when regression line is hovered

Dependencies ​

  • d3
  • d3-regression
  • useRegression