{"id":2902,"date":"2024-10-22T13:20:22","date_gmt":"2024-10-22T13:20:22","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=2902"},"modified":"2024-10-22T13:55:35","modified_gmt":"2024-10-22T13:55:35","slug":"a-dougnut-chart-in-net-blazor","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/a-dougnut-chart-in-net-blazor\/","title":{"rendered":"A Dougnut Chart in .NET Blazor"},"content":{"rendered":"<p>In this blog post we are going to walk through the steps necessary to build the beautiful doughnut chart that you see at the image below:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/mindfusion.dev\/samples\/blazor\/chart\/doughnut_chart.webp\" \/><\/p>\n<p>We are going to use <a href=\"https:\/\/mindfusion.dev\/blazor-chart.html\" title=\"MindFusion Charting for Blazor\">MindFusion Charting library for Blazor<\/a> and Visual Studio as the IDE for the project. <\/p>\n<p><!--more--><\/p>\n<h2>I. Project Setup<\/h2>\n<p>We create a new Blazor Project &#8211; a Web Assembly. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/mindfusion.dev\/samples\/blazor\/chart\/blazor_web_assembly.webp\" title=\"A new Blazor WebAssembly App\" \/><\/p>\n<p>This is the easiest way to integrate the Charting library. You can also go with a Blazor Web App, the details on how to use the chart with this type of project are described in details in the <a href=\"https:\/\/mindfusion.dev\/onlinehelp\/chart.blazor\/Create_Chart_in_Web_App_Project.htm\" title=\"Using Mindfusion.Charting for Blazor in a Web App\">online documentation.<\/a><\/p>\n<p>Once you have followed the project wizard steps, you end up with a new project, named after your wish. Then you need to add the charting library. Open the NuGet package manager and search for &#8220;MindFusion Blazor Chart&#8221;. When you find the project, install it. Upgrade any other packages that prompt for update.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/mindfusion.dev\/samples\/blazor\/chart\/nuget_package.webp\" title=\"Ad MindFusion Carting for Blazor as a NuGet Package\" \/><\/p>\n<h2>II. Create the Chart<\/h2>\n<p>Open the Home.razor file and import the namespaces you are going to need:<\/p>\n<pre>\n@using System.Collections.ObjectModel\n@using System.Reflection\n\n@using Microsoft.Maui.Graphics\n\n@using MindFusion.Drawing\n@using MindFusion.Charting\n@using MindFusion.Charting.Blazor\n<\/pre>\n<p>Then we declare the &lt;PieChart&gt; blazor element, where we adjust some properties, which are exposed as attributes:<\/p>\n<pre>\n&lt;PieChart @ref=\"pieChart\"\n\t\t  Doughnut=\"true\"\n\t\t  ShowLegend=\"false\"\n\t\t  ShowDataLabels=LabelKinds.All\n\t\t  AllowRotate=\"true\"\n\t\t  StartAngle=10\n\t\t  OuterLabelPadding=20 \/&gt;\n\n<\/pre>\n<p>You can read the full list of the properties supported by the PieChart control at the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/T_MindFusion_Charting_WinForms_PieChart_Members_0.htm\" title=\"PieChart class: members list\">members list of the class<\/a> in the online documentation. The Doughnut attribute makes the pie be rendered as a doughnut. We also hid the legend, allow rotation and specify that the doughnut is going to render all types of labels &#8211; inner and outer. The last two attributes are for specifying the start angle and the padding of the outer labels.<\/p>\n<h2>III. Specifying the Data<\/h2>\n<p>Data for every type of chart is specified with the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/P_MindFusion_Charting_WinForms_PieChart_Series_0.htm\" title=\"The Series property of a Blazor Chart\">Series property.<\/a> For a pie chart the Series accepts a single object instance of a class that implements the Series interface. For other type of charts, the Series property can be of type list.<\/p>\n<pre>\n\nvar values = new List<double> { 90.01, 3.95, 2.34, 1.35, 0.81, 0.65 };\npieChart.Series = new PieSeries(\n\tvalues,\n\tnew List&lt;string&gt;()\n\t{\n\t\t\"Google\\n90.01%\", \"3.95%\", \"\", \"\", \"\", \"\"\n\t\t},\n\tnew List&lt;string&gt;()\n\t{\n\t\t\"Google has lost little\\nmarket share over\\nthe last decade\",\n\t\t\"Bing is determined to\\nincrease its user base\\nthanks to AI tools\",\n\t\t\"2.34% Yandex\",\n\t\t\"1.35% Yahoo\", \n\t\t\"0.81% Baidu\", \n\t\t\"0.65% DuckDuckGo\"\n\t}\n);\n\n<\/pre>\n<p>The PieSeries class has a constructor that accepts three lists: one for the data, one for the inner labels and one for the outer labels. If you want to miss a certain type of labels, you can provide null instead of a list. <\/p>\n<h2>IV. Styling and Appearance Properties<\/h2>\n<p>The next we are going to do for our chart is add a Title. The title is an arbitrary chunk of text and can be styled in a variety of ways using the designated properties:<\/p>\n<pre>\n\npieChart.Title = \"Search Engine Market Share\";\npieChart.TitleBrush = new SolidBrush(Colors.White);\npieChart.TitleFontSize = 18;\npieChart.TitleFontName = \"Sans-serif\";\npieChart.TitleMargin = new Margins(0, 20, 0, 0);\n\n<\/pre>\n<p>You have <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/P_MindFusion_Charting_WinForms_Chart_TitleBrush_0_0.htm\" title=\"MindFusion Charting for Blazor: TitleBrush\">TitleBrush<\/a>, <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/P_MindFusion_Charting_AxisRenderer_TitleFontSize_0.htm\" title=\"MindFusion Charting for Blazor: TitleFontSize\">TitleFontSize<\/a> and <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/P_MindFusion_Charting_Theme_TitleFontName_0.htm\" title=\"MindFusion Charting for Blazor: TitleFontName\">TitleFontName<\/a>, all of which customize the looks of the title. The <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/P_MindFusion_Charting_WinForms_Chart_TitleMargin_0.htm\" title=\"MindFusion Charting for Blazor: TitleMargin\">TitleMargin<\/a> property sets the space around the title label. The same set of properties is available for the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/P_MindFusion_Charting_WinForms_Chart_Subtitle_0.htm\" title=\"MindFusion Charting for Blazor: Subtitle\">Subtitle<\/a> , if you want to add one.<\/p>\n<p>We then specify a transparent <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/P_MindFusion_Charting_Plot_HighlightStroke_0.htm\" title=\"MindFusion Charting for Blazor: HighlightStroke\">HighlightStroke<\/a> for the chart. The <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/P_MindFusion_Charting_Theme_HighlightStroke_0.htm\" title=\"MindFusion Charting for Blazor: HighlightStroke\">HighlightStroke<\/a> is used to paint the outline of the element over which the mouse hovers. <\/p>\n<p>We would like to paint white labels on a dark background. FOr that purpose we set the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/P_MindFusion_Charting_Blazor_Dashboard_BackgroundColor_0.htm\" title=\"MindFusion Charting for Blazor: BackgroundColor\">BackgroundColor<\/a> to a deep blue and we use the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/P_MindFusion_Charting_Theme_DataLabelsBrush_0.htm\" title=\"MindFusion Charting for Blazor: DataLabelsBrush\">DataLabelsBrush<\/a> property to set the colour of the labels &#8211; both inner and outer &#8211; to white. Here is the code: <\/p>\n<pre>\n\npieChart.Theme.HighlightStroke = new SolidBrush(Colors.Transparent);\npieChart.Theme.DataLabelsBrush = new SolidBrush(Colors.White);\npieChart.Theme.DataLabelsFontSize = 11;\npieChart.Theme.DataLabelsFontName = \"Sans-serif\";\npieChart.BackgroundColor = Color.FromRgb(20, 72, 140);\n\t\n<\/pre>\n<p>Last but not least comes the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/T_MindFusion_Charting_SeriesStyle.htm\" title=\"MindFusion Charting for Blazor: SeriesStyle\">SeriesStyle<\/a> The <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/T_MindFusion_Charting_SeriesStyle.htm\" title=\"MindFusion Charting for Blazor: SeriesStyle\">SeriesStyle<\/a> property specify how the visual elements that represent the chart series are painted and outlined. There are several predefined <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/T_MindFusion_Charting_SeriesStyle.htm\" title=\"MindFusion Charting for Blazor: SeriesStyle\">SeriesStyle<\/a> instances, which you can check in <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/\" title=\"Charting for Blazor: online documentation\">the online documentation.<\/a> In our sample we use the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.blazor\/T_MindFusion_Charting_PerElementSeriesStyle.htm\" title=\"MindFusion Charting for Blazor: PerElementSeriesStyle\">PerElementSeriesStyle<\/a> class, which tells the chart that each brush\/stroke from the provided lists is for just one element to be painted with. If the count of the brushes\/strokes is less than the element, the controls cycles through them again.<\/p>\n<pre>\n\npieChart.Plot.SeriesStyle = new PerElementSeriesStyle()\n{\n\tFills = new List<List<MindFusion.Drawing.Brush>>()\n\t{\n\t\tnew List&lt;MindFusion.Drawing.Brush&gt;()\n\t\t{\n\t\t\tnew SolidBrush(Color.FromRgb(52, 132, 244)),\n\t\t\tnew SolidBrush(Color.FromRgb(7, 215, 222)),\n\t\t\tnew SolidBrush(Color.FromRgb(224, 36, 31)),\n\t\t\tnew SolidBrush(Color.FromRgb(95, 1, 209)),\n\t\t\tnew SolidBrush(Color.FromRgb(255, 151, 0)),\n\t\t\tnew SolidBrush(Color.FromRgb(223, 110, 79))\n\t\t}\n\t},\n\tStrokes = new List&lt;List&lt;MindFusion.Drawing.Brush&gt;&gt;()\n\t{\n\t\tnew List&lt;MindFusion.Drawing.Brush&gt;()\n\t\t{\n\t\t\tnew SolidBrush(Colors.White)\n\t\t}\n\t}\n};\n\n<\/pre>\n<p>And with that our chart is ready. Congratulations, you&#8217;ve just built a beautiful doughnut chart in .NET Blazor. Use the link below to download the full source code of the project:<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/samples\/blazor\/chart\/BlazorDoughnutChart.zip\" title=\"Download Charting for Blazor Sample\">Download Charting for .NET Blazor Sample<\/a><\/p>\n<p>You can write at <a href=\"https:\/\/mindfusion.dev\/Forum\/YaBB.pl?board=chart_blazor\" title=\"MindFusion Online Forum\" >MindFusion discussion board<\/a> for technical inquiries. <\/p>\n<p><i>About Charting for Blazor:<\/i> <a href=\"https:\/\/mindfusion.dev\/blazor-chart.html\" title=\"MindFusion Charting for Blazor\">MindFusion Charting library for Blazor<\/a> is a reusable web component that allows you to create any type of chart and dashboard fast and easy. Built around a flexible and versatile API, the component allows creating standard charts as well as assembling dashboards and charts from separate components: axes, plots, labels and renderers of different series. The data is provided through Series-derived classes and you have the option to create a custom Series-based class to handle your specific type of data and label requirements. The control offers a huge variety of appearance options. Themes allow the re-use of appearance settings.<br \/>\nCharting for Blazor is a commercial product. The trial version has no time limits. Licenses are sold per developer seat. No royalties are charged. Small companies, freelancers, academic institutions and others are eligible for a discount &#8211; <a href=\"https:\/\/mindfusion.dev\/discounts.html\" title=\"Discounts\">details here.<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post we are going to walk through the steps necessary to build the beautiful doughnut chart that you see at the image below: We are going to use MindFusion Charting library for Blazor and Visual Studio as &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/a-dougnut-chart-in-net-blazor\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[725,61],"tags":[726,70,727,78],"class_list":["post-2902","post","type-post","status-publish","format-standard","hentry","category-blazor","category-charting-2","tag-blazor","tag-chart","tag-data-viz","tag-pie-chart"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-KO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=2902"}],"version-history":[{"count":9,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2902\/revisions"}],"predecessor-version":[{"id":2912,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2902\/revisions\/2912"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=2902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=2902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=2902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}