Scatter plot

author(s): Torstein Hønsi

by koh_edwin

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
        olympic contestants by sport, showing how various sports prefer different
        characteristics.
    </p>
</figure>

CSS

#container {
    max-width: 800px;
    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 #ebebeb;
    margin: 10px auto;
    text-align: center;
    width: 100%;
    max-width: 500px;
}

.highcharts-data-table caption {
    padding: 1em 0;
    font-size: 1.2em;
    color: #555;
}

.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 tr:nth-child(even) {
    background: #f8f8f8;
}

.highcharts-data-table tr:hover {
    background: #f1f7ff;
}

JavaScript

Highcharts.setOptions({
    colors: ['rgba(5,141,199,0.5)', 'rgba(80,180,50,0.5)', 'rgba(237,86,27,0.5)']
});

const series = [{
    name: 'Basketball',
    id: 'basketball',
    marker: {
        symbol: 'circle'
    }
},
{
    name: 'Triathlon',
    id: 'triathlon',
    marker: {
        symbol: 'triangle'
    }
},
{
    name: 'Volleyball',
    id: 'volleyball',
    marker: {
        symbol: 'square'
    }
}];


async function getData() {
   // const response = await fetch(
   //     'https://cdn.jsdelivr.net/gh/highcharts/highcharts@24912efc85/samples/data/olympic2012.json'
    //);
    return [{
    "continent": "Asia",
    "country": "AFG",
    "height": 1.75,
    "weight": 68,
    "BMI": 22.2,
    "age": 24.2,
    "gender": "male",
    "sport": "athletics"
  }, {
    "continent": "Asia",
    "country": "AFG",
    "height": 1.65,
    "weight": 55,
    "BMI": 20.2,
    "age": 20.2,
    "gender": "female",
    "sport": "athletics"
  }, {
    "continent": "Asia",
    "country": "AFG",
    "height": 1.81,
    "weight": 99,
    "BMI": 30.2,
    "age": 30.4,
    "gender": "male",
    "sport": "judo"
  }, {
    "continent": "Europe",
    "country": "ALB",
    "height": 1.7,
    "weight": 69,
    "BMI": 23.9,
    "age": 26.5,
    "gender": "male",
    "sport": "weightlifting"
  }, {
    "continent": "Europe",
    "country": "ALB",
    "height": 1.6,
    "weight": 52,
    "BMI": 20.3,
    "age": 25.1,
    "gender": "female",
    "sport": "weightlifting"
  }];
}


getData().then(data => {
    const getData = sportName => {
        const temp = [];
        data.forEach(elm => {
            if (elm.sport === sportName && elm.weight > 0 && elm.height > 0) {
                temp.push([elm.height, elm.weight]);
            }
        });
        return temp;
    };
    series.forEach(s => {
        s.data = getData(s.id);
    });

    Highcharts.chart('container', {
        chart: {
            type: 'scatter',
            zoomType: 'xy'
        },
        title: {
        ...