项目作者: muqeet-khan

项目描述 :
Simple reusable Blazor component library
高级语言: C#
项目地址: git://github.com/muqeet-khan/BlazorComponents.git
创建时间: 2018-03-28T21:42:53Z
项目社区:https://github.com/muqeet-khan/BlazorComponents

开源协议:

下载


Simple Components for Blazor Projects

  1. Note: Just as Blazor, this repo is also experimental.

If you like the idea of this repo leave your feedback as an issue or star the repo or let me know on @ma_khan

Currently, starting with a simple ChartJS implementation.

Prerequisites

Don’t know what Blazor is? Read here

Complete all Blazor dependencies.

  1. Visual Studio 2017 (15.8 or later)
  2. DotNetCore 2.1 (2.1.402 or later).

Installation

NuGet NuGet Pre Release

To Install

  1. Install-Package BlazorComponents

or

  1. dotnet add package BlazorComponents

Usage

  1. In cshtml file add this:
  1. <div class="row">
  2. <button class="btn btn-primary" onclick="@UpdateChart">Update Chart </button>
  3. </div>
  4. <ChartJsBarChart ref="barChartJs" Chart="@blazorBarChartJS" Width="600" Height="300" ></ChartJsBarChart>
  1. @functions {
  2. public ChartJSBarChart blazorBarChartJS { get; set; } = new ChartJSBarChart();
  3. ChartJsBarChart barChartJs;
  4. protected override void OnInit()
  5. {
  6. blazorBarChartJS = new ChartJSBarChart()
  7. {
  8. ChartType = "bar",
  9. CanvasId = "myFirstBarChart",
  10. Options = new ChartJsOptions()
  11. {
  12. Text = "Sample chart from Blazor",
  13. BorderWidth = 1,
  14. Display = true,
  15. // Title of the chart
  16. Title = new ChartJsTitle()
  17. {
  18. Display = true, // Set to false for hiding the title
  19. Text = "Title",
  20. FontSize = 40
  21. },
  22. Layout = new ChartJsLayout()
  23. {
  24. // add some space to the chart for better rendering
  25. Padding = new ChartJsPadding()
  26. {
  27. Bottom = 0,
  28. Left = 0,
  29. Right = 0,
  30. Top = 50
  31. }
  32. },
  33. // move the legend
  34. Legend = new ChartJsLegend()
  35. {
  36. Position = "top",
  37. Display = true // set to false for hiding legend
  38. },
  39. Scales = new ChartJsScale()
  40. {
  41. XAxes = new List<ChartJsXAxes>()
  42. {
  43. new ChartJsXAxes()
  44. {
  45. Ticks = new ChartJsTicks()
  46. {
  47. BeginAtZero = true,
  48. FontSize = 20
  49. },
  50. Position = "top"
  51. }
  52. },
  53. YAxes = new List<ChartJsYAxes>()
  54. {
  55. new ChartJsYAxes()
  56. {
  57. Ticks = new ChartJsTicks()
  58. {
  59. BeginAtZero = true,
  60. FontSize = 20,
  61. Max = 50 // set a maxmimum value for this axis
  62. }
  63. }
  64. }
  65. },
  66. Plugins = new ChartJsPlugins()
  67. {
  68. // if you have enabled the plugin you can use these parameters, otherwise it will be ignored
  69. Datalabels = new ChartJsDataLabels()
  70. {
  71. Align = "end",
  72. Anchor = "end",
  73. Color = "black",
  74. Display = true,
  75. Font = new ChartJsDataLabelsFont()
  76. {
  77. Size = 20
  78. }
  79. }
  80. }
  81. },
  82. Data = new ChartJsBarData()
  83. {
  84. Labels = new List<string>() { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" },
  85. Datasets = new List<ChartJsBarDataset>()
  86. {
  87. new ChartJsBarDataset()
  88. {
  89. Label = "# of Votes from blazor",
  90. BackgroundColor = new List<string>(){"#cc65fe" },
  91. BorderColor = "#cc65fe",
  92. PointHoverRadius = 2,
  93. Data = new List<double>(){ 19.187,12.2253,5.5,3,3,2}
  94. },
  95. new ChartJsBarDataset()
  96. {
  97. Label = "# of Likes from blazor",
  98. BackgroundColor = new List<string>() {
  99. "#a4cef0",
  100. "#3498db",
  101. "#95a5a6",
  102. "#9b59b6",
  103. "#f1c40f",
  104. "#e74c3c",
  105. "#34495e" },
  106. BorderColor = "#36a2eb",
  107. PointHoverRadius = 2,
  108. Data = new List<int>(){ 30,10,15,13,13,12}.Select<int,double>(i=> i).ToList()
  109. }
  110. }
  111. }
  112. };
  113. }
  114. public async Task<bool> UpdateChart()
  115. {
  116. //Update existing dataset
  117. blazorBarChartJS.Data.Labels.Add($"New{DateTime.Now.Second}");
  118. var firstDataSet = blazorBarChartJS.Data.Datasets[0];
  119. firstDataSet.Data.Add(DateTime.Now.Second);
  120. //Add new dataset
  121. //blazorLineChartJS.Data.Datasets.Add(new ChartJsLineDataset()
  122. //{
  123. // BackgroundColor = "#cc65fe",
  124. // BorderColor = "#cc65fe",
  125. // Label = "# of Votes from blazor 1",
  126. // Data = new List<int> {20,21,12,3,4,4},
  127. // Fill = true,
  128. // BorderWidth = 2,
  129. // PointRadius = 3,
  130. // PointBorderWidth = 1
  131. //});
  132. return true;
  133. }
  134. }
  1. In index.html add:
  1. <script src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
  2. <script type="blazor-boot">
  3. </script>

2.1. For using the data label plugin add this, too:

  1. <script src="//cdn.jsdelivr.net/npm/chartjs-plugin-datalabels"></script>
  1. In _ViewImports.cshtml add:
  1. @using BlazorComponents.ChartJS
  2. @using BlazorComponents.Shared
  3. @addTagHelper *,BlazorComponents

Sample Output

Bar Chart Example:
Barchart