Skip to content
OpenChart

Color palettes

Categorical, sequential and diverging color, highlighting one series, and colors that survive print and color blindness.

The palette depends on what the colored field holds. A nominal field, such as the six power sources here, gets the categorical palette: one distinct hue per value, assigned in the order of the color domain.

20162017201820192020202120222023020406080100Share of generation (%)CoalGasNuclearHydroWindSolarCoal's share of US electricity fell by almosthalf from 2016 to 2023 as gas, wind and solargrewShare of US electricity from the six main sources, %, 2016–202320162017201820192020202120222023020406080100Share of generation (%)CoalGasNuclearHydroWindSolarCoal's share of US electricity fell by almosthalf from 2016 to 2023 as gas, wind and solargrewShare of US electricity from the six main sources, %, 2016–2023
US electricity mix by source, stacked. Source: Our World in Data (owid/electricity-mix).
{
  "mark": "bar",
  "data": [
    {
      "year": "2016",
      "source": "Coal",
      "share": 31.2
    },
    {
      "year": "2016",
      "source": "Gas",
      "share": 34.8
    },
    {
      "year": "2016",
      "source": "Nuclear",
      "share": 20.3
    },
    {
      "year": "2016",
      "source": "Hydro",
      "share": 6.6
    },
    {
      "year": "2016",
      "source": "Wind",
      "share": 5.7
    },
    {
      "year": "2016",
      "source": "Solar",
      "share": 1.4
    },
    // … 42 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of generation (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal",
      "title": "Source",
      "sort": [
        "Coal",
        "Gas",
        "Nuclear",
        "Hydro",
        "Wind",
        "Solar"
      ]
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas, wind and solar grew",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of generation (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal",
      "title": "Source",
      "sort": [
        "Coal",
        "Gas",
        "Nuclear",
        "Hydro",
        "Wind",
        "Solar"
      ]
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas, wind and solar grew",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of generation (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal",
      "title": "Source",
      "sort": [
        "Coal",
        "Gas",
        "Nuclear",
        "Hydro",
        "Wind",
        "Solar"
      ]
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas, wind and solar grew",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of generation (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal",
      "title": "Source",
      "sort": [
        "Coal",
        "Gas",
        "Nuclear",
        "Hydro",
        "Wind",
        "Solar"
      ]
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas, wind and solar grew",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "stack": "zero",
      "axis": {
        "title": "Share of generation (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal",
      "title": "Source",
      "sort": [
        "Coal",
        "Gas",
        "Nuclear",
        "Hydro",
        "Wind",
        "Solar"
      ]
    }
  },
  "labels": {
    "density": "none"
  },
  "chrome": {
    "title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas, wind and solar grew",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–2023"
  }
};

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

The default palette has six hues. A chart with more series than that borrows from a second ramp and logs a warning. Past six series, group the smallest into one, split the chart into small multiples, or label the series directly. sort on the color channel fixes the order, and with it which source gets which hue.

Sequential

