class RubyvisCharts::AbstractTimelineChart

Constants

EXTRA_WEEKEND_BARS_WIDTH
TITLE_TOP_INDENT

Attributes

custom_legend_offset[R]
dates[R]
dates_color[R]
dates_font[R]
dates_formatter[R]
dates_height_ratio[R]
layer_dates[R]
layer_legend[R]
layer_marks[R]
layer_numbers[R]
layer_timeline[R]
layer_title[R]
legend_chars[R]
legend_colors[R]
legend_font[R]
legend_shape[R]
legend_text_color[R]
legend_titles[R]
marks[R]
marks_color[R]
marks_font[R]
marks_formatter[R]
marks_height_ratio[R]
numbers_color[R]
numbers_font[R]
numbers_formatter[R]
rules_color[R]
rules_count[R]
threshold_caption[R]
threshold_color[R]
threshold_number[R]
threshold_width[R]
timeline_width_ratio[R]
title_color[R]
title_font[R]
title_text[R]
weekend_bar_color[R]
y_scale_max[R]

Public Class Methods

new( dates:, marks: DefaultArguments::MARKS, y_scale_max: DefaultArguments::Y_SCALE_MAX, numbers_formatter: DefaultArguments::NUMBERS_FORMATTER, numbers_color: DefaultArguments::NUMBERS_COLOR, numbers_font: DefaultArguments::NUMBERS_FONT, title_text: DefaultArguments::TITLE_TEXT, title_color: DefaultArguments::TITLE_COLOR, title_font: DefaultArguments::TITLE_FONT, dates_formatter: DefaultArguments::DATES_FORMATTER, dates_color: DefaultArguments::DATES_COLOR, dates_font: DefaultArguments::DATES_FONT, marks_color: DefaultArguments::MARKS_COLOR, marks_font: DefaultArguments::MARKS_FONT, marks_formatter: DefaultArguments::MARKS_FORMATTER, rules_color: DefaultArguments::RULES_COLOR, rules_count: DefaultArguments::RULES_COUNT, weekend_bar_color: DefaultArguments::WEEKEND_BAR_COLOR, timeline_width_ratio: DefaultArguments::TIMELINE_WIDTH_RATIO, dates_height_ratio: DefaultArguments::DATES_HEIGHT_RATIO, marks_height_ratio: DefaultArguments::MARKS_HEIGHT_RATIO, legend_titles: DefaultArguments::LEGEND_TITLES, legend_colors: DefaultArguments::LEGEND_COLORS, legend_text_color: DefaultArguments::LEGEND_TEXT_COLOR, legend_font: DefaultArguments::LEGEND_FONT, legend_shape: DefaultArguments::LEGEND_SHAPE, legend_chars: DefaultArguments::LEGEND_CHARS, custom_legend_offset: nil, threshold_number: DefaultArguments::THRESHOLD_NUMBER, threshold_color: DefaultArguments::THRESHOLD_COLOR, threshold_width: DefaultArguments::THRESHOLD_WIDTH, threshold_caption: DefaultArguments::THRESHOLD_CAPTION, **other ) click to toggle source
Calls superclass method RubyvisCharts::AbstractChart::new
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 52
def initialize(
  dates:,
  marks: DefaultArguments::MARKS,
  y_scale_max: DefaultArguments::Y_SCALE_MAX,
  numbers_formatter: DefaultArguments::NUMBERS_FORMATTER,
  numbers_color: DefaultArguments::NUMBERS_COLOR,
  numbers_font: DefaultArguments::NUMBERS_FONT,
  title_text: DefaultArguments::TITLE_TEXT,
  title_color: DefaultArguments::TITLE_COLOR,
  title_font: DefaultArguments::TITLE_FONT,
  dates_formatter: DefaultArguments::DATES_FORMATTER,
  dates_color: DefaultArguments::DATES_COLOR,
  dates_font: DefaultArguments::DATES_FONT,
  marks_color: DefaultArguments::MARKS_COLOR,
  marks_font: DefaultArguments::MARKS_FONT,
  marks_formatter: DefaultArguments::MARKS_FORMATTER,
  rules_color: DefaultArguments::RULES_COLOR,
  rules_count: DefaultArguments::RULES_COUNT,
  weekend_bar_color: DefaultArguments::WEEKEND_BAR_COLOR,
  timeline_width_ratio: DefaultArguments::TIMELINE_WIDTH_RATIO,
  dates_height_ratio: DefaultArguments::DATES_HEIGHT_RATIO,
  marks_height_ratio: DefaultArguments::MARKS_HEIGHT_RATIO,
  legend_titles: DefaultArguments::LEGEND_TITLES,
  legend_colors: DefaultArguments::LEGEND_COLORS,
  legend_text_color: DefaultArguments::LEGEND_TEXT_COLOR,
  legend_font: DefaultArguments::LEGEND_FONT,
  legend_shape: DefaultArguments::LEGEND_SHAPE,
  legend_chars: DefaultArguments::LEGEND_CHARS,
  custom_legend_offset: nil,
  threshold_number: DefaultArguments::THRESHOLD_NUMBER,
  threshold_color: DefaultArguments::THRESHOLD_COLOR,
  threshold_width: DefaultArguments::THRESHOLD_WIDTH,
  threshold_caption: DefaultArguments::THRESHOLD_CAPTION,
  **other
)
  super(other)

  @dates = dates
  @marks = marks
  @y_scale_max = y_scale_max
  @numbers_formatter = numbers_formatter
  @numbers_color = numbers_color
  @numbers_font = numbers_font
  @title_text = title_text
  @title_color = title_color
  @title_font = title_font
  @dates_formatter = dates_formatter
  @dates_color = dates_color
  @marks_color = marks_color
  @marks_formatter = marks_formatter
  @marks_font = marks_font
  @rules_color = rules_color
  @rules_count = rules_count
  @weekend_bar_color = weekend_bar_color
  @timeline_width_ratio = timeline_width_ratio
  @dates_height_ratio = dates_height_ratio
  @marks_height_ratio = marks_height_ratio
  @legend_titles = legend_titles
  @legend_colors = legend_colors
  @legend_text_color = legend_text_color
  @legend_font = legend_font
  @legend_shape = legend_shape
  @legend_chars = legend_chars
  @dates_font = dates_font
  @custom_legend_offset = custom_legend_offset
  @threshold_number = threshold_number
  @threshold_color = threshold_color
  @threshold_width = threshold_width
  @threshold_caption = threshold_caption

  initialize_layers!

  initialize_weekend_bars!
  initialize_numbers!
  initialize_rules!
  initialize_title!
  initialize_dates!
  initialize_marks!
  initialize_threshold!
  initialize_legend!
