Articles in this section

How to render a Blazor Charts with CascadingValue?

This article explains how to render Blazor Charts with CascadingValue.

Rendering Chart with CascadingValue:

Blazor Charts provides an option to create a custom chart component that inherits from the chart component. This can be achieved by passing the value to the CascadingValue component, which wraps the component and provides the value to all components.

You need to pass the chart instance as a cascading parameter in one Razor file and access the instance in the CascadingValue component.

The code example below demonstrates how to render the chart with CascadingValue.

Index.razor:

@using Syncfusion.Blazor.Charts
<CascadingValue Value="@Chart">
  <Counter></Counter>
</CascadingValue>
@code {
  SfChart Chart;
}  

Counter.razor:

@using Syncfusion.Blazor.Charts
<SfChart @ref="@Chart">
   <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category"></ChartPrimaryXAxis>
  <ChartSeriesCollection>
      <ChartSeries DataSource="@Sales" XName="Month" YName="SalesValue" Type="ChartSeriesType.Column">
      </ChartSeries>
  </ChartSeriesCollection>
</SfChart>

@code {
  [CascadingParameter] SfChart Chart { get; set; }
  public class SalesInfo
  {
      public string Month { get; set;}
      public double SalesValue { get; set;}
  }    
  public List<SalesInfo> Sales = new List<SalesInfo>
  {
      new SalesInfo { Month = "Jan", SalesValue = 35 },
      new SalesInfo { Month = "Feb", SalesValue = 28 },
      new SalesInfo { Month = "Mar", SalesValue = 34 },
      new SalesInfo { Month = "Apr", SalesValue = 32 },
      new SalesInfo { Month = "May", SalesValue = 40 },
      new SalesInfo { Month = "Jun", SalesValue = 32 },
      new SalesInfo { Month = "Jul", SalesValue = 35 }
  };
} 

The following screenshot illustrates the output of the code snippet.

Output:

image.png

CascadingValue Chart Demo

Conclusion:

We hope you enjoyed learning how to render the chart with CascadingValue 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