
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  heatMap.r                                                                              #
  #           Last Updated Funclist  :  15 Feb 2014,  1:13 AM (Saturday)                                                       #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   MakeHeatMap        ( DT, Arts=NULL, start.date=as.Date("2013-01-01"), end.date=as.Date("2014-01-02") )                   #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

MakeHeatMap <- function(DT, Arts=NULL, start.date = as.Date("2013-01-01"), end.date   = as.Date("2014-01-02"))  {
  DRng <- DT[, range(Date, na.rm=TRUE)]
  DSeq <- seq.Date(max(start.date, DRng[[1]]) , min(end.date, DRng[[2]]), by=1)
  ind <- {if (is.null(Arts)) CJ(DSeq) else CJ(DSeq, Arts)}

    {
    if (is.null(Arts))
      ptitle <- "Number of Artists in Roster Available Each Day"
    else {
      if (length(Arts) > 1)
        ptitle <- "Number of Total Availabilities For the Selected Artists"
      else 
        ptitle <- paste0("Available Dates for ", Arts[[1]])
      }
    }

    if (length(Arts) != 1)
      ptitle <- paste(ptitle, "(higher value means more availability)", sep="\n")

  Values <- DT[.(ind)][, list(Value=sum(Avail)), by=Date][, Value]
  
  ncols <- max(length(Arts)+1, ceiling(diff(range(Values)) / 3))  # about 1/3 of the range

  calendarHeat(DSeq, Values, ncolors=ncols, plot.title=ptitle)
}

MakeHeatMap(CAA.byArtistDate)
