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.
- C#
- VB.NET
public class DataSetCollection : IList, ICollection, IEnumerable, IList<DataSet>, ICollection<DataSet>, IEnumerable<DataSet>
Public Class DataSetCollection
Inherits IList
Implements ICollection, IEnumerable, IList(Of DataSet), ICollection(Of DataSet), IEnumerable(Of 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
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];
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:
- ArgumentOutOfRangeException index is less than 0 or equal to or greater than Count.
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 } };
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
Add(dataSet)
Adds a new DataSet 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:
- ArgumentNullException dataSet is null.
Example:
var dataSet = new LineDataSet
{
Label = "Temperature",
Data = new object[] { 12, 15, 19, 17 },
BorderColor = Color.OrangeRed,
Fill = false
};
this.chartJS31.DataSets.Add(dataSet);
Add(name)
Creates and adds a new DataSet to the collection.
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:
- ArgumentNullException name is null or empty.
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 };
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 };
Contains(dataSet)
Checks if the specified DataSet exists in the collection.
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);
CopyTo(array, arrayIndex)
Copies all data sets to the specified array.
| Parameter | Type | Description |
|---|---|---|
| array | DataSet[] | The destination array. |
| arrayIndex | Int32 | The index at which to begin the copy. |
Example:
var copy = new DataSet[this.chartJS31.DataSets.Count];
this.chartJS31.DataSets.CopyTo(copy, 0);
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();
IndexOf(dataSet)
Returns the index of the specified DataSet 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);
Insert(index, dataSet)
Inserts the DataSet into the collection at the specified index.
| Parameter | Type | Description |
|---|---|---|
| index | Int32 | The zero-based index at which dataSet is inserted. |
| dataSet | DataSet | The DataSet to insert. |
Inserting a data set redraws the chart.
Throws:
- ArgumentNullException dataSet is null.
- ArgumentOutOfRangeException index is less than 0 or greater than Count.
Example:
var baseline = new BarDataSet { Label = "Baseline", Data = new object[] { 10, 10, 10 } };
this.chartJS31.DataSets.Insert(0, baseline);
Remove(dataSet)
Removes the DataSet from the collection and updates the chart.
Returns: Boolean. true if dataSet was removed; false if it was not found in the collection.
Throws:
- ArgumentNullException dataSet is null.
Example:
var target = this.chartJS31.DataSets["Target"];
if (target != null)
this.chartJS31.DataSets.Remove(target);
RemoveAt(index)
Removes the DataSet at the specified index from the collection and updates the chart.
Throws:
- ArgumentOutOfRangeException index is less than 0 or equal to or greater than Count.
Example:
// keep only the most recent data set.
while (this.chartJS31.DataSets.Count > 1)
this.chartJS31.DataSets.RemoveAt(0);