0

I have a webview in my page where I'm showing a chart(Fusion Chart). I wrote a html page with js function as follows:

<html lang="en">
<head>
<script src="file:///android_asset/fusioncharts.js"></script>
<script src="file:///android_asset/fusioncharts.charts.js"></script>
</head>
<body>
<div id="chart-container"></div>

<script>
    function loadChart(chartData) {

        const dataSource = {
            chart: {
                theme: "fusion",
                "showPlotBorder": "1",
                "plotBorderColor": "#ffffff"
            },
            data: chartData
        };

        FusionCharts.ready(function () {
            var myChart = new FusionCharts({
                type: "column2d",
                renderAt: "chart-container",
                width: "100%",
                height: "100%",
                dataFormat: "json",
                dataSource
            }).render();
        });
    }

    loadChart(chartData);
</script>
</body>
</html>

I need to pass data to the loadchart function from my shared project with the help of my webview. Is this possible?

Saamer
  • 4,687
  • 1
  • 13
  • 55
Sai Sunkari
  • 179
  • 3
  • 27
  • Hi , do you mean want to pass stream data(chart data) from forms to Html ? – Junior Jiang Nov 08 '19 at 05:25
  • @JuniorJiang-MSFT Yes. – Sai Sunkari Nov 11 '19 at 16:44
  • Okey , here is a [swift method](https://stackoverflow.com/questions/32113933/how-do-i-pass-a-swift-object-to-javascript-wkwebview-swift) for reference . You can use [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript?language=objc) to passing data from native to html . You can have a try with transforming swift to c# . – Junior Jiang Nov 12 '19 at 02:24

1 Answers1

1

There are so many ways of doing this on Android, though I would caution you to test it well on different devices depending on what default browser is being used for the WebView.

Luckily, since you are using Xamarin Forms, you could just use the Javascript Evaluation function that enables you to just put your JS code inside the function. And then time it right, and you can call a JS function which will take values from your C#.

Here's the documentation by MS on the functions Eval() or EvaluateJavaScriptAsync().

Here are some examples of it's usage:

webView.EvaluateJavaScriptAsync(string.Format("document.getElementById(\"{0}\").value = \"{1}\"", "langs1", from));
webView.EvaluateJavaScriptAsync(string.Format("DDMENU(0)"));

Let me know if you have any questions!

Saamer
  • 4,687
  • 1
  • 13
  • 55
  • thanks it worked in xam android. Can you suggest any links to implement in Xamarin iOS like where to put .js and html files in iOS project? – Sai Sunkari Nov 13 '19 at 23:39
  • This same solution should work for Xamarin iOS as well. If it doesn't then you have a separate problem. Can you ask that in a separate question and tag it over here? – Saamer Nov 14 '19 at 15:51