Do charts support dynamic objects

Hello,

Do charts support binding to dynamic type objects? Specifically, is it possible to bind to objects inherited from "DynamicObject". If yes, can you please share an example with Accumulation Chart and Regular Chart?

Thanks,
Mayur

6 Replies 1 reply marked as answer

SM Srihari Muthukaruppan Syncfusion Team June 10, 2021 10:11 AM UTC

Hi Mayur, 
 
Yes chart support binding to dynamic type objects. Based on your request, we have prepared a sample with dynamic type objects. Please find the sample, code snippet and screenshot below. 
 
 
Code Snippet: 
// add your additional code here 
 
@using Syncfusion.Blazor.Charts 
@using Syncfusion.Blazor 
@using Syncfusion.Blazor.Grids 
@using System.Dynamic 
@inject NavigationManager NavigationManager 

<div class="control-section"> 
    <SfChart Title="Inflation - Consumer Price" > 
        <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.DateTime"> 
            <ChartAxisMajorGridLines Width="0"></ChartAxisMajorGridLines> 
        </ChartPrimaryXAxis> 
        // add your additional code here 
        <ChartSeriesCollection> 
            <ChartSeries DataSource="@Orders" Name="Germany" XName="OrderDate" Width="2" 
                         Opacity="1" YName="Freight" Type="ChartSeriesType.Line"> 
            </ChartSeries> 
        </ChartSeriesCollection> 
    </SfChart> 
</div> 

@code { 
    public List<DynamicDictionary> Orders = new List<DynamicDictionary>() { }; 
    protected override void OnInitialized() 
    { 
        Orders = Enumerable.Range(1, 1075).Select((x) => 
        { 
            dynamic d = new DynamicDictionary(); 
            d.Freight = (new double[] { 2, 1, 4, 5, 3 })[new Random().Next(5)] * x; 
            d.OrderDate = DateTime.Now.AddDays(-x); 
            return d; 
        }).Cast<DynamicDictionary>().ToList<DynamicDictionary>(); 
    } 
    public class DynamicDictionary : DynamicObject 
    { 
        Dictionary<string, object> dictionary = new Dictionary<string, object>(); 
 
        public override bool TryGetMember(GetMemberBinder binder, out object result) 
        { 
            string name = binder.Name; 
            return dictionary.TryGetValue(name, out result); 
        } 
        public override bool TrySetMember(SetMemberBinder binder, object value) 
        { 
            dictionary[binder.Name] = value; 
            return true; 
        } 
 
        public override System.Collections.Generic.IEnumerable<string> GetDynamicMemberNames() 
        { 
            return this.dictionary?.Keys; 
        } 
    } 
} 
 
// add your additional code here 
 
 

Screenshot: 
 
 
Let us know if you have any concerns. 

Regards, 
Srihari M 



MA Mayur June 10, 2021 11:43 AM UTC

Hello,

Thanks for writing back. However, the example fails if Accumulation or TreeMap is used. AccumulationChart crashes with NullPointerException and TreeMap does not render:

@page "/"
@using Syncfusion.Blazor.Charts
@using Syncfusion.Blazor
@using Syncfusion.Blazor.TreeMap
@using Syncfusion.Blazor.Grids
@using System.Dynamic
@inject NavigationManager NavigationManager

