Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/squid/axis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class Axis
include Format
attr_reader :data

def initialize(data, steps:, stack:, format:, &block)
@data, @steps, @stack, @format = data, steps, stack, format
def initialize(data, min:, max:, steps:, stack:, format:, &block)
@data, @min, @max, @steps, @stack, @format = data, min, max, steps, stack, format
@width_proc = block if block_given?
end

Expand Down Expand Up @@ -39,12 +39,14 @@ def label_width(label)
end

def min
return @min if @min
if @data.any? && values.first && values.first.any?
[values.first.min, 0].min
end
end

def max
return @max if @max
if @data.any? && values.last && values.last.any?
closest_step_to values.last.max
end
Expand Down
4 changes: 3 additions & 1 deletion lib/squid/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.boolean
end

def self.integer
-> (value) { value.to_i }
-> (value) { value.to_i if value.present? }
end

def self.symbol
Expand All @@ -59,6 +59,8 @@ def self.array(proc = nil)
labels: {as: array(boolean)},
legend: {as: boolean, default: 'true'},
line_widths: {as: array(float)},
min: {as: integer},
max: {as: integer},
steps: {as: integer, default: '4'},
ticks: {as: boolean, default: 'true'},
type: {as: symbol, default: 'column'},
Expand Down
6 changes: 3 additions & 3 deletions lib/squid/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Squid
class Graph
extend Settings
has_settings :baseline, :border, :chart, :colors, :every, :formats, :height
has_settings :legend, :line_widths, :steps, :ticks, :type, :labels
has_settings :legend, :line_widths, :min, :max, :steps, :ticks, :type, :labels

def initialize(document, data = {}, settings = {})
@data, @settings = data, settings
Expand Down Expand Up @@ -65,7 +65,7 @@ def draw_charts
end

def draw_chart(axis, second_axis: false)
args = {minmax: axis.minmax, height: grid_height, stack: stack?}
args = {minmax: axis.minmax, height: grid_height, stack: stack?, type: type}
args[:labels] = items_of labels, skip_first_if: second_axis
args[:formats] = items_of formats, skip_first_if: second_axis
points = Point.for axis.data, args
Expand Down Expand Up @@ -96,7 +96,7 @@ def right

def axis(first:, last:)
series = @data.values[first, last].map(&:values)
options = {steps: steps, stack: stack?, format: formats[first]}
options = {min: min, max: max, steps: steps, stack: stack?, format: formats[first]}
Axis.new(series, options) {|label| @plot.width_of label}
end

Expand Down
43 changes: 39 additions & 4 deletions lib/squid/point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,52 @@ module Squid
class Point
extend Format

def self.for(series, minmax:, height:, labels:, stack:, formats:)
def self.for(series, minmax:, height:, labels:, stack:, formats:, type:)
@min = Hash.new 0
@max = Hash.new 0
min, max = minmax

case type
when :point
adjusted_series = series.map{|array| array.map{ |n| (minmax[0]..minmax[1]).include?(n) ? n : nil } }
when :line, :two_axis
adjusted_series = series
when :column
adjusted_series = series.map do |array|
array.map do |n|
if (minmax[0]..minmax[1]).include?(n)
n - minmax[0]
elsif n > max
max - min
else
nil
end
end
end
when :stack
adjusted_series = series
end

offset = -> (value) { value * height.to_f / (max-min) }
series.map.with_index do |values, series_i|

case type
when :point
offset = -> (value) { value * height.to_f / (max-min) }
when :line, :two_axis
offset = -> (value) { value * height.to_f / (max-min) }
when :column
offset = -> (value) { value * height.to_f / (max-min) }
when :stack
offset = -> (value) { value * height.to_f / (max-min) }
end

adjusted_series.map.with_index do |values, series_i|
values.map.with_index do |value, i|
h = y_for value, index: i, stack: false, &offset if value
y = y_for value, index: i, stack: stack, &offset if value
y = y - offset.call([min, 0].min) if value
unless type == :column
y = y - offset.call(min) if value
end
label = format_for value, formats[series_i] if labels[series_i]
new y: y, height: h, index: i, label: label, negative: value.to_f < 0
end
Expand All @@ -27,7 +63,6 @@ def initialize(y:, height:, index:, label:, negative:)
end

private

def self.y_for(value, index:, stack:, &block)
if stack
hash = (value > 0) ? @max : @min
Expand Down