mattosaurus / ChartJSCore

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

Different amount of label and data in Chart.js #65

Closed soufianetahiri closed 2 years ago

soufianetahiri commented 3 years ago

Sorry its not an issue but could'nt find a way to ask!

Since data is implemented as List<double?>, how and why not let the user defines data as something similar to:

data = [{'x':'Apr', 'y':40},{'x':'July', 'y':70},{'x':'Dec', 'y':120}];

mattosaurus commented 3 years ago

Good point!

I'm not sure if the data object only worked with doubles when I first created this or if I saw it took other datatypes and I figured it would be too hard to implement.

I'll have a think about this and see if it's possible to add in without breaking anything.

soufianetahiri commented 3 years ago

In my opinion, it would be quite easy to let Data be either a list of nullable double or just a raw string (son) and it's up to the implementer to choose which property he wants to use.

Here is the turnaround I've found (in case someone came here to ask the same question :d)

 if (!data.Labels.Contains(line.xAxis))
                        {
                            data.Labels.Add(line.xAxis);
                        }

                        if (c.Count() < data.Labels.IndexOf(line.xAxis))
                        {
                            for (int i = c.Count(); i < data.Labels.IndexOf(line.xAxis); i++)
                            {
                                c.Add(0);
                            }
                        }
                        c.Insert(data.Labels.IndexOf(line.xAxis), (line.yAxis));

c is a List<double?>

mattosaurus commented 3 years ago

Looks like I'd actually done this already.

LineScatterDataset takes an object that allows you to set the x and y values. If you need this for other chart types you can always create a custom dataset class that inherits from the regular dataset class and override the data type.


public class MyBarDataset : BarDataset
{
    public new IList<LineScatterData> Data { get; set; }
}
soufianetahiri commented 3 years ago

Awsome I'll get a look at this ! Thanks !