简体   繁体   中英

How can I use the following html to dynamically create wordclouds for my android app?

I've trying and trying these past few days but I just can't get my wodcloud to print the way I want it, this is due to very little knowledge about javascript that I have and very little knowledge on how I can incorporate a javascriptInterface into a webview.

I am using 3 files - d3.html, d3.layout.cloud.js, d3.js. I am mainly editing d3.html as it is what I am loading into my webview through the loadUrl method. The other two .js files are from Json Davies D3 Word Cloud generator. Here is the content of my d3.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
    "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
</head>
<h1></h1>
<script src="d3.js"></script>
<script src="d3.layout.cloud.js"></script>
<script type="text/javascript">
function getWebViewSize(){
    return Android.getWebViewSize();
}
</script>
<script>(function() {
var fill = d3.scale.category20();
var layout = d3.layout.cloud()
    .size([getWebViewSize()[0], getWebViewSize()[1]])
    .words([
      "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean",
      "KitKat", "Lollipop", "Marshmallow"].map(function(d) {
      return {text: d, size: 10 + Math.random() * 40};
    }))
    .padding(5)
    .rotate(0)
    .font("Impact")
    .fontSize(function(d) { return d.size; })
    .on("end", draw);
layout.start();
function draw(words) {
  d3.select("body").append("svg")
      .attr("width", layout.size()[0])
      .attr("height", layout.size()[1])
    .append("g")
      .attr("transform", "translate(" + layout.size()[0] / 2 + "," + layout.size()[1] / 2 + ")")
    .selectAll("text")
      .data(words)
    .enter().append("text")
      .style("font-size", function(d) { return d.size + "px"; })
      .style("font-family", "Impact")
      .style("fill", function(d, i) { return fill(i); })
      .attr("text-anchor", "middle")
      .attr("transform", function(d) {
        return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
      })
      .text(function(d) { return d.text; });
}
})();</script>
<body/>

The problems I am facing :

  1. I can't get the width and height of the webview inside the javascript interface. The .size( method only accepts I think integer numbers and I can't get the size of the webview I am loading it into. I tried using metrics and getting from document which I have no idea how it works but it didn't work.

  2. I can't change the color of the words, whenever I edit any of the styles, it doesn't load into the webview. I can't also add shadows onto the words.

  3. As you can see the words there are still static, how can I get this function then dynamically change the words and sizes before I load them into the webview?

EDIT :

I tried this to get the width and height but I am getting the following logs, and am nervous about the first one

10-04 10:14:56.234 4550-4550/[package] I/Choreographer: Skipped 35 frames!  The application may be doing too much work on its main thread.
10-04 10:15:11.935 30842-30842/[package] I/chromium: [INFO:CONSOLE(20)] 
"Uncaught TypeError: Cannot read property '0' of undefined", source: file:///android_asset/wordcloud_html/d3.html (20)
10-04 10:15:14.478 30842-30842/[package] W/art: Attempt to remove local handle scope entry from IRT, ignoring
10-04 10:15:14.478 30842-30842/[package] W/art: Attempt to remove local handle scope entry from IRT, ignoring
10-04 10:15:14.524 30842-30842/[package] W/cr.BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 30842
10-04 10:15:14.537 30842-30842/[package] I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2d741dbf time:1235333
10-04 10:15:14.842 30842-30842/[package] I/chromium: [INFO:CONSOLE(20)] "Uncaught TypeError: Cannot read property '0' of undefined", source: file:///android_asset/wordcloud_html/d3.html (20)

Here is my JavascriptInterface

public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public int[] getWebViewSize()
    {
        int[] size = new int[2];
        size[0] = webview.getWidth();
        size[1] = webview.getHeight();
        return size;
    }
}

Here is the code for loading the webview

        webView = view.getWebView();
        WebSettings ws = webView.getSettings();
        ws.setJavaScriptEnabled(true);

        JSInterface = new JavaScriptInterface(context);
        webView.setWebViewClient(new InternalWebViewClient());
                webView.setBackgroundColor(fragment.getResources().getColor(R.color.analysis_complete_bg));
        webView.loadUrl("file:///android_asset/wordcloud_html/d3.html");
        webView.addJavascriptInterface(JSInterface, "Android");

For your first problem, I believe you can only return primitives (no arrarys). Also, how is the variable webview created/accessible? Perhaps you can try to use the interface as a simple pass through. For your third problem, where are the words coming from? Is this back in your android Java? Again, you could wire it as a pass through.

public class JavaScriptInterface {
    Context mContext;
    int mWidth, mHeight;
    string mJSON;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c, int w, int h, string json) {
        mContext = c;
        mWidth = w;
        mHeight = h;
        mJSON = json;
    }

    @JavascriptInterface
    public int getWidth()
    {
        return wWidth;
    }

    @JavascriptInterface
    public int getHeight()
    {
        return mHeight;
    }

    @JavascriptInterface
    public string getWords()
    {
        return mJSON;
    }
}

And create it as:

new JavaScriptInterface(AppilyApplication.appContext, 
  webview.getWidth(), 
  webview.getHeight(),
  "[{\"text\":\"Hello\",\"size\":42},{\"text\":\"world\",\"size\":96}]"
);

And use it in the JavaScript:

.size([Android.getWidth(), Android.getHeight()])
.words(JSON.parse(Android.getWords())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM