Bar Chart DateTime Format

Is there anyway to format date time in a bar chart (I don’t see a way)? Data is coming from tag history. Or, is there an easy way to edit data in a dataset to the correct format, then use this data in the binding? Typically, the data will be to display values for each hour over a period of time, usually shifts.

1 Like

Unfortunately the bar chart does not have a way of doing this natively, but here’s hopefully a simple solution. I see this same question has been asked regularly in the past, hopefully this helps other out as well.

What the below will do is take the timestamp column of the tag history binding on a custom property and convert it to a string and we will manipulate the timestamp format with python and put that into the main data property of the bar chart back along with the values.

  1. Create a custom property on the bar chart which will act as your main “Tag History” binding.
  2. Setup the binding on the newly created custom property as you would have on the main “data” property. This would most likely be using the “Tag History” with any settings you’d like.
  3. Create a component script on the bar chart. Use the propertyChange event and put something like this in:

Thanks to @schenk for the below if wanting to format the date.

if event.propertyName == 'dataNew':
	data = system.dataset.formatDates(event.source.dataNew, 'hh:mm a')
	event.source.data = data

The below would be the long way to do it for dates, but can be used to do other things with the data or more customizable formatting (maybe helpful for beginners?).

if event.propertyName == "dataNew": # on change only of the new custom property
	ds = event.source.dataNew
	ds = system.dataset.toPyDataSet(ds)

	newDs = []
	for row in ds:
		ts = str(row[0])
		subts = ts.split(":")  #example: we are extracting the hour and minute
		newts  = subts[0][-2:] + ":" + subts[1] 
		val = row[1]  #get the value as is
		newDs.append([newts, val])  #put timestamp and value back in new list
		
	headers = ["timestamp", "value"]
	event.source.data = system.dataset.toDataSet(headers, newDs)

I’ve also found that this trick helps anytime you need to manipulate data from using the nice built-in bindings, then use a script or expression to put the data on the main components data property.

2 Likes

You could also use system.dataset.formatDates()

if event.propertyName == 'dataNew':
	data = system.dataset.formatDates(event.source.dataNew, 'hh a')
	event.source.data = data
4 Likes

@schenk - Much simpler code, thanks!

Didn’t think of the system.dataset.formatDates, so many Ignition functions that help make life easier that’s easy to overlook sometimes.

I’ll keep my code up as originally, in case someone would find it helpful to do other things within the dataset that may not have a built in function.

Thanks guys. I forget what I did now to get it to work, but I did find a solution. I’ll bookmark this for later in case I need it.