Hello I have a Bars visualization that is using a field called day
as a group and count
as a metric, the problem is that the day
field is a number with 1 for Monday, 2 for Tuesday…, and so on, I want to render the name of the days instead of the number, is it possible?
Changing group data rendering
hector.tl
#1
emoya
#2
Of course, you need a derived field, it is a custom field that you can set in your metadata, let me show you an example:
// this is the derived field:
let dayAlias = {
'name': 'day_str',// set a name
'label': 'Day',// set a label
'type': 'ATTRIBUTE',
'dependencies': ['day'],// the name the field in your source
'function': (dayInt) => { // the argument is the value of the day in the data
let dayValues = {
'1' : 'Monday'
'2' : 'Tuesday',
'3' : 'Wednesday',
'4' : 'Thursday',
'5' : 'Friday'
'6' : 'Saturday'
'7' : 'Sunday'
}
return dayValues[dayInt];
}
}
// Then just use this config in your metadata, when defining your provider:
var providers = [ {
name:'your_provider_name',
provider:'elasticsearch',
url:'https://your_provider_url/',
metadata: {
'your_source_name': {
'fields': {
'day_str': dayAlias, // the derived field
}
}
}
}
]
// Set the data provider to chartfactor
cf.setProviders(providers);
hector.tl
#3
That works good, but just one thing, I cannot sort the days properly, they are now sorted alphabetically, so how can I put them in a specific order?
emoya
#4
You have the reason, let me modify a little bit the example, you can use something like this:
let dayAlias = {
'name': 'day_str',// set a name
'label': 'Day',// set a label
'type': 'ATTRIBUTE',
'dependencies': ['day'],// the name the field in your source
'function': (dayInt) => { // the argument is the value of the day in the data
let alias = {
'1' : { value: 'Monday', order: 1 },
'2' : { value: 'Tuesday', order: 2 },
'3' : { value: 'Wednesday', order: 3 },
'4' : { value: 'Thursday', order: 4 },
'5' : { value: 'Friday', order: 5 },
'6' : { value: 'Saturday', order: 6 },
'7' : { value: 'Sunday', order: 7 }
}
return alias[channel];
}
}
The order
key will help you to sort the days properly