Chart.js 與 ASP.NET MVC 整合應用

来源:https://www.cnblogs.com/WizardWu/archive/2019/01/01/10206095.html
-Advertisement-
Play Games

介紹 Chart.js 這套 open source 的免費「圖表」繪製函式庫,及其與 ASP.NET MVC 的整合應用。 ...


Chart.js 是一套開放原始碼的「圖表」繪製函式庫,和其他第三方的圖表工具相比,Chart.js 的特色如下:

  • 支援 HTML 5、響應式網頁 (RWD, Responsive Web Design)
  • 可免費使用,且可作為商業用途
  • 開放原始碼 (GitHub)
  • 可用 JavaScript 操作及開發
  • 可與 JSON 格式整合,因此能與 ASP.NET MVC、ASP.NET WebAPI、AJAX 技術作整合,便於資料傳遞

圖 1 本文範例的執行畫面 (.html、.cshtml)

-------------------------------------------------
本文的 ASP.NET MVC 範例下載:
https://files.cnblogs.com/files/WizardWu/190101.zip
---------------------------------------------------

Chart.js 官方網站:
https://www.chartjs.org/

Chart.js 使用方式:
Visual Studio 中的引用方式,用 NuGet 安裝 Chart.js,並在頁面中引用 Chart.min.js。

----------------------------------------------------------------------------------------------------------------------------------------