20162017201820192020202120222023GasCoalNuclearWindHydroSolar1.443.5Gas has been the largest source of USelectricity every year since 2016Share of US electricity from the six main sources, %, 2016–202320162017201820192020202120222023GasCoalNuclearWindHydroSolar1.443.5Gas has been the largest source of USelectricity every year since 2016Share of US electricity from the six main sources, %, 2016–2023
Darker cells are larger shares. Source: Our World in Data (owid/electricity-mix).
{
  "mark": "rect",
  "data": [
    {
      "year": "2016",
      "source": "Coal",
      "share": 31.2
    },
    {
      "year": "2016",
      "source": "Gas",
      "share": 34.8
    },
    {
      "year": "2016",
      "source": "Nuclear",
      "share": 20.3
    },
    {
      "year": "2016",
      "source": "Hydro",
      "share": 6.6
    },
    {
      "year": "2016",
      "source": "Wind",
      "share": 5.7
    },
    {
      "year": "2016",
      "source": "Solar",
      "share": 1.4
    },
    // … 42 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "source",
      "type": "nominal",
      "sort": [
        "Gas",
        "Coal",
        "Nuclear",
        "Wind",
        "Hydro",
        "Solar"
      ]
    },
    "color": {
      "field": "share",
      "type": "quantitative",
      "title": "Share (%)",
      "scale": {
        "scheme": "blues"
      },
      "format": ".1f"
    }
  },
  "chrome": {
    "title": "Gas has been the largest source of US electricity every year since 2016",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "rect",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "source",
      "type": "nominal",
      "sort": [
        "Gas",
        "Coal",
        "Nuclear",
        "Wind",
        "Hydro",
        "Solar"
      ]
    },
    "color": {
      "field": "share",
      "type": "quantitative",
      "title": "Share (%)",
      "scale": {
        "scheme": "blues"
      },
      "format": ".1f"
    }
  },
  "chrome": {
    "title": "Gas has been the largest source of US electricity every year since 2016",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "rect",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "source",
      "type": "nominal",
      "sort": [
        "Gas",
        "Coal",
        "Nuclear",
        "Wind",
        "Hydro",
        "Solar"
      ]
    },
    "color": {
      "field": "share",
      "type": "quantitative",
      "title": "Share (%)",
      "scale": {
        "scheme": "blues"
      },
      "format": ".1f"
    }
  },
  "chrome": {
    "title": "Gas has been the largest source of US electricity every year since 2016",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "rect",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "source",
      "type": "nominal",
      "sort": [
        "Gas",
        "Coal",
        "Nuclear",
        "Wind",
        "Hydro",
        "Solar"
      ]
    },
    "color": {
      "field": "share",
      "type": "quantitative",
      "title": "Share (%)",
      "scale": {
        "scheme": "blues"
      },
      "format": ".1f"
    }
  },
  "chrome": {
    "title": "Gas has been the largest source of US electricity every year since 2016",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "rect",
  "encoding": {
    "x": {
      "field": "year",
      "type": "ordinal"
    },
    "y": {
      "field": "source",
      "type": "nominal",
      "sort": [
        "Gas",
        "Coal",
        "Nuclear",
        "Wind",
        "Hydro",
        "Solar"
      ]
    },
    "color": {
      "field": "share",
      "type": "quantitative",
      "title": "Share (%)",
      "scale": {
        "scheme": "blues"
      },
      "format": ".1f"
    }
  },
  "chrome": {
    "title": "Gas has been the largest source of US electricity every year since 2016",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–2023"
  }
};

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

A quantitative field on the color channel gets a sequential scale, light for small values and dark for large ones. scale.scheme picks the ramp: "blue", "green", "orange", "purple" or "teal", and the Vega-Lite names such as "blues" also work. A heatmap like this one is where sequential color does the most work, since color is the only channel left for the value.

Diverging

