我正在使用融合图表手机间隙在我的Android应用程序中显示图表。
当我使用网页视图并提供静态html
文件时,它工作正常,但我不知道使用动态数据请指导我。
谢谢!这是我的代码:
java文件
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWeb = (WebView) findViewById(R.id.webview);
mWeb.getSettings().setJavaScriptEnabled(true);
mWeb.getSettings().setPluginsEnabled(true);
// mWeb.loadUrl("file:///android_asset/www/index1.html"); /*working*/
mWeb.loadData(getHTML(), "text/html; charset=UTF-8", null); /*NOT working*/
}
private String getHTML() {
String html = "<html><head><script language=\"JavaScript\"src=\"file:///android_asset/www/FusionCharts.js\"></script></head><body bgcolor=\"#ffffff\"><div id=\"chartdiv\" align=\"center\">The chart will appear within this DIV. This text will be replaced by the chart.</div><script type=\"text/javascript\">FusionCharts.setCurrentRenderer(\"javascript\");var myChart = new FusionCharts(\"file:///android_asset/www/Column3D.swf\", \"myChartId\", \"400\",\"400\");myChart.setXMLData(\"<graph caption='Title' decimalPrecision='0' formatNumberScale='0' showNames='1' xAxisName='XData' yAxisName='YData' ><set name='One' value='120' color='456553' /><set name='Two' value='345' color='234567' /><set name='Three' value='565' color='098765' /></graph>\");myChart.render(\"chartdiv\");</script></body></html>";
return html;
}
index1.html
<html>
<head>
<script language="JavaScript"
src="file:///android_asset/www/FusionCharts.js"></script>
</head>
<body bgcolor="#ffffff">
<div id="chartdiv" align="center">The chart will appear within
this DIV. This text will be replaced by the chart.</div>
<script type="text/javascript">
FusionCharts.setCurrentRenderer("javascript");
var myChart = new FusionCharts(
"file:///android_asset/www/Column3D.swf", "myChartId", "400",
"400");
myChart
.setXMLData("<graph caption='Title' decimalPrecision='0' formatNumberScale='0' showNames='1' xAxisName='XData' yAxisName='YData' ><set name='One' value='120' color='456553' /><set name='Two' value='345' color='234567' /><set name='Three' value='565' color='098765' /></graph>");
myChart.render("chartdiv");
</script>
</body>
</html>
答案 0 :(得分:1)
我怀疑 loadData()方法无法加载依赖的JavaScript和CSS文件。而是使用如下所示的 loadDataWithBaseURL()方法。试试下面的代码,如果这对您有用,请告诉我。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWeb = (WebView) findViewById(R.id.webview);
mWeb.getSettings().setJavaScriptEnabled(true);
mWeb.getSettings().setPluginsEnabled(true);
mWeb.loadDataWithBaseURL("", getHTML(), "text/html", "UTF-8", "");
}
private String getHTML() {
String html = "<html><head><script language=\"JavaScript\"src=\"file:///android_asset/www/FusionCharts.js\"></script></head><body bgcolor=\"#ffffff\"><div id=\"chartdiv\" align=\"center\">The chart will appear within this DIV. This text will be replaced by the chart.</div><script type=\"text/javascript\">FusionCharts.setCurrentRenderer(\"javascript\");var myChart = new FusionCharts(\"file:///android_asset/www/Column3D.swf\", \"myChartId\", \"400\",\"400\");myChart.setXMLData(\"<graph caption='Title' decimalPrecision='0' formatNumberScale='0' showNames='1' xAxisName='XData' yAxisName='YData' ><set name='One' value='120' color='456553' /><set name='Two' value='345' color='234567' /><set name='Three' value='565' color='098765' /></graph>\");myChart.render(\"chartdiv\");</script></body></html>";
return html;
}
答案 1 :(得分:0)
您可以使用服务器端脚本动态生成XML或JSON数据, 因此,您可以提供脚本的URL,该脚本将动态生成的数据中继到图表。
使用数据库中的动态数据创建图表包括以下步骤:
该链接可能有用 - http://docs.fusioncharts.com/charts/contents/Code/J2EE/JSP_DB.html