5.20203.748
5.20203.748

Using Enumerations in Wijmo

Several Wijmo controls have properties that take enumeration values.

For example, the FlexChart's chartType property takes wijmo.chart.ChartType values.

Setting enumeration properties

The recommended way to set enumeration properties is as follows:

import { ChartType } from '@grapecity/wijmo.chart';

// setting the value of an enumeration property
chart.chartType = ChartType.Line;

The following alternatives are also valid and produce the same result:

// wijmo.chart.ChartType.Line has value 3:
chart.chartType = 3;

// enumerations are automatically parsed
chart.chartType = 'Line';

Getting enumeration properties

Getting the property will return 3 in all cases. If you want to get the value as a string (to show in the UI for example), you can do it as follows:

// getting the enumerated value as a number
console.log(chart.chartType); // outputs "3"

// getting the enumerated value as a string
console.log(ChartType\[chart.chartType\]); // outputs "Line"

Converting enumeration values

You can use the enumeration classes to convert between strings and the corresponding numbers by indexing. For example:

// convert enumeration value to string
console.log(ChartType\[3\]); // outputs "Line"
console.log(ChartType\[1000\]); // outputs "null"

// convert string to enumeration value
console.log(ChartType\['Line'\]); // outputs "3"
console.log(ChartType\['NoSuchValue'\]); // outputs "null"

Note for .NET Developers

The .NET, Enum class provides methods called GetNames and GetValues that return the names and values defined by any enumeration.

The code below shows how you could implement similar methods to get the names and values defined by TypeScript enumerations (as used in Wijmo):

import { DataType } from '@grapecity/wijmo';
// get the names defined by an enumeration
function getEnumNames(enumClass) {
    var names = \[\];
    for (var key in enumClass) {
        var val = parseInt(key);
        if (isNaN(val)) names.push(key);
    }
    return names;
}

// get the values defined by an enumeration
function getEnumValues(enumClass) {
    var values = \[\];
    for (var key in enumClass) {
        var val = parseInt(key);
        if (!isNaN(val)) values.push(val);
    }
    return values;
}

// sample usage:
var nn = getEnumNames(DataType); // returns \[ 'Object', 'String', 'Number', 'Boolean', 'Array' \]
var vv = getEnumValues(DataType); // returns \[ 0, 1, 2, 3, 4 \]