2014-06-26 30 views
0

我想要做的是能夠動態地呈現圖形,從url中定義一組參數。例如http://example.html?team=team1%chartype=pie以編程方式構建不同類型的Highcharts。

這是我的一個簡單的「列」 http://jsfiddle.net/4mBWg/ 這是我的一個簡單的「餡餅」 http://jsfiddle.net/YFdKt/

Code block (SO is not letting me post without code) 

編程什麼會我需要的是能夠接受一個「類型」參數代碼代碼示例(餅圖,圖表,行)並能夠繪製圖表。

僅僅改變「類型」是不夠的。看看這兩種類型的圖我假設plotOptions需要動態構建......我需要做些什麼來完成這個任務?

回答

1

如果只是html然後嘗試,

// http://stackoverflow.com/a/3855394/1817690 
var qs = (function(a) { 
    if (a == "") return {}; 
    var b = {}; 
    for (var i = 0; i < a.length; ++i) 
    { 
     var p=a[i].split('='); 
     if (p.length != 2) continue; 
     b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " ")); 
    } 
    return b; 
})(window.location.search.substr(1).split('&')); 

if(qs['type']) && qs['type']=='pie') { 
    Pie chart js code goes here 
} else if(qs['type'] && qs['type']=='line') { 
    Line chart js code goes here 
} 

如果您正在使用PHP的,你可以做到這一點像,

<?php if(isset($_GET['type']) && $_GET['type']=='pie') { ?> 
    Pie chart js code goes here 
<?php } else if(isset($_GET['type']) && $_GET['type']=='line') { ?> 
    Line chart js code goes here 
<?php } ?>