Sha256: c04f9cae27cfd5d9143fd5c24455a7f05b2712174a7fbe63a9275e46347c102f

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

module RubyvisCharts
  class DonutChart < AbstractChart
    module DefaultArguments
      CONES_COLORS = ["#cf2030", "#F73000", "#ffcc00", "#64bbe3", "#6cc04a", "#f2f2f2"].freeze

      OUTER_RADIUS = 120
    end

    attr_reader :cones_colors, :outer_radius, :inner_radius, :percentage_key

    def initialize(
      cones_colors: DefaultArguments::CONES_COLORS,
      outer_radius: DefaultArguments::OUTER_RADIUS,
      inner_radius: DefaultArguments::OUTER_RADIUS * 0.7,
      percentage_key: :percentage,
      **other
    )
      super(**other)
      @outer_radius = outer_radius
      @inner_radius = inner_radius
      @cones_colors = cones_colors
      @percentage_key = percentage_key

      initialize_cones!
    end

    private

    def initialize_cones!
      chart = self
      angle = ->(segment_hash) { segment_hash[chart.percentage_key].to_f * 2 * Math::PI }
      fill_style = -> { chart.cones_colors[self.index] || "##{SecureRandom.hex(3)}"}

      parent_layer.add(Rubyvis::Wedge)
        .data(values)
        .left(width / 2)
        .top(height / 2)
        .innerRadius(inner_radius)
        .outerRadius(outer_radius)
        .angle(angle)
        .fillStyle(fill_style)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubyvis_charts-0.1.6 lib/rubyvis_charts/donut_chart.rb