module Gle
def bar(filename, array_of_series_names, array_of_series_value_arrays)
File.open("gle/#{filename}.gle", "w") do |file|
file.write bar_src(filename, *generate_bar_src_params(array_of_series_names, array_of_series_value_arrays))
end

File.open("gle/#{filename}.dat", "w") do |file|
file.write bar_data(array_of_series_value_arrays)
end

puts `rm gle/#{filename}.png` # Clear any previous png so we know if something has gone wrong.
puts `gle -r 300 -d png -o gle/#{filename}.png gle/#{filename}.gle`
File.read("gle/#{filename}.png")
end

def generate_bar_src_params(array_of_series_names, array_of_series_value_arrays)
all_values = array_of_series_value_arrays.flatten
[array_of_series_names[0].to_s.titleize, all_values.min, all_values.max]
end

def bar_data(series)
raise "Data series should be of same length." unless (series[0].size === series[1].size)
str = ""
(0..(series[0].size-1)).each do |i|
str << "#{i+1} #{series[0][i]} #{series[1][i]}\n"
end
str
end

def bar_src(filename, series_name, series_min, series_max)
%{! Requires 4.0.13
! THIS FILE IS AUTOGENERATED BY Gle::bar_src

size 12 8
set font ss
include "barstyles.gle"
set lwidth 0.01

sub local_palette z r g b
! r, g, b is the color of the gradient which will fade towards
! white/grey where (y,y,y) is the shade of white/grey.
! i.e. y = 1 will fade to white
local y = 1
return cvtrgb((z*0.7+0.3)*(r/255-1)+y,(z*0.7+0.3)*(g/255-1)+y,(z*0.7+0.3)*(b/255-1)+y)
end sub

sub custom_palette z
return local_palette(z, global_red, global_blue, global_green)
end sub

! cr cb cg will be passed to rgb255
sub customcolormap x1 y1 x2 y2 b g cr cb cg
global_red = cr
global_blue = cb
global_green = cg
amove x1 y1
begin clip
begin path clip
box x2-x1 y2-y1
end path
colormap y 0 1 0 1 1 200 x2-x1 y2-y1 palette custom_palette
end clip
end sub

sub bar_purplecolormap x1 y1 x2 y2 b g
customcolormap x1 y1 x2 y2 b g 94 19 175
end sub

sub bar_yellowcolormap x1 y1 x2 y2 b g
customcolormap x1 y1 x2 y2 b g 255 255 0
end sub

sub seriesmarkersub size mdata multiplier
gsave
set hei 0.23 just center
t$ = format$(mdata, "fix 2") + "%"
rmove multiplier*0.45 0.1
write t$
set hei 0.3633 ! restore default
grestore
end sub

sub series1markersub size mdata
seriesmarkersub size mdata -1
end sub

sub series2markersub size mdata
seriesmarkersub size mdata 1
end sub

define marker series1marker series1markersub
define marker series2marker series2markersub

begin graph
ytitle "Performance"
data "gle/#{filename}.dat"
xaxis min 0.5 max 4.5 dticks 1 ! This is fixed, since we are always plotting 4 data points.
yaxis min #{series_min - 5} max #{series_max + 15} dticks 10
xnames "1 Month" "YTD" "1 Year" "Inception"
xdsubticks off
x2axis off
y2axis off
yaxis grid
yticks color grey20
bar d1,d2 width 0.3,0.3 style purplecolormap,yellowcolormap

d1 marker series1marker mdata d1
d2 marker series2marker mdata d2
end graph

begin key
position bc
offset 0 -0.5
nobox
base 0.25
hei 0.25
coldist 0.5
text "#{series_name}" fill rgb255(97,19,175) separator
text "World Index" fill rgb255(255,255,0)
end key
}
end
end