Skip to content

Custom Metadata

As mentioned before, metadata is automatically introspected from the provider. However, sometimes this metadata needs to be enhanced for ChartFactor to better render data visualizations. For example we can have a field named pricepaid, which represents a monetary value, but the information coming from the provider says that this field is of type "NUMBER". Since fields with type "MONEY" are formatted with the "$" sign, the value for pricepaid won't be displayed as it should:

CM1

Also note the X Axis label, it could be more expressive. We can enhance almost any field metadata coming from the provider with the following structure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
var providers = [ {
        name:'Elasticsearch',
        provider:'elasticsearch',
        url:'https://chartfactor.com/elastic/',
        metadata: {
            'ticket_sales': {
                'fields': {
                    'catname': { 'label': 'Category Name' },
                    'pricepaid': { 'label': 'Price Paid', 'type': 'MONEY' }
                }
            }
        }
    }
]

// Set the data provider to chartfactor
cf.setProviders(providers);

myChart = cf.provider('Elasticsearch')....execute();

Note

In the metadata example above, 'ticket_sales' is the source name.

After this, we can see how the tooltip's format has changed as well as the labels for the Y and X axis respectively:

CM2

Metadata structure

As we saw in the previous example, the structure to update field metadata is the following:

1
2
3
4
5
6
7
8
9
var customData = {
    'source_name_1': {
        'fields': {
            'field1': { 'label': 'My Custom Label' },
            'field2': { 'type': 'ATTRIBUTE | INTEGER | NUMBER | MONEY | PERCENT | TIME' }
        }
    },
    'source_name_2': { ... }
})

Using custom metadata to update time granularity

Sometimes the default granularity of a time field in the data source is not what we need. To update it, use the timestampGranularity attribute to pass the new granularity as shown below. Valid values are MINUTE, HOUR, DAY, MONTH, YEAR.

1
2
3
4
5
6
7
var customData = {
    [source]: {
        fields: {
            '@timestamp': { 'label': 'saletime', 'type': 'TIME', 'timestampGranularity': 'MONTH'}
        }
    }
}

How to access the data provider's current metadata

We can access the current provider's metadata as follows:

1
elasticProv._metadata