2

I am attempting to use Plotly.js in a C# Winforms desktop application from within a WebBrowser toolbox component for the sake of seamlessness, and am having some difficulties. The code is loaded into the component by assigning the literal string to WebBrowser.DocumentText property.

Specifically, I get the following error when the program is run: "Plotly is undefined." The same code, when pasted into a file called index.html, loads and runs correctly in a FireFox browser window when double-clicked. Must be something REALLY simple and newbie or just ignorant.

Here are the contents of the index.html file:

<html>
<head>
<script src = 'plotly-latest.min.js'></script>
</head>
<body>
<div id = "myDiv"></div>
<script>
var myDiv = document.getElementById('myDiv');
var trace = {
x : [9,8,5,1],
y : [1,2,4,8],
z : [11,8,15,3],
mode : 'lines' };
var data = [trace];
Plotly.newPlot(myDiv, data);
</script>
</body>
<html>

Thanks for any assistance you might render (pun intended!).

2 Answers2

1

WebBrowser control by default uses IE6 which is not compatible to Plotly.js. You can make it use IE11 to display your graph using windows registry. You can also do it in code if you need to distribute your app.

You also have an option of ditching WebBrowser control and use third party browser controls like CefSharp (free) or DotNetBrowser (commercial).

imlokesh
  • 2,176
  • 2
  • 20
  • 25
  • Tried the CefSharp component, and it runs correctly (apparently) because I have some alerts in there, but get the same error !! in console message which I trapped. Nice try, though. I'll try the registry method next. – user7191223 Dec 11 '18 at 22:09
  • I tested your code after setting registry values to `00002af9` and it works fine. I did set script src to `https://cdn.plot.ly/plotly-latest.min.js` when testing. – imlokesh Dec 11 '18 at 23:09
  • Tried it again, using your src, and it worked this time. Thanks for your continued patience, matched only by my own perseverence...really WANTED to get this to work!! – user7191223 Dec 11 '18 at 23:25
  • Wonder what the problem is with using a local file...I'm sure that's just another hurdle !! – user7191223 Dec 11 '18 at 23:26
  • You can use `file://` link to load it from local folder. Checkout https://stackoverflow.com/a/7194956/1311748. Good luck! – imlokesh Dec 11 '18 at 23:45
0

Maybe too old to answer but I just tried doing it like this and it seems to work for now:

Using the Plotly.NET package, make a chart and then use chart.SaveHTMLAs<> and give it a name. Then, with a webBrowser object navigate to that html.

Here is my code:

        Plotly.NET.GenericChart.GenericChart point3Dchart =  Plotly.NET.Chart3D.Chart.Point3D<int, int, int, int>(tuppList, "p");
        point3Dchart.SaveHtmlAs<int>("a", null);
        //point3Dchart.Show(); // Will open firefox
        webBrowser1.Navigate(@"C:\...\bin\Debug\a.html");

I'm unsure right now why the SaveHTMLAs is asking for <> but writing int seems to work!

juniper25
  • 23
  • 4