Articles in this section

How to display chips with condition-based content and colors in Blazor Charts?

This article explains how to display chips with condition-based content and colors using annotations in Blazor Charts.

Customizing annotations as chips

Blazor Charts provides an option to render ChartAnnotations as chips based on y values.

This can be achieved by calculating the annotation x position and assigning the required content and color using the OnPointRender event. Then these values are updated by triggering the StateHasChanged method in the chart Loaded event.

The code example below demonstrates how to render annotations as chips.

Index.razor:

@using Syncfusion.Blazor.Charts
<SfChart>
   <ChartPrimaryXAxis ValueType="@Syncfusion.Blazor.Charts.ValueType.Category">
   </ChartPrimaryXAxis>
   <ChartEvents OnPointRender="PointRenderEvent" Loaded="ChartLoaded"></ChartEvents>
   <ChartAnnotations>
       @foreach (var annotation in annotations)
       {
           <ChartAnnotation X="@annotation.Year" Y="130" Region="Regions.Series" CoordinateUnits="Units.Point">
               <ContentTemplate>
                   <div style="color: @annotation.Color; font-size: medium; font-style: italic">@annotation.Content</div>
               </ContentTemplate>
           </ChartAnnotation>
       }
   </ChartAnnotations>
   <ChartSeriesCollection>
       <ChartSeries DataSource="@ChartPoints" Fill="#1e90ff" Opacity="0.2" XName="Year" YName="USA_Total" GroupName="USA" ColumnWidth="0.5" ColumnSpacing="0.1" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Column">
       </ChartSeries>
       <ChartSeries DataSource="@ChartPoints" Fill="#1e90ff"  XName="Year" YName="USA_Gold" GroupName="USA" ColumnWidth="0.5" ColumnSpacing="0.1" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Column">
       </ChartSeries>
       <ChartSeries DataSource="@ChartPoints" Fill="#1c2841" Opacity="0.2" XName="Year" YName="UK_Total" GroupName="UK" ColumnWidth="0.5" ColumnSpacing="0.1" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Column">
       </ChartSeries>
       <ChartSeries DataSource="@ChartPoints" Fill="#1c2841"  XName="Year" YName="UK_Gold" GroupName="UK" ColumnWidth="0.5" ColumnSpacing="0.1" Type="Syncfusion.Blazor.Charts.ChartSeriesType.Column">
       </ChartSeries>
   </ChartSeriesCollection>
</SfChart>

@code {
   List<AnnotationDetails> annotations { get; set; } = new List<AnnotationDetails>();
   public class AnnotationDetails
   {
       public string Year { get; set; }
       public string Content { get; set; }
       public string Color { get; set; }
   }
   public class ColumnData
   {
       public string Year { get; set; }
       public double USA_Total { get; set; }
       public double USA_Gold { get; set; }
       public double UK_Total { get; set; }
       public double UK_Gold { get; set; }
   }
   public List<ColumnData> ChartPoints { get; set; } = new List<ColumnData>
   {
       new ColumnData { Year = "2012", USA_Total = 104, USA_Gold = 46, UK_Total = 65, UK_Gold = 29},
       new ColumnData { Year = "2016", USA_Total = 115, USA_Gold = 46, UK_Total = 67, UK_Gold = 27},
       new ColumnData { Year = "2020", USA_Total = 100, USA_Gold = 100, UK_Total = 100, UK_Gold = 100},
   };
   Dictionary<string, double> SumDetails { get; set; } = new Dictionary<string, double>();
   public void PointRenderEvent(PointRenderEventArgs args)
   {
       if (!SumDetails.ContainsKey(args.Series.GroupName + args.Point.Index))
       {
           SumDetails.Add(args.Series.GroupName + args.Point.Index, args.Point.YValue);
       }
       else
       {
           SumDetails[args.Series.GroupName + args.Point.Index] += args.Point.YValue;
       }
       if (annotations.Find(x => x.Year == args.Point.X.ToString()) == null)
       {
           annotations.Add(new AnnotationDetails()
               {
                   Year = args.Point.X.ToString(),
                   Color = SumDetails[args.Series.GroupName + args.Point.Index] >= 100 ? "green" : "red",
                   Content = SumDetails[args.Series.GroupName + args.Point.Index] >= 100 ? "Acheived" : "Not Acheived"
               });
       }
       else
       {
           annotations.Find(x => x.Year == args.Point.X.ToString()).Color = SumDetails[args.Series.GroupName + args.Point.Index] >= 100 ? "green" : "red";
           annotations.Find(x => x.Year == args.Point.X.ToString()).Content = SumDetails[args.Series.GroupName + args.Point.Index] >= 100 ? "Acheived" : "Not Acheived";
       }
   }
   void ChartLoaded(Syncfusion.Blazor.Charts.LoadedEventArgs args)
   {
       StateHasChanged();
   }
} 

The following screenshot illustrates the output of the code snippet.

Output:

image.png

Live Sample for Annotation Chip

Conclusion:

We hope you enjoyed learning how to display chips with condition-based content and colors in the Blazor Chart component.

You can refer to our Blazor Chart feature tour page to learn about its other groundbreaking features, documentation, and how to quickly get started with configuration specifications. You can also explore our Blazor Chart example to understand how to create and manipulate data.

For current customers, our Blazor components are available on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to evaluate our Blazor Charts and other Blazor components.

If you have any questions or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied