# Line

> Trace one or more series across a continuous axis, then annotate, label the line ends, focus a series, change the curve, format dates and switch to a log scale.

A line chart shows how a value changes along a continuous axis, usually time. The reader follows the slope: a steep stretch is fast change, a flat one is stability. Several series can share one plot when each has its own color.

Use [columns](/docs/comparison/column/) when you have a handful of separate periods and the reader should compare each value. Use an [area chart](/docs/change-over-time/area/) when the size of the total matters more than the slope, and a [slope chart](/docs/change-over-time/slope-bump/) when only two moments matter.

## A basic line chart

Set `mark` to `"line"`, put the date on `x` with `type: "temporal"` and the number on `y`. Here a `text` annotation with a connector marks the peak, and a dashed `refline` marks a target.

```json
{
  "mark": {
    "type": "line",
    "stroke": "#0e7490",
    "strokeWidth": 2
  },
  "data": [
    {
      "date": "2019-01-01",
      "rate": 1.6
    },
    {
      "date": "2019-04-01",
      "rate": 2
    },
    {
      "date": "2019-07-01",
      "rate": 1.8
    },
    {
      "date": "2019-10-01",
      "rate": 1.8
    },
    {
      "date": "2020-01-01",
      "rate": 2.5
    },
    {
      "date": "2020-04-01",
      "rate": 0.3
    },
    {
      "date": "2020-07-01",
      "rate": 1
    },
    {
      "date": "2020-10-01",
      "rate": 1.2
    },
    {
      "date": "2021-01-01",
      "rate": 1.4
    },
    {
      "date": "2021-04-01",
      "rate": 4.2
    },
    {
      "date": "2021-07-01",
      "rate": 5.4
    },
    {
      "date": "2021-10-01",
      "rate": 6.2
    },
    {
      "date": "2022-01-01",
      "rate": 7.5
    },
    {
      "date": "2022-04-01",
      "rate": 8.3
    },
    {
      "date": "2022-07-01",
      "rate": 8.5
    },
    {
      "date": "2022-10-01",
      "rate": 7.7
    },
    {
      "date": "2023-01-01",
      "rate": 6.4
    },
    {
      "date": "2023-04-01",
      "rate": 4.9
    },
    {
      "date": "2023-07-01",
      "rate": 3.2
    },
    {
      "date": "2023-10-01",
      "rate": 3.2
    },
    {
      "date": "2024-01-01",
      "rate": 3.1
    },
    {
      "date": "2024-04-01",
      "rate": 3.4
    },
    {
      "date": "2024-07-01",
      "rate": 2.9
    },
    {
      "date": "2024-10-01",
      "rate": 2.6
    }
  ],
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "axis": {
        "tickCount": 6
      }
    },
    "y": {
      "field": "rate",
      "type": "quantitative",
      "axis": {
        "title": "CPI (year-over-year %)",
        "format": ".1f",
        "grid": true
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": "2022-07-01",
      "y": 8.5,
      "text": "Peak: 8.5%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -20
      },
      "connector": true
    },
    {
      "type": "refline",
      "y": 2,
      "label": "Fed 2% target",
      "style": "dashed",
      "stroke": "#64748b",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "US inflation peaked at 8.5% in 2022 and fell below 3% in mid-2024",
    "subtitle": "US consumer price index, year-over-year % change, quarterly 2019-2024"
  }
}
```

## Several series

Add a `color` channel to draw one line per value of a field. With a handful of series, `endpointLabels` names each line at its right end, so the reader does not have to match colors to a legend. A `range` annotation shades a stretch of the x axis, and a `refline` at `y: 0` marks the baseline.

