diff --git a/src/transforms.jl b/src/transforms.jl index 4cf9a4c..f978939 100644 --- a/src/transforms.jl +++ b/src/transforms.jl @@ -200,13 +200,104 @@ function make_add_const_function(constant::Real) end Base.:+(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(add_cols_fn) -Base.:+(s1::Symbol, s2::Real) = s1 => AesTransform(make_add_const_function(s2)) -Base.:+(s2::Real, s1::Symbol) = s1 => AesTransform(make_add_const_function(s2)) -#import Base.:- -#import Base.:/ -#import Base.:* +Base.:+(s1::Symbol, s2::Real) = [s1] => AesTransform(make_add_const_function(s2)) +Base.:+(s2::Real, s1::Symbol) = [s1] => AesTransform(make_add_const_function(s2)) +import Base.:- +function subtract_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame) + result = data[!, source[1]] .- data[!, source[2]] + return Dict{Symbol, PlottableData}( + target => PlottableData( + result, + identity, + nothing, + nothing + ) + ) +end + +function make_subtract_const_function(constant::Real) + return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame) + result = data[!, source[1]] .- constant + return Dict{Symbol, PlottableData}( + target => PlottableData( + result, # get the column out of the dataframe + identity, # apply generic_fn to it + nothing, + nothing + ) + ) + end +end + +Base.:-(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(subtract_cols_fn) +Base.:-(s1::Symbol, s2::Real) = [s1] => AesTransform(make_subtract_const_function(s2)) +Base.:-(s2::Real, s1::Symbol) = [s1] => AesTransform(make_subtract_const_function(s2)) + +import Base.:/ + +function divide_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame) + result = data[!, source[1]] ./ data[!, source[2]] + return Dict{Symbol, PlottableData}( + target => PlottableData( + result, + identity, + nothing, + nothing + ) + ) +end + +function make_divide_const_function(constant::Real) + return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame) + result = data[!, source[1]] ./ constant + return Dict{Symbol, PlottableData}( + target => PlottableData( + result, # get the column out of the dataframe + identity, # apply generic_fn to it + nothing, + nothing + ) + ) + end +end + +Base.:/(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(divide_cols_fn) +Base.:/(s1::Symbol, s2::Real) = [s1] => AesTransform(make_divide_const_function(s2)) +Base.:/(s2::Real, s1::Symbol) = [s1] => AesTransform(make_divide_const_function(s2)) + +import Base.:* + +function multiply_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame) + result = data[!, source[1]] .* data[!, source[2]] + return Dict{Symbol, PlottableData}( + target => PlottableData( + result, + identity, + nothing, + nothing + ) + ) +end + +function make_multiply_const_function(constant::Real) + return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame) + result = data[!, source[1]] .* constant + return Dict{Symbol, PlottableData}( + target => PlottableData( + result, # get the column out of the dataframe + identity, # apply generic_fn to it + nothing, + nothing + ) + ) + end +end + +Base.:*(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(multiply_cols_fn) +Base.:*(s1::Symbol, s2::Real) = [s1] => AesTransform(make_multiply_const_function(s2)) +Base.:*(s2::Real, s1::Symbol) = [s1] => AesTransform(make_multiply_const_function(s2)) # tweaks