我尝试在我的Android应用程序中使用谷歌饼图,这是我的代码
public class HomeFragment extends Fragment {
static HomeFragment fragment;
private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
webView = (WebView) rootView.findViewById(R.id.webView1);
String customHtml = "<html>"+"<head>"+"<script type=\"text/javascript\" src=\"jsapi.js\"> </script>"
+"<script type=\"text/javascript\">"
+"google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});"
+"google.setOnLoadCallback(drawChart);"
+"function drawChart() {"
+"var data = google.visualization.arrayToDataTable(["+"['Task', 'Hours per Day'],"+"['Work', 11],"+"['Eat', 2],"+"['Commute', 2],"+"['Watch TV', 2],"+"['Sleep', 7]"+"]);"
+"var options = {"+" title: 'My Daily Activities'"+" };"
+" var chart = newgoogle.visualization.PieChart(document.getElementById('piechart'));"
+" chart.draw(data, options);"+"}"+"</script>"+"</head>"+"<body>"
+" <div id=\"piechart\" style=\"width: 900px; height: 500px;\"></div>"
+"</body>"+"</html>";
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.requestFocusFromTouch();
webView.loadData(customHtml, "text/html", "UTF-8");
return rootView;
}
}
//布局
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
我添加了互联网权限但是它没有工作,我收到此错误
E/NativeCrypto(24611): ssl=0x5bf95f38 cert_verify_callback x509_store_ctx=0x5e6dfab0 arg=0x0
E/NativeCrypto(24611): ssl=0x5bf95f38 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
E/PerfService(528): perfBoostEnable bypassed!
E/PerfService(528): perfBoostDisable bypassed!
E/hwcomposer_v1.0(499): [WKR] Timed out waiting for FrameSyncThread...
E/PerfService(528): perfBoostEnable bypassed!
E/PerfService(528): perfBoostDisable bypassed!
E/PerfService(528): perfBoostEnable bypassed!
E/PerfService(528): perfBoostDisable bypassed!
答案 0 :(得分:0)
在Android浏览器中绘制图表的一种方法是进行以下2次更改。
“var chart = newgoogle .visualization.PieChart(document.getElementById('piechart'));”
在上面的行中添加 new 和 google 之间的空格,例如,
“var chart = new google .visualization.PieChart(document.getElementById('piechart'));”
和
将 src = \“jsapi.js **替换为** src = \”https://www.google.com/jsapi \“
其他方法是在assets文件夹中添加“jsapi.js”并调用方法loadDataWithBaseURL()而不是loadData(),如下所示。
webView.loadDataWithBaseURL(“file:/// android_asset /”,customHtml,“text / html”,“UTF-8”,null);
如果您在加载图表时遇到任何问题,请使用任何webbrowser(chrome,mozilla firefox,Internet Explorer)的调试工具。然后你可以轻松找到错误。
继续发展......