Overview
TDataItem is the main class of TeeBI, representing a column of data that can own sub-columns. It’s similar to a table column but with support for hierarchical structures. TDataItem can store data of various types (integers, floats, text, datetime, boolean) and provides rich functionality for data manipulation, sorting, and statistical analysis.
Constructors
Creates a new TDataItem with unknown data kind.var Data: TDataItem;
Data := TDataItem.Create;
Creates a new TDataItem, optionally configured as a table.Set to True to create a table-mode data item that can contain sub-items
var Table: TDataItem;
Table := TDataItem.Create(True);
Creates a new TDataItem with a specific data kind.The data type for this column (dkInt32, dkText, etc.)
var IntColumn: TDataItem;
IntColumn := TDataItem.Create(dkInt32);
Create(AKind: TDataKind; AName: String)
Creates a new TDataItem with a specific data kind and name.The data type for this column
The name of the data column
var AgeColumn: TDataItem;
AgeColumn := TDataItem.Create(dkInt32, 'Age');
Create(AData: TDataArray)
Creates a new TDataItem containing the specified array of sub-items.Array of TDataItem objects to add as sub-items
Create(AProvider: TDataProvider)
Creates a new TDataItem with a data provider for lazy loading.Provider responsible for loading data just-in-time
Properties
Returns the number of data values in this item.ShowMessage('Rows: ' + IntToStr(Data.Count));
The name/label of this data item.
The data type of this item (read-only). Once set, it should never be changed.Possible values:
dkInt32 - 32-bit integer
dkInt64 - 64-bit integer
dkSingle - Single precision float
dkDouble - Double precision float
dkExtended - Extended precision float
dkText - String/text
dkDateTime - Date and time
dkBoolean - Boolean
dkUnknown - Unknown/unspecified
if Data.Kind = dkInt32 then
ShowMessage('Integer column');
The parent TDataItem that owns this data item.
Collection of sub-items (child columns) within this data item.for I := 0 to Data.Items.Count - 1 do
ShowMessage(Data.Items[I].Name);
Statistical information about the data (min, max, mean, variance, etc.).ShowMessage('Average: ' + FloatToStr(Data.Stats.Mean));
Manages null/missing values in this data item.Data.Missing[5] := True; // Mark row 5 as missing
if Data.Missing[10] then
ShowMessage('Row 10 has missing data');
The object responsible for loading this item’s data on-demand.
The master data item in a master-detail relationship.
True when this data represents a table (containing sub-items as columns).
True when this data is the primary key of its parent.
True when all values are distinct (no duplicates).
Data Arrays
Each TDataItem has typed arrays for storing actual data values:
Array of 32-bit integers when Kind = dkInt32.
Array of 64-bit integers when Kind = dkInt64.
Array of single-precision floats when Kind = dkSingle.
Array of double-precision floats when Kind = dkDouble.
Array of extended-precision floats when Kind = dkExtended.
Array of strings when Kind = dkText.
Array of datetime values when Kind = dkDateTime.
Array of boolean values when Kind = dkBoolean.
Methods
Data Manipulation
Changes the size of the data arrays.The new size for the data arrays
Data.Resize(100); // Resize to hold 100 rows
Adds a new row to the data item with the provided values.Values to append. For table mode, must match column count.
Returns the index of the newly added row.// For a table with Name, Age, City columns
Table.Append(['John', 25, 'NYC']);
// For a single column
Column.Append([42]);
Inserts a new item at the specified position.The position where to insert the new item
Whether to mark the inserted value as missing
Data.Insert(10, False); // Insert at position 10
Deletes one or more rows starting at the specified position.Starting row index to delete
Data.Delete(5); // Delete row 5
Data.Delete(10, 3); // Delete rows 10, 11, 12
Removes all data and sub-items from this data item.
Removes all data values but keeps the structure.If True, also clears data from all sub-items
Data.ClearData(True); // Clear all data recursively
Sorting
Sorts the data by the specified data item column.Sort direction (True = ascending, False = descending)
For text columns, whether to ignore case when sorting
Table.SortBy(Table['Name'], True, True); // Sort by Name, case-insensitive
Data Access
Converts the data value at the specified index to a string.Returns the string representation of the value.S := Data.DataToString(0); // Get first value as string
Compares two data values at specified positions.For text data, whether to ignore case
Returns -1 if A < B, 0 if A = B, 1 if A > B.if Data.Compare(0, 1) < 0 then
ShowMessage('First value is smaller');
Copies a value from a source data item.Destination row index in this item
Source data item to copy from
Data.CopyFrom(0, SourceData, 5); // Copy row 5 from source to row 0
Loading and Persistence
Loads data from the associated Provider.Whether to also load child items
Data.Load(True); // Load this item and all children
Loads a TDataItem from a file.Returns the loaded TDataItem.Data := TDataItem.LoadFromFile('data.bi');
Saves this TDataItem to a file.Path where to save the file
Data.SaveToFile('output.bi');
Frees memory by unloading data (can be reloaded later).Whether to invalidate the items structure
Data.UnLoadData; // Free memory, keep structure
Statistics and Analysis
Recalculates all statistics for this item and optionally for all sub-items.Whether to use parallel threads for calculation
Data.ReCalculate(True); // Recalculate stats using threads
Returns the total number of rows (recursively for hierarchical data).
Returns the total number of columns (recursively for hierarchical data).Total := Data.TotalColumns;
Returns the full hierarchical name including parent names.ShowMessage(Data.FullName); // e.g., "Customers Sales Amount"
Usage Examples
Creating and Populating a Simple Column
var
Ages: TDataItem;
I: Integer;
begin
// Create an integer column
Ages := TDataItem.Create(dkInt32, 'Age');
// Resize to hold 5 values
Ages.Resize(5);
// Fill with data
Ages.Int32Data[0] := 25;
Ages.Int32Data[1] := 30;
Ages.Int32Data[2] := 22;
Ages.Int32Data[3] := 35;
Ages.Int32Data[4] := 28;
// Access statistics
ShowMessage('Average age: ' + FloatToStr(Ages.Stats.Mean));
ShowMessage('Minimum age: ' + IntToStr(TInt32Stats(Ages.Stats).Min));
Ages.Free;
end;
Creating a Table Structure
var
Customers: TDataItem;
Name, City: TDataItem;
Age: TDataItem;
begin
// Create a table
Customers := TDataItem.Create(True);
Customers.Name := 'Customers';
// Add columns
Name := TDataItem.Create(dkText, 'Name');
Age := TDataItem.Create(dkInt32, 'Age');
City := TDataItem.Create(dkText, 'City');
Customers.Items.Add(Name);
Customers.Items.Add(Age);
Customers.Items.Add(City);
// Add rows using Append
Customers.Append(['John Smith', 25, 'New York']);
Customers.Append(['Jane Doe', 30, 'Boston']);
Customers.Append(['Bob Wilson', 22, 'Chicago']);
// Sort by name
Customers.SortBy(Name, True, True);
ShowMessage('Total rows: ' + IntToStr(Customers.Count));
Customers.Free;
end;
Working with Missing Data
var
Sales: TDataItem;
begin
Sales := TDataItem.Create(dkDouble, 'Sales');
Sales.Resize(5);
// Set some values
Sales.DoubleData[0] := 1000.50;
Sales.DoubleData[1] := 1500.25;
Sales.Missing[2] := True; // Mark row 2 as missing
Sales.DoubleData[3] := 2000.00;
Sales.Missing[4] := True; // Mark row 4 as missing
// Check for missing values
if Sales.Missing[2] then
ShowMessage('Row 2 has missing data');
ShowMessage('Missing count: ' + IntToStr(Sales.Missing.MissingCount));
// Statistics automatically handle missing values
ShowMessage('Average (excluding missing): ' + FloatToStr(Sales.Stats.Mean));
Sales.Free;
end;
Saving and Loading Data
var
Data, LoadedData: TDataItem;
begin
// Create and populate data
Data := TDataItem.Create(dkInt32, 'Numbers');
Data.Resize(3);
Data.Int32Data[0] := 10;
Data.Int32Data[1] := 20;
Data.Int32Data[2] := 30;
// Save to file
Data.SaveToFile('numbers.bi');
Data.Free;
// Load from file
LoadedData := TDataItem.LoadFromFile('numbers.bi');
ShowMessage('Loaded ' + IntToStr(LoadedData.Count) + ' values');
LoadedData.Free;
end;
See Also