Skip to content

API for incremental updates #152

@malisimo

Description

@malisimo

It would be really useful if XPlot had a more intellisense-friendly way of discovering a chart's capabilities, and making incremental updates to the properties of a chart or chart element.

It would also be useful if this way of working with charts were uniform across the different backends (Plotly and GoogleCharts) which allowed you to switch between without necessarily learning the differences in API's.

Solution

Would this API change / addition be of interest?
Rather than specifying all upfront, as in:

let trace1 =
    Scatter(
        x = [0.; 1.; 2.; 3.],
        y = [0.; 2.; 1.; 4.],
        line = Line(
            color="rgb(22, 94, 184)",
            width=3
        )
    )

let trace2 =
    Scatter(
        x = [0.; 1.; 2.; 3.],
        y = [1.; 3.; 2.5; 6.]
    )

let chart =
    [trace1; trace2]
    |> Chart.Plot

You could do something like:

let trace1 =
    Scatter(
        x = [0.; 1.; 2.; 3.],
        y = [0.; 2.; 1.; 4.]
    )

let trace2 =
    Scatter(
        x = [0.; 1.; 2.; 3.],
        y = [1.; 3.; 2.5; 6.]
    )

let chart =
    [trace1; trace2]
    |> Chart.Plot
    |> Chart.With (chart.traces.[0].asScatter.line.width 3)
    |> Chart.With (chart.traces.[0].asScatter.line.color "rgb(22, 94, 184)")

This way you could dot into the chart object and get intellisense to find all the statically typed properties available to you.
Such an approach would also feel similar no matter the plotting library used to render the charts.

It would work in C# like this:

var chart =
    Chart.Plot(traceList)
    .With(chart.traces[0].asScatter.line.width(3))
    .With(chart.traces[0].asScatter.line.color("rgb(22, 94, 184)"));

Implementation

The Chart.With function (used in the F# version) would need the following signature:

 Func<Chart,Chart> -> Chart -> Chart

where the Func is a function which, given a chart, makes a copy and sets a property value in this chart before returning it. Therefore, the Withfunction is taking this function, and also a chart object to apply it to, and returning a new updated chart by applying the function the the chart object it is passed.

A C# version would better suit an instance method approach:

Chart Chart.With(Func<Chart, Chart> applyPropFun)

Final

I do have a proof of concept (using reflection) which suggests this approach could work really well, but wanted to sound it out before going any deeper.

Any suggestions, improvements?

This would really bloat the generated codebase, as properties (classes) would need to be defined for every chart element that could be set, at any point in the hierarchy. Understandably therefore, it may not be the direction that the current contributors wish to take it..

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions