【问题标题】:Gracefully handle amcharts that have no data优雅地处理没有数据的 amcharts
【发布时间】:2015-03-31 06:10:22
【问题描述】:

我想知道我的 dataProvider 是否为空。 amCharts 绘制时默认为空。我该如何动态处理它。

var chart = AmCharts.makeChart("chartdiv", {
    "theme": "none",
    "type": "serial",
    "dataProvider": data,
    "valueAxes": [{
        "title": "Income in millions, USD"
    }]

如果数据应该为空。怎么办啊。。

【问题讨论】:

  • 我不明白你的要求是什么。当您的数据提供者为空或为空时会发生什么?

标签: javascript php jquery amcharts ammap


【解决方案1】:

你有两个真正的选择。

您可以做的第一件事是在调用AmCharts.makeChart() 之前检查数据,并且只有在有数据时才执行该函数。如果没有任何数据,您可能希望向用户显示某种消息。

您的第二个选项是在图表本身上显示一条消息。 Here is an example on the AmCharts website that can help achieve this.

【讨论】:

  • @GMStevents,你有同样的 amcharts 版本 4 的链接吗?我试图找到它,但没有成功。
  • @TonyMontana,如果您找到了 amcharts 版本 4 的解决方案,可以分享一下吗?
  • @Ram,我尝试了 GMStevens 提供的链接,但我的情况不同,所以我不得不放弃这个想法。但我认为您可以使用 GMStevens 提供的链接。
  • @TonyMontana,我在 GMStevens 提供的链接的帮助下进行了尝试,但出现错误。也不确定我的代码是否正确。让我添加我的代码作为我的答案,你能检查一次吗?
  • @Ram 如果您创建带有错误的新帖子会更好,因此包括我在内的其他人也可以为您提供解决方案。
【解决方案2】:

这可能会对您有所帮助,我遇到过这种情况,我的饼图数据来自 API 调用,它可能有一个空数组,我想在 amchart 4 中显示一些东西而不是什么都没有。

所以我在他们的网站上找到了这个对我有帮助的例子:

pie chart placeholder in amchart 4

基本上它的作用是在图表数据数组为空时显示一个占位符(一个空饼图)。它看起来又漂亮又干净。

【讨论】:

    【解决方案3】:

    这就是我用 amcharts 4 解决它的方法:

    chart.events.on("beforevalidated", function (event) {
        // check if there's data
        if (event.target.data.length === 0) {
            chart.closeAllPopups();
            chart.modal.container = document.body;
            chart.openModal("<strong>No data found!</strong>");
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 2011-11-26
      • 2012-07-09
      相关资源
      最近更新 更多