mariusmuntean / ChartJs.Blazor

Brings Chart.js charts to Blazor
https://www.iheartblazor.com/
MIT License
676 stars 151 forks source link

Undefined in BarChart using wrapper #168

Closed simeondahl closed 3 years ago

simeondahl commented 3 years ago

Describe your question

I'm trying to make a barchart that has a wrapper object that contains the properties:

class DataItem<TData>
{
    [JsonProperty("t")]
    public string Name { get; set; }
    [JsonProperty("y")]
    public TData Amount { get; set; }
}

image

// Barset one
var barSet = new ChartJs.Blazor.ChartJS.BarChart.BarDataset<DataItem<int>>()
{
    Label = "Test",
    BackgroundColor = ColorUtil.RandomColorString(),
    BorderWidth = 1,
    BorderColor = "#ffffff",
};
var returnList = new List<DataItem<int>>() {
    new DataItem<int> { Amount = 10, Name = "Test regel 1" },
    new DataItem<int> { Amount = 10, Name = "Test regel 2" }
};
barSet.AddRange(returnList);
_config.Data.Datasets.Add(barSet);

// Barset two
var barSet2 = new ChartJs.Blazor.ChartJS.BarChart.BarDataset<DataItem<int>>()
{
    Label = "Test 2",
    BackgroundColor = ColorUtil.RandomColorString(),
    BorderWidth = 1,
    BorderColor = "#ffffff",
};
var returnList2 = new List<DataItem<int>>() {
    new DataItem<int> { Amount = 30, Name = "Test regel 1" }
};
barSet2.AddRange(returnList2);
_config.Data.Datasets.Add(barSet2);

But when I hover over the bars, then it says undefined. Can you tell me why?

Which Blazor project type is your question related to?

Which charts is this question related to?

BarChart

simeondahl commented 3 years ago

I found the problem. I needed to add "labels" to the config.

Joelius300 commented 3 years ago

I found the problem. I needed to add "labels" to the config.

I was going to suggest the same. You could also try to serialize Name as x instead of t (t is only for time-stuff, x is generic for points). However, when working with strings on the X-axis you should use labels.

Glad you got it working 👍🏼