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.
- 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 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
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 };
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:
- ArgumentOutOfRangeException index is less than 0 or greater than or equal to Count.
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 } };
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
Add(dataSet)
Adds a new DataSet 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:
- ArgumentNullException dataSet is null.
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);
Add(name)
Creates and adds a new DataSet to the collection.
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:
- ArgumentNullException name is null or empty.
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 };
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;
}
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.chartJS1.DataSets.Contains(this.targetDataSet))
this.chartJS1.DataSets.Add(this.targetDataSet);
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. |
Throws:
- ArgumentNullException array is null.
- ArgumentOutOfRangeException arrayIndex is less than 0.
- ArgumentException The destination array is too small to hold all the data sets starting at arrayIndex .
Example:
var copy = new DataSet[this.chartJS1.DataSets.Count];
this.chartJS1.DataSets.CopyTo(copy, 0);
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();
IndexOf(dataSet)
Returns the index of the specified DataSet in the collection.
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;
}
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 should be 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:
// 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);
Remove(dataSet)
Removes the DataSet from the collection and updates the chart.
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:
- ArgumentNullException dataSet is null.
Example:
var forecast = this.chartJS1.DataSets["Forecast"];
if (forecast != null)
this.chartJS1.DataSets.Remove(forecast);
RemoveAt(index)
Removes the DataSet at the specified index from the collection and updates the chart.
Throws:
- ArgumentOutOfRangeException index is less than 0 or greater than or equal to Count.
Example:
// remove the last data set.
var dataSets = this.chartJS1.DataSets;
if (dataSets.Count > 0)
dataSets.RemoveAt(dataSets.Count - 1);