【问题标题】:dimple js what if there is no data returned after using dimple.filterData()酒窝js如果使用dimple.filterData()后没有返回数据怎么办
【发布时间】:2014-08-27 00:43:48
【问题描述】:

我正在尝试使用dimple JS 绘制图表。我需要在选择下拉菜单时过滤数据。当我使用 dimple.filterData(....) 时没有返回数据时,我收到错误消息。当 dimple.filterData() 函数没有返回数据时,请告诉我如何处理。

当没有数据返回时,我想显示“当前选择没有数据”。

JS Fiddle Link Here

<select id="Time">
<option value="">Please Select</option>
<option value="Year">YTD</option>
<option value="Month">MTD</option>
<option value="Week">WTD</option>

$("#Time").on('change',function(){

$("#chartContainer").html('');
drawChart($(this).prop('id'), $(this).val());

});

function drawChart(id, value){

var svg = dimple.newSvg("#chartContainer", 590, 400);
var data = [
    { Time: "Year", Value: 1 },
    { Time: "Month", Value: 2 },
    { Time: "Month", Value: 3 }
];
data = dimple.filterData(data, id, value);
var myChart = new dimple.chart(svg, data);

myChart.setBounds(60, 30, 510, 305)
var x = myChart.addCategoryAxis("x", "Time");
x.addOrderRule(["Year", "Month", "Week"]);
myChart.addMeasureAxis("y", "Value");
myChart.addSeries(null, dimple.plot.bar);
myChart.draw();

}

【问题讨论】:

    标签: javascript jquery html d3.js dimple.js


    【解决方案1】:

    当没有匹配的数据时,dimple.filterData(...) 调用返回一个空数组。只需检查数组是否为空:

     data = dimple.filterData(data, id, value);
     if(data.length > 0) {
       // Charting code here...   
     } else { 
       alert("No data for the current selection");
     }
    

    【讨论】:

    • 谢谢你的回答! :) 在问这个问题之前,我尝试使用 if(data){ //charting code.. } 它显示了一些错误.. 它现在正在使用您的答案..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多