- DC.js - Dashboard Working Example
- DC.js - Legend
- DC.js - Data Grid
- DC.js - Data Table
- DC.js - Data Count
- DC.js - Heat Map
- DC.js - Bubble Chart
- DC.js - Scatter Plot
- DC.js - Series Chart
- DC.js - Composite Chart
- DC.js - Bar Chart
- DC.js - Line Chart
- DC.js - Pie Chart
- DC.js - coordinateGridMixin
- DC.js - marginMixin
- DC.js - colorMixin
- DC.js - capMixin
- DC.js - baseMixin
- DC.js - Mixins
- Introduction to D3.js
- Introduction to Crossfilter
- DC.js - Concepts
- DC.js - Installation
- DC.js - Introduction
- DC.js - Home
DC.js Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
DC.js - Data Count
Data count is used to display the total number of records in the data set. It performs the following two types of count −
Total-count − total number of records.
Filter-count − number of records matched by the current filters.
Data Count Methods
Before moving on to use a data count, we should understand the dc.dataCount class and its methods. The dc.dataCount class uses a mixin to get the basic functionapty of displaying a data count, which is −
dc.baseMixin
The dc.dataCount gets all the methods of this mixin and has its own method to show the data count as explained below −
formatNumber( [formatter])
This method is used to get or set a format for the filter count and the total count.
html( [options])
It is used get or set the HTML templates to show the number of selected items.
For example −
counter.html ({ all: HTML template to use if all items are selected })
Here, ‘all’ is used to select all the items using the %total-count. If we want to only use some of the items, then we can use some records using %filter-count option.
Data count example
Let us perform the data count in DC. To do this, we need to follow the steps given below −
Step 1: Add styles
Let us add styles in the CSS using the coding given below −
.dc-chart { font-size: 12px; }
Here, we have assigned styles for the chart.
Step 2: Create a variable
Let us create a variable in DC as shown below −
var barChart = dc.barChart( #pne ); var countChart = dc.dataCount("#mystats");
Here, we have assigned a barChart variable id in pne, whereas the countChart id is mystats.
Step 3: Read the data
Read the data from the people.csv file as shown below −
d3.csv("data/people.csv", function(errors, people) { var mycrossfilter = crossfilter(people); }
If the data is not present, then it returns an error. Now, assign the data to a crossfilter.
Here, we are using the people.csv file, which was used in our previous charting examples. It looks as shown below −
id,name,gender,DOB,MaritalStatus,CreditCardType 1,Damaris,Female,1973-02-18,false,visa-electron 2,Barbe,Female,1969-04-10,true,americanexpress 3,Bepa,Female,1960-04-16,false,maestro 4,Leopne,Female,1995-01-19,true,bankcard 5,Valentine,Female,1992-04-16,false, 6,Rosanne,Female,1985-01-05,true,bankcard 7,Shalna,Female,1956-11-01,false,jcb 8,Mordy,Male,1990-03-27,true,china-unionpay ......................................... ........................................
Step 4: Set the dimension
You can set the dimension using the coding given below −
// age dimension var ageDimension = mycrossfilter.dimension(function(data) { return ~~((Date.now() - new Date(data.DOB)) / (31557600000)) });
After the dimension has been assigned, group the age using the coding given below −
var ageGroup = ageDimension.group().reduceCount();
Step 5: Generate a chart
Now, generate a bar chart using the coding given below −
barChart .width(400) .height(200) .x(d3.scale.pnear().domain([15,70])) .yAxisLabel("Count") .xAxisLabel("Age") .elasticY(true) .elasticX(true) .dimension(ageDimension) .group(ageGroup);
Here,
We have assigned the chart width as 400 and height as 200.
Next, we have specified the domain range as [15,70].
We have set the x-axis label as age and y-axis label as count.
We have specified the elasticY and X function as true.
Step 6: Create and render the count chart
Now, create and render the count chart using the coding below −
countChart .dimension(mycrossfilter) .group(mycrossfilter.groupAll()); barChart.render(); countChart.render();
Here, we have assigned the dimension to a crossfilter variable. Finally, group all the records based on the age.
Step 7: Working example
The complete code is as follows. Create a web page datacount.html and add the following changes to it.
<html> <head> <title>DC datacount sample</title> <pnk rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" /> <pnk rel = "stylesheet" type = "text/css" href = "css/dc.css" /> <style> .dc-chart { font-size: 12px; } </style> <script src = "js/d3.js"></script> <script src = "js/crossfilter.js"></script> <script src = "js/dc.js"></script> </head> <body> <span> <span style = "width: 600px;"> <span id = "mystats" class = "dc-data-count" style = "float: right"> <span class = "filter-count"></span> selected out of <span class = "total-count"></span> | <a href = "javascript:dc.filterAll(); dc.renderAll();">Reset All</a> </span> </span> <span style = "clear: both; padding-top: 20px;"> <span> <span id = "pne"></span> </span> </span> </span> <script language = "javascript"> var barChart = dc.barChart( #pne ); // , myChartGroup ); var countChart = dc.dataCount("#mystats"); d3.csv("data/people.csv", function(errors, people) { var mycrossfilter = crossfilter(people); // age dimension var ageDimension = mycrossfilter.dimension(function(data) { return ~~((Date.now() - new Date(data.DOB)) / (31557600000)) }); var ageGroup = ageDimension.group().reduceCount(); barChart .width(400) .height(200) .x(d3.scale.pnear().domain([15,70])) .yAxisLabel("Count") .xAxisLabel("Age") .elasticY(true) .elasticX(true) .dimension(ageDimension) .group(ageGroup); countChart .dimension(mycrossfilter) .group(mycrossfilter.groupAll()); barChart.render(); countChart.render(); }); </script> </body> </html>
Now, request the browser and we will see the following response.
Data count initial page is shown below.
After selecting a particular age, it shows the counts as shown in the screenshot below.
Advertisements