Skip to main content

DataSetCollection

Namespace: Wisej.Web.Ext.ChartJS

Assembly: Wisej.Web.Ext.ChartJS (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 ChartJS control and is available through its DataSets property; it cannot be created directly. Any change to the collection (adding, inserting, replacing, removing or clearing data sets) redraws the owner chart.

Example:

this.chartJS1.ChartType = ChartType.Bar;
this.chartJS1.Labels = new[] { "Mon", "Tue", "Wed" };

var dataSets = this.chartJS1.DataSets;
dataSets.Add("Orders").Data = new object[] { 5, 8, 3 };
dataSets.Add("Returns").Data = new object[] { 1, 0, 2 };

foreach (var dataSet in dataSets)
System.Diagnostics.Debug.WriteLine(dataSet.Label);

Properties​

Instance member Count​

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

Example:

if (this.chartJS1.DataSets.Count == 0)
this.chartJS1.DataSets.Add("Default").Data = new object[] { 1, 2, 3 };

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 redraws the chart.

Throws:

Example:

var first = this.chartJS1.DataSets[0];
first.Label = "Actual";

// replace the second data set.
this.chartJS1.DataSets[1] = new LineDataSet { Label = "Forecast", Data = new object[] { 3, 6, 9 } };

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.

The label comparison is case sensitive. When setting, the first data set with the specified label is replaced; if there is no data set with that label, the new data set is added to the collection. Setting a data set redraws the chart.

Throws:

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

Example:

var sales = this.chartJS1.DataSets["Sales"];
if (sales != null)
{
sales.Data = new object[] { 40, 55, 32 };
this.chartJS1.UpdateData();
}

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 type should match the ChartType of the owner chart; use Add to create a matching data set automatically.

Throws:

Example:

var dataSet = new BarDataSet
{
Label = "Visitors",
Data = new object[] { 300, 450, 380 },
BackgroundColor = new[] { Color.SteelBlue, Color.SteelBlue, Color.Orange }
};
this.chartJS1.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.

The type of the new data set matches the ChartType of the owner chart: i.e. BarDataSet for Bar or PieDataSet for Pie. Bubble and Scatter charts create a LineDataSet. Adding the data set redraws the chart.

Throws:

Example:

this.chartJS1.ChartType = ChartType.Pie;

var pie = (PieDataSet)this.chartJS1.DataSets.Add("Market Share");
pie.Data = new object[] { 45, 30, 25 };
pie.BackgroundColor = new[] { Color.Red, Color.Green, Color.Blue };

Instance member Clear()​

Removes all data sets.

Clearing the collection redraws the chart.

Example:

private void buttonReset_Click(object sender, EventArgs e)
{
this.chartJS1.DataSets.Clear();
this.chartJS1.Labels = null;
}

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.chartJS1.DataSets.Contains(this.targetDataSet))
this.chartJS1.DataSets.Add(this.targetDataSet);

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.

Throws:

Example:

var copy = new DataSet[this.chartJS1.DataSets.Count];
this.chartJS1.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 DataSet objects in the collection.

Example:

// hide all the data sets except the first one.
bool first = true;
foreach (DataSet dataSet in this.chartJS1.DataSets)
{
dataSet.Hidden = !first;
first = false;
}
this.chartJS1.Update();

Instance member IndexOf(dataSet)​

Returns the index of the specified DataSet in the collection.

ParameterTypeDescription
dataSetDataSetThe DataSet to locate.

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

Example:

private void chartJS1_ChartClick(object sender, ChartClickEventArgs e)
{
int index = this.chartJS1.DataSets.IndexOf(e.SelectedDataSet);
this.labelInfo.Text = "Data set #" + index;
}

Instance member Insert(index, dataSet)​

Inserts the DataSet into the collection at the specified index.

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

Inserting a data set redraws the chart.

Throws:

Example:

// show the target line as the first data set.
var target = new LineDataSet { Label = "Target", Data = new object[] { 50, 50, 50 } };
this.chartJS1.DataSets.Insert(0, target);

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 in the collection.

The chart is redrawn even when dataSet is not in the collection.

Throws:

Example:

var forecast = this.chartJS1.DataSets["Forecast"];
if (forecast != null)
this.chartJS1.DataSets.Remove(forecast);

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:

// remove the last data set.
var dataSets = this.chartJS1.DataSets;
if (dataSets.Count > 0)
dataSets.RemoveAt(dataSets.Count - 1);