Example 2 – AbstractPlotting.jl ConversionTrait

AbstractPlotting.jl is an abstract plotting library that is part of the Makie plotting system. The source code for this library can be found at https://github.com/JuliaPlots/AbstractPlotting.jl.

Let's take a look at a trait that's related to data conversion:

abstract type ConversionTrait end

struct NoConversion <: ConversionTrait end
struct PointBased <: ConversionTrait end
struct SurfaceLike <: ConversionTrait end

# By default, there is no conversion trait for any object
conversion_trait(::Type) = NoConversion()
conversion_trait(::Type{<: XYBased}) = PointBased()
conversion_trait(::Type{<: Union{Surface, Heatmap, Image}}) = SurfaceLike()

It defines a ConversionTrait that can be used for the convert_arguments function. As it stands, the conversion logic can be applied to three different scenarios:

  1. No conversion. This is handled by the default trait type of NoConversion.
  2. PointBased conversion.
  3. SurfaceLike conversion.

By default, the convert_arguments function just returns the arguments untouched when conversion is not required:

# Do not convert anything if there is no conversion trait
convert_arguments(::NoConversion, args...) = args

Then, various convert_arguments functions are defined. Here is the function for 2D plotting:

"""
convert_arguments(P, x, y)::(Vector)

Takes vectors `x` and `y` and turns it into a vector of 2D points of the values
from `x` and `y`.

`P` is the plot Type (it is optional).
"""
convert_arguments(::PointBased, x::RealVector, y::RealVector) = (Point2f0.(x, y),)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.233.41