Declined
Last Updated: 21 Nov 2023 20:05 by ADMIN
Haribabu
Created on: 17 Nov 2023 09:39
Category: UI for Blazor
Type: Bug Report
0
Telerik chart theme

Hi Support Team,

in our project we are using Telerik Chart and has required to update theme of chart from dark to light and light to dark.

telerik chart in child razor page and dark/light button in mainlayout.razor file but telerik chart updating theme when click on button exist telerik chart razor page. as below link  it is an existing issue. 

https://feedback.telerik.com/blazor/1496389-using-custom-themes-changing-at-runtime-does-not-change-the-chart-component-style

please advice how to resolve the issue.

3 comments
ADMIN
Hristian Stefanov
Posted on: 21 Nov 2023 20:05

Hi Haribabu,

Thank you for sharing your code, which has been immensely helpful in gaining a comprehensive understanding of the scenario and pinpointing the root cause of the issue. Allow me to share my observations and propose a solution.

The challenge at hand stems from the Chart within the child component not automatically receiving notifications for theme changes. Manual intervention is required to ensure the Chart is refreshed accordingly. To address this and attain the desired outcome, the most straightforward approach is to introduce a service responsible for notifying the Chart of any theme changes and triggering the necessary refresh.

For your convenience, I've prepared a runnable project enclosed in the attached "chart-theme-change.zip". Please take the time to explore and test it to witness the desired result.

Should you encounter any challenges with the provided sample, rest assured that I stand ready to extend my assistance.

Regards,
Hristian Stefanov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!
Attached Files:
Haribabu
Posted on: 20 Nov 2023 16:40

Hi Hristian Stefanov,

i am using following code to using in my project

 

Child Component which is rendering telerik chart

 <TelerikChart @ref="@ChartRef" Transitions="false">
                <ChartSeriesItems>
                    @*configuring the chart data , color and field *@
                    <ChartSeries Type="ChartSeriesType.Pie"
                                 Data="@pieData"
                                 ColorField="@nameof(MyPieChartModel.SegmentColor)"
                                 Field="@nameof(MyPieChartModel.SegmentValue)"
                                 CategoryField="@nameof(MyPieChartModel.SegmentName)">
                        <ChartSeriesLabels Visible="true"
                                           Template="#=category#"
                                           Background="transparent"
                                           Position="ChartSeriesLabelsPosition.OutsideEnd" />
                    </ChartSeries>
                </ChartSeriesItems>

                <ChartTitle Text="WorkSpaces"></ChartTitle>

                <ChartLegend Visible="false"></ChartLegend>
            </TelerikChart>

 

public class MyPieChartModel
    {
        public string SegmentName { get; set; }
        public double SegmentValue { get; set; }
        public string SegmentColor { get; set; }
    }

    public List<MyPieChartModel> pieData = new List<MyPieChartModel>();

private async Task RebindComponents()
    {
        Log.Debug("Enter into method RebindComponents");
        try
        {
            DmsWorkspaceDto dmsWorkspaceDto = new DmsWorkspaceDto()
            {

            };
            WorkspaceCountAll = //Calling API
            WorkspacePublicCount = //Calling API
            WorkspacePrivateCount =//Calling API

            pieData = new List<MyPieChartModel>()
            {
                new MyPieChartModel
                {
                    SegmentName = "Workspaces All ("+WorkspaceCountAll+")",
                    SegmentValue = WorkspaceCountAll,
                    SegmentColor=Countallcolor
                },

                        new MyPieChartModel
                {
                    SegmentName = "Workspaces Public/View ("+WorkspacePublicCount+")",
                    SegmentValue = WorkspacePublicCount,
                    SegmentColor=Publiccolor
                },
                new MyPieChartModel
                {
                    SegmentName = "Workspaces Private ("+WorkspacePrivateCount+")",
                    SegmentValue = WorkspacePrivateCount,
                    SegmentColor=Privatecolor
                }

            };
            ChartRef.Refresh();
            StateHasChanged();
        }
        catch (Exception ex)
        {
            Log.Error("Problem encountered at:RebindComponents: {message}", ex.Message);
        }
        Log.Debug("Exit from method RebindComponents");
    }                

 

MainLayout.Razor

on button clicks calling following methods to switch pages from light to dark and dark to light

public async Task lightTheme()
    {
       
        string newThemeUrl = "https://cdn.kendostatic.com/themes/7.0.2/bootstrap/bootstrap-main.css";
        await JSRuntime.InvokeVoidAsync("themeChanger.changeCss", new[] { newThemeUrl });
        
    }

    public async Task darkTheme()
    {
        
        string newThemeUrl = "https://cdn.kendostatic.com/themes/7.0.2/bootstrap/bootstrap-main-dark.css";
        await JSRuntime.InvokeVoidAsync("themeChanger.changeCss", new[] { newThemeUrl });
      
    }


var themeChanger = {
            changeCss: function (cssFileUrl) {
                var oldLink = document.getElementById("TelerikThemeLink"); // we have this id on the <link> that references the theme

                if (cssFileUrl === oldLink.getAttribute("href")) {
                    return;
                }

                var newLink = document.createElement("link");
                newLink.setAttribute("id", "TelerikThemeLink");
                newLink.setAttribute("rel", "stylesheet");
                newLink.setAttribute("type", "text/css");
                newLink.setAttribute("href", cssFileUrl);
                newLink.onload = () => {
                    oldLink.parentElement.removeChild(oldLink);
                };

                document.getElementsByTagName("head")[0].appendChild(newLink);
            }
        }           
ADMIN
Hristian Stefanov
Posted on: 20 Nov 2023 15:16

Hi Haribabu,

I've made an attempt to recreate the issue you described by creating an example project that follows our knowledge base article for the ability to change the theme dynamically. However, on my end, it seems that the changed theme applies to every component correctly. It's possible that I may be overlooking a configuration detail that you are testing, which could help me reproduce the problem.

To gain a more comprehensive understanding of the scenario and offer a tailored solution, please share a runnable sample that demonstrates the issue. This will enable me to thoroughly investigate the case.

I eagerly await your response and the opportunity to assist further.

Regards,
Hristian Stefanov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!