Handling Data Storage
The Charts Library has been designed with a clear separation between the data to be displayed and the graphical representation of the data. The storage of the data sets that have to be displayed by a given chart object is handled by a dedicated object referred to as the chart data object.
The base class used to represent a chart data object is the IlvAbstractChartData class. A subclass called IlvMemoryChartData class is provided in the library. The IlvMemoryChartData class stores the data sets it manages in memory. When you create a chart object, an instance of the IlvMemoryChartData class is created by default and is set on the created chart object to handle the data sets it is going to display.
The data sets managed by a chart data object are all instances of subclasses of the IlvChartDataSet class. The subclasses of the IlvChartDataSet class that are provided in the library define the different types of data sets that can be displayed in a chart.
Types of Data Sets
A data set to be displayed by a given chart can be defined as:
*A set of points with two coordinates (set-of-points data set)
*A set of values (set-of-values data set)
*A function of the type y = f(x) (function data set)
*A cyclic set of points (cyclic set-of-points data set)
Set-of-Points Data Set
A set-of-points data set is represented by an instance of the IlvChartPointSet class. The data points managed by this data set are instances of the IlvDoublePoint class. The IlvDoublePoint class allows you to represent a point with two coordinates of the IlvDouble type.
The following table shows an example of some data that can be represented by an IlvChartPointSet object. Each point (x, y) is stored by using an IlvDoublePoint instance within the IlvChartPointSet object.
X
Y
0.5
1.0
1.2
2.3
1.6
3.1
This data set can be created by using the following code:
IlvChartPointSet* dataSet = new IlvChartPointSet();
dataSet->addPoint(IlvDoublePoint(0.5, 1.0));
dataSet->addPoint(IlvDoublePoint(1.2, 2.3));
dataSet->addPoint(IlvDoublePoint(1.6, 3.1));
Set-of-Values Data Set
A set-of-values data set is represented by an instance of the IlvChartYValueSet class. The data values managed by this data set are of the IlvDouble type. The values correspond to the ordinates of the data points that will be displayed. The abscissas of the data points are by definition the indexes of the stored values.
The following table shows an example of data that can be represented by an IlvChartYValueSet object.
X
Y
0
1.2
1
3.1
2
4.6
Each y-value is stored using an IlvDouble instance within the IlvChartYValueSet object. The x-values are not stored because they correspond to the indexes of the stored y-values.
A set-of-values data set can be created by using the following code:
IlvChartYValueSet* dataSet = new IlvChartYValueSet();
dataSet->addValue(1.2);
dataSet->addValue(3.1);
dataSet->addValue(4.6);
This code is equivalent to the following:
IlvChartYValueSet* dataSet = new IlvChartYValueSet();
dataSet->addPoint(IlvDoublePoint(0.0, 1.2));
dataSet->addPoint(IlvDoublePoint(1.0, 3.1));
dataSet->addPoint(IlvDoublePoint(2.0, 4.6));
Function Data Set
A function data set is defined with a function of the type y=f(x). It is represented by an instance of a subclass of the IlvAbstractChartFunction class.
The abscissa values of the data points are computed from the minimum and maximum values considered for the abscissa and from the number of data points. The ordinate values of the data points are then obtained by applying the function to the computed abscissa values. The abscissa and the ordinate values of the ith data point that is represented by a data set defined with a function f are obtained by using the following formula:
x = ((_xMax - _xMin) / (_dataCount -1)) * i + _xMin;
y = f(x);
where _xMin and _xMax are the minimum and the maximum values considered for the abscissa and _dataCount is the number of considered data points. The values _xMin, _xMax, _dataCount, and the function f have to be defined by the user.
Two subclasses of the IlvAbstractChartFunction class are available in the library:
*IlvCallbackChartFunction for which the function to be represented is defined by a callback. This callback must be of the IlvDoubleFunction type:
typedef IlvDouble (* IlvDoubleFunction)(IlvDouble);
*IlvScriptChartFunction for which the function to be represented is defined by a script.
Either one of these two subclasses can be used to represent any function of the type y = f(x). The following examples show how to use each of the subclasses. We will create an IlvCallbackChartFunction object and an IlvScriptChartFunction object, each representing the square function y = x2. Both data sets are initialized with 0 and 5 as the minimum and maximum values for the abscissa and 9 as the number of considered data points.
Using an IlvCallbackChartFunction Data Set Object
To create an IlvCallbackChartFunction object, perform the following steps:
1. Define the callback function for computing the square function.
IlvDouble
square(IlvDouble x)
{
return x*x;
}
2. Create the IlvCallbackChartFunction data set representing the square function.
IlvCallbackChartFunction* function =
new IlvCallbackChartFunction(IlvCoordInterval(0.,5.), 9, square);
The minimum and maximum values considered for the abscissa are set to 0 and 5, respectively. The number of data points considered is set to 9 and the callback function is set to square.
Using an IlvScriptChartFunction Data Set Object
This example shows you how to create an IlvScriptChartFunction object directly in your code. However, you can easily create an IlvScriptChartFunction data set by using the Chart Inspector in Views Studio (see Using the Chart Inspector).
To create an IlvScriptChartFunction object, perform the following steps:
1. Set a script context on the holder of the container or manager that contains the chart, if a script context is not defined for the holder.
The script function used in an IlvScriptChartFunction object is assumed to be stored in a script context associated with the holder of the container or manager that contains the chart. If a script context is not defined for the holder, a script context must be created and set on the holder. To do this, use the following code:
First, create a script context using the JavaScript™ scripting language:
const IlvSymbol* scriptLanguageName = IlvGetSymbol("JvScript");
IlvScriptLanguage* jvscript = IlvScriptLanguage::Get(scriptLanguageName);
IlvJvScriptContext* jvscriptContext = new IlvJvScriptContext(jvscript, 0);
Then, set the created script context on the holder of the container or manager that contains the chart:
cont->getHolder()->setScriptContext(jvscriptContext);
2. Load the script function for computing the square function into the created script context.
IlvScriptContext* context =
(cont->getHolder())->getScriptContext(scriptLanguageName);
context->loadScript("../data/scriptfile");
Since several script contexts may be associated with the holder, the name of the scripting language (JavaScript in this example) is passed as a parameter to the getScriptContext method in order to retrieve the script context using this scripting language. The file scriptfile contains the implementation of the script function in JavaScript:
function square(x)
{
return x*x;
}
3. Create the IlvScriptChartFunction data set representing the square function.
IlvScriptChartFunction* function =
new IlvScriptChartFunction(IlvCoordInterval(0.,5.),
9,
"square",
cont->getHolder(),
scriptLanguageName);
The minimum and maximum values considered for the abscissa are set to 0 and 5, respectively. The number of data points considered is set to 9. The name of the script function is set to "square". The holder in which the script function will be retrieved is set to the holder of the container or manager that contains the chart. The name of the scripting language used to write the script function is set to scriptLanguageName.
Cyclic Set-of-Points Data Set
A cyclic set-of-points data set is represented by an instance of the IlvChartCyclicPointSet. It is similar to the normal set-of-points data set described above, except that when the number of added points reaches a fixed maximum number, the “old” points are discarded so that the actual number of points kept in memory remains constant. As shown by the chart, the count will grow until it reaches the limit and then it remains constant. Each time a new point is added, it will be at the last index, while the old ones are shifted back by one.
The maximum number of points can be set with setMaxCount() and is infinite by default.
For example:
IlvChartDataSet* dataSet;
dataSet = new ilvChartCyclicPointSet(“Cyclic point set”);
dataSet->setMaxCount(100);
For an example of how to use IlvChartCyclicPointSet, see in the shiftcar sample in samples/charts/scrolling directory.
Published date: 05/24/2022
Last modified date: 02/24/2022