end

Public Instance Methods

dates_height() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 154
def dates_height
  inner_box_height * dates_height_ratio
end
dates_width() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 150
def dates_width
  timeline_width
end
legend_height() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 178
def legend_height
  legend_titles.any? ? dates_height : 0
end
legend_width() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 174
def legend_width
  timeline_width
end
marks_height() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 158
def marks_height
  inner_box_height * marks_height_ratio
end
marks_width() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 162
def marks_width
  timeline_width
end
numbers_height() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 138
def numbers_height
  timeline_height
end
numbers_width() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 134
def numbers_width
  inner_box_width - timeline_width
end
timeline_height() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 170
def timeline_height
  inner_box_height - dates_height - legend_height - marks_height
end
timeline_width() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 166
def timeline_width
  inner_box_width * timeline_width_ratio
end
title_height() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 146
def title_height
  dates_height
end
title_width() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 142
def title_width
  numbers_width
end

Protected Instance Methods

numbers_max() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 192
def numbers_max
  y_scale_max || values_max
end
numbers_range() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 196
def numbers_range
  @numbers_range ||= if need_extra_tick?
                       Rubyvis::Scale.linear(0, numbers_range_ticks.last + numbers_range_ticks[1])
                         .range(0, timeline_height)
                     else
                       numbers_scaled_heights
                     end
