標籤:
?
?
直接上步驟:
?
產生一個options,選項包含了一些基本的配置,如標題,座標刻度,serial等;
配置X軸顯示的Category資料,為一個數組;
配置Y軸顯示的資料,也為一個資料;
用產生option構建一個Hightcharts對象,即可以自動畫出一個折線圖了;
?
?
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.html
ASP.NET MVC中使用highcharts 產生簡單的折線圖