Day of week column


#1

Hi,
I got my day of week trend line working. However, my “day_of_week” column is a number from 1-7, where 1 is Monday, 2 is Tuesday and so on. I’d like to show days instead of numbers (e.g. Mon, Tue, etc.) Is there an option for this?

Thanks in advance!


#2

Hi @noah,
Yes, you can use ChartFactor’s derived fields described here: https://chartfactor.com/doc/1.10/derived_fields/. You could define a derived field as shown below:

 let dayOfWeekStr = {
  'name': 'day_of_week_str',
  'label': 'Day of Week (derived)',
  'type': 'ATTRIBUTE',
  'dependencies': ['day_of_week'],
  'function': (dayOfWeek) => {
        let day = {
            '7' : { value: 'Sun', order: 1 },
            '1' : { value: 'Mon', order: 2 },
            '2' : { value: 'Tue', order: 3 },
            '3' : { value: 'Wed', order: 4 },
            '4' : { value: 'Thu', order: 5 },
            '5' : { value: 'Fri', order: 6 },
            '6' : { value: 'Sat', order: 7 }
        }
        return day[dayOfWeek];
    }
  
}

Notice 'function' that receives the dependency as parameter and returns the new vlue. Notice also the custom order so that Sunday is first.

Then, just plug-in the definition above in your source metadata definition.

var _META_ = {
    'sales': {    
        'fields': {
            'day_of_week_str': dayOfWeekStr,
            'price_paid': {
                'label': "Price"
            }
        }
    }
}

Hope it helps.


#3

Derived fields with custom sort order saved me a ton of time, thanks!