fslaborg / fslaborg/XPlot

API for incremental updates

Open
#152 1 comment 0 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
F#
Stars
289
Forks
70
PR merge metrics
No merged PRs in 30d

Description

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:

```fsharp
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:

```fsharp
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:
```csharp
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:

```fsharp
Func -> 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 `With`function 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:
```csharp
Chart Chart.With(Func 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..

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.