Basic Horizontal Bar Chart

This code sample of a horizontal bar chart report.

Horizontal bar charts are ideal when you want to show a discrete value (such as an on/off or open/closed/transitioning status) over time.

Asset names vary wildly in terms of length. Because of this, we suggest placing your asset names inside the report rather than placing them on one of your axes.

400

Customising
ElementReplace With
[PROPERTY]The name of the property you want to report on
The Code
import os
import sys
 
sys.path.insert(0,os.path.dirname(os.path.dirname(__file__)))
 
import mplreport
import discretepatterns as dp
import pandas as pd
import datetime
 
@mplreport.ardireport("Sample Report")
def CreateReport(report,args):    
 
    #Create a page containing a single plot.
    fig,ax = report.CreatePage(1)
 
    #Print a title block for the page.
    report.Title()                   
 
    #Our AQL query goes here
    query = "('[PROPERTY]') PROPERTY ALLPOINTS"
 
    #Get the pandas data-frame with the results. Use the extended version that includes metadata.
    hist = report.FetchHistory(query)        
    df = hist.data
 
    #Use the 'Discrete Patterns' class to convert discrete data into
    #  time frames
    patterns = dp.DiscretePatterns()
    patterns.SetDataframe(df)
    patterns.Ready()        
 
    #Draw the bar chart
 
    indx = len(df.columns)-1
    for col in df.columns:
        #Convert a discrete value into a set of time frames
        frames = patterns.GetAllTimeframes(col)
 
        #Get a colour map to convert values to colours
        map = report.GetDiscreteColourMap(hist,col)
 
        #For every value found in the frame...
        for value in frames:
            #Draw bars for every time it had that value
            for frame in frames[value]:
                ax.broken_barh([(frame[0],frame[1] - frame[0])],(indx,0.9),color=map[value])
 
        indx -= 1
 
    #Draw in names
    indx = len(df.columns)-1
    for col in df.columns:
        ax.text(df.index[0] + datetime.timedelta(seconds=30), indx+0.18, col.replace(" [PROPERTY]","").strip(), c=(0,0,0,0.7))
        indx -= 1
 
    #Clear the Y axis
    ax.set_yticks(range(0,len(df.columns)))
    ax.set_yticklabels([""] * len(df.columns))        
 
    #Clean up and prettify        
    ax.set_ylabel("Drive")
    ax.set_xlabel("Time")    
    ax.margins(0,0)    
    report.TimeAxis(ax.xaxis)
    legend = report.GetDiscreteLegend(hist,df.columns[0])
    ax.legend(handles=legend[0])
 
    #Save this report out.
    report.Save()