apexcharts / Blazor-ApexCharts

A blazor wrapper for ApexCharts.js
https://apexcharts.github.io/Blazor-ApexCharts
MIT License
788 stars 91 forks source link

Can not set ApexChart Title through ApexChartOptions<T> #501

Open Jorden-Quast opened 1 month ago

Jorden-Quast commented 1 month ago

Summary

The ApexChartOptions<T> class provides an accessible "Title" property that can be set, suggesting that a plot's title may be set through the options class and not just the "Title" parameter on an ApexChart component. However, if a user tries to do this and does not also set the Title parameter, the option gets set to null and the plot has no title.

Steps to Reproduce

  1. Create an instance of ApexChartOptions<TItem> named Options
  2. Set Options.Title = new Title { Text = "My Title Name" };
  3. Create an ApexChart instance and set the options parameter equal to the Options class just created
  4. Provide some data to the chart so that it renders
  5. Run program and see no title

Suggested Solution

This seems to stem from ApexChart.razor.cs OnParametersSet which has these lines:

if (string.IsNullOrEmpty(Title))
{
    Options.Title = null;
}
else
{
    if (Options.Title == null) { Options.Title = new Title(); }
    Options.Title.Text = Title;
}

In this, if the Title parameter is null the Options.Title will automatically be set to null, regardless of it's current value. To make it more optional, and allow for the title to be set through the Options class, I suggest changing the logic to something similar to

if(Title is not null)
{
    if(Options.Title == null) { Options.Title = new Title(); }
    Options.Title.Text = Title;
}

This way, if the title parameter is set, it will take priority over what the Options class may have already provided, but if it is null, the current options are respected, or the Options remain null.

joadan commented 2 weeks ago

Hi,

Makes sense, do you want to provide a PR or should I fix it? Thank you,