【问题标题】:Fusion chart not working when data is dynamic数据动态时融合图表不起作用
【发布时间】:2013-08-17 13:03:42
【问题描述】:

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

当我使用 Web 视图并提供静态 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>

【问题讨论】:

标签: android cordova fusioncharts


【解决方案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;

}

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多