0

我正在使用融合图表电话间隙在我的 android 应用程序中显示图表。

当我使用 Web 视图并提供静态html文件时,它工作正常,但我不知道如何使用动态数据,请指导我。谢谢!这是我的代码:

文件

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>
4

2 回答 2

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;

}
于 2013-08-19T13:22:05.233 回答
0

您可以使用服务器端脚本来动态生成 XML 或 JSON 数据,因此,您可以提供脚本的 URL,它将动态生成的数据中继到图表。

使用数据库中的动态数据创建图表涉及以下步骤:

  1. 在服务器端脚本中启动数据库连接。
  2. 从数据库中检索您希望在图表上显示的数据。
  3. 通过遍历每条记录来生成图表数据(XML 或 JSON)

该链接可能会有所帮助-http ://docs.fusioncharts.com/charts/contents/Code/J2EE/JSP_DB.html

于 2013-08-19T04:33:17.937 回答