plotly / Plotly.NET

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

Compile error while building a C# proyect with Plotly.NET library using C# binding #390

Closed ivanjimenez closed 1 year ago

ivanjimenez commented 1 year ago

Description

Can't build a simple code for Plotly.NET C# Binding in a rage plot graph:

https://plotly.net/02_3_range-plots.html

I am trying to translate F# code to C#

image

Repro steps

  1. I am trying to make a Range Graph with code provided in F# translated to C#:
...
using Plotly.NET.CSharp;
using Microsoft.Web.WebView2;
using Microsoft.Web.WebView2.WinForms;
using Plotly.NET.TraceObjects;
using Plotly.NET;
...

        Plotly.NET.Chart2D.Chart.Range(
             x: new double[] { 1, 2 },
             y: new double[] { 5, 10 },
             upper: X,
             lower: Y,
             mode: StyleParam.Mode.Lines_Markers,
            MarkerColor: System.Drawing.Color.FromName("Blue"),
            RangeColor : System.Drawing.Color.FromName("Red")

         )
         .WithTraceInfo("Hello from C#", ShowLegend: true)
         .WithXAxisStyle<double, double, string, double>(Title: Plotly.NET.Title.init("xAxis"))
         .WithYAxisStyle<double, double, string, double>(Title: Plotly.NET.Title.init("yAxis"))
         .SaveHtml(strPath);
  1. There is an error in IDE

image

Expected behavior

With other charts this library works

Actual behavior

Maybe there is an error in the argument, but with no documentation for API in C# it results tricky.

Known workarounds

Unknown

Related information

kMutagene commented 1 year ago

Your issue is three-fold:

  1. This is not an error, it is how C# wants you to annotate Generics: in the case of the method you are using, you must annotate 7 Generics. In F# you do not have to do that, and this is the main reason why the C# bindings even exist, instead of further optimizing the F# API to be directly consumed from C#.

  2. The Color type must be Plotly.NET.Color.

  3. While you reference Plotly.NET.CSharp, you are not using the Chart method from it. I'd suggest not opening the Plotly.NET root namespace at all if you want to use native C# bindings to avoid confusion (many things are named the same naturally).

    While the sample documentation is not ported, the API reference of Plotly.NET.CSharp is documented extensively. Here is the section on Chart.Range: https://plotly.net/reference/plotly-net-csharp-chart.html#Range

    Here is a working example:

    image

    Note that you have to annotate less (3) Generics with the actual C# binding, and they have helpful names:

    image

ivanjimenez commented 1 year ago

Hi,

Thanks for your explanations. I will follow your indications. Although Plotly C# Api is well documented, I think is a little messy and there are not many examples of using it. You must convert a lot of F# examples in C#. F# is not complicated, but I miss more resources about fantastic plotly .net c# library.

Thanks a bunch!