R/identify.R

Defines functions draw_identify

Documented in draw_identify

#' Draw the identify labels under the mouse
#'
#' The identify labels are generated by specific plotting functions, and this
#' function draw them as texts under the mouse, with a transparent gray
#' background.
#'
#' These components in the meta data are required to draw the labels:
#' \code{meta$pos} (mouse position), \code{meta$limits} (layer limits) and
#' \code{meta$identify.labels} (labels to draw). The color of the labels is set
#' in the \code{\link{brush}} object as \code{brush(data)$label.color}.
#'
#' The label will automatically adjust its own positions to ensure it does not
#' exceed the plot margin. For example, when the mouse is near the right margin,
#' the label will be drawn to the left.
#' @param layer the layer argument of the painting function
#' @param painter the painter argument of the painting function
#' @param data the data object used by the plot
#' @param meta the meta object in the plot
#' @return \code{NULL}
#' @export
#' @author Yihui Xie <\url{http://yihui.name}>
#' @examples ## see source code of qparallel() or qhist()
draw_identify = function(layer, painter, data, meta) {
  if (!length(meta$identify.labels)) return()
  b = brush(data)
  qfont(painter) = Qt$QFont('Monospace')
  bgwidth = qstrWidth(painter, meta$identify.labels)[1]
  bgheight = qstrHeight(painter, meta$identify.labels)[1]
  # adjust drawing directions when close to the boundary
  hflag = meta$limits[2] - meta$pos[1] > bgwidth  # draw to the right
  vflag = meta$pos[2] - meta$limits[3] > bgheight # draw to the bottom
  # add an offset to move text labels a little bit away from the cursor
  pos = meta$pos + 10 * one_pixel(painter) * c(ifelse(hflag, 1, -1), ifelse(vflag, -1, 1))
  qdrawRect(painter, pos[1], pos[2],
            pos[1] + ifelse(hflag, 1, -1) * bgwidth,
            pos[2] + ifelse(vflag, -1, 1) * bgheight,
            stroke = rgb(1, 1, 1, 0.8), fill = rgb(1, 1, 1, 0.8))
  qstrokeColor(painter) = b$label.color
  qdrawText(painter, meta$identify.labels, pos[1], pos[2],
            halign = ifelse(hflag, "left", "right"),
            valign = ifelse(vflag, "top", "bottom"))
}
ggobi/cranvas documentation built on May 17, 2019, 3:10 a.m.