end
numbers_range_ticks() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 209
def numbers_range_ticks
  @numbers_range_ticks ||= numbers_scaled_heights.ticks(rules_count)
end
numbers_scaled_heights() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 205
def numbers_scaled_heights
  @numbers_scaled_heights ||= Rubyvis::Scale.linear(0, numbers_max).range(0, timeline_height)
end
values_max() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 184
def values_max
  @values_max ||= values.flatten.max
end
values_max_count() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 188
def values_max_count
  @values_max_count ||= values.map(&:length).max
end

Private Instance Methods

bars_colors_iterator(index, height, colors) click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 437
def bars_colors_iterator(index, height, colors)
  colors[index % colors.length] if height.nonzero?
end
bars_heights() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 424
def bars_heights
  @bars_heights ||= numbers_range
end
create_layer(width:, height:, top: nil, right: nil, bottom: nil, left: nil) click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 225
def create_layer(width:, height:, top: nil, right: nil, bottom: nil, left: nil)
  parent_layer.panel
    .width(width)
    .height(height)
    .top(top)
    .right(right)
    .bottom(bottom)
    .left(left)
end
custom_legend_width() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 420
def custom_legend_width
  custom_legend_offset ? legend_width - custom_legend_offset : legend_width
end
dates_range() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 408
def dates_range
  @dates_range ||= Rubyvis::Scale.ordinal(Rubyvis.range(dates.length)).split_banded(0, dates_width)
end
graph_width() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 404
def graph_width
  @graph_width ||= Rubyvis::Scale.linear(0, dates.length).range(0, timeline_width)
end
initialize_dates!() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 286
def initialize_dates!
  chart = self

  label_left_indent = -> { index * chart.send(:dates_range).range_band }

  dates_panels = layer_dates.panel
    .data(dates)
    .width(dates_range.range_band)
    .left(label_left_indent)

  dates_panels.add(Rubyvis::Label)
    .text(dates_formatter)
    .textAlign('center')
    .textBaseline('middle')
    .font(dates_font)
    .textStyle(dates_color)
end
initialize_layers!() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 215
def initialize_layers!
  @layer_title    = create_layer(width: title_width, height: title_height, left: 0, top: 0)

  @layer_marks    = create_layer(width: marks_width,    height: marks_height,    top: 0,                              right: 0)
  @layer_numbers  = create_layer(width: numbers_width,  height: numbers_height,  top: marks_height,                   left:  0)
  @layer_timeline = create_layer(width: timeline_width, height: timeline_height, top: marks_height,                   right: 0)
  @layer_dates    = create_layer(width: dates_width,    height: dates_height,    top: marks_height + timeline_height, right: 0)
  @layer_legend   = create_layer(width: legend_width,   height: legend_height,   bottom: 0,                           right: 0)
end
initialize_legend!() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 345
def initialize_legend!
  return if legend_titles.empty?

  chart = self

  legend_left_indent = -> { index * chart.send(:legend_range).range_band }
  legend_text = -> { chart.send(:legend_titles)[self.parent.index] }
  legend_color = -> { chart.send(:legend_colors)[self.parent.index] }

  legend_panels = layer_legend.panel
    .data(legend_titles)
    .top(10)
    .width(legend_range.range_band)
    .left(legend_left_indent)

  if legend_chars.blank?
    legend_panels.add(Rubyvis::Dot)
      .shape(legend_shape)
      .left(5)
      .fillStyle(legend_color)
      .strokeStyle(legend_color)
  else
    legend_char = -> { chart.send(:legend_chars)[self.parent.index] }

    legend_panels.add(Rubyvis::Label)
      .text(legend_char)
      .left(-3)
      .textAlign('left')
      .textStyle(legend_color)
      .textBaseline('middle')
      .font(legend_font)
  end

  legend_panels.add(Rubyvis::Label)
    .text(legend_text)
    .left(10)
    .textStyle(legend_text_color)
    .textBaseline('middle')
    .font(legend_font)