1900192019401960198020002020Year−0.5+0.0+0.5+1.0Difference from the 1951–1980 average (°C)−0.43+0.38+1.196 of 7 readings before 1970 were below the1951–1980 average; all 7 from 1970 on wereaboveGlobal surface temperature, difference from the 1951–1980 average (°C), by decade from1900, plus 20251900192019401960198020002020Year−0.5+0.0+0.5+1.0Difference from the 1951–1980 average (°C)−0.43+0.38+1.196 of 7 readings before 1970 were below the1951–1980 average; all 7 from 1970 on wereaboveGlobal surface temperature, difference from the 1951–1980 average (°C), by decade from1900, plus 2025
Global temperature anomaly on a diverging scale. Source: NASA GISS Surface Temperature Analysis (GISTEMP v4).
{
  "mark": "bar",
  "data": [
    {
      "year": "1900",
      "anomaly": -0.08,
      "trend": "Cooler"
    },
    {
      "year": "1910",
      "anomaly": -0.43,
      "trend": "Cooler"
    },
    {
      "year": "1920",
      "anomaly": -0.27,
      "trend": "Cooler"
    },
    {
      "year": "1930",
      "anomaly": -0.16,
      "trend": "Cooler"
    },
    {
      "year": "1940",
      "anomaly": 0.12,
      "trend": "Warmer"
    },
    {
      "year": "1950",
      "anomaly": -0.17,
      "trend": "Cooler"
    },
    // … 8 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "title": "Year",
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Difference from the 1951–1980 average (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "anomaly",
      "type": "quantitative",
      "title": "Anomaly (°C)",
      "scale": {
        "scheme": "redBlue",
        "reverse": true,
        "domain": [
          -1.2,
          1.2
        ]
      },
      "format": "+.2f"
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "6 of 7 readings before 1970 were below the 1951–1980 average; all 7 from 1970 on were above",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average (°C), by decade from 1900, plus 2025"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

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

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "title": "Year",
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Difference from the 1951–1980 average (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "anomaly",
      "type": "quantitative",
      "title": "Anomaly (°C)",
      "scale": {
        "scheme": "redBlue",
        "reverse": true,
        "domain": [
          -1.2,
          1.2
        ]
      },
      "format": "+.2f"
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "6 of 7 readings before 1970 were below the 1951–1980 average; all 7 from 1970 on were above",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average (°C), by decade from 1900, plus 2025"
  }
};

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 = [/* 14 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "title": "Year",
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Difference from the 1951–1980 average (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "anomaly",
      "type": "quantitative",
      "title": "Anomaly (°C)",
      "scale": {
        "scheme": "redBlue",
        "reverse": true,
        "domain": [
          -1.2,
          1.2
        ]
      },
      "format": "+.2f"
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "6 of 7 readings before 1970 were below the 1951–1980 average; all 7 from 1970 on were above",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average (°C), by decade from 1900, plus 2025"
  }
};
</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 = [/* 14 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "title": "Year",
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Difference from the 1951–1980 average (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "anomaly",
      "type": "quantitative",
      "title": "Anomaly (°C)",
      "scale": {
        "scheme": "redBlue",
        "reverse": true,
        "domain": [
          -1.2,
          1.2
        ]
      },
      "format": "+.2f"
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "6 of 7 readings before 1970 were below the 1951–1980 average; all 7 from 1970 on were above",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average (°C), by decade from 1900, plus 2025"
  }
};
</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 = [/* 14 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "title": "Year",
        "format": "%Y"
      }
    },
    "y": {
      "field": "anomaly",
      "type": "quantitative",
      "axis": {
        "title": "Difference from the 1951–1980 average (°C)",
        "format": "+.1f"
      }
    },
    "color": {
      "field": "anomaly",
      "type": "quantitative",
      "title": "Anomaly (°C)",
      "scale": {
        "scheme": "redBlue",
        "reverse": true,
        "domain": [
          -1.2,
          1.2
        ]
      },
      "format": "+.2f"
    }
  },
  "labels": {
    "density": "none"
  },
  "annotations": [
    {
      "type": "refline",
      "y": 0,
      "style": "solid",
      "strokeWidth": 1
    }
  ],
  "chrome": {
    "title": "6 of 7 readings before 1970 were below the 1951–1980 average; all 7 from 1970 on were above",
    "subtitle": "Global surface temperature, difference from the 1951–1980 average (°C), by decade from 1900, plus 2025"
  }
};

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

A diverging scale runs two hues out from a neutral middle, for values that sit above or below a reference point. The schemes are "redBlue" and "brownTeal". Put the reference at the middle of the domain: here domain: [-1.2, 1.2] centers the neutral color on zero, the 1951–1980 average. reverse: true flips redBlue so warm years are red.

Highlighting one series

2016201820202022010203040Share of generation (%)CoalGasNuclearWindSolarSolar's share of generation from six sourcesrose from 1.4% in 2016 to 5.8% in 2023Share of US electricity from the six main sources, %, 2016–20232016201820202022010203040Share of generation (%)CoalGasNuclearWindSolarSolar's share of generation from six sourcesrose from 1.4% in 2016 to 5.8% in 2023Share of US electricity from the six main sources, %, 2016–2023
US electricity mix with solar highlighted. Source: Our World in Data (owid/electricity-mix).
{
  "mark": {
    "type": "line",
    "strokeWidth": 2.5
  },
  "data": [
    {
      "year": "2016",
      "source": "Coal",
      "share": 31.2
    },
    {
      "year": "2016",
      "source": "Gas",
      "share": 34.8
    },
    {
      "year": "2016",
      "source": "Nuclear",
      "share": 20.3
    },
    {
      "year": "2016",
      "source": "Hydro",
      "share": 6.6
    },
    {
      "year": "2016",
      "source": "Wind",
      "share": 5.7
    },
    {
      "year": "2016",
      "source": "Solar",
      "share": 1.4
    },
    // … 42 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y",
        "tickCount": 8
      }
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "axis": {
        "title": "Share of generation (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal",
      "highlight": "Solar"
    }
  },
  "endpointLabels": {
    "content": "label"
  },
  "chrome": {
    "title": "Solar's share of generation from six sources rose from 1.4% in 2016 to 5.8% in 2023",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "line",
    "strokeWidth": 2.5
  },
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y",
        "tickCount": 8
      }
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "axis": {
        "title": "Share of generation (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal",
      "highlight": "Solar"
    }
  },
  "endpointLabels": {
    "content": "label"
  },
  "chrome": {
    "title": "Solar's share of generation from six sources rose from 1.4% in 2016 to 5.8% in 2023",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "line",
    "strokeWidth": 2.5
  },
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y",
        "tickCount": 8
      }
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "axis": {
        "title": "Share of generation (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal",
      "highlight": "Solar"
    }
  },
  "endpointLabels": {
    "content": "label"
  },
  "chrome": {
    "title": "Solar's share of generation from six sources rose from 1.4% in 2016 to 5.8% in 2023",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "line",
    "strokeWidth": 2.5
  },
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y",
        "tickCount": 8
      }
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "axis": {
        "title": "Share of generation (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal",
      "highlight": "Solar"
    }
  },
  "endpointLabels": {
    "content": "label"
  },
  "chrome": {
    "title": "Solar's share of generation from six sources rose from 1.4% in 2016 to 5.8% in 2023",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "line",
    "strokeWidth": 2.5
  },
  "encoding": {
    "x": {
      "field": "year",
      "type": "temporal",
      "axis": {
        "format": "%Y",
        "tickCount": 8
      }
    },
    "y": {
      "field": "share",
      "type": "quantitative",
      "axis": {
        "title": "Share of generation (%)",
        "format": ".0f",
        "grid": true
      }
    },
    "color": {
      "field": "source",
      "type": "nominal",
      "highlight": "Solar"
    }
  },
  "endpointLabels": {
    "content": "label"
  },
  "chrome": {
    "title": "Solar's share of generation from six sources rose from 1.4% in 2016 to 5.8% in 2023",
    "subtitle": "Share of US electricity from the six main sources, %, 2016–2023"
  }
};

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

highlight on the color channel names the series to keep in color and turns the rest grey. It takes one name or a list. With endpointLabels: { content: "label" } each line is named at its end, so the chart needs no legend.

Color blindness and print

The default palette was checked so that every pair of hues stays distinguishable under deuteranopia and protanopia. Grayscale print loses hue entirely, so give each series a second cue:

  • fillPattern: "auto" on a bar, area or pie mark lays a different pattern over each series.
  • strokeDash on the encoding gives each line its own dash.

Accessibility covers both, and the contrast checks the library runs.

Fields that matter

Field What it does
encoding.color.type "nominal" for a categorical palette, "quantitative" for a sequential or diverging scale.
encoding.color.scale.scheme A named ramp, such as "blues" or "redBlue".
encoding.color.scale.domain The value range the ramp spans. Make it symmetric to center a diverging scale.
encoding.color.scale.range Your own colors, which win over scheme.
encoding.color.highlight Series to keep in color; the rest turn grey.
theme.colors.categorical Replaces the categorical palette for every chart that uses the theme.