2016-02-15 60 views
0

當我在條形圖中選擇任何特定字段時,所有圖形都會相應更改。如何使用dc.js在圖上添加標籤?

我想介紹一個圖表上面的標題,它應該顯示哪些數據當前顯示在圖表和計數中。

那麼我應該在我的代碼中做些什麼改變?

var data = [{ 
 
    "city": "New York", 
 
     "neighborhood": "N/A", 
 
     "hits": 200 
 
}, { 
 
    "city": "New York", 
 
     "neighborhood": "Brooklyn", 
 
     "hits": 225 
 
}, { 
 
    "city": "New York", 
 
     "neighborhood": "Queens", 
 
     "hits": 1 
 
}, { 
 
    "city": "San Francisco", 
 
     "neighborhood": "Chinatown", 
 
     "hits": 268 
 
}, { 
 
    "city": "San Francisco", 
 
     "neighborhood": "Downtown", 
 
     "hits": 22 
 
}, { 
 
    "city": "Seattle", 
 
     "neighborhood": "N/A", 
 
     "hits": 2 
 
}, { 
 
    "city": "Seattle", 
 
     "neighborhood": "Freemont", 
 
     "hits": 25 
 
}]; 
 
var pieChart = dc.pieChart("#pieChart"), 
 
    rowChart = dc.rowChart("#rowChart"); 
 
var ndx = crossfilter(data), 
 
    cityDimension = ndx.dimension(function (d) { 
 
     return d.city; 
 
    }), 
 
    cityGroup = cityDimension.group().reduceSum(function (d) { 
 
     return d.hits; 
 
    }), 
 
    neighborhoodDimension = ndx.dimension(function (d) { 
 
     return d.neighborhood; 
 
    }), 
 
    neighborhoodGroup = neighborhoodDimension.group().reduceSum(function (d) { 
 
     return d.hits; 
 
    }); 
 

 
pieChart.width(200) 
 
    .height(200) 
 
    .slicesCap(4) 
 
    .dimension(cityDimension) 
 
    .group(cityGroup); 
 
pieChart.filter = function() {}; 
 

 
rowChart.width(500) 
 
    .height(500) 
 
    .dimension(neighborhoodDimension) 
 
    .group(neighborhoodGroup); 
 

 
dc.renderAll();
<div id="pieChart"> </div> 
 
<div id="rowChart"> </div>

回答

0

用於顯示當前過濾器,你可以使用帶有class='reset' and類='在圖表中filter'` span S,作爲annotated stock example

證明,用於顯示計數記錄已過濾,您可以使用dataCount widgetnumberDisplay小部件。

還有什麼,你必須實現顯示本身,可能掛鉤filtered event,以確定何時過濾器已更改,並使用例如更改自己的div。 d3或jQuery。

+0

但我想爲我的網站做的是,我有一長串客戶,併爲每個客戶選擇圖表根據客戶數據的變化,所以我想介紹一個標籤上方的圖表,目前的數據顯示是爲這個客戶和這個特定的服務。那麼,我該怎麼做才能做到這一點。 – amru

+0

所以你在過濾器中過濾客戶?該過濾器是通過dc.js還是外部的?如果是通過dc.js,那麼'過濾的'事件就是要走的路。 – Gordon