我正在尝试在android应用程序中创建一个交互式可视化。
将要可视化的数据将存储在本地的sqlite数据库中,并将被查询以生成可视化。
我还没有决定是构建原生应用程序还是web应用程序。
根据我的研究,d3.js似乎非常适合需求,但我不确定如何在移动环境中使用它。
发布于 2012-05-26 11:28:50
您可以使用WebView组件或PhoneGap或类似平台轻松地将Javascript嵌入到安卓应用程序中。不幸的是,在Android3.0之前,原生Android浏览器不显示SVG,而D3是基于svg的,因此它不能在android2.3(或更低版本)浏览器中工作。然而,它确实可以在Firefox浏览器中工作。
另一种选择是在WebView或phone gap中使用InfoVis Toolkit。InfoVis工具包似乎在安卓上运行得更好。
我不知道有没有类似的Android可视化平台,但D3,以及它的前身Protovis,Flare和Prefuse都是开源的。也许你可以尝试将其中一个移植到Android上。
这里有一个相关的问题:visualization tool for mobile (tablet)
发布于 2016-03-23 10:33:37
我通过关注this tutorial让D3在我的安卓应用上工作。从那时起,很容易扩展教程的代码来满足我的需求。
这是本教程的一个稍微修改过的版本(我添加了缩放控件和缩放):
创建一个JS资源文件夹(ProjectName/app/src/main/assets/js)
<html>
<head>
<script type="text/javascript" src="file:///android_asset/js/d3.min.js"></script>
<script type="text/javascript" src="file:///android_asset/js/drawGraph.js"></script>
<meta name="viewport" content="width=device-width, user-scalable=yes" />
</head>
<body>
<svg id="piechart"></svg>
</body>
</html>ProjectName/app/src/main/assets/js/drawGraph.js.:
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; });
}<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tvOutgoing"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
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");
}
}发布于 2012-05-15 18:27:12
据我所知,Android应用程序只支持java。因此,要使用d3,您需要一个java javascript引擎/包装器(如Rhino或apparently PhoneGap),或者只制作一个HTML5应用程序(而不是Android应用程序)。
这个关于PhoneGap的链接似乎是关于从HTML5生成一个应用程序,但你需要检查你是否可以包含任意的附加库。
它一定要是一个应用程序吗?D3更适合于编写HTML5站点,而不是应用程序。
https://stackoverflow.com/questions/10590168
复制相似问题