# @(#) $Id$

# Draw a histogram given values, colors and labels for each bar,
# the maximum value to be shown in a bar and the size of the histogram.
#
begin histogram colors, labels, values, maxValue, size

  local x, y, numValues

  font "Helvetica", 3
  linestyle 0.01
  let y = 1, numValues = length(values)

  # Draw histogram in square, numValues units wide.
  # Scale this to desired size.
  #
  scale size / numValues

  while y <= numValues
  do
    # Display colored bar with width dependent on the value it shows.
    #
    clearpath
    color colors[y]
    let x = values[y] / maxValue * numValues
    box 0, y - 1, x, y
    fill
    color "black"
    stroke
    clearpath

    # Position label to the right of the bar.
    #
    move x + 0.3, y - 0.8
    label labels[y]
    let y = y + 1
  done

  # Display a border for the histogram.
  #
  clearpath
  color "grey"
  move numValues, 0
  draw 0, 0, 0, numValues
  stroke
end