end
initialize_marks!() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 304
def initialize_marks!
  return if marks.empty?

  chart = self

  label_left_indent = -> { index * chart.send(:marks_range).range_band }

  marks_panels = layer_marks.panel
    .data(marks)
    .width(marks_range.range_band)
    .left(label_left_indent)

  marks_panels.add(Rubyvis::Label)
    .text(marks_formatter)
    .textAlign('center')
    .textBaseline('middle')
    .font(marks_font)
    .textStyle(marks_color)
end
initialize_numbers!() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 250
def initialize_numbers!
  numbers_rules = layer_numbers.rule
    .data(numbers_ticks)
    .right(0)
    .width(0)
    .bottom(numbers_range)

  numbers_rules.add(Rubyvis::Label)
    .text(numbers_formatter)
    .textAlign('right')
    .textBaseline('middle')
    .font(numbers_font)
    .textStyle(numbers_color)
end
initialize_rules!() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 265
def initialize_rules!
  layer_timeline.rule
    .data(rules_ticks)
    .left(0)
    .right(0)
    .bottom(rules_range)
    .strokeStyle(rules_color)
end
initialize_threshold!() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 324
def initialize_threshold!
  return if threshold_number.nil?

  scaled_threshold = threshold_number * timeline_height / numbers_ticks.last

  threshold_rule = layer_timeline.rule
    .left(0)
    .right(0)
    .bottom(scaled_threshold)
    .strokeStyle(threshold_color)
    .lineWidth(threshold_width)

  threshold_rule.add(Rubyvis::Label)
    .left(timeline_width)
    .text(threshold_caption)
    .textAlign('left')
    .textBaseline('middle')
    .font(numbers_font)
    .textStyle(numbers_color)
end
initialize_title!() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 274
def initialize_title!
  return if title_text.nil?

  layer_title.add(Rubyvis::Label)
    .text(title_text)
    .top(TITLE_TOP_INDENT)
    .left(0)
    .textBaseline('middle')
    .font(title_font)
    .textStyle(title_color)
end
initialize_weekend_bars!() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 235
def initialize_weekend_bars!
  chart = self

  bar_left_indent = -> { index * chart.send(:weekend_bars_range).range_band }
  fill_style_colors = ->(timestamp) { chart.send(:weekend_bars_colors, timestamp) }

  layer_timeline.add(Rubyvis::Bar)
    .data(dates)
    .width(weekend_bars_range.range_band + EXTRA_WEEKEND_BARS_WIDTH)
    .height(timeline_height + dates_height)
    .left(bar_left_indent)
    .bottom(-dates_height)
    .fillStyle(fill_style_colors)
end
legend_range() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 416
def legend_range
  @legend_range ||= Rubyvis::Scale.ordinal(Rubyvis.range(legend_titles.length)).split_banded(0, custom_legend_width)
end
marks_range() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 412
def marks_range
  dates_range
end
need_extra_tick?() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 428
def need_extra_tick?
  numbers_max > numbers_range_ticks.last
end
numbers_ticks() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 386
def numbers_ticks
  ticks = numbers_range_ticks.length
  ticks += 1 if need_extra_tick?
  numbers_range.ticks(ticks)
end
rules_range() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 392
def rules_range
  numbers_range
end
rules_ticks() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 396
def rules_ticks
  numbers_ticks
end
weekend_bars_colors(timestamp) click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 432
def weekend_bars_colors(timestamp)
  time = Time.at(timestamp)
  weekend_bar_color if time.sunday? || time.saturday?
end
weekend_bars_range() click to toggle source
# File lib/rubyvis_charts/abstract_timeline_chart.rb, line 400
def weekend_bars_range
  dates_range
end