Scatter plot
author(s):
Torstein Hønsi
HTML
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<figure class="highcharts-figure">
<div id="container"></div>
<p class="highcharts-description">
Scatter charts are often used to visualize the relationships
between data in two dimensions. This chart is visualizing european
olympic contestants by sport, showing how various sports prefer different
characteristics.
</p>
</figure>
CSS
body {
font-family:
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
Roboto,
Helvetica,
Arial,
"Apple Color Emoji",
"Segoe UI Emoji",
"Segoe UI Symbol",
sans-serif;
background: var(--highcharts-background-color);
color: var(--highcharts-neutral-color-100);
}
#container {
max-width: 800px;
height: 500px;
margin: auto;
}
.highcharts-figure,
.highcharts-data-table table {
min-width: 360px;
max-width: 800px;
margin: 1em auto;
}
.highcharts-data-table table {
font-family: Verdana, sans-serif;
border-collapse: collapse;
border: 1px solid var(--highcharts-neutral-color-10, #e6e6e6);
margin: 10px auto;
text-align: center;
width: 100%;
max-width: 500px;
}
.highcharts-data-table caption {
padding: 1em 0;
font-size: 1.2em;
color: var(--highcharts-neutral-color-60, #666);
}
.highcharts-data-table th {
font-weight: 600;
padding: 0.5em;
}
.highcharts-data-table td,
.highcharts-data-table th,
.highcharts-data-table caption {
padding: 0.5em;
}
.highcharts-data-table thead tr,
.highcharts-data-table tbody tr:nth-child(even) {
background: var(--highcharts-neutral-color-3, #f7f7f7);
}
.highcharts-description {
background: light-dark(#f2f6f2, #323232);
border-radius: 0 4px 4px 0;
border-left: 2px solid light-dark(#1d6aff, #fff);
font-size: 0.8rem;
font-style: italic;
padding: 8px 16px;
margin: 2em 10px 1em;
}
TypeScript
type Athlete = {
sport: string;
weight: number;
height: number;
continent: string;
};
const series: Highcharts.SeriesScatterOptions[] = [{
name: 'Triathlon',
type: 'scatter',
id: 'triathlon',
marker: {
symbol: 'triangle'
}
},
{
name: 'Volleyball',
type: 'scatter',
id: 'volleyball',
marker: {
symbol: 'square'
}
},
{
name: 'Basketball',
type: 'scatter',
id: 'basketball',
marker: {
symbol: 'circle'
}
}];
async function getData(): Promise<Athlete[]> {
const response = await fetch(
'https://www.highcharts.com/samples/data/olympic2012.json'
);
return response.json();
}
getData().then(data => {
series.forEach(s => {
s.data = data
.filter(athlete =>
athlete.sport === s.id &&
athlete.weight > 0 &&
athlete.height > 0 &&
athlete.continent === 'Europe'
)
.map(athlete => [athlete.weight, athlete.height]);
});
Highcharts.chart('container', {
chart: {
type: 'scatter',
plotBorderColor: 'var(--highcharts-neutral-color-10, #e6e6e6)',
plotBorderWidth: 1,
plotBorderRadius: 5,
zooming: {
type: 'xy'
}
},
title: {
text: 'European olympic athletes by height and weight',
align: 'left'
},
subtitle: {
text: 'Source: <a href="https://www.theguardian.com/sport/datablo' +
'g/2012/aug/07/olympics-2012-athletes-age-weight-height">The ' +
'Guardian</a>',
align: 'left'
},
xAxis: {
labels: {
format: '{value} kg'
},
gridLineWidth: 1,
lineWidth: 0,
startOnTick: true,
endOnTick: true,
tickLength: 0
},
yAxis: {
...