```json
{
  "mark": "line",
  "data": [
    {
      "date": "2018-01-01",
      "gdp": 3,
      "country": "United States"
    },
    {
      "date": "2019-01-01",
      "gdp": 2.5,
      "country": "United States"
    },
    {
      "date": "2020-01-01",
      "gdp": -2.2,
      "country": "United States"
    },
    {
      "date": "2021-01-01",
      "gdp": 6.1,
      "country": "United States"
    },
    {
      "date": "2022-01-01",
      "gdp": 2.5,
      "country": "United States"
    },
    {
      "date": "2023-01-01",
      "gdp": 2.9,
      "country": "United States"
    },
    {
      "date": "2024-01-01",
      "gdp": 2.8,
      "country": "United States"
    },
    {
      "date": "2018-01-01",
      "gdp": 1.8,
      "country": "Euro Area"
    },
    {
      "date": "2019-01-01",
      "gdp": 1.6,
      "country": "Euro Area"
    },
    {
      "date": "2020-01-01",
      "gdp": -6.1,
      "country": "Euro Area"
    },
    {
      "date": "2021-01-01",
      "gdp": 5.9,
      "country": "Euro Area"
    },
    {
      "date": "2022-01-01",
      "gdp": 3.4,
      "country": "Euro Area"
    },
    {
      "date": "2023-01-01",
      "gdp": 0.5,
      "country": "Euro Area"
    },
    {
      "date": "2024-01-01",
      "gdp": 0.8,
      "country": "Euro Area"
    },
    {
      "date": "2018-01-01",
      "gdp": 6.7,
      "country": "China"
    },
    {
      "date": "2019-01-01",
      "gdp": 6,
      "country": "China"
    },
    {
      "date": "2020-01-01",
      "gdp": 2.2,
      "country": "China"
    },
    {
      "date": "2021-01-01",
      "gdp": 8.4,
      "country": "China"
    },
    {
      "date": "2022-01-01",
      "gdp": 3,
      "country": "China"
    },
    {
      "date": "2023-01-01",
      "gdp": 5.2,
      "country": "China"
    },
    {
      "date": "2024-01-01",
      "gdp": 5,
      "country": "China"
    }
  ],
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "axis": {
        "tickCount": 4
      }
    },
    "y": {
      "field": "gdp",
      "type": "quantitative",
      "axis": {
        "title": "GDP growth (%)",
        "format": "+.0f",
        "grid": true
      }
    },
    "color": {
      "field": "country",
      "type": "nominal"
    }
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "stroke": "#94a3b8",
      "strokeWidth": 1
    },
    {
      "type": "range",
      "x1": "2019-09-01",
      "x2": "2020-06-01",
      "label": "Pandemic",
      "fill": "#dc2626",
      "opacity": 0.06
    }
  ],
  "endpointLabels": {
    "format": "+.1f"
  },
  "chrome": {
    "title": "All three economies slowed or shrank in 2020 and rebounded in 2021",
    "subtitle": "Annual real GDP growth rate, 2018-2024"
  }
}
```

## Focusing one series

To focus one series, put an explicit `scale` on the color channel that maps that company to the accent and the others to grey. `seriesStyles` thickens the focus line, and `legend: { show: false }` drops the legend because `endpointLabels` with `content: "label"` already names every line.

```json
{
  "mark": "line",
  "data": [
    {
      "year": "2019-01-01",
      "revenue": 280,
      "company": "Amazon"
    },
    {
      "year": "2020-01-01",
      "revenue": 386,
      "company": "Amazon"
    },
    {
      "year": "2021-01-01",
      "revenue": 470,
      "company": "Amazon"
    },
    {
      "year": "2022-01-01",
      "revenue": 514,
      "company": "Amazon"
    },
    {
      "year": "2023-01-01",
      "revenue": 574,
      "company": "Amazon"
    },
    {
      "year": "2024-01-01",
      "revenue": 638,
      "company": "Amazon"
    },
    {
      "year": "2019-01-01",
      "revenue": 268,
      "company": "Apple"
    },
    {
      "year": "2020-01-01",
      "revenue": 296,
      "company": "Apple"
    },
    {
      "year": "2021-01-01",
      "revenue": 378,
      "company": "Apple"
    },
    {
      "year": "2022-01-01",
      "revenue": 387,
      "company": "Apple"
    },
    {
      "year": "2023-01-01",
      "revenue": 387,
      "company": "Apple"
    },
    {
      "year": "2024-01-01",
      "revenue": 396,
      "company": "Apple"
    },
    {
      "year": "2019-01-01",
      "revenue": 161,
      "company": "Alphabet"
    },
    {
      "year": "2020-01-01",
      "revenue": 182,
      "company": "Alphabet"
    },
    {
      "year": "2021-01-01",
      "revenue": 257,
      "company": "Alphabet"
    },
    {
      "year": "2022-01-01",
      "revenue": 283,
      "company": "Alphabet"
    },
    {
      "year": "2023-01-01",
      "revenue": 307,
      "company": "Alphabet"
    },
    {
      "year": "2024-01-01",
      "revenue": 350,
      "company": "Alphabet"
    },
    {
      "year": "2019-01-01",
      "revenue": 135,
      "company": "Microsoft"
    },
    {
      "year": "2020-01-01",
      "revenue": 153,
      "company": "Microsoft"
    },
    {
      "year": "2021-01-01",
      "revenue": 185,
      "company": "Microsoft"
    },
    {
      "year": "2022-01-01",
      "revenue": 204,
      "company": "Microsoft"
    },
    {
      "year": "2023-01-01",
      "revenue": 228,
      "company": "Microsoft"
    },
    {
      "year": "2024-01-01",
      "revenue": 262,
      "company": "Microsoft"
    },
    {
      "year": "2019-01-01",
      "revenue": 71,
      "company": "Meta"
    },
    {
      "year": "2020-01-01",
      "revenue": 86,
      "company": "Meta"
    },
    {
      "year": "2021-01-01",
      "revenue": 118,
      "company": "Meta"
    },
    {
      "year": "2022-01-01",
      "revenue": 117,
      "company": "Meta"
    },
    {
      "year": "2023-01-01",
      "revenue": 135,
      "company": "Meta"
    },
    {
      "year": "2024-01-01",
      "revenue": 164,
      "company": "Meta"
    }
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "tickCount": 6
      }
    },
    "y": {
      "field": "revenue",
      "type": "quantitative",
      "axis": {
        "title": "Annual revenue ($B)",
        "format": ",.0f",
        "grid": true
      }
    },
    "color": {
      "field": "company",
      "type": "nominal",
      "scale": {
        "domain": [
          "Amazon",
          "Apple",
          "Alphabet",
          "Microsoft",
          "Meta"
        ],
        "range": [
          "#0e7490",
          "#7a858f",
          "#7a858f",
          "#7a858f",
          "#7a858f"
        ]
      }
    }
  },
  "legend": {
    "show": false
  },
  "endpointLabels": {
    "content": "label",
    "showMarker": true
  },
  "seriesStyles": {
    "Amazon": {
      "strokeWidth": 3
    }
  },
  "chrome": {
    "title": "Amazon's revenue lead over the rest of big tech widened every year",
    "subtitle": "Annual revenue by company, 2019-2024 (billions USD)"
  }
}
```

