直接上步驟: javascript
生成一個options,選項包含了一些基本的配置,如標題,座標刻度,serial等; php
配置X軸顯示的Category數據,爲一個數組; html
配置Y軸顯示的數據,也爲一個數據; java
用生成option構建一個Hightcharts對象,便可以自動畫出一個折線圖了; jquery
1.配置BundleConfig
-
bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js", "~/Scripts/jquery.metadata.js"));
-
bundles.Add(new ScriptBundle("~/bundles/highcharts").Include("~/Scripts/hightcharts/highcharts-custom.js"));
2.視圖模板
-
@Scripts.Render("~/bundles/highcharts")
-
-
<script type="text/javascript">
-
$(function () {
-
var option = option = getOption("container", '@ViewBag.titletext');
-
//生成兩個serial
-
option.xAxis.categories = @Html.Raw(@ViewBag.categories)
-
option.series[0].data = @ViewBag.amount
-
option.series[1].data = @ViewBag.quantity
-
option.subtitle.text = '@ViewBag.subtitle'
-
-
chart = new Highcharts.Chart(option);
-
-
$("text:contains('銷售數量')").trigger('click');
-
});
-
-
function getOption(container, title) {
-
var options = {
-
chart: {
-
renderTo: container,
-
type: 'line'
-
},
-
title: {
-
text: title
-
},
-
subtitle: {
-
text: 'imc'
-
},
-
xAxis: {
-
title: {
-
text: '日期'
-
}
-
},
-
yAxis: {
-
title: {
-
text: '數量或金額'
-
},
-
min: 0, // 定義最小值
-
},
-
plotOptions: {
-
line: {
-
dataLabels: {
-
enabled: true
-
}
-
}
-
},
-
tooltip: {
-
shared: true, //共享數據提示框
-
},
-
credits: {
-
enabled: false
-
},
-
series: [{
-
name: "銷售金額"
-
}, {
-
name: "銷售數量"
-
}]
-
}
-
-
return options;
-
}
-
-
</script>
3.後臺代碼
-
categories = "['" + string.Join("','", list.Select(zw => zw.DTStr)) + "']";
-
quantity = "[" + string.Join(",", list.Select(zw => zw.Quantity)) + "]";
-
amount = "[" + string.Join(",", list.Select(zw => zw.Amount)) + "]";
-
-
ViewBag.titletext = title;
-
ViewBag.categories = categories;
-
ViewBag.quantity = quantity;
-
ViewBag.amount = amount;
-
ViewBag.subtitle = subtitle;
4.效果圖
5.參考資料
中文教程與資料:http://www.hcharts.cn/docs/index.php?doc=basic-axis 數組
示例:http://www.cnblogs.com/TivonStone/p/3539766.html 動畫
更多的charts插件:http://www.cnblogs.com/chu888chu888/archive/2012/12/22/2828962.htmlspa