To generate XYChart.props.dataSources.history:
# Load the process unit history for the configured process unit ID.
processUnitID = self.view.params.process_unit_id
# Controlling parameters
dateStart = system.date.fromMillis(self.view.params.dateStart)
dateEnd = system.date.fromMillis(self.view.params.dateEnd)
dateDays = system.date.daysBetween(dateStart, dateEnd)
# For each day in the history range, load the statistics from that day
# After loading the statistics from that day, build the format required by the chart
jsonResults = []
for day in range(dateDays + 1):
currentStartDate = system.date.addDays(dateStart, day)
currentEndDate = system.date.addDays(dateStart, day + 1)
# Named Query: "Process Units/History/Usage History Unit"
# Parameters: process_unit_id (Int4) - ID of the process unit to store history for.
# begin (DateTime) - Begining time to select history.
# end (DateTime) - End time to select history.
params = {
'process_unit_id': processUnitID,
'begin': currentStartDate,
'end': currentEndDate
}
query = 'Process Units/History/Usage History Unit'
queryResults = system.db.runNamedQuery('global', query, params)
# Turn the query results in a json list for easy charting.
# Loop through the query results, and extract all the modes and percentages.
currentResults = {}
for row in range(queryResults.rowCount):
percent = queryResults.getValueAt(row, "Percent")
# Remove all spaces from the mode name
mode = queryResults.getValueAt(row, "Mode").replace(' ', '')
# If the percentage is Null, set it to zero.
# If the percentage is not null, divide it by 100
if percent is None:
percent = 0
else:
percent = percent/100
currentResults[mode] = percent
# After extracting all the modes, create a key that represents the date of the historical data.
currentResults['date'] = str(system.date.getMonth(currentStartDate)+1) + '/' + str(system.date.getDayOfMonth(currentStartDate))
# Append the current results, and the repeat for the rest of the remaining days.
jsonResults.append(currentResults)
# After looping through the days, return the resultset.
return jsonResults
To generate XYChart.props.series:
# Load the process unit history for the configured process unit ID.
processUnitID = self.view.params.process_unit_id
# Controlling parameters
dateStart = system.date.fromMillis(self.view.params.dateStart)
dateEnd = system.date.addDays(system.date.fromMillis(self.view.params.dateEnd), 1)
# Named Query: 'Process Units/History/Usage History Unit'
# Parameters: process_unit_id (Int4) - ID of the process unit to read history for.
# begin (DateTime) - Begining time to select history.
# end (DateTime) - End time to select history.
params = {
'process_unit_id': processUnitID,
'begin': dateStart,
'end': dateEnd
}
query = 'Process Units/History/Usage History Unit'
# Run the named query
queryResults = system.dataset.sort(system.db.runNamedQuery('global', query, params), "Mode")
# Turn the query results in a json list for easy charting.
# Loop through the query results, and extract all the modes and percentages.
# For each mode, create a series.
jsonResults = []
for row in range(queryResults.rowCount):
currentSeries = {}
percent = queryResults.getValueAt(row, "Percent")
modeWS = queryResults.getValueAt(row, "Mode")
mode = modeWS.replace(' ','')
time = queryResults.getValueAt(row, "SecondsElapsed")
currentSeries = self.createSeries(modeWS, modeWS, 'history', 'date', mode, 'time', 'percent')
# Append the current results, and the repeat for the rest of the remaining days.
jsonResults.append(currentSeries)
# After looping through the days, return the resultset.
return jsonResults
Where self.createSeries() is a custom method on the XYChart that builds the style of series I’m looking for:
def createSeries(name, title, source, dataXAxis, dataYAxis, chartXAxis, chartYAxis):
return {
"name": name,
"sequencedInterpolation": False,
"label": {
"text": title
},
"visible": True,
"hiddenInLegend": False,
"defaultState": {
"visible": True
},
"data": {
"source": source,
"x": dataXAxis,
"y": dataYAxis
},
"xAxis": chartXAxis,
"yAxis": chartYAxis,
"zIndex": 0,
"tooltip": {
"enabled": True,
"text": "{name}: [bold]{valueY}[/]",
"cornerRadius": 3,
"pointerLength": 4,
"background": {
"color": "",
"opacity": 1
}
},
"render": "column",
"candlestick": {
"open": {
"x": "",
"y": ""
},
"high": {
"x": "",
"y": ""
},
"low": {
"x": "",
"y": ""
},
"appearance": {
"fill": {
"color": "",
"opacity": 1
},
"stroke": {
"color": "",
"opacity": 1,
"width": 1
},
"stacked": False,
"deriveFieldsFromData": {
"fill": {
"color": "",
"opacity": ""
},
"stroke": {
"color": "",
"opacity": "",
"width": ""
}
},
"heatRules": {
"enabled": False,
"max": "",
"min": "",
"dataField": ""
}
}
},
"column": {
"open": {
"x": "",
"y": ""
},
"appearance": {
"fill": {
"color": "",
"opacity": 1
},
"stroke": {
"color": "",
"opacity": 1,
"width": 0
},
"stacked": True,
"deriveFieldsFromData": {
"fill": {
"color": "",
"opacity": ""
},
"stroke": {
"color": "",
"opacity": "",
"width": ""
}
},
"heatRules": {
"enabled": False,
"max": "",
"min": "",
"dataField": ""
}
}
},
"line": {
"open": {
"x": "",
"y": ""
},
"appearance": {
"connect": True,
"tensionX": 1,
"tensionY": 1,
"minDistance": 0.5,
"stroke": {
"width": 3,
"opacity": 1,
"color": "",
"dashArray": ""
},
"fill": {
"color": "",
"opacity": 0
},
"bullets": [
{
"enabled": False,
"render": "circle",
"width": 10,
"height": 10,
"label": {
"text": "{value}",
"position": {
"dx": 0,
"dy": 0
}
},
"tooltip": {
"enabled": True,
"text": "{name}: [bold]{valueY}[/]",
"cornerRadius": 3,
"pointerLength": 4,
"background": {
"color": "",
"opacity": 1
}
},
"fill": {
"color": "",
"opacity": 1
},
"stroke": {
"color": "",
"width": 1,
"opacity": 1
},
"rotation": 0,
"deriveFieldsFromData": {
"fill": {
"color": "",
"opacity": ""
},
"stroke": {
"color": "",
"opacity": "",
"width": ""
},
"rotation": ""
},
"heatRules": {
"enabled": False,
"max": 100,
"min": 2,
"dataField": ""
}
}
]
}
},
"stepLine": {
"open": {
"x": "",
"y": ""
},
"appearance": {
"connect": True,
"tensionX": 1,
"tensionY": 1,
"minDistance": 0.5,
"stroke": {
"width": 3,
"opacity": 1,
"color": "",
"dashArray": ""
},
"fill": {
"color": "",
"opacity": 0
},
"bullets": [
{
"enabled": True,
"render": "circle",
"width": 10,
"height": 10,
"label": {
"text": "{value}",
"position": {
"dx": 0,
"dy": 0
}
},
"tooltip": {
"enabled": True,
"text": "{name}: [bold]{valueY}[/]",
"cornerRadius": 3,
"pointerLength": 4,
"background": {
"color": "",
"opacity": 1
}
},
"fill": {
"color": "",
"opacity": 1
},
"stroke": {
"color": "",
"width": 1,
"opacity": 1
},
"rotation": 0,
"deriveFieldsFromData": {
"fill": {
"color": "",
"opacity": ""
},
"stroke": {
"color": "",
"opacity": "",
"width": ""
},
"rotation": ""
},
"heatRules": {
"enabled": False,
"max": 100,
"min": 2,
"dataField": ""
}
}
]
}
}
}
Here’s the JSON for the view: Usage Chart.json (21.6 KB)