## Step lines

`interpolate` on the mark sets the curve between points. The default, `"monotone"`, draws a smooth curve through every point that never overshoots between them. `"linear"` draws straight segments. `"step-after"` holds each value until the next point, which suits a quantity that changes at discrete moments; `"step"` switches halfway between points. `"natural"` draws a cubic spline through every point, which can overshoot. `point: true` draws a dot at every observation.

```json
{
  "mark": {
    "type": "line",
    "interpolate": "step-after",
    "point": true,
    "stroke": "#0e7490",
    "strokeWidth": 2
  },
  "data": [
    {
      "month": "Jan",
      "temp": 2.1
    },
    {
      "month": "Feb",
      "temp": 3.5
    },
    {
      "month": "Mar",
      "temp": 7.8
    },
    {
      "month": "Apr",
      "temp": 12.4
    },
    {
      "month": "May",
      "temp": 17.2
    },
    {
      "month": "Jun",
      "temp": 21
    },
    {
      "month": "Jul",
      "temp": 23.5
    },
    {
      "month": "Aug",
      "temp": 22.8
    },
    {
      "month": "Sep",
      "temp": 18.6
    },
    {
      "month": "Oct",
      "temp": 12.9
    },
    {
      "month": "Nov",
      "temp": 7.1
    },
    {
      "month": "Dec",
      "temp": 3.2
    }
  ],
  "encoding": {
    "x": {
      "field": "month",
      "type": "ordinal"
    },
    "y": {
      "field": "temp",
      "type": "quantitative",
      "axis": {
        "title": "Temperature (°C)",
        "grid": true,
        "tickCount": 5
      }
    }
  },
  "chrome": {
    "title": "A step-after line holds each value until the next point",
    "subtitle": "Monthly temperature with interpolate: \"step-after\""
  }
}
```

## Date ticks

`axis.format` on a temporal channel takes a d3-time-format string, here `"%b '%y"` for ticks like "Jan '19", and `tickCount` suggests how many ticks to draw. The y axis uses a d3-format string: the data is stored as fractions, so `".0%"` prints 8% for 0.08.

