Can i use d3.js for creating interactive visualizations inside an android app?

后端 未结 5 1924
無奈伤痛
無奈伤痛 2020-12-24 13:21

I am trying to create an interactive visualization within an android app.

The data that will be visualized will be stored locally in a sqlite database and will be q

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 14:02

    I got D3 to work on my Android app by following this tutorial. From there, it was easy to extend the tutorial's code to fit my needs.

    Here is a slightly modified version of the tutorial (I added zoom controls and scaling):

    1. Create a JS assets folder (ProjectName/app/src/main/assets/js)
    2. Download D3 (https://d3js.org/) and copy the minified d3.min.js source file into ProjectName/app/src/main/assets/js
    3. Create a directory for HTML assets: (ProjectName/app/src/main/assets/html)
    4. Create file ProjectName/app/src/main/assets/html/graph.html. Copy this code into the new file:
    
    
        
        
        
    
    
    
    
    
    
    
    1. Create the JS file that will run D3: ProjectName/app/src/main/assets/js/drawGraph.js. Add the following code to it:
    function initGraph(dataset, pHeight, pWidth) {
      var height = parseInt(pHeight);
      var width = parseInt(pWidth);        
      var svg = d3.select("#piechart");
      var textLabelSuffix = "%";
    
      showPieChart(dataset, svg, height, width, 
          textLabelSuffix);
    }
    
    function showPieChart(dataset, svg, height, width, 
      textLabelSuffix)
    {
      var outerRadius = width / 2;
      var innerRadius = 0;
    
      // set height/width to match the SVG element
      svg.attr("height", height).attr("width", width);
    
      // create a new pie layout
      var pie = d3.layout.pie();
    
      // initialize arcs/wedges to span 
      // the radius of the circle
      var arc = d3.svg.arc()
                   .innerRadius(innerRadius)
                   .outerRadius(outerRadius);
    
      // create groups
      var arcs = svg.selectAll("g.arc")   
                    // bind dataset to pie layout
                    .data(pie(dataset))   
                    // create groups
                    .enter()              
                    // append groups
                    .append("g")          
                    // create arcs
                    .attr("class", "arc") 
                    // position each arc in the pie layout
                    .attr("transform", "translate(" + 
                     outerRadius + "," + 
                     outerRadius + ")");
    
    
      // initialize color scale - refer to
      // https://github.com/mbostock/d3/wiki/Ordinal-Scales
      var color = d3.scale.category10();
    
      arcs.append("path")
          .attr("fill", function(d,i) { return color(i); })
          .attr("d", arc);
    
      arcs.append("text")
          .attr("transform", function(d) {
              return "translate(" + arc.centroid(d) + ")";
           })
          .attr("text-anchor", "middle")
          .text(function(d) { return d.value + 
             textLabelSuffix; });
    }
    
    1. Create an activity layout that will host D3 code:
    
    
        
    
    
    1. Initialize the webview in your activity:

    
    public class VisualizationActivity extends AppCompatActivity {

    private WebView webview;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visualization);
    
        // Prepare webview: add zoom controls and start zoomed out
        webview = (WebView) findViewById(R.id.webview);
        final WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        webSettings.setUseWideViewPort(true);
        webview.setWebChromeClient(new WebChromeClient());
        webview.setInitialScale(1);
    
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                // after the HTML page loads, run JS to initialize graph
                int dataset[] = new int[] {5,10,15,20,35};
                String text = Arrays.toString(dataset);
    
                webview.loadUrl("javascript:initGraph(" + 
                        text + ", "
                        (webview.getHeight()) + ", "
                        + (webview.getWidth()) + ")");
            }
        });
    
        // Load base html from the assets directory
        webview.loadUrl("file:///android_asset/html/graph.html");
    }
    

    }

提交回复
热议问题