Documentation icon indicating copy to clipboard operation
Documentation copied to clipboard

Add C# Plotting Examples in Research

Open AlexCatarino opened this issue 3 years ago • 2 comments

Expected Behavior

To have examples of C# plotting libraries, e.g. Plotly.NET.

Actual Behavior

QuantConnect images support this library, but we haven't been able to make it work in the coding environment yet.

Checklist

  • [x] I have completely filled out this template
  • [x] I have confirmed that this issue exists on the current master branch
  • [x] I have confirmed that this is not a duplicate issue by searching issues

AlexCatarino avatar Oct 03 '22 23:10 AlexCatarino

Is it possible that the Plotly.NET assemblies are not getting loaded by the notebook environment?

tactevo avatar Oct 07 '22 16:10 tactevo

I got it to work. There are two issues 1) The Plotly assemblies are not automatically loaded by the research notebook environment, 2) The .Show() method does not work with QC Notebooks (not sure why)

As a workaround for now, do the following.

  1. Manually load the Plotly.NET assembly
#r "/home/jovyan/Plotly.NET.dll"

// Plotly.NET.Interactive.dll is not needed for the workaround
// #r "/home/jovyan/Plotly.NET.Interactive.dll"

  1. Instead of calling the .Show method on a GenericChart object, generate HTML directly and use the notebook HTML() function to display HTML inline.

For instance (showing what works and what doesn't)

using Plotly.NET;
using Plotly.NET.LayoutObjects;

// Plotly.NET.Interactive is not needed for the workaround
// using Plotly.NET.Interactive;

double[] x = new double[] { 1, 2 };
double[] y = new double[] { 5, 10 };

LinearAxis xAxis = new LinearAxis();
xAxis.SetValue("title", "xAxis");
xAxis.SetValue("showgrid", false);
xAxis.SetValue("showline", true);

LinearAxis yAxis = new LinearAxis();
yAxis.SetValue("title", "yAxis");
yAxis.SetValue("showgrid", false);
yAxis.SetValue("showline", true);

Layout layout = new Layout();
layout.SetValue("xaxis", xAxis);
layout.SetValue("yaxis", yAxis);
layout.SetValue("showlegend", true);

Trace trace = new Trace("scatter");
trace.SetValue("x", x);
trace.SetValue("y", y);
trace.SetValue("mode", "markers");
trace.SetValue("name", "Hello from C#");

var c = GenericChart
    .ofTraceObject(true, trace)
    .WithLayout(layout);

// DOES NOT WORK
// c.Show()

// DOES WORK
HTML(GenericChart.toChartHTML(c))

tactevo avatar Oct 09 '22 17:10 tactevo