```json
{
  "mark": {
    "type": "line",
    "stroke": "#0e7490",
    "strokeWidth": 2
  },
  "data": [
    {
      "date": "2019-01-01",
      "rate": 0.016
    },
    {
      "date": "2019-04-01",
      "rate": 0.02
    },
    {
      "date": "2019-07-01",
      "rate": 0.018000000000000002
    },
    {
      "date": "2019-10-01",
      "rate": 0.018000000000000002
    },
    {
      "date": "2020-01-01",
      "rate": 0.025
    },
    {
      "date": "2020-04-01",
      "rate": 0.003
    },
    {
      "date": "2020-07-01",
      "rate": 0.01
    },
    {
      "date": "2020-10-01",
      "rate": 0.012
    },
    {
      "date": "2021-01-01",
      "rate": 0.013999999999999999
    },
    {
      "date": "2021-04-01",
      "rate": 0.042
    },
    {
      "date": "2021-07-01",
      "rate": 0.054000000000000006
    },
    {
      "date": "2021-10-01",
      "rate": 0.062
    },
    {
      "date": "2022-01-01",
      "rate": 0.075
    },
    {
      "date": "2022-04-01",
      "rate": 0.083
    },
    {
      "date": "2022-07-01",
      "rate": 0.085
    },
    {
      "date": "2022-10-01",
      "rate": 0.077
    },
    {
      "date": "2023-01-01",
      "rate": 0.064
    },
    {
      "date": "2023-04-01",
      "rate": 0.049
    },
    {
      "date": "2023-07-01",
      "rate": 0.032
    },
    {
      "date": "2023-10-01",
      "rate": 0.032
    },
    {
      "date": "2024-01-01",
      "rate": 0.031
    },
    {
      "date": "2024-04-01",
      "rate": 0.034
    },
    {
      "date": "2024-07-01",
      "rate": 0.028999999999999998
    },
    {
      "date": "2024-10-01",
      "rate": 0.026000000000000002
    }
  ],
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "axis": {
        "format": "%b '%y",
        "tickCount": 8
      }
    },
    "y": {
      "field": "rate",
      "type": "quantitative",
      "axis": {
        "title": "CPI (year-over-year)",
        "format": ".0%",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "A custom tick format keeps a dense time axis readable",
    "subtitle": "Quarterly 2019-2024, ticks formatted as month and two-digit year"
  }
}
```

## Log scale

`scale: { type: "log" }` on the y channel spaces values by ratio. A series that grows by a similar percentage each year plots as a near-straight line, so a change in the growth rate stands out where a linear axis would bury it.

```json
{
  "mark": {
    "type": "line",
    "stroke": "#0e7490",
    "strokeWidth": 2,
    "point": true
  },
  "data": [
    {
      "year": "2015-01-01",
      "fleet": 1.3
    },
    {
      "year": "2016-01-01",
      "fleet": 2.1
    },
    {
      "year": "2017-01-01",
      "fleet": 3.2
    },
    {
      "year": "2018-01-01",
      "fleet": 5.4
    },
    {
      "year": "2019-01-01",
      "fleet": 7.5
    },
    {
      "year": "2020-01-01",
      "fleet": 10.5
    },
    {
      "year": "2021-01-01",
      "fleet": 17.1
    },
    {
      "year": "2022-01-01",
      "fleet": 27
    },
    {
      "year": "2023-01-01",
      "fleet": 41
    },
    {
      "year": "2024-01-01",
      "fleet": 58
    }
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "tickCount": 5
      }
    },
    "y": {
      "field": "fleet",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "Cumulative EV fleet (millions, log)",
        "format": ",.0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "On a log scale, steady growth plots as a straight line",
    "subtitle": "Cumulative global electric car fleet, 2015-2024 (BEV + PHEV)"
  }
}
```

## Fields that matter

| Field | What it does |
| --- | --- |
| `mark` | `"line"`, or an object such as `{ type: "line", interpolate: "step-after", point: true }` to set the curve, point markers and stroke. |
| `encoding.x` | The horizontal position. Use `type: "temporal"` for dates and `"ordinal"` for named periods. |
| `encoding.y` | The value. `scale.type: "log"` switches to a log axis. |
| `encoding.color` | One line per value of this field. `scale.domain` and `scale.range` pin colors to series. |
| `endpointLabels` | Names each line at its end. `content` picks the label text. |
| `seriesStyles` | Per-series overrides, keyed by the series name, such as a thicker stroke. |
| `annotations` | `text`, `refline` and `range` marks placed in data coordinates. |
| `axis.format` | A d3-format or d3-time-format string for tick labels. |

## Related

- [Area](/docs/change-over-time/area/) fills under the line.
- [Slope and bump](/docs/change-over-time/slope-bump/) compare two moments or rank over time.
- [Sparkline](/docs/change-over-time/sparkline/) shrinks a line to fit beside a number.
- [Column](/docs/comparison/column/) compares values across a few periods.
- [Marks reference](/docs/spec/marks/) lists the channels and options each mark type takes.
