Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for incremental updates #152

Open
malisimo opened this issue Oct 11, 2020 · 1 comment
Open

API for incremental updates #152

malisimo opened this issue Oct 11, 2020 · 1 comment

Comments

@malisimo
Copy link

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..

@malisimo
Copy link
Author

malisimo commented Nov 2, 2020

... so I've made an initial demonstration of the API here:

https://github.com/malisimo/IPlot

Very bare bones right now. Ideally I wouldn't want to make an entirely new library for this change, and would love to contribute to XPlot development instead! However:

  • I'm not certain this is the direction that the current maintainers wish for XPlot, and
  • I actually generated some of the code in C# for a few (probably silly) reasons

Nonetheless, if anyone is interested I'd be very happy to discuss ways of incorporating this work into XPlot.
Thanks for the wonderful work done on this library!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant