Skip to content
OpenChart

Trendlines

A scatter fits one least-squares line through its points by default. Turn it off, restyle it, draw it over a dense cloud and see how it treats color groups.

A trend line summarizes a scatter chart with a straight line, a least-squares fit through the points. It shows the direction and steepness of the relationship in one stroke, which helps when the cloud is too loose to judge by eye.

The line is a linear fit only, so it describes a curved relationship poorly. To draw a different line, combine marks with layers. The line works on any point chart, including a bubble chart.

The default trend line

A point mark draws a dashed trend line without any option. The line spans from the smallest x to the largest x. No line is drawn when there are fewer than two points or every point has the same x.

4681012141618Spending per student ($K, PPP)400450500550PISA math scoreA scatter draws its trend line without beingaskedCumulative per-student spending vs. PISA 2022 math performance4681012141618Spending per student ($K, PPP)400450500550PISA math scoreA scatter draws its trend line without beingaskedCumulative per-student spending vs. PISA 2022 math performance
The default trend line. Source: OECD PISA 2022, Education at a Glance.
{
  "mark": "point",
  "data": [
    {
      "country": "Singapore",
      "spending": 14.5,
      "math": 575
    },
    {
      "country": "Japan",
      "spending": 10.1,
      "math": 536
    },
    {
      "country": "South Korea",
      "spending": 12.2,
      "math": 527
    },
    {
      "country": "Estonia",
      "spending": 8.4,
      "math": 510
    },
    {
      "country": "Switzerland",
      "spending": 17.8,
      "math": 508
    },
    {
      "country": "Canada",
      "spending": 12.4,
      "math": 497
    },
    // … 16 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "A scatter draws its trend line without being asked",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "A scatter draws its trend line without being asked",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "A scatter draws its trend line without being asked",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "A scatter draws its trend line without being asked",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "A scatter draws its trend line without being asked",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Turning it off

Set trendline: false on the mark to remove the line. Do this when the chart carries its own reference line, such as a manual diagonal, which would otherwise compete with the fitted one.

4681012141618Spending per student ($K, PPP)400450500550PISA math scoreThe same points with no fitted lineCumulative per-student spending vs. PISA 2022 math performance4681012141618Spending per student ($K, PPP)400450500550PISA math scoreThe same points with no fitted lineCumulative per-student spending vs. PISA 2022 math performance
A scatter with the trend line off. Source: OECD PISA 2022, Education at a Glance.
{
  "mark": {
    "type": "point",
    "trendline": false
  },
  "data": [
    {
      "country": "Singapore",
      "spending": 14.5,
      "math": 575
    },
    {
      "country": "Japan",
      "spending": 10.1,
      "math": 536
    },
    {
      "country": "South Korea",
      "spending": 12.2,
      "math": 527
    },
    {
      "country": "Estonia",
      "spending": 8.4,
      "math": 510
    },
    {
      "country": "Switzerland",
      "spending": 17.8,
      "math": 508
    },
    {
      "country": "Canada",
      "spending": 12.4,
      "math": 497
    },
    // … 16 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "The same points with no fitted line",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "The same points with no fitted line",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "The same points with no fitted line",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "The same points with no fitted line",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "The same points with no fitted line",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Styling the line

Pass an object to trendline to restyle it. stroke sets the color, in any CSS color, and strokeWidth sets the width in pixels, which defaults to 1.5. A heavier, colored line stands out where the default blends into the axes.

4681012141618Spending per student ($K, PPP)400450500550PISA math scoreStroke color and width set how much the linestands outCumulative per-student spending vs. PISA 2022 math performance4681012141618Spending per student ($K, PPP)400450500550PISA math scoreStroke color and width set how much the linestands outCumulative per-student spending vs. PISA 2022 math performance
A styled trend line. Source: OECD PISA 2022, Education at a Glance.
{
  "mark": {
    "type": "point",
    "trendline": {
      "stroke": "#d1495b",
      "strokeWidth": 2.5
    }
  },
  "data": [
    {
      "country": "Singapore",
      "spending": 14.5,
      "math": 575
    },
    {
      "country": "Japan",
      "spending": 10.1,
      "math": 536
    },
    {
      "country": "South Korea",
      "spending": 12.2,
      "math": 527
    },
    {
      "country": "Estonia",
      "spending": 8.4,
      "math": 510
    },
    {
      "country": "Switzerland",
      "spending": 17.8,
      "math": 508
    },
    {
      "country": "Canada",
      "spending": 12.4,
      "math": 497
    },
    // … 16 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "Stroke color and width set how much the line stands out",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "trendline": {
      "stroke": "#d1495b",
      "strokeWidth": 2.5
    }
  },
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "Stroke color and width set how much the line stands out",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "trendline": {
      "stroke": "#d1495b",
      "strokeWidth": 2.5
    }
  },
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "Stroke color and width set how much the line stands out",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "trendline": {
      "stroke": "#d1495b",
      "strokeWidth": 2.5
    }
  },
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "Stroke color and width set how much the line stands out",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 22 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "trendline": {
      "stroke": "#d1495b",
      "strokeWidth": 2.5
    }
  },
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "chrome": {
    "title": "Stroke color and width set how much the line stands out",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Over a dense cloud

The line is drawn below the dots by default, and a thick cloud of dots can hide it. layer: "above" draws it on top. This chart also sets a small dot size and a partial opacity so the density shows through.

020406080100Students from low-income households (%)020406080100Reading at grade level (%)Reading scores fall as the low-income sharerisesShare of students reading at grade level vs. low-income share, by campus020406080100Students from low-income households (%)020406080100Reading at grade level (%)Reading scores fall as the low-income sharerisesShare of students reading at grade level vs. low-income share, by campus
A seeded sample of 60 invented campuses. Source: Illustrative data.
{
  "mark": {
    "type": "point",
    "size": 3,
    "opacity": 0.45,
    "trendline": {
      "layer": "above",
      "strokeWidth": 2.5
    }
  },
  "data": [
    {
      "lowIncome": 42.5,
      "reading": 80.5
    },
    {
      "lowIncome": 20.6,
      "reading": 79.1
    },
    {
      "lowIncome": 68.1,
      "reading": 75.5
    },
    {
      "lowIncome": 8.7,
      "reading": 98
    },
    {
      "lowIncome": 57.3,
      "reading": 61.8
    },
    {
      "lowIncome": 24.1,
      "reading": 96.2
    },
    // … 54 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "lowIncome",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Students from low-income households (%)"
      }
    },
    "y": {
      "field": "reading",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Reading at grade level (%)"
      }
    }
  },
  "chrome": {
    "title": "Reading scores fall as the low-income share rises",
    "subtitle": "Share of students reading at grade level vs. low-income share, by campus"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 60 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 3,
    "opacity": 0.45,
    "trendline": {
      "layer": "above",
      "strokeWidth": 2.5
    }
  },
  "encoding": {
    "x": {
      "field": "lowIncome",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Students from low-income households (%)"
      }
    },
    "y": {
      "field": "reading",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Reading at grade level (%)"
      }
    }
  },
  "chrome": {
    "title": "Reading scores fall as the low-income share rises",
    "subtitle": "Share of students reading at grade level vs. low-income share, by campus"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 60 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 3,
    "opacity": 0.45,
    "trendline": {
      "layer": "above",
      "strokeWidth": 2.5
    }
  },
  "encoding": {
    "x": {
      "field": "lowIncome",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Students from low-income households (%)"
      }
    },
    "y": {
      "field": "reading",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Reading at grade level (%)"
      }
    }
  },
  "chrome": {
    "title": "Reading scores fall as the low-income share rises",
    "subtitle": "Share of students reading at grade level vs. low-income share, by campus"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 60 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 3,
    "opacity": 0.45,
    "trendline": {
      "layer": "above",
      "strokeWidth": 2.5
    }
  },
  "encoding": {
    "x": {
      "field": "lowIncome",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Students from low-income households (%)"
      }
    },
    "y": {
      "field": "reading",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Reading at grade level (%)"
      }
    }
  },
  "chrome": {
    "title": "Reading scores fall as the low-income share rises",
    "subtitle": "Share of students reading at grade level vs. low-income share, by campus"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 60 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 3,
    "opacity": 0.45,
    "trendline": {
      "layer": "above",
      "strokeWidth": 2.5
    }
  },
  "encoding": {
    "x": {
      "field": "lowIncome",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Students from low-income households (%)"
      }
    },
    "y": {
      "field": "reading",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Reading at grade level (%)"
      }
    }
  },
  "chrome": {
    "title": "Reading scores fall as the low-income share rises",
    "subtitle": "Share of students reading at grade level vs. low-income share, by campus"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Color groups

The fit uses every point on the chart, so a chart with a color channel still gets one line, not one per group. Read it as the overall trend, and look at the colors for groups that sit off it.

406080100120140Cost of living index707580859095100Quality of living indexEuropeAsiaAsia-PacificAmericasThe fitted line covers every region at onceCost of living vs. quality of living in global cities, 2024406080100120140Cost of living index707580859095100Quality of living indexEuropeAsiaAsia-PacificAmericasThe fitted line covers every region at onceCost of living vs. quality of living in global cities, 2024
One trend line across color groups. Source: Mercer Quality of Living & Cost of Living surveys.
{
  "mark": "point",
  "data": [
    {
      "city": "Zurich",
      "cost": 131,
      "quality": 98,
      "region": "Europe"
    },
    {
      "city": "Vienna",
      "cost": 79,
      "quality": 97,
      "region": "Europe"
    },
    {
      "city": "Geneva",
      "cost": 124,
      "quality": 96,
      "region": "Europe"
    },
    {
      "city": "Copenhagen",
      "cost": 89,
      "quality": 95,
      "region": "Europe"
    },
    {
      "city": "Singapore",
      "cost": 107,
      "quality": 93,
      "region": "Asia"
    },
    {
      "city": "Sydney",
      "cost": 83,
      "quality": 92,
      "region": "Asia-Pacific"
    },
    // … 12 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "cost",
      "type": "quantitative",
      "axis": {
        "title": "Cost of living index"
      },
      "scale": {
        "domain": [
          35,
          140
        ]
      }
    },
    "y": {
      "field": "quality",
      "type": "quantitative",
      "axis": {
        "title": "Quality of living index"
      },
      "scale": {
        "domain": [
          68,
          100
        ]
      }
    },
    "color": {
      "field": "region",
      "type": "nominal"
    }
  },
  "chrome": {
    "title": "The fitted line covers every region at once",
    "subtitle": "Cost of living vs. quality of living in global cities, 2024"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 18 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "cost",
      "type": "quantitative",
      "axis": {
        "title": "Cost of living index"
      },
      "scale": {
        "domain": [
          35,
          140
        ]
      }
    },
    "y": {
      "field": "quality",
      "type": "quantitative",
      "axis": {
        "title": "Quality of living index"
      },
      "scale": {
        "domain": [
          68,
          100
        ]
      }
    },
    "color": {
      "field": "region",
      "type": "nominal"
    }
  },
  "chrome": {
    "title": "The fitted line covers every region at once",
    "subtitle": "Cost of living vs. quality of living in global cities, 2024"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 18 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "cost",
      "type": "quantitative",
      "axis": {
        "title": "Cost of living index"
      },
      "scale": {
        "domain": [
          35,
          140
        ]
      }
    },
    "y": {
      "field": "quality",
      "type": "quantitative",
      "axis": {
        "title": "Quality of living index"
      },
      "scale": {
        "domain": [
          68,
          100
        ]
      }
    },
    "color": {
      "field": "region",
      "type": "nominal"
    }
  },
  "chrome": {
    "title": "The fitted line covers every region at once",
    "subtitle": "Cost of living vs. quality of living in global cities, 2024"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 18 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "cost",
      "type": "quantitative",
      "axis": {
        "title": "Cost of living index"
      },
      "scale": {
        "domain": [
          35,
          140
        ]
      }
    },
    "y": {
      "field": "quality",
      "type": "quantitative",
      "axis": {
        "title": "Quality of living index"
      },
      "scale": {
        "domain": [
          68,
          100
        ]
      }
    },
    "color": {
      "field": "region",
      "type": "nominal"
    }
  },
  "chrome": {
    "title": "The fitted line covers every region at once",
    "subtitle": "Cost of living vs. quality of living in global cities, 2024"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 18 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "cost",
      "type": "quantitative",
      "axis": {
        "title": "Cost of living index"
      },
      "scale": {
        "domain": [
          35,
          140
        ]
      }
    },
    "y": {
      "field": "quality",
      "type": "quantitative",
      "axis": {
        "title": "Quality of living index"
      },
      "scale": {
        "domain": [
          68,
          100
        ]
      }
    },
    "color": {
      "field": "region",
      "type": "nominal"
    }
  },
  "chrome": {
    "title": "The fitted line covers every region at once",
    "subtitle": "Cost of living vs. quality of living in global cities, 2024"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Fields that matter

Field What it does
mark.trendline true by default on a point mark. false removes the line.
mark.trendline.layer "below" (default) draws the line behind the dots, "above" draws it over them.
mark.trendline.stroke The line color, any CSS color. The theme sets the default.
mark.trendline.strokeWidth The line width in pixels. Defaults to 1.5.
mark.size, mark.opacity The dot radius and opacity, used to keep a dense cloud readable.
encoding.x, encoding.y The two quantitative fields the line is fitted through.
  • Scatter: the chart the line belongs to.
  • Bubble: the same line over sized dots.
  • Layers: combine marks on shared scales.
  • Line: a line drawn from the data itself.
  • Error bars: show an interval around an estimate.
  • LayerSpec reference: the layer fields and what a layer inherits.