mattosaurus / ChartJSCore

Implementation of Chart.js for use with .NET Core.
GNU General Public License v3.0
116 stars 34 forks source link

Horizontal Bar model not working/missing #54

Closed Michal-Sitarz closed 4 years ago

Michal-Sitarz commented 4 years ago

I believe that the dataset model for Horizontal Bar is not implemented. I tried to swap the chart that I use in my project from standard Bar Chart into Horizontal Bar Chart, but it doesn't render "bars" inside the chart. When I swapped BarDataset to parent Dataset class for this Chart object, then it worked, but I lost the way to set BackgroundColor, BorderColor, etc.

Is it not implemented or am I doing something wrong?

I assume that it's not implemented, as there is no unit test for the horizontal bar. Any plans to implement it?

mattosaurus commented 4 years ago

This should theoretically work, the config options are the same as for the Bar dataset which is why there's no horizontal specific one.

https://www.chartjs.org/docs/latest/charts/bar.html

If you can provide me some samnple code I'll take a look.

mattosaurus commented 4 years ago

Ah, it's because the dataset type is still set to bar, will see if I can fix this.

Michal-Sitarz commented 4 years ago

Yes, indeed, both use the same config, but it's set to the BarDataset only, so I can't use it, when I'll change the Enums.ChartType from .Bar to .HorizontalBar :/ Your contribution is much appreciated! :) Apart from that it's a lovely wrapper to work with!

mattosaurus commented 4 years ago

Actually, looks like it's the just the dataset type that needs to be set to get it working, this defaults to Bar for the BarDataset but needs to be set to HorizontalBar for it to render correctly.

BarDataset dataset = new BarDataset()
{
    Type = Enums.ChartType.HorizontalBar,
    Label = "# of Votes",
    Data = new List<double>() { 12, 19, 3, 5, 2, 3 }
    // More settings
}
Michal-Sitarz commented 4 years ago

Got it!!! :) I know what you meant now in your reply... So, I have a ChartType specified as a property of a Chart, but I also have to specify the Dataset.Type to Enums.ChartType.HorizontalBar

var chart = new Chart
{
    Type = Enums.ChartType.HorizontalBar,
    Data = new ChartJSCore.Models.Data
    {
        Labels = new List<string>() { "One", "Two", "Three" },
        Datasets = new List<Dataset>()
        {
            new BarDataset 
            {
                Type = Enums.ChartType.HorizontalBar, // <- needed this! as you suggested
                Data = new List<double>(){ 1, 2, 3 }
            }
        }

    }
};

Thanks for quick response and all your help! :)

Michal-Sitarz commented 4 years ago

P.S. If you want to resuse same class of BarDataset for both chart types: Bar and HorizontalBar, I would suggest to not set the default type in the BarDataset class:

public Enums.ChartType Type { get; set; } = Enums.ChartType.Bar;

Instead of, force to set it to a right type, when it's needed. This way, it will suggest to change it, when you have to change the Chart.Type Just my humble opinion ;)

mattosaurus commented 4 years ago

Glad that works for you now :)

I think you're right that it needs a better solution will see if I can set it from the general chart type or if not maybe add a check or a new HorizontalBar dataset type.