最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Gracefully handle amcharts that have no data - Stack Overflow

programmeradmin0浏览0评论

I want know if my dataProvider is empty. When amCharts draw default null. How can i handle it dynamically.

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

If data should empty. How could be handle it..

I want know if my dataProvider is empty. When amCharts draw default null. How can i handle it dynamically.

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

If data should empty. How could be handle it..

Share Improve this question edited Apr 8, 2015 at 11:25 gerric 2,2971 gold badge16 silver badges32 bronze badges asked Mar 31, 2015 at 6:10 Rajeev Kumar VermaRajeev Kumar Verma 311 silver badge2 bronze badges 1
  • I don't understand what your demand is. What should happen when your data provider is empty or null? – gerric Commented Apr 8, 2015 at 11:01
Add a ment  | 

3 Answers 3

Reset to default 7

You have two real options here.

The first thing you can do is check for data before calling AmCharts.makeChart() and only execute that function if there is data. You will probably want to show some kind of message to the user in the event there isnt any data.

Your second option is to display a message on the chart itself. Here is an example on the AmCharts website that can help achieve this.

This might help you, I had this situation where my pie chart data was ing from an API call and it could have an empty array and I wanted to show something instead of nothing in amchart 4.

so i found this example on their site that helped me:

pie chart placeholder in amchart 4

basically what it does is that it shows a placeholder (an empty pie chart) when the chart data array is empty. It looks nice and clean.

This is how I solved it with 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>");
    }
});
发布评论

评论列表(0)

  1. 暂无评论