【问题标题】:Chart.js not displaying on Android Webview if animation is set to false如果动画设置为 false,Chart.js 不会在 Android Webview 上显示
【发布时间】:2014-10-15 09:59:49
【问题描述】:

我有一个启用了 javascript 的 WebView 和一个 ChromeWebClient 并且他们很好地显示了 Chart.Js 饼图示例。在我将选项设置为动画后:false,然后停止显示图表。

var pieOptions = {
            animation : false
        }
        window.onload = function(){
            var ctx1 = document.getElementById("chart-area1").getContext("2d");
            window.myPie1 = new Chart(ctx1).Pie(pieData,pieOptions);

            var ctx2 = document.getElementById("chart-area2").getContext("2d");
            window.myPie2 = new Chart(ctx2).Pie(pieData);
        };

第一个带有禁用动画的饼图不显示,第二个显示。两者都在桌面 Chrome 上显示良好。顺便说一句,我使用的是 Android 4.4.4 设备。点击无动画图表应该在的位置后,它会显示自己(我猜想用触摸事件刷新图表)。

是我遗漏了什么还是这是一个错误?

【问题讨论】:

    标签: javascript android webview chart.js


    【解决方案1】:

    我在Android WebView renders blank/white 上尝试了这个答案,它解决了这个问题。基本上我们告诉 WebView 避免使用硬件渲染:

    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    

    【讨论】:

      【解决方案2】:

      除了接受的答案之外,我还必须为我的 webView 启用 javascript:

              webView.getSettings().setJavaScriptEnabled(true);
      

      【讨论】:

        【解决方案3】:

        根据此处的先前答案和其他帖子中的其他一些答案,我最终得到了以下代码,对我来说很好。

        注意,我的 HTML 是存储为字符串的完整“页面”。我的项目中有/assets/html/ 下的资产,我将其用作我的基本网址。资产包括 CSS、其他 JS 文件等。

        // Reference the webview
        WebView webView = view.findViewById(R.id.webView);
        // Avoid hardware rendering (force software rendering)
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        
        // Reference settings so we can change some
        WebSettings settings = webView.getSettings();
        
        // Enable items needed for proper chartJS execution
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        
        // Load the chartJS HTML (from a class property)
        String html = mHtml;
        // Load the HTML into the webView (note the other needed files reside: css, js, etc.)
        webView.loadDataWithBaseURL("file:///android_asset/html/", html, "text/html", "UTF-8", null);
        // Set to white, the default html bkg color
        webView.setBackgroundColor(Color.WHITE);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多