Hierarchical data sources
There are two classes in the FastReport Business Graphics
standard set that provide data representation for hierarchical charts.
HierarchicalRecordsSource
This class allows specifying data manually. To do this, fill the Records
collection with HierarchicalRecord
elements, each with Text
and Value
fields to define the text and value of the record, and the Children
collection in which child records are placed.
Thus, the Children
and Parent
fields (filled automatically) build a hierarchical relationship between records.
In the following example, we create an instance of HierarchicalRecordsSource
and add to it the Bakery products
record, which in turn contains three child records: Ciabatta
, Bread
and Croissant
.
HierarchicalRecordsSource recordsSource = new HierarchicalRecordsSource();
// Creating a "Bakery products" record. The constructor for creating a record may not take a value if it is a record containing child nodes
HierarchicalRecord r = new HierarchicalRecord("Bakery products");
// Creating child records. Text and value are passed to the constructor
r.Children.Add(new HierarchicalRecord("Ciabatta", 3));
r.Children.Add(new HierarchicalRecord("Bread", 5));
r.Children.Add(new HierarchicalRecord("Croissant", 1));
// Adding a parent record to the list of source records
recordsSource.Records.Add(r);
Some charts, such as Sunburst
, can display a root element. To set the parameters of the root element, the Root
property must be filled in.
recordsSource.Root.Text = "Root element";
HierarchicalListSource
This class allows building a hierarchical structure based on tabular sources (System.Data.DataSet
, System.Array
) using the standard DataBinding
procedure.
To build a hierarchy, set the LevelTextMembers
collection with field names in nesting order.
For example:
listSource.LevelTextMembers.Add("Country");
listSource.LevelTextMembers.Add("City");
The ValueMember
field specifies the field that will be used to define the record value.
Suppose you have a DataTable
with the Country
and City
fields setting the hierarchy and the Population
field setting the value. To load the data from DataTable
, we need the following code:
listSource = new HierarchicalListSource();
listSource.BeginInit();
listSource.DataSource = dataTable;
listSource.LevelTextMembers.Add("Country");
listSource.LevelTextMembers.Add("City");
listSource.ValueMember = "Population";
listSource.EndInit();
Note that we use the BeginInit()
and EndInit()
calls to disable events triggering during property setup.