Parliament (item) chart
author(s): Torstein Hønsi
by owen_pengtao
HTML
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/item-series.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">
Chart showing representatives in the German Parliament. The chart is
using the <code>item</code> series type with a semi circle layout.
Item charts create a number of items corresponding to the value of
each data point.
</p>
</figure>
CSS
.highcharts-figure,
.highcharts-data-table table {
min-width: 320px;
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;
}
input[type="number"] {
min-width: 50px;
}
JavaScript
Highcharts.chart('container', {
title: {
text: 'Distribution of seats'
},
subtitle: {
text: 'Bundestag election 2021. Source: ' +
'<a href="https://www.bundeswahlleiter.de/en/bundeswahlleiter.html"' +
'target="_blank">Bundeswahlleiter</a> '
},
legend: {
labelFormat: '{name} <span style="opacity: 0.4">{y}</span>'
},
series: [{
type: 'item',
name: 'Representatives',
keys: ['name', 'y', 'color', 'label'],
data: [
['The Left', 3, '#CC0099', 'DIE LINKE'],
['Social Democratic Party', 4, '#EE0011', 'SPD'],
['Alliance 90/The Greens', 5, '#448833', 'GRÜNE'],
],
layout: "horizontal",
marker: {
symbol: "square"
},
events: {
legendItemClick: function() {
var clickedSeriesName = this.name; // 使用系列的名字作为标识符
var totalItemsToShow = 0;
// 遍历所有系列
this.chart.series.forEach(function(series) {
series.data.forEach(function(point) {
console.log(point);
});
});
console.log("将要显示的项目总数:", totalItemsToShow);
}
}
}],
});