JavaScript圖表工具FusionCharts Suite XT入門(mén)教程(十):在運(yùn)行時(shí)更改圖表類(lèi)型
FusionCharts Suite XT是全面的跨平臺(tái)、跨瀏覽器JavaScript圖表套包,其中包括FusionCharts XT、PowerCharts XT 、FusionWidgets XT 、FusionMaps XT。支持 ASP、 ASP.NET、 PHP、 JSP、 ColdFusion、 Ruby on Rails、 JavaScript、甚至簡(jiǎn)單的HTML頁(yè)面。它是你值得信賴(lài)的JavaScript圖表解決方案,目前在全球有45萬(wàn)用戶(hù)選擇Fusioncharts來(lái)制作專(zhuān)業(yè)的JavaScript圖表。
JavaScript圖表工具,為什么選擇FusionCharts?點(diǎn)擊了解!
點(diǎn)擊下載FusionCharts Suite XT最新試用版
在運(yùn)行時(shí)更改圖表類(lèi)型
FusionCharts Suite XT包含高級(jí)功能,可讓您向圖表添加更多上下文并簡(jiǎn)化數(shù)據(jù)可視化。這些功能包括圖表更新,運(yùn)行時(shí)更新圖表類(lèi)型和事件。
本文重點(diǎn)介紹如何在運(yùn)行時(shí)更改圖表的圖表類(lèi)型。該示例中使用的圖表類(lèi)型為:
Column 2D
Bar 2D
Pie 2D
配置為更改圖表類(lèi)型的圖表如下所示:
上面示例的完整代碼如下:
import FusionCharts from 'fusioncharts';import Charts from 'fusioncharts/fusioncharts.charts';import FusionTheme from 'fusioncharts/themes/fusioncharts.theme.fusion';// Add the chart and theme as dependencyFusionCharts.addDep(Charts);FusionCharts.addDep(FusionTheme);// Create an Instance with chart optionsvar chartInstance = new FusionCharts({ type: 'column2d', height: '400', width: '700', dataFormat: 'json', renderAt: 'chart-container', dataSource: { "chart": { "caption": "Countries With Most Oil Reserves [2017-18]", "subCaption": "In MMbbl = One Million barrels", "xAxisName": "Country", "yAxisName": "Reserves (MMbbl)", "numberSuffix": "K", "theme": "fusion" }, "data": [{ "label": "Venezuela", "value": "290" }, { "label": "Saudi", "value": "260" }, { "label": "Canada", "value": "180" }, { "label": "Iran", "value": "140" }, { "label": "Russia", "value": "115" }, { "label": "UAE", "value": "100" }, { "label": "US", "value": "30" }, { "label": "China", "value": "30" }], }, "events": { "beforeRender": function(e, d) { var container = e.data.container; // Change the sizes according to your need var options = { 'column2d': 'column2d', 'bar2d': 'bar2d', 'pie2d': 'pie2d' }; var chartSelected = 'column2d'; function instantiate() { // Create option containers var parent = container.parentNode; var optionsContainer = document.createElement('div'); optionsContainer.id = 'config-container'; var spanLabel = document.createElement('span'); spanLabel.id = 'select-text'; spanLabel.innerText = "Choose a chart type: "; var radioContainer = document.createElement('div'); addClass(radioContainer, 'change-type'); window.__onChange = function(option) { e.sender.chartType(option) } // Util to add class function addClass(element, className) { var element, name = className, arr; arr = element.className.split(" "); if (arr.indexOf(name) == -1) { element.className += " " + name; } } function radioWrapper(wrapperId, inputId, label, selected, optionLabel) { var item = ""; item += ""; item += "" + label + "" item += ""; return item; } var changeTypeChilds = ''; Object.keys(options).forEach(function(option, index) { var label = options[option]; var selected = chartSelected === option; var radioOption = radioWrapper('radio' + (index + 1), 'radioButton' + (index + 1), label.toUpperCase(), selected, option); changeTypeChilds += radioOption; }); radioContainer.innerHTML = changeTypeChilds; optionsContainer.appendChild(spanLabel); optionsContainer.appendChild(radioContainer); parent.appendChild(optionsContainer); var css = '.change-type{display:inline-block;margin:0 10px;font-family:basefontRegular,Helvetica Neue,Arial,sans-serif}.change-type>div{display:inline-flex;position:relative;margin:0 10px}.change-type label{position:relative;padding:5px 4px 5px 30px;border-radius:4px}.change-type input{opacity:0;cursor:pointer;z-index:1;width:100%;height:100%;left:0;position:absolute}.change-type label:after,.change-type label:before{content:"";position:absolute}.change-type label:before{display:block;background:#fff;border:2px solid #949697;box-shadow:none;border-radius:50%;top: 15px;left: 9px;width:1rem;height:1rem}.change-type label:after{ width: .55rem;height: .55rem;top: 18px;left: 11px;border-radius: 100%;}.change-type input:checked~label{color:#48b884;font-weight:600;box-shadow:0 4px 9px 0 rgba(104,105,128,.22)}.change-type input:checked~label:before{color:#fff;box-shadow:none;border:2px solid #48b884}.change-type input:checked~label:after{background:#55bd8d}'; var styleNode = document.createElement('style'); styleNode.innerHTML = css; document.body.appendChild(styleNode); } if (!window.__sample_change_chart_type_instansiated) { instantiate(); } window.__sample_change_chart_type_instansiated = true; } }});// RenderchartInstance.render();
上圖通過(guò)以下步驟呈現(xiàn):
1、使用包括必要的庫(kù)和組件import。例如fusioncharts圖書(shū)館等
2、將圖表和主題添加為依賴(lài)項(xiàng)。
3、將圖表配置存儲(chǔ)在JSON對(duì)象中。在JSON對(duì)象中:
將圖表類(lèi)型設(shè)置為column2d。
設(shè)置圖表的寬度和高度(以像素為單位)。
將設(shè)置dataFormat為JSON。
將json數(shù)據(jù)嵌入為的值dataSource。
4、beforeRender事件被稱(chēng)為從更新圖表類(lèi)型column2d到bar2d或pie2d。
5、在中創(chuàng)建單選按鈕,
Column 2D
Bar 2D
Pie 2D
6、功能已添加到單選按鈕,以在運(yùn)行時(shí)更新圖表類(lèi)型。
=====================================================
想了解更多關(guān)于FusionCharts Suite XT資源,請(qǐng)點(diǎn)擊此處
想要了解或者購(gòu)買(mǎi)FusionCharts Suite XT正版授權(quán)的朋友歡迎咨詢(xún)慧都官方客服
關(guān)注下方微信公眾號(hào),及時(shí)獲取產(chǎn)品最新消息和最新資訊