erossini / BlazorChartjs

Creates beautiful charts in Blazor using Chart.js. Library for NET6, NET7 and NET8. Source code and demo available.
https://www.puresourcecode.com/dotnet/blazor/blazor-component-for-chartjs/
MIT License
116 stars 44 forks source link

Legend Labels Filtering. Support to Ticks' AutoSkip and Font properties. Tooltip Callback Label problem fixed. Ticks callback added #59

Closed heitoreleuterio closed 6 months ago

heitoreleuterio commented 10 months ago

I had opened a pull request #57, but during the week I worked on some additional changes and fixes. Therefore, I decided to close pull request #57 and open a new one.

Changes:

[1] Legend Labels Filtering e.g:

legend: {
      display: true,
      labels: {
           filter: function(legendItem, data) {
                return legendItem.index != 1 
           }
      }
 }

[2] Scales Ticks' AutoSkip and Font properties e.g:

scales: {
    yAxes: [{
        ticks: {
          color: "black",
          autoSkip: false
        }
      }],
    xAxes: [{
        ticks: {
          color: "red",
          font: 14
        }
      }]
  }

[3] Tooltip CallbackLabel parameters type fixed:

[JSInvokable]
public static string[] TooltipCallbacksLabel(DotNetObjectReference <IChartConfig> config, decimal[] parameters) {
  var ctx = new CallbackGenericContext((int) parameters[0], (int) parameters[1], (int) parameters[2]);
  if (config.Value.Options is Options options)
    return options.Plugins.Tooltip.Callbacks.Label(ctx);
  else
    throw new NotSupportedException();
}

The parameters were initially defined as int, but the JavaScript wasn't passing integer values. To address this, I changed the type from int[] to decimal[].

[4] Ticks Callback e.g

const config = {
  type: 'line',
  data: data,
  options: {
    responsive: true,
    plugins: {
      title: {
        display: true,
        text: 'Chart with Tick Configuration'
      }
    },
    scales: {
      x: {
        ticks: {
          callback: function(val, index) {
            return index % 2 === 0 ? this.getLabelForValue(val) : '';
          },
          color: 'red',
        }
      }
    }
  },
};

I've implemented these functionalities following the project structure, but the .NET version needed to be updated to .NET 7 because System.Text.Json in .NET 6 can't handle polymorphic entities. This feature is important to enable the LegendFilterContext to receive generic data that can be parsed into the correct type of chart data.

Note: I realized that my formatting differed from yours in my personal projects. I addressed this in the commit https://github.com/erossini/BlazorChartjs/commit/8361ad61840bad553b945dc07c78a48b8623f0af

erossini commented 6 months ago

I'm struggle to run the project with those changes.

heitoreleuterio commented 6 months ago

I'm struggle to run the project with those changes.

My project has been running with these changes for the last few months without any problems so far. What is happening?

erossini commented 6 months ago

When I run the project, I can see this error:

Uncaught SyntaxError SyntaxError: Unexpected token 'export' at (program) (localhost꞉7194/_content/PSC.Blazor.Components.Chartjs/Chart.js:46:1) Debugging hotkey: Shift+Alt+D (when application has focus) Uncaught SyntaxError SyntaxError: Cannot use 'import.meta' outside a module at (program) (localhost꞉7194/_framework/dotnet.7.0.16.y6degfjxhe.js:8:27)

image

Are you using NET8? If not, I will release one package for NET7.

heitoreleuterio commented 6 months ago

I'm using .NET 7 as described in this commit #33a31a1

heitoreleuterio commented 6 months ago

I've found this article