翻譯|使用教程|編輯:楊鵬連|2020-07-21 11:47:47.970|閱讀 747 次
概述:了解如何創建一個交互式條形圖與Highcharts比賽。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Highcharts是一款純JavaScript編寫的圖表庫,為你的Web網站、Web應用程序提供直觀、交互式圖表。當前支持折線、曲線、區域、區域曲線圖、柱形圖、條形圖、餅圖、散點圖、角度測量圖、區域排列圖、區域曲線排列圖、柱形排列圖、極坐標圖等幾十種圖表類型。
您聽說過條形圖比賽嗎?沒有?嗯,這不是數據科學家的棋盤游戲,但實際上是一種通過動畫以條形圖格式顯示時間序列的有用方法。
這是一個示例,下面我們將展示如何創建此圖表。
借助dataSorting功能,使用Highcharts庫創建條形圖競賽非常容易和直接。在本教程中,我們將向您展示如何創建世界人口條形圖競賽。
讓我們開始吧!
本教程中使用的數據是1960年至2018年的世界人口。這是此演示中使用的數據的鏈接?,F在,我們有了數據;讓我們做一個處理特定年份數據的函數。
/** * Calculate the data output */ function getData(year) { let output = initialData.map(data => { return [data["Country Name"], data[year]] }).sort((a, b) => b[1] - a[1]); return ([output[0], output.slice(1, 11)]); }該演示中的第一個結果顯示了與1960年有關的數據:
@import "http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"; #parentContainer { min-width: 400px; max-width: 800px; } #play-controls { position: absolute; left: 100px; top: 350px; } #play-pause-button { width: 30px; height: 30px; cursor: pointer; border: 1px solid silver; border-radius: 3px; background: #f8f8f8; } #play-range { transform: translateY(2.5px); }
我們將在窗口調整大小后添加此功能以適應范圍寬度:
events: { render() { let chart = this; // Responsive input input.style.width = chart.plotWidth - chart.legend.legendWidth + 'px' } },
先前更改的結果在此演示中:
/** * Update the chart. This happens either on updating (moving) the range input, * or from a timer when the timeline is playing. */ function update(increment) { if (increment) { input.value = parseInt(input.value) + increment; } if (input.value >= endYear) { // Auto-pause pause(btn); } chart.update({ title: { useHTML: true, text: `在這里,我們將上面的功能鏈接到按鈕元素:World population - overall: ${getData(input.value)[0][1]}`: `World population - overall: ${getData(input.value)[0][1]}` },}, }, false, false, false)}, false, false, false) chart.series[0].update({.series[0].update({ name: input.value,: input.value, data: getData(input.value)[1]: getData(input.value)[1] })}) }}
/** * Play the timeline. */ function play(button) {function play(button) { button.title = 'pause';.title = 'pause'; button.className = 'fa fa-pause';.className = 'fa fa-pause'; chart.sequenceTimer = setInterval(function() {.sequenceTimer = setInterval(function() { update(1);(1); }, 500);}, 500); }} /** /** * Pause the timeline, either when the range is ended, or when clicking the pause button. * Pausing stops the timer and resets the button to play mode. */ function pause(button) {function pause(button) { button.title = 'play';.title = 'play'; button.className = 'fa fa-play';.className = 'fa fa-play'; clearTimeout(chart.sequenceTimer);(chart.sequenceTimer); chart.sequenceTimer = undefined;.sequenceTimer = undefined; }} btn.addEventListener('click', function() {.addEventListener('click', function() { if (chart.sequenceTimer) {if (chart.sequenceTimer) { pause(this)(this) } else {} else { play(this)(this) }} })}) /** /** * Trigger the update on the range bar click. */ input.addEventListener('click', function() {.addEventListener('click', function() { update()() })})現在,我們有一個完全正常工作的種族條形圖:
/** * Animate dataLabels functionality */ (function(H) {(function(H) { const FLOAT = /^-?\d+\.?\d*$/;const FLOAT = /^-?\d+\.?\d*$/; // Add animated textSetter, just like fill/strokeSetters// Add animated textSetter, just like fill/strokeSetters H.Fx.prototype.textSetter = function(proceed) {.Fx.prototype.textSetter = function(proceed) { var startValue = this.start.replace(/ /g, ''),var startValue = this.start.replace(/ /g, ''), endValue = this.end.replace(/ /g, ''),= this.end.replace(/ /g, ''), currentValue = this.end.replace(/ /g, '');= this.end.replace(/ /g, ''); if ((startValue || '').match(FLOAT)) {if ((startValue || '').match(FLOAT)) { startValue = parseInt(startValue, 10);= parseInt(startValue, 10); endValue = parseInt(endValue, 10); // No support for float currentValue = Highcharts.numberFormat( Math.round(startValue + (endValue - startValue) * this.pos), 0); } this.elem.endText = this.end; this.elem.attr( this.prop, currentValue, null, true ); }; // Add textGetter, not supported at all at this moment: H.SVGElement.prototype.textGetter = function(hash, elem) { var ct = this.text.element.textContent || ''; return this.endText ? this.endText : ct.substring(0, ct.length / 2); } // Temporary change label.attr() with label.animate(): // In core it's simple change attr(...) => animate(...) for text prop H.wrap(H.Series.prototype, 'drawDataLabels', function(proceed) { var ret, attr = H.SVGElement.prototype.attr, chart = this.chart; if (chart.sequenceTimer) { this.points.forEach( point => (point.dataLabels || []).forEach( label => label.attr = function(hash, val) { if (hash && hash.text !== undefined) { var text = hash.text; delete hash.text; this.attr(hash); this.animate({ text: text }); return this; } else { return attr.apply(this, arguments); } } ) ); } ret = proceed.apply(this, Array.prototype.slice.call(arguments, 1)); this.points.forEach( p => (p.dataLabels || []).forEach(d => d.attr = attr) ); return ret; }); })(Highcharts);最終結果在下面的演示中:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:Highsoft