highcharts.js數(shù)據(jù)綁定方式代碼實例
這篇文章主要介紹了highcharts.js數(shù)據(jù)綁定方式代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
一,我們先來看看異步加載數(shù)據(jù)的寫法(這是使用MVC的例子)
1 js寫法
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/highcharts.js"></script>
<div id="chart"></div>
<script type="text/javascript">
//定義一個Highcharts的變量,初始值為null
var highCharts = null;
//定義highCharts的布局環(huán)境
//布局環(huán)境組成:X軸、Y軸、數(shù)據(jù)顯示、圖標(biāo)標(biāo)題
var oOptions = {
chart: {
renderTo: 'chart', //設(shè)置顯示的頁面塊
type: 'column' //設(shè)置顯示的方式
},
title: {
text: '' //設(shè)置null則不顯示標(biāo)題
},
plotOptions: {
column: {
pointPadding: 0,
borderWidth: 1,
groupPadding: 0,
shadow: false
}
},
xAxis: {
tickWidth: 0,
//labels: {
// enabled: false //禁止X軸的標(biāo)題顯示
//},
type: 'category',
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: ''
},
//labels: {
// enabled: false //禁止Y軸的標(biāo)題顯示
//},
},
legend: {
enabled: false
},
credits: {
enabled: false
},
tooltip: {
formatter: function () {
return '<span>' + this.series.name + '</span>: <b>' + this.y + '</b>'+ '<span>分?jǐn)?shù)范圍</span>: <b>' + (this.x + 10) * 10 + "-" + (this.x + 11) * 10 + '</b>'
},
}
};
$(function () {
highCharts = new Highcharts.Chart(oOptions);
highCharts.showLoading();
$.ajax({
url: '/home/getJosn2',
type: 'POST',
success: function (Data) {
Data = eval("(" + Data + ")");
var Series = {
name: "人數(shù)",
data: Data.rows,
color: '#ddd'
};
highCharts.addSeries(Series);
}
});
highCharts.hideLoading();
});
</script>
2 C#后臺代碼(MVC)
[HttpPost]
public JsonResult getJosn2()
{
return Json("{\"rows\":[120, 360, 560, 60, 360, 160, 40, 360, { y: 150, color: '#45a9f4' }, 60, 230, 230, 300, 60, 230, 230, 300, 300]}");
}
看我返回的json這個{ y: 150, color: '#45a9f4' }, 是什么效果呢?如下圖,高亮的那條

二,有兩種數(shù)據(jù)綁定的方法,這里使用固定數(shù)據(jù)來展示例子
第一種:
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/highcharts.js"></script>
<div id="chart"></div>
<script type="text/javascript">
$(function () {
$('#chart').highcharts({
chart: {
type: 'column'
},
title: {
text: ''
},
plotOptions: {
column: {
pointPadding: 0,
borderWidth: 1,
groupPadding: 0,
shadow: false
}
},
xAxis: {
tickWidth: 0,
//labels: {
// enabled: false
//},
type: 'category'
},
yAxis: {
title: {
text: ''
},
//labels: {
// enabled: false
//}
},
legend: {
enabled: false
},
credits: {
enabled: false
},
tooltip: {
formatter: function () {
return '<span>' + this.series.name + '</span>: <b>' + this.y + '</b>'
},
},
series: [{
name: '人數(shù)',
data: [
['Jan', 50],
['Feb', 60],
['Mar', 70],
{
name: "Apr",
y: 150,
color: "#45a9f4"
},
['May', 140],
],
color: '#ddd'
}]
});
});
</script>
我們可以同時在series給X賦名字和值的一個json集合
第二種:
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/highcharts.js"></script>
<div id="chart"></div>
<script type="text/javascript">
$(function () {
$('#chart').highcharts({
chart: {
type: 'column'
},
title: {
text: ''
},
plotOptions: {
column: {
pointPadding: 0,
borderWidth: 1,
groupPadding: 0,
shadow: false
}
},
xAxis: {
tickWidth: 0,
//labels: {
// enabled: false
//},
type: 'category',
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May']
},
yAxis: {
title: {
text: ''
},
//labels: {
// enabled: false
//}
},
legend: {
enabled: false
},
credits: {
enabled: false
},
tooltip: {
formatter: function () {
return '<span>' + this.series.name + '</span>: <b>' + this.y + '</b>'
},
},
series: [{
name: '人數(shù)',
data: [120, 360, { y: 150, color: "#45a9f4" }, 560, 60],
color: '#ddd'
}]
});
});
</script>
我們X軸的標(biāo)題和值也可以分開賦值,如上
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序中使用ECharts 異步加載數(shù)據(jù)實現(xiàn)圖表功能
本文通過實例代碼給大家介紹了微信小程序中使用ECharts 異步加載數(shù)據(jù)實現(xiàn)圖表功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-07-07
使用JS監(jiān)聽鍵盤按下事件(keydown event)
這篇文章主要介紹了使用JS監(jiān)聽鍵盤按下事件(keydown event),本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
JavaScript Event學(xué)習(xí)第十一章 按鍵的檢測
檢測用戶的按鍵是事件處理程序的一個很特別的環(huán)節(jié)。這一章我們著力解決一些非常棘手的問題,并且制定一個完備的表格。2010-02-02
uniapp調(diào)用百度語音實現(xiàn)錄音轉(zhuǎn)文字功能
這篇文章主要介紹了uniapp通過調(diào)用百度語音,實現(xiàn)錄音轉(zhuǎn)文字的功能。文中的示例代碼對我們學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2021-12-12
JavaScript 進(jìn)度條實現(xiàn)代碼(Firefox等相似瀏覽器下不支持)
JavaScript實現(xiàn)的進(jìn)度條,可惜在Firefox等相似瀏覽器下不支持(遠(yuǎn)程)2009-07-07

