plotly / Plotly.NET

interactive graphing library for .NET programming languages :chart_with_upwards_trend:
https://plotly.net
MIT License
654 stars 84 forks source link

Candlestick example in C# #297

Closed dharmatech closed 2 years ago

dharmatech commented 2 years ago

I have a simple program that uses Plotly.NET to create a candlestick chart of S&P 500 data:

https://github.com/dharmatech/PlotlyNetCandlestickCsv/blob/master/PlotlyNetCandlestickCsv/Program.cs

The output in a browser window is as follows:

image

Questions

Tuple.Create vs C# tuples

Note that here I had to use Tuple.Create instead of a native C# tuple:

var seq = items.Select(elt =>
    Tuple.Create(
        elt.DateTime,
        StockData.Create((double)elt.Open, (double)elt.High, (double)elt.Low, (double)elt.Close)
    ));

Will we be able to use C# tuples after https://github.com/plotly/Plotly.NET/issues/296 is implemented?

WithYAxis

Note that the call to WithYAxis is currently quite verbose:

.WithYAxis(LinearAxis.init<IConvertible, IConvertible, IConvertible, IConvertible, IConvertible, IConvertible>(
    FixedRange: false))

Are there plans to make this more concise as part of https://github.com/plotly/Plotly.NET/issues/296?

Responsive layout vs hardcoded

I currently have the chart size hardcoded via:

.WithSize(1800, 900)

It would be nice to have the chart resize based on the size of the browser window. I added the following:

.WithConfig(Config.init(Responsive: true))

but it doesn't seem to have the intended effect.

Zoom with mouse wheel

Is there a way to enable zoom via the mouse wheel?

Comments

Any suggestions regarding making the example a more idiomatic demonstration of Plotly.NET are welcome. πŸ™‚

Thank you for Plotly.NET.

kMutagene commented 2 years ago

Thanks for your questions/feedback, i did not know that there are differences between tuples for both languages.

Will we be able to use C# tuples after https://github.com/plotly/Plotly.NET/issues/296 is implemented?

No, we will focus on the overloads that take the dimensions of the data as separate arguments (so in the case of candlestick, that would be the one taking ohlc and time data as separate collections each). The reason for this is simply the large overhead of writing 2+ bindings for each chart type instead of initially just 1. This way, all chart types get at least 1 native C# binding faster. The overloads with tupled arguments will be next.

Note that the call to WithYAxis is currently quite verbose

Not much to be done about this, that's just how C# works with generics. The native C# binding will however provide names for the type arguments, so the correct types it will be easier to infer from intellisense (see #285 for more details).

Is there a way to enable zoom via the mouse wheel?

Scrolling via zoom is a plotly property settable on the Config object, which is currently not directly supported via Config.init - there are some other missing attributes, 'ill open a separate issue for that. You can however set it manually (this is true for everything that has no direct binding equivalent):

using Plotly.NET;

var config = new Config();
config.SetValue("scrollZoom",true);

Chart2D.Chart.Point<int,int,string>(
    x: new int [] {1,2},
    y: new int [] {1,2}
).WithConfig(config);

Any suggestions regarding making the example a more idiomatic demonstration of Plotly.NET are welcome.

I would redirect this right back at you πŸ˜‰ - I don't know how idiomatic usage of Plotly.NET in C# should look like from the C# user perspective. One thing i take away from this issue is the usage of C# tuples for the overloads that take tupled data. Plotly.NET is written in F# with a C# compatibility layer, which comes with some awkwardness for C#. Some of this is solved (see #285), but any kind of feedback is greatly appreciated here. There will be a preview package of the current C# API soon.

kMutagene commented 2 years ago

Closing this, as there is now a C# binding available for Chart.Candlestick (and all other finance charts).