以下的範例 1,以單純的 .html 來測試 Chart.js (不使用 .NET / C#)。資料來源,是寫死在頁面裡的 JavaScript。

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8" />
 5     <title></title>
 6     <!--<link href="../Content/bootstrap.min.css" rel="stylesheet" />-->
 7     <script src="../Scripts/Chart.min.js"></script>
 8 </head>
 9 <body>
10     <canvas id="myChart"></canvas>
11 
12     <script>
13         var ctx = document.getElementById("myChart");
14         var chart = new Chart(ctx, {
15             type: "line",
16             data: {
17                 labels: ['1月', '2月', '3月', '4月', '5月', '6月'],
18                 datasets: [{
19                     label: "臺北",
20                     fill: false,
21                     backgroundColor: 'rgba(255,165,0,0.3)',
22                     borderColor: 'rgb(255,135,20)',
23                     pointStyle: "circle",
24                     pointBackgroundColor: 'rgb(0,222,0)',
25                     pointRadius: 5,
26                     pointHoverRadius: 10,
27                     data: [13.1, 10.2, 13.5, 20.9, 25.2, 27.1, 31.8]
28                 }, {
29                     label: "高雄",
30                     fill: false,
31                     backgroundColor: 'rgba(0,255,255,0.3)',
32                     borderColor: 'rgb(0,225,255)',
33                     pointStyle: "triangle",
34                     pointBackgroundColor: 'blue',
35                     pointRadius: 5,
36                     pointHoverRadius: 10,
37                         data: [29.1, 28.3, 22.6, 25.4, 27.5, 23.4]
38 
39                 }, {
40                     label: "越南",
41                     fill: false,
42                     backgroundColor: 'rgba(153,50,204,0.3)',
43                     borderColor: 'rgb(123,55,190)',
44                     pointStyle: "rect",
45                     pointBackgroundColor: 'rgb(220,20,60)',
46                     pointRadius: 5,
47                     pointHoverRadius: 10,
48                         data: [16.6, 17.3, 19.2, 23.8, 12.0, 17.6]
49 
50                 }]
51             },
52             options: {
53                 responsive: true,
54                 title: {
55                     display: true,
56                     fontSize: 26,
57                     text: '2019 年各分公司 1 - 6 月份營業額'
58                 },
59                 tooltips: {
60                     mode: 'point',
61                     intersect: true,
62                 },
63                 hover: {
64                     mode: 'nearest',
65                     intersect: true
66                 },
67                 scales: {
68                     xAxes: [{
69                         display: true,
70                         scaleLabel: {
71                             display: true,
72                             labelString: '月份',
73                             fontSize: 15
74                         },
75                         ticks: {
76                             fontSize: 15
77                         }
78                     }],
79                     yAxes: [{
80                         display: true,
81                         scaleLabel: {
82                             display: true,
83                             labelString: '百萬(美元)',
84                             fontSize: 15
85                         },
86                         ticks: {
87                             fontSize: 15
88                         }
89                     }]
90                 },
91                 animation: {
92                     duration: 2000
93                 }
94             }
95         });
96     </script>
97 </body>
98 </html>
\htmlPages\LineChart.html

 

以下的範例 2,使用 ASP.NET MVC 來測試 Chart.js。

資料來源,取自 Controller 層 (C# Collection 轉成 JSON 格式)。亦可改成取自 WebAPI 或資料庫。

 

 1 using System.Web.Mvc;
 2 using ChartJS.Models;
 3 using Newtonsoft.Json;
 4 using Newtonsoft.Json.Linq;
 5 
 6 namespace ChartJS.Controllers
 7 {
 8     public class BranchController : Controller
 9     {
10         // GET: Branch
11         public ActionResult Index()
12         {
13             return View();
14         }
15 
16         public ActionResult getBusinessJsonData()
17         {
18             string[] arrMonth = { "1月", "2月", "3月", "4月", "5月", "6月" };
19 
20             //C# convert JSON string
21             string jsonMonth = Newtonsoft.Json.JsonConvert.SerializeObject(arrMonth);
22             ViewBag.JsonMonth = jsonMonth;
23 
24             List<Branch> branchs = new List<Branch>
25             {
26                 new Branch
27                 {
28                     City="臺北",
29                     Business = new double[] { 13.1, 10.2, 13.5, 20.9, 25.2, 27.1, 31.8 }
30                 },
31                 new Branch{
32                     City="高雄",
33                     Business = new double[] { 29.1, 28.3, 22.6, 25.4, 27.5, 23.4 }
34 
35                 },
36                 new Branch{
37                     City="越南",
38                     Business = new double[] { 16.6, 17.3, 19.2, 23.8, 12.0, 17.6 }
39                 }
40             };
41 
42             //C# convert JSON string
43             string jsonBusiness = Newtonsoft.Json.JsonConvert.SerializeObject(branchs);
44             ViewBag.JsonBusiness = jsonBusiness;
45 
46             //回傳 JSON 格式,讓各種 client 裝置,以 AJAX 方式呼叫的 API
47             //return Json(branchs, JsonRequestBehavior.AllowGet);
48 
49             return View(branchs);
50         }
51     }
52 }
\Controllers\BranchController.cs

 

  1 @model IEnumerable<ChartJS.Models.Branch>
  2 
  3 @{
  4     ViewBag.Title = "Chart.js 範例";
  5 }
  6 @*<h2>Index</h2>*@
  7 
  8 <script src="../Scripts/Chart.min.js"></script>
  9 
 10 <canvas id="myChart"></canvas>
 11 
 12 <table>
 13     <!--從 Model 讀取 Business 資料-->
 14     @*@{ var js = new System.Web.Script.Serialization.JavaScriptSerializer(); }
 15 
 16     @foreach (var m in Model)
 17     {
 18         <tr>
 19             <td>@Html.DisplayFor(x => m.City)</td>
 20             <td>@js.Serialize(m.Business)</td>
 21         </tr>
 22     }*@
 23 </table>
 24 
 25 <script>
 26     //將 JSON 資料,指派給 JavaScript array
 27     //月份 (1月~6月)
 28     var jsMonth = @Html.Raw(ViewBag.JsonMonth);
 29 
 30     //三個城市的 City、Business
 31     var jsBusiness = @Html.Raw(ViewBag.JsonBusiness);
 32 
 33     var ctx = document.getElementById("myChart");
 34     var chart = new Chart(ctx, {
 35         type: "line",
 36         data: {
 37             //labels: ['1月', '2月', '3月', '4月', '5月', '6月'],
 38             labels: jsMonth,
 39             datasets: [{
 40                 //label: "臺北",
 41                 label: jsBusiness[0].City,
 42                 fill: false,
 43                 backgroundColor: 'rgba(255,165,0,0.3)',
 44                 borderColor: 'rgb(255,135,20)',
 45                 pointStyle: "circle",
 46                 pointBackgroundColor: 'rgb(0,222,0)',
 47                 pointRadius: 5,
 48                 pointHoverRadius: 10,
 49                 data: jsBusiness[0].Business
 50                 //data: [13.1, 10.2, 13.5, 20.9, 25.2, 27.1, 31.8]
 51             }, {
 52                 //label: "高雄",
 53                 label: jsBusiness[1].City,
 54                 fill: false,
 55                 backgroundColor: 'rgba(0,255,255,0.3)',
 56                 borderColor: 'rgb(0,225,255)',
 57                 pointStyle: "triangle",
 58                 pointBackgroundColor: 'blue',
 59                 pointRadius: 5,
 60                 pointHoverRadius: 10,
 61                 data: jsBusiness[1].Business
 62                 //data: [29.1, 28.3, 22.6, 25.4, 27.5, 23.4]
 63                 }, {
 64                 //label: "越南",
 65                 label: jsBusiness[2].City,
 66                 fill: false,
 67                 backgroundColor: 'rgba(153,50,204,0.3)',
 68                 borderColor: 'rgb(123,55,190)',
 69                 pointStyle: "rect",
 70                 pointBackgroundColor: 'rgb(220,20,60)',
 71                 pointRadius: 5,
 72                 pointHoverRadius: 10,
 73                 data: jsBusiness[2].Business
 74                 //data: [16.6, 17.3, 19.2, 23.8, 12.0, 17.6]
 75             }]
 76         },
 77         options: {
 78             responsive: true,
 79             title: {
 80                 display: true,
 81                 fontSize: 26,
 82                 text: '2019 年各分公司 1 - 6 月份營業額'
 83             },
 84             tooltips: {
 85                 mode: 'point',
 86                 intersect: true,
 87             },
 88             hover: {
 89                 mode: 'nearest',
 90                 intersect: true
 91             },
 92             scales: {
 93                 xAxes: [{
 94                     display: true,
 95                     scaleLabel: {
 96                         display: true,
 97                         labelString: '月份',
 98                         fontSize: 15
 99                     },
100                     ticks: {
101                         fontSize: 15
102                     }
103                 }],
104                 yAxes: [{
105                     display: true,
106                     scaleLabel: {
107                         display: true,
108                         labelString: '百萬(美元)',
109                         fontSize: 15
110                     },
111                     ticks: {
112                         fontSize: 15
113                     }
114                 }]
115             },
116             animation: {
117                 duration: 2000
118 	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • "原文參考" 非同步傳值 前臺往後臺傳值呢,有很多種方式,大家聽我細細道來。 第一種呢,也是最簡單的一種,通過get提交方式,將參數在鏈接中以問號的形式進行傳遞。 第二種呢,是將參數體現到鏈接中,在後臺通過占位進行傳遞。 第三種呢,通過post提交方式將form表單中的數據序列化後傳遞到後臺。 第四種 ...
  • 什麼是Hystrix,Hystrix如何使用 容錯框架Hystrix,SpringCloud將Hystrix整合到Netflix項目中了。它主要用來添加一個延遲的閥值和容錯邏輯。來幫助我們控制分散式系統之間的組件交互。 那麼什麼是延遲閥值呢:就拿下圖中的銷售模塊舉例,在銷售模塊調用會員模塊的時候,會 ...
  • 本文實例講述了PHP實現在資料庫百萬條數據中隨機獲取20條記錄的方法。PHP實例分享給大家供大家參考,具體如下:為什麼要寫這個?在去某個公司面試時,讓寫個演算法出來,當時就蒙了,我開發過程中用到演算法的嗎?又不是大數據開發,分析。今天偶然想起來一個坑爹數據,如:PHP取百萬條數據中隨機20條記錄,當時就 ...
  • 可能你只想簡單的使用,暫時不想瞭解太多的知識,那麼請看這裡,瞭解一下如何讀文件,寫文件 讀文件示例代碼 File file = new File("D:\\test\\t.txt"); //這裡的有兩條斜杠,其實實際的路徑為D:\test\t.txt,多的那一條斜杠是轉義用的 InputStream ...
  • 用Spring Data JPA操作資料庫 這份教程教你用Spring Data JPA從關係資料庫mysql中存儲和提取數據。總結來自https://spring.io/guides/gs/accessing-data-jpa/ 1.用Maven構建工程 添加依賴 <!-- spring boot ...
  • "裝飾器1:函數裝飾器" "裝飾器2:類裝飾器" "裝飾器3:進階" 本文是裝飾器相關內容的第二篇,關於類裝飾器。 "類裝飾器"有兩種解讀方式:用來裝飾類的裝飾器;類作為裝飾器裝飾其它東西。你如何認為取決於你,兩種說法都有出現在其它的文章中。我的文章中是將"類裝飾器"解讀為第一種方式,即裝飾類的東西 ...
  • 在Java中,介面是對類的一組需求描述,它相當於不同系統之間交互時使用的一種契約,實現了介面的類要遵從介面定義的規則。本文將詳細介紹有關介面的內容。 ...
  • 在上一章(Asp.Net Core 輕鬆學-多線程之Task快速上手)[https://www.cnblogs.com/viter/p/10201228.html]文章中,介紹了使用Task的各種常用場景,但是感覺有部分內容還沒有完善,在這裡補充一下。 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...