AngleSharp / AngleSharp.Js

:angel: Extends AngleSharp with a .NET-based JavaScript engine.
https://anglesharp.github.io
MIT License
103 stars 22 forks source link

I want to use third party js plugin draw chart,AngleSahrp can do it? #70

Closed JohnnyZhang0628 closed 3 years ago

JohnnyZhang0628 commented 4 years ago

I am planning to do draw chart plugins for .net. I want to use echarts.js . this is my code,i want get base64Pic variable ,then i will get awesome chart image by using C# .But i got undefined.What should i do ?

    var source = @"<!doctype html>
        <html>
        <head><title>Event sample</title></head>
        <body>
   <div style='width:800px;height:600px' id='main'>

    </div>
    <script src='echarts.min.js'></script>
    <script>

        var myChart = echarts.init(document.getElementById('main'))
        var option = {
            xAxis: {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                data: [820, 932, 901, 934, 1290, 1330, 1320],
                type: 'line'
            }]
        };
        myChart.setOption(option)

        var base64Pic = ''
        myChart.on('finished', function () {
            console.log('echarts render finished!');
            base64Pic = myChart.getDataURL()
        })

    </script>
</body>
</html>

";

           var config = Configuration.Default.WithJs().WithConsoleLogger(context =>new StandardConsoleLogger())
                .WithEventLoop();

            var context = BrowsingContext.New(config);

            var document = await context.OpenAsync(req => req.Content(source)).WaitUntilAvailable();

            var jsEngine = context.GetService<JsScriptingService>();

            string base64Pic = jsEngine.EvaluateScript(document, "base64Pic").ToString();

            Console.WriteLine($"base64Pic: {base64Pic}");
FlorianRappl commented 3 years ago

The canvas APIs are not implemented.

For these we most likely also need some rendering capabilities first; which do not come out of the box with AngleSharp.Js.