NVD3 - Showing empty chart instead of noData message

╄→гoц情女王★ 提交于 2019-12-12 08:44:56

问题


Is there a way to show an empty chart instead of the "No Data Available" message when there is no data to show?

http://jsfiddle.net/sammla/pYWkD/2/

data2 = [ 
    { 
      "key" : "A key" , 
      "values" : []
    }

];

Thanks!


回答1:


You can "hack" this by having an empty array that contains an empty array:

data2 = [ 
  { 
    "key" : "A key" , 
    "values" : [[]]
  }
];



回答2:


The answer provided by Lars works well when you do not want to show the noData message on a chart when its empty.

Recently I had charts with content being loaded dynamically. I found a similar question to this Updating with no data does not clear old data from the chart.

If a chart is populated with data and then update is called after the data has been emptied, the noData text will overlay the existing data.

Consider if the current data should be cleared from the chart as it can be confusing to see both at the same time.

I was not able to find a clean solution to that, So here's what I did to overcome it :

Used Lars answer to empty the chart :

data2 = [{
    "key" : "A key",
    "values" : [[]]
}]; 

And then added the code below.

d3.select('#chart svg').append("text")
        .attr("x", "235")
        .attr("y", "35")
        .attr("dy", "-.7em")
        .attr("class", "nvd3 nv-noData")
        .style("text-anchor", "middle")
        .text("My Custom No Data Message");

Even I am after a proper solution for it, to show the noData text without it overlaying the existing data. But for now this works perfectly.

Hope it helps some one, trying to achieve the same thing.




回答3:


you can call noData and pass a string during the chart creation:

(coffeescript)

self.chart = nv.models.lineChart()
               .margin  left: 100, right: 100
               .useInteractiveGuideline true
               .transitionDuration 150
               .showLegend true
               .showYAxis true
               .showXAxis true
               .noData 'no data, there is'


来源:https://stackoverflow.com/questions/17458236/nvd3-showing-empty-chart-instead-of-nodata-message

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!