<div class="control-section">
    <SfAccumulationChart>
        <AccumulationChartSeriesCollection>
            <AccumulationChartSeries DataSource="@Orders" XName="OrderDate" YName="Freight">
            </AccumulationChartSeries>
        </AccumulationChartSeriesCollection>
    </SfAccumulationChart>
    <SfTreeMap DataSource="@Orders"
               WeightValuePath="Freight"
               Background="transparent"
               EnableGroupingSeparator="true">
        <TreeMapTitleSettings Text="Total" Alignment="Syncfusion.Blazor.TreeMap.Alignment.Near">

        </TreeMapTitleSettings>
        <TreeMapLevels>
                <TreeMapLevel GroupPath="OrderDate">
                    <TreeMapLevelBorder Color="black" Width="0.5">
                    </TreeMapLevelBorder>
                </TreeMapLevel>
            </TreeMapLevels>
        <TreeMapLeafItemSettings LabelPath="OrderDate" Opacity="0.5" Gap="20"
                                 InterSectAction="Syncfusion.Blazor.TreeMap.LabelAlignment.Trim">
            <TreeMapLeafLabelStyle Color="#fdfdfd" />
        </TreeMapLeafItemSettings>
    </SfTreeMap>
    <SfChart Title="Inflation - Consumer Price">
        <ChartArea><ChartAreaBorder Width="0"></ChartAreaBorder></ChartArea>
        <ChartPrimaryXAxis ValueType="Syncfusion.Blazor.Charts.ValueType.Category">
            <ChartAxisMajorGridLines Width="0"></ChartAxisMajorGridLines>
        </ChartPrimaryXAxis>
        <ChartPrimaryYAxis LabelFormat="{value}%" RangePadding="ChartRangePadding.None">
            <ChartAxisLineStyle Width="0"></ChartAxisLineStyle>
            <ChartAxisMajorTickLines Width="0"></ChartAxisMajorTickLines>
        </ChartPrimaryYAxis>
        <ChartTooltipSettings Enable="true"></ChartTooltipSettings>
        <ChartLegendSettings Visible="true" Alignment="Syncfusion.Blazor.Charts.Alignment.Center" Position="Syncfusion.Blazor.Charts.LegendPosition.Top"></ChartLegendSettings>
        <ChartSeriesCollection>
            <ChartSeries DataSource="@Orders" Name="Germany" XName="OrderDate" Width="2"
                         Opacity="1" YName="Freight" Type="ChartSeriesType.Line">
                <ChartMarker Visible="true" Width="10" Height="10">
                </ChartMarker>
            </ChartSeries>
        </ChartSeriesCollection>
    </SfChart>
</div>

@code {
    public List<DynamicDictionary> Orders = new List<DynamicDictionary>() { };
    protected override void OnInitialized()
    {
        Orders = Enumerable.Range(1, 5).Select((x) =>
        {
            dynamic d = new DynamicDictionary();
            d.Freight = (new double[] { 2, 1, 4, 5, 3 })[new Random().Next(5)] * x;
            d.OrderDate = DateTime.Now.AddDays(-x).ToString();
            return d;
        }).Cast<DynamicDictionary>().ToList();
    }
    public class DynamicDictionary : DynamicObject
    {
        Dictionary<string, object> dictionary = new Dictionary<string, object>();

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            string name = binder.Name;
            return dictionary.TryGetValue(name, out result);
        }
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            dictionary[binder.Name] = value;
            return true;
        }

        public override System.Collections.Generic.IEnumerable<string> GetDynamicMemberNames()
        {
            return this.dictionary?.Keys;
        }
    }
}




DG Durga Gopalakrishnan Syncfusion Team June 13, 2021 03:25 PM UTC

Hi Mayur, 

# 1 : AccumulationChart crashes with NullPointerException 

We have considered this as a bug and logged a defect report. This fix will be available in our upcoming weekly patch release which is scheduled to be rolled out on 22nd June 2021. We appreciate your patience until then. You can keep track of the bug from the below feedback link. 
 
 
If you have any more specification/precise replication procedure or a scenario to be tested, you can add it as a comment in the portal.  
 
# 2 : TreeMap does not render 
 
We have analyzed the reported query in Treemap component and inform there is no direct support to achieve this, still we are trying to achieve your query with work around and update you further details on 15th June 2021. 
 
Please revert us if you have any concerns. 
 
Regards,  
Durga G


DV Duraimurugan Vedagiri Syncfusion Team June 15, 2021 01:31 PM UTC

Hi Mayur,

We have considered your requirement as a feature request in Treemap component and it will be included in any of our upcoming release. You can keep track of the status using the feedback portal link below.

https://www.syncfusion.com/feedback/26198/need-to-provide-the-support-for-dynamicobject-datasource-type

Regards,
Durai Murugan V


DG Durga Gopalakrishnan Syncfusion Team June 22, 2021 02:17 PM UTC

Hi Mayur, 

We regret for the inconvenience. We will include this fix in our upcoming Volume 2 Main Release which is expected to be rolled out at end of June 2021. We appreciate your patience until then. 

Regards,  
Durga G


SM Srihari Muthukaruppan Syncfusion Team July 2, 2021 10:35 AM UTC

Hi Mayur, 
 
We are glad to announce that our Essential Studio 2021 Volume 2 release v19.2.0.44  is rolled out, we have added the fix for the reported scenario. And it is available for download under the following link. 
 
 
We thank you for your support and appreciate your patience in waiting for this release. Please get in touch with us if you would require any further assistance. 
 
Regards, 
Srihari 


Marked as answer
Loader.
Up arrow icon