Skip to main content

DataSetCollection

Namespace: Wisej.Web.Ext.ChartJS3

Assembly: Wisej.Web.Ext.ChartJS3 (4.1.0.0)

Collection of DataSet objects. Represents the sets of data to plot.

public class DataSetCollection : IList, ICollection, IEnumerable, IList<DataSet>, ICollection<DataSet>, IEnumerable<DataSet>

The collection is owned by a ChartJS3 control and is available through its DataSets property. Adding, inserting, replacing or removing data sets redraws the chart. Changing the properties of a data set already in the collection does not redraw the chart: call Update on the chart, or UpdateData when only the data values changed.

Example:

DataSetCollection dataSets = this.chartJS31.DataSets;
dataSets.Clear();

dataSets.Add("2023").Data = new object[] { 50, 60, 70 };
dataSets.Add("2024").Data = new object[] { 55, 72, 81 };

Properties​

Instance member Count​

Int32: Returns the number of DataSet objects in the collection.

Example:

if (this.chartJS31.DataSets.Count == 0)
this.chartJS31.DataSets.Add("Empty").Data = new object[0];

Instance member Item(index)​

DataSet: Returns or sets the DataSet at the specified position.

Value: The DataSet at the specified index .

Setting a data set replaces the one at the specified position and redraws the chart.

Throws:

Example:

var first = (BarDataSet)this.chartJS31.DataSets[0];
first.BackgroundColor = new[] { Color.SteelBlue };

// replace the second data set.
this.chartJS31.DataSets[1] = new BarDataSet { Label = "Forecast", Data = new object[] { 5, 8, 13 } };

Instance member Item(label)​

DataSet: Returns or sets the DataSet identified by the label name.

Value: The first DataSet with the specified label , or null if not found.

Setting a data set replaces the first data set with the specified label; when there is no data set with that label, the new data set is added to the collection. The label of the assigned data set is not changed. The chart is redrawn.

Throws:

  • ArgumentNullException label is null when setting the value, or the value is null and no data set has the specified label.

Example:

var sales = this.chartJS31.DataSets["Sales"];
if (sales != null)
sales.Hidden = true;

// adds or replaces the "Target" data set.
this.chartJS31.DataSets["Target"] = new LineDataSet { Label = "Target", Data = new object[] { 100, 100, 100 } };

Methods​

Instance member Add(dataSet)​

Adds a new DataSet to the collection.

ParameterTypeDescription
dataSetDataSetThe DataSet to add to the collection.

Adding a data set redraws the chart. The data set is added as is: use a data set class that matches the ChartType of the chart, or Add to create the matching type automatically.

Throws:

Example:

var dataSet = new LineDataSet
{
Label = "Temperature",
Data = new object[] { 12, 15, 19, 17 },
BorderColor = Color.OrangeRed,
Fill = false
};
this.chartJS31.DataSets.Add(dataSet);

Instance member Add(name)​

Creates and adds a new DataSet to the collection.

ParameterTypeDescription
nameStringThe name of the new DataSet, assigned to its Label.

Returns: DataSet. The new DataSet added to the collection.

The type of the new data set matches the ChartType of the chart: LineDataSet for line, bubble and scatter charts, BarDataSet for bar and horizontal bar charts, and PieDataSet, DoughnutDataSet, PolarAreaDataSet or RadarDataSet for the other types. Cast the result to access the type specific properties. Adding the data set redraws the chart.

Throws:

Example:

this.chartJS31.ChartType = ChartType.Pie;

var shares = (PieDataSet)this.chartJS31.DataSets.Add("Market Share");
shares.Data = new object[] { 45, 30, 25 };
shares.BackgroundColor = new[] { Color.Gold, Color.Teal, Color.Coral };

Instance member Clear()​

Removes all data sets.

Clearing the collection redraws the chart.

Example:

this.chartJS31.DataSets.Clear();
this.chartJS31.DataSets.Add("New Data").Data = new object[] { 1, 2, 3 };

Instance member Contains(dataSet)​

Checks if the specified DataSet exists in the collection.

ParameterTypeDescription
dataSetDataSetThe DataSet to look for.

Returns: Boolean. true if dataSet is in the collection; otherwise, false.

Example:

if (!this.chartJS31.DataSets.Contains(this.forecastDataSet))
this.chartJS31.DataSets.Add(this.forecastDataSet);

Instance member CopyTo(array, arrayIndex)​

Copies all data sets to the specified array.

ParameterTypeDescription
arrayDataSet[]The destination array.
arrayIndexInt32The index at which to begin the copy.

Example:

var copy = new DataSet[this.chartJS31.DataSets.Count];
this.chartJS31.DataSets.CopyTo(copy, 0);

Instance member GetEnumerator()​

Returns an enumerator that iterates all the DataSet objects in the collection.

Returns: IEnumerator<DataSet>. An IEnumerator for the collection.

Example:

foreach (var dataSet in this.chartJS31.DataSets)
dataSet.BorderWidth = 2;

this.chartJS31.Update();

Instance member IndexOf(dataSet)​

Returns the index of the specified DataSet in the collection.

ParameterTypeDescription
dataSetDataSetThe DataSet to locate in the collection.

Returns: Int32. The zero-based index of dataSet in the collection, or -1 if not found.

Example:

int index = this.chartJS31.DataSets.IndexOf(this.chartJS31.DataSets["Sales"]);
if (index > -1)
this.chartJS31.DataSets.RemoveAt(index);

Instance member Insert(index, dataSet)​

Inserts the DataSet into the collection at the specified index.

ParameterTypeDescription
indexInt32The zero-based index at which dataSet is inserted.
dataSetDataSetThe DataSet to insert.

Inserting a data set redraws the chart.

Throws:

Example:

var baseline = new BarDataSet { Label = "Baseline", Data = new object[] { 10, 10, 10 } };
this.chartJS31.DataSets.Insert(0, baseline);

Instance member Remove(dataSet)​

Removes the DataSet from the collection and updates the chart.

ParameterTypeDescription
dataSetDataSetThe DataSet to remove.

Returns: Boolean. true if dataSet was removed; false if it was not found in the collection.

Throws:

Example:

var target = this.chartJS31.DataSets["Target"];
if (target != null)
this.chartJS31.DataSets.Remove(target);

Instance member RemoveAt(index)​

Removes the DataSet at the specified index from the collection and updates the chart.

ParameterTypeDescription
indexInt32The index of the DataSet to remove.

Throws:

Example:

// keep only the most recent data set.
while (this.chartJS31.DataSets.Count > 1)
this.chartJS31.DataSets.RemoveAt(0);