Skip to content
OpenChart

Range (dumbbell)

Show the span between two values per category as a dumbbell, an arrow or a floating bar, for change between two periods or a low to high range.

A range chart draws one row per category with a start and an end value. The reader sees where each category began, where it ended and how big the move was. It answers “who moved, and by how much?” for one pair of periods, or shows a low to high span such as a monthly temperature range.

For a trajectory over many periods, use a line chart or a slope chart. For one value per category, use a bar chart.

A dumbbell

Set mark to "range" with the start value on x and the end value on x2. Each row has a dot at both values. title on each channel names the endpoint, and the sort object on the category channel orders the rows by one of the fields.

50607080Life expectancy at birth (years)NigeriaIndiaRussiaBrazilChinaUSASouth KoreaJapanLife expectancy rose in all eight countriesfrom 2000 to 2023Life expectancy at birth, 2000 (gray) vs. 2023 (accent), selected countries50607080Life expectancy at birth (years)NigeriaIndiaRussiaBrazilChinaUSASouth KoreaJapanLife expectancy rose in all eight countriesfrom 2000 to 2023Life expectancy at birth, 2000 (gray) vs. 2023 (accent), selected countries
Life expectancy, 2000 against 2023. Source: UN World Population Prospects 2024.
{
  "mark": "range",
  "data": [
    {
      "country": "Japan",
      "y2000": 81.1,
      "y2023": 84.7
    },
    {
      "country": "South Korea",
      "y2000": 76,
      "y2023": 84.3
    },
    {
      "country": "USA",
      "y2000": 76.7,
      "y2023": 79.3
    },
    {
      "country": "China",
      "y2000": 71.6,
      "y2023": 78.6
    },
    {
      "country": "Brazil",
      "y2000": 70.1,
      "y2023": 75.8
    },
    {
      "country": "Russia",
      "y2000": 65.5,
      "y2023": 73
    },
    {
      "country": "India",
      "y2000": 62.7,
      "y2023": 72
    },
    {
      "country": "Nigeria",
      "y2000": 46.5,
      "y2023": 54.6
    }
  ],
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": {
        "field": "y2023",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2000",
      "type": "quantitative",
      "title": "2000",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "x2": {
      "field": "y2023",
      "type": "quantitative",
      "title": "2023"
    }
  },
  "chrome": {
    "title": "Life expectancy rose in all eight countries from 2000 to 2023",
    "subtitle": "Life expectancy at birth, 2000 (gray) vs. 2023 (accent), selected countries"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "range",
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": {
        "field": "y2023",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2000",
      "type": "quantitative",
      "title": "2000",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "x2": {
      "field": "y2023",
      "type": "quantitative",
      "title": "2023"
    }
  },
  "chrome": {
    "title": "Life expectancy rose in all eight countries from 2000 to 2023",
    "subtitle": "Life expectancy at birth, 2000 (gray) vs. 2023 (accent), selected countries"
  }
};

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 = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "range",
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": {
        "field": "y2023",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2000",
      "type": "quantitative",
      "title": "2000",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "x2": {
      "field": "y2023",
      "type": "quantitative",
      "title": "2023"
    }
  },
  "chrome": {
    "title": "Life expectancy rose in all eight countries from 2000 to 2023",
    "subtitle": "Life expectancy at birth, 2000 (gray) vs. 2023 (accent), selected countries"
  }
};
</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 = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "range",
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": {
        "field": "y2023",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2000",
      "type": "quantitative",
      "title": "2000",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "x2": {
      "field": "y2023",
      "type": "quantitative",
      "title": "2023"
    }
  },
  "chrome": {
    "title": "Life expectancy rose in all eight countries from 2000 to 2023",
    "subtitle": "Life expectancy at birth, 2000 (gray) vs. 2023 (accent), selected countries"
  }
};
</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 = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "range",
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": {
        "field": "y2023",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2000",
      "type": "quantitative",
      "title": "2000",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "x2": {
      "field": "y2023",
      "type": "quantitative",
      "title": "2023"
    }
  },
  "chrome": {
    "title": "Life expectancy rose in all eight countries from 2000 to 2023",
    "subtitle": "Life expectancy at birth, 2000 (gray) vs. 2023 (accent), selected countries"
  }
};

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

An arrow plot

style: "arrow" puts an arrowhead at the end value, so direction reads at a glance. colorByDirection: true colors each arrow by whether its value grew or shrank. The data holds one row per source with a value in 2010 and a value in 2024.

010203040Share of US electricity generation (%)HydroSolarWindCoalNuclearNatural gasCoal's share fell from 45% to 15% as gas, windand solar grewShare of US electricity generation by source, 2010 to 2024. Green arrows grew, red shrank.010203040Share of US electricity generation (%)HydroSolarWindCoalNuclearNatural gasCoal's share fell from 45% to 15% as gas, windand solar grewShare of US electricity generation by source, 2010 to 2024. Green arrows grew, red shrank.
US electricity generation shares, 2010 to 2024. Source: US Energy Information Administration, Electric Power Monthly.
{
  "mark": {
    "type": "range",
    "style": "arrow",
    "colorByDirection": true
  },
  "data": [
    {
      "source": "Coal",
      "y2010": 44.8,
      "y2024": 15
    },
    {
      "source": "Natural gas",
      "y2010": 23.9,
      "y2024": 43.2
    },
    {
      "source": "Nuclear",
      "y2010": 19.6,
      "y2024": 18.2
    },
    {
      "source": "Wind",
      "y2010": 2.3,
      "y2024": 10.3
    },
    {
      "source": "Hydro",
      "y2010": 6.3,
      "y2024": 5.6
    },
    {
      "source": "Solar",
      "y2010": 0.1,
      "y2024": 6.9
    }
  ],
  "encoding": {
    "y": {
      "field": "source",
      "type": "nominal",
      "sort": {
        "field": "y2024",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2010",
      "type": "quantitative",
      "title": "2010",
      "axis": {
        "title": "Share of US electricity generation (%)"
      }
    },
    "x2": {
      "field": "y2024",
      "type": "quantitative",
      "title": "2024"
    }
  },
  "chrome": {
    "title": "Coal's share fell from 45% to 15% as gas, wind and solar grew",
    "subtitle": "Share of US electricity generation by source, 2010 to 2024. Green arrows grew, red shrank."
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 6 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "arrow",
    "colorByDirection": true
  },
  "encoding": {
    "y": {
      "field": "source",
      "type": "nominal",
      "sort": {
        "field": "y2024",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2010",
      "type": "quantitative",
      "title": "2010",
      "axis": {
        "title": "Share of US electricity generation (%)"
      }
    },
    "x2": {
      "field": "y2024",
      "type": "quantitative",
      "title": "2024"
    }
  },
  "chrome": {
    "title": "Coal's share fell from 45% to 15% as gas, wind and solar grew",
    "subtitle": "Share of US electricity generation by source, 2010 to 2024. Green arrows grew, red shrank."
  }
};

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 = [/* 6 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "arrow",
    "colorByDirection": true
  },
  "encoding": {
    "y": {
      "field": "source",
      "type": "nominal",
      "sort": {
        "field": "y2024",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2010",
      "type": "quantitative",
      "title": "2010",
      "axis": {
        "title": "Share of US electricity generation (%)"
      }
    },
    "x2": {
      "field": "y2024",
      "type": "quantitative",
      "title": "2024"
    }
  },
  "chrome": {
    "title": "Coal's share fell from 45% to 15% as gas, wind and solar grew",
    "subtitle": "Share of US electricity generation by source, 2010 to 2024. Green arrows grew, red shrank."
  }
};
</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 = [/* 6 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "arrow",
    "colorByDirection": true
  },
  "encoding": {
    "y": {
      "field": "source",
      "type": "nominal",
      "sort": {
        "field": "y2024",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2010",
      "type": "quantitative",
      "title": "2010",
      "axis": {
        "title": "Share of US electricity generation (%)"
      }
    },
    "x2": {
      "field": "y2024",
      "type": "quantitative",
      "title": "2024"
    }
  },
  "chrome": {
    "title": "Coal's share fell from 45% to 15% as gas, wind and solar grew",
    "subtitle": "Share of US electricity generation by source, 2010 to 2024. Green arrows grew, red shrank."
  }
};
</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 = [/* 6 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "arrow",
    "colorByDirection": true
  },
  "encoding": {
    "y": {
      "field": "source",
      "type": "nominal",
      "sort": {
        "field": "y2024",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2010",
      "type": "quantitative",
      "title": "2010",
      "axis": {
        "title": "Share of US electricity generation (%)"
      }
    },
    "x2": {
      "field": "y2024",
      "type": "quantitative",
      "title": "2024"
    }
  },
  "chrome": {
    "title": "Coal's share fell from 45% to 15% as gas, wind and solar grew",
    "subtitle": "Share of US electricity generation by source, 2010 to 2024. Green arrows grew, red shrank."
  }
};

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

A floating bar

style: "bar" draws a plain bar from the start to the end value. Swap the channels to run it vertically: the category on x, the low value on y and the high value on y2 give a column for each month.

JanFebMarAprMayJunJulAugSepOctNovDec051015202530Temperature (°C)New York's monthly averages run from about 1°Cto 30°CAverage daily low to high temperature by month, Central Park, 2023JanFebMarAprMayJunJulAugSepOctNovDec051015202530Temperature (°C)New York's monthly averages run from about 1°Cto 30°CAverage daily low to high temperature by month, Central Park, 2023
Monthly temperature range in New York. Source: NOAA GHCN Daily, Central Park (USW00094728).
{
  "mark": {
    "type": "range",
    "style": "bar",
    "fill": "#0e7490"
  },
  "data": [
    {
      "month": "Jan",
      "low": 3.5,
      "high": 9.3
    },
    {
      "month": "Feb",
      "low": 0.9,
      "high": 9.2
    },
    {
      "month": "Mar",
      "low": 3.2,
      "high": 10.8
    },
    {
      "month": "Apr",
      "low": 9.4,
      "high": 19
    },
    {
      "month": "May",
      "low": 12,
      "high": 22.1
    },
    {
      "month": "Jun",
      "low": 16.8,
      "high": 25.4
    },
    {
      "month": "Jul",
      "low": 22.1,
      "high": 30.2
    },
    {
      "month": "Aug",
      "low": 20.2,
      "high": 27.6
    },
    {
      "month": "Sep",
      "low": 17.3,
      "high": 24.3
    },
    {
      "month": "Oct",
      "low": 12.4,
      "high": 19.3
    },
    {
      "month": "Nov",
      "low": 4.5,
      "high": 11.8
    },
    {
      "month": "Dec",
      "low": 4.2,
      "high": 9.8
    }
  ],
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "low",
      "type": "quantitative",
      "title": "Avg low",
      "axis": {
        "title": "Temperature (°C)"
      }
    },
    "y2": {
      "field": "high",
      "type": "quantitative",
      "title": "Avg high"
    }
  },
  "chrome": {
    "title": "New York's monthly averages run from about 1°C to 30°C",
    "subtitle": "Average daily low to high temperature by month, Central Park, 2023"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "low",
      "type": "quantitative",
      "title": "Avg low",
      "axis": {
        "title": "Temperature (°C)"
      }
    },
    "y2": {
      "field": "high",
      "type": "quantitative",
      "title": "Avg high"
    }
  },
  "chrome": {
    "title": "New York's monthly averages run from about 1°C to 30°C",
    "subtitle": "Average daily low to high temperature by month, Central Park, 2023"
  }
};

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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "low",
      "type": "quantitative",
      "title": "Avg low",
      "axis": {
        "title": "Temperature (°C)"
      }
    },
    "y2": {
      "field": "high",
      "type": "quantitative",
      "title": "Avg high"
    }
  },
  "chrome": {
    "title": "New York's monthly averages run from about 1°C to 30°C",
    "subtitle": "Average daily low to high temperature by month, Central Park, 2023"
  }
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "low",
      "type": "quantitative",
      "title": "Avg low",
      "axis": {
        "title": "Temperature (°C)"
      }
    },
    "y2": {
      "field": "high",
      "type": "quantitative",
      "title": "Avg high"
    }
  },
  "chrome": {
    "title": "New York's monthly averages run from about 1°C to 30°C",
    "subtitle": "Average daily low to high temperature by month, Central Park, 2023"
  }
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "low",
      "type": "quantitative",
      "title": "Avg low",
      "axis": {
        "title": "Temperature (°C)"
      }
    },
    "y2": {
      "field": "high",
      "type": "quantitative",
      "title": "Avg high"
    }
  },
  "chrome": {
    "title": "New York's monthly averages run from about 1°C to 30°C",
    "subtitle": "Average daily low to high temperature by month, Central Park, 2023"
  }
};

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

A horizontal floating bar

The same style with x and x2 gives horizontal bars. Use it when category names are long or when you want the bar form with the same data as the dumbbell. fill sets the bar color.

50607080Life expectancy at birth (years)NigeriaIndiaRussiaBrazilChinaUSASouth KoreaJapanEach bar spans a country's gain since 2000Life expectancy at birth, 2000 to 2023, selected countries50607080Life expectancy at birth (years)NigeriaIndiaRussiaBrazilChinaUSASouth KoreaJapanEach bar spans a country's gain since 2000Life expectancy at birth, 2000 to 2023, selected countries
Life expectancy spans as floating bars. Source: UN World Population Prospects 2024.
{
  "mark": {
    "type": "range",
    "style": "bar",
    "fill": "#0e7490"
  },
  "data": [
    {
      "country": "Japan",
      "y2000": 81.1,
      "y2023": 84.7
    },
    {
      "country": "South Korea",
      "y2000": 76,
      "y2023": 84.3
    },
    {
      "country": "USA",
      "y2000": 76.7,
      "y2023": 79.3
    },
    {
      "country": "China",
      "y2000": 71.6,
      "y2023": 78.6
    },
    {
      "country": "Brazil",
      "y2000": 70.1,
      "y2023": 75.8
    },
    {
      "country": "Russia",
      "y2000": 65.5,
      "y2023": 73
    },
    {
      "country": "India",
      "y2000": 62.7,
      "y2023": 72
    },
    {
      "country": "Nigeria",
      "y2000": 46.5,
      "y2023": 54.6
    }
  ],
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": {
        "field": "y2023",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2000",
      "type": "quantitative",
      "title": "2000",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "x2": {
      "field": "y2023",
      "type": "quantitative",
      "title": "2023"
    }
  },
  "chrome": {
    "title": "Each bar spans a country's gain since 2000",
    "subtitle": "Life expectancy at birth, 2000 to 2023, selected countries"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": {
        "field": "y2023",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2000",
      "type": "quantitative",
      "title": "2000",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "x2": {
      "field": "y2023",
      "type": "quantitative",
      "title": "2023"
    }
  },
  "chrome": {
    "title": "Each bar spans a country's gain since 2000",
    "subtitle": "Life expectancy at birth, 2000 to 2023, selected countries"
  }
};

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 = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": {
        "field": "y2023",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2000",
      "type": "quantitative",
      "title": "2000",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "x2": {
      "field": "y2023",
      "type": "quantitative",
      "title": "2023"
    }
  },
  "chrome": {
    "title": "Each bar spans a country's gain since 2000",
    "subtitle": "Life expectancy at birth, 2000 to 2023, selected countries"
  }
};
</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 = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": {
        "field": "y2023",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2000",
      "type": "quantitative",
      "title": "2000",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "x2": {
      "field": "y2023",
      "type": "quantitative",
      "title": "2023"
    }
  },
  "chrome": {
    "title": "Each bar spans a country's gain since 2000",
    "subtitle": "Life expectancy at birth, 2000 to 2023, selected countries"
  }
};
</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 = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "y": {
      "field": "country",
      "type": "nominal",
      "sort": {
        "field": "y2023",
        "order": "ascending"
      }
    },
    "x": {
      "field": "y2000",
      "type": "quantitative",
      "title": "2000",
      "axis": {
        "title": "Life expectancy at birth (years)"
      }
    },
    "x2": {
      "field": "y2023",
      "type": "quantitative",
      "title": "2023"
    }
  },
  "chrome": {
    "title": "Each bar spans a country's gain since 2000",
    "subtitle": "Life expectancy at birth, 2000 to 2023, selected countries"
  }
};

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 "range", or an object with type: "range" and a style of "dumbbell", "arrow" or "bar".
encoding.x, encoding.x2 The start and end values of a horizontal range.
encoding.y, encoding.y2 The start and end values of a vertical range, with the category on x.
encoding.*.title Names an endpoint, such as “2000” and “2023”.
encoding.y.sort Orders the categories by another field, with order set to "ascending" or "descending".
colorByDirection On the mark, colors each range by whether it rose or fell.
fill On the mark, sets the bar color.
  • Slope and bump for the same before and after question across many series.
  • Lollipop for one value per category, and for dumbbells built from long-format data.
  • Error bars for an interval around an estimate.
  • Marks reference lists the channels and options each mark type takes.