Search results

Woking with Data

The Chart uses EjsDataManager, which supports both RESTful JSON data services binding and IEnumerable binding. The DataSource value can be assigned either with the property values from EjsDataManager or list of business objects. It supports the following kinds of data binding method:

  • List binding
  • Remote data

List binding

To bind list to the chart, you can assign a IEnumerable object to the DataSource property. The list data source can also be provided as an instance of EjsDataManager or by using EjsDataManager component. Now map the fields in list to XName and YName properties.

local-data.razor
@using Syncfusion.EJ2.Blazor.Charts

<EjsChart Title="Inflation - Consumer Price" Width="60%">
   <ChartPrimaryXAxis IntervalType="IntervalType.Years" LabelFormat="y" ValueType="Syncfusion.EJ2.Blazor.Charts.ValueType.DateTime"></ChartPrimaryXAxis>
        <ChartSeriesCollection>
            <ChartSeries DataSource="@ConsumerReports" XName="XValue" YName="YValue" Type="ChartSeriesType.Line">
            </ChartSeries>                                                           
        </ChartSeriesCollection>
</EjsChart>    
   
@code{
     public class ChartData
        {
           public DateTime XValue;
            public double YValue;
        }

        public List<ChartData> ConsumerReports =  new List<ChartData> 
		{
            new ChartData { XValue = new DateTime(2005, 01, 01), YValue = 21 },
            new ChartData { XValue = new DateTime(2006, 01, 01), YValue = 24 },
            new ChartData { XValue = new DateTime(2007, 01, 01), YValue = 36 },
            new ChartData { XValue = new DateTime(2008, 01, 01), YValue = 38 },
            new ChartData { XValue = new DateTime(2009, 01, 01), YValue = 54 },
            new ChartData { XValue = new DateTime(2010, 01, 01), YValue = 57 },
            new ChartData { XValue = new DateTime(2011, 01, 01), YValue = 70 },
        };
}

Local Data

Remote Data

To bind remote data to chart component, assign service data as an instance of EjsDataManager to the DataSource property or by using EjsDataManager component. To interact with remote data source, provide the endpoint Url.

remote-data.razor
@using Syncfusion.EJ2.Blazor.Data
@using Syncfusion.EJ2.Blazor.Charts

<EjsChart>
    <EjsDataManager Url="https://services.odata.org/V4/Northwind/Northwind.svc/Orders" Adaptor="Adaptors.ODataV4Adaptor"></EjsDataManager>

    <ChartPrimaryXAxis Title="Orders" ValueType="Syncfusion.EJ2.Blazor.Charts.ValueType.Category"
                       RangePadding="ChartRangePadding.Additional"></ChartPrimaryXAxis>

    <ChartSeriesCollection>
        <ChartSeries XName="OrderID" YName="Freight" Type="ChartSeriesType.Column"></ChartSeries>
    </ChartSeriesCollection>
</EjsChart>

Binding with OData services

OData is a standardized protocol for creating and consuming data. You can retrieve data from OData service using the EjsDataManager. Refer to the following code example for remote Data binding using OData service.

Binding with OData v4 services

The ODataV4 is an improved version of OData protocols, and the EjsDataManager can also retrieve and consume OData v4 services. For more details on OData v4 services, refer to the OData documentation. To bind OData v4 service, use the ODataV4Adaptor.

Remote Data

Web API

You can use WebApiAdaptor to bind chart with Web API created using OData endpoint.

@using Syncfusion.EJ2.Blazor.Data
@using Syncfusion.EJ2.Blazor.Charts

<EjsChart>
    <EjsDataManager Url="https://ej2services.syncfusion.com/production/web-services/api/Orders" Adaptor="Adaptors.WebApiAdaptor"></EjsDataManager>

    <ChartPrimaryXAxis Title="Orders" ValueType="Syncfusion.EJ2.Blazor.Charts.ValueType.Category"
                       RangePadding="ChartRangePadding.Additional"></ChartPrimaryXAxis>

    <ChartSeriesCollection>
        <ChartSeries XName="OrderID" YName="Freight" Type="ChartSeriesType.Column"></ChartSeries>
    </ChartSeriesCollection>
</EjsChart>

Web API

Enable EjsDataManager after initial rendering

It is possible to render the datasource in Chart after initial rendering. This can be achieved by conditionally enabling the EjsDataManager component after Chart rendering.

The following sample code demonstrates enabling data manager condition in the Chart on button click,

@using Syncfusion.EJ2.Blazor.Buttons
@using Syncfusion.EJ2.Blazor.Data
@using Syncfusion.EJ2.Blazor.Charts

    <EjsChart>
        @if (IsInitialRender)
        {
            <EjsDataManager Url="https://ej2services.syncfusion.com/production/web-services/api/Orders" Adaptor="Adaptors.WebApiAdaptor">
            </EjsDataManager>
        }

        <ChartPrimaryXAxis Title="Orders" ValueType="Syncfusion.EJ2.Blazor.Charts.ValueType.Category"
                           RangePadding="ChartRangePadding.Additional"></ChartPrimaryXAxis>

        <ChartSeriesCollection>
            <ChartSeries XName="OrderID" YName="Freight" Type="ChartSeriesType.Column"></ChartSeries>
        </ChartSeriesCollection>
    </EjsChart>

<EjsButton OnClick="Enable" CssClass="e-primary" IsPrimary="true" Content="Enable data manager"></EjsButton>

@code{
    public bool IsInitialRender = false;

    public void Enable()
    {
        // Enabling condition to render the data manager
        this.IsInitialRender = true;
    }
}

Sending additional parameters to the server

To add a custom parameter to the data request. Assign the Query object with additional parameters to the Chart’s Query property.

The following sample code demonstrates sending parameters using the Query property in the series,

@using Syncfusion.EJ2.Blazor.Data
@using Syncfusion.EJ2.Blazor.Charts

<EjsChart>
    <EjsDataManager Url="https://mvc.syncfusion.com/Services/Northwnd.svc/Tasks/">
    </EjsDataManager>

    <ChartPrimaryXAxis Title="Assignee" ValueType="Syncfusion.EJ2.Blazor.Charts.ValueType.Category"
                       RangePadding="ChartRangePadding.Additional"></ChartPrimaryXAxis>

    <ChartSeriesCollection>
        <ChartSeries Query="new ej.data.Query().take(10).where('Estimate', 'lessThan', 3, false)" XName="Assignee" YName="Estimate" Type="ChartSeriesType.Column"></ChartSeries>
    </ChartSeriesCollection>
</EjsChart>

Change Query parameter value dynamically

It is possible to dynamically modify Chart’s Query property value.

The following sample code demonstrates achieving this,

@using Syncfusion.EJ2.Blazor.Buttons
@using Syncfusion.EJ2.Blazor.Data
@using Syncfusion.EJ2.Blazor.Charts

<EjsButton Content="Modify query data" OnClick="BtnClick"></EjsButton>
<EjsChart>
    <EjsDataManager Url="https://mvc.syncfusion.com/Services/Northwnd.svc/Tasks/">
    </EjsDataManager>

    <ChartPrimaryXAxis Title="Assignee" ValueType="Syncfusion.EJ2.Blazor.Charts.ValueType.Category"
                       RangePadding="ChartRangePadding.Additional"></ChartPrimaryXAxis>

    <ChartSeriesCollection>
        <ChartSeries Query="@QueryData" XName="Assignee" YName="Estimate" Type="ChartSeriesType.Column"></ChartSeries>
    </ChartSeriesCollection>
</EjsChart>

@code{
    private string QueryData = "new ej.data.Query().take(10).where('Estimate', 'lessThan', 3, false)";
    private string UpdatedQueryData = "new ej.data.Query().take(5).where('Estimate', 'lessThan', 3, false)";

    public void BtnClick(UIMouseEventArgs args)
    {
        QueryData = UpdatedQueryData;
    }
}

Before modify:

Before Modify query dynamically

After modified:

After Modify query dynamically

Empty points

The Data points that contains NaN value are considered as empty points. By using EmptyPointSettings property in series, you can customize the empty point. Default Mode of the empty point is Gap.

empty-points.razor
@using Syncfusion.EJ2.Blazor.Charts

<EjsChart Title="Inflation - Consumer Price" Width="60%">
    <ChartPrimaryXAxis ValueType="Syncfusion.EJ2.Blazor.Charts.ValueType.Category">
    </ChartPrimaryXAxis>

    <ChartSeriesCollection>
        <ChartSeries DataSource="@ConsumerReports" XName="XValue" YName="YValue" Type="ChartSeriesType.Column">
            <ChartEmptyPointSettings Fill="blue" Mode="@mode"></ChartEmptyPointSettings>
        </ChartSeries>
    </ChartSeriesCollection>
</EjsChart>

@code{
    public class ChartData
    {
        public String XValue;
        public double YValue;
    }
        public string mode = "Average";
        public List<ChartData> ConsumerReports = new List<ChartData> {
            new ChartData { XValue = "Rice", YValue = 80 },
            new ChartData { XValue = "Wheat", YValue = Double.NaN },
            new ChartData { XValue = "Oil", YValue = 70 },
            new ChartData { XValue = "Corn", YValue = 60 },
            new ChartData { XValue = "Gram", YValue = Double.NaN },
            new ChartData { XValue = "Milk", YValue = 70 },
            new ChartData { XValue = "Peas", YValue = 80 },
            new ChartData { XValue = "Fruit", YValue = 60 },
            new ChartData { XValue = "Butter", YValue = Double.NaN },
        };
}

Customizing empty point

Specific color for empty point can be set by Fill property in EmptyPointSettings. Border for a empty point can be set by Border property.

custom-emptypoint.razor
@using Syncfusion.EJ2.Blazor.Charts

<EjsChart> 
     <ChartPrimaryXAxis ValueType="Syncfusion.EJ2.Blazor.Charts.ValueType.Category">
     </ChartPrimaryXAxis>

     <ChartSeriesCollection>
          <ChartSeries DataSource="@SalesReports" XName="Month" YName="Sales" Type="ChartSeriesType.Column">
               <ChartEmptyPointSettings Fill="blue" Mode="@Mode"></ChartEmptyPointSettings>
          </ChartSeries>  			 
     </ChartSeriesCollection>
</EjsChart>

@code{
        public class ChartData
        {
            public string Month;
            public Nullable<double> Sales;
        }
        public string Mode = "Average";
        public List<ChartData> SalesReports = new List<ChartData>
            {
               new ChartData{ Month="Jan", Sales= 35 },
               new ChartData{ Month= "Feb", Sales= 28 },
               new ChartData{ Month="Mar", Sales=null },
               new ChartData{ Month="Apr", Sales=32 },
               new ChartData{ Month="May", Sales=40 },
               new ChartData{ Month= "Jun", Sales=32 },
               new ChartData{ Month="Jul", Sales=35 },
               new ChartData{ Month="Aug", Sales=null },
               new ChartData{ Month="Sep", Sales=38 },
               new ChartData{ Month="Oct", Sales=30 },
               new ChartData{ Month="Nov", Sales=25 },
               new ChartData{ Month= "Dec", Sales=32 }
            };
}

Empty points

See Also