keeping the selection in sync between two heatmaps - dc.js

I am building multiple Heat-maps from a sample data set given below.
[
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
}];
When I select a cell from a heat-map, other than the selected cell all the other cells are defaulted to the #ccc color. In the corresponding filtered heat-maps, the color is defaulted to a primary blue color. I am attaching a image below for reference.
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/dc/3.1.5/dc.css"/>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.1.1/es6-promise.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/3.0.0/fetch.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.12.0/d3.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dc/3.1.5/dc.js"></script>
var runDim;
var data = [
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 1,
"y2": 1,
"x2_TT": 20,
"y2_TT": 23
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.9163309159,
"y2": 0.9163309159,
"x2_TT": 18,
"y2_TT": 21
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.7849029059,
"y2": 0.7849029059,
"x2_TT": 16,
"y2_TT": 19
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.6695846541,
"y2": 0.6695846541,
"x2_TT": 14,
"y2_TT": 17
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.5604744826,
"y2": 0.5604744826,
"x2_TT": 12,
"y2_TT": 15
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.433624775,
"y2": 0.433624775,
"x2_TT": 9,
"y2_TT": 12
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.3117981232,
"y2": 0.3117981232,
"x2_TT": 7,
"y2_TT": 10
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.2070667799,
"y2": 0.2070667799,
"x2_TT": 5,
"y2_TT": 8
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0.0826720825,
"y2": 0.0826720825,
"x2_TT": 3,
"y2_TT": 6
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
},
{
"x2": 0,
"y2": 0,
"x2_TT": 2,
"y2_TT": 5
}];
var data1 = [];
var f=0; var c=1;
for( var i=0; i<=data.length-1; i++){
f++;
if(f>Math.sqrt(data.length)){ f=1; c++; }
data[i].column = f;
data[i].row = c;
}
data.filter( (x) => {
var obj = {}
obj.value = x.x2;
obj.tooltip = x.x2_TT;
obj.row = x.row;
obj.column = x.column;
data1.push(obj)
})
console.log( JSON.stringify(data1) )
console.log(data);
var ndx = crossfilter(data);
Object.keys(data[0]).filter(x => !x.includes('_TT') && !x.includes('column') && !x.includes('row') ).forEach(function(i, index){
//Dynamic DIV appended to body
var testid = "<div class='col-lg-6 dcjs' id='chart"+index+"' onclick='HeatMap(this)'></div>";
$('#test').append(testid);
//$("#chart"+index).append("<h4 align='center'>"+i+"</h4>")
var chart = dc.heatMap("#chart"+index);
var holder = Object.keys(data[0]).filter(x => !x.includes('_TT') && !x.includes('column') && !x.includes('row'))[index];
var width = document.getElementById('chart'+index).offsetWidth;
var height = document.getElementById('chart'+index).offsetHeight;
runDim = ndx.dimension(function(d) { return [ +d.row, +d.column, +d[holder+'_TT'] ]; }),
runGroup = runDim.group().reduceSum(function(d) { return +d[holder]; });
chart
.width(width)
.height(height)
.dimension(runDim)
.group(runGroup)
.keyAccessor(function(d) { return [ +d.key[0], ] ; })
.valueAccessor(function(d) { return +d.key[1]; })
.colorAccessor(function(d) { return +d.value; })
.title(function(d) {
//return "Value: " + d.key[0] + ","+ d.key[1] + " : " + d.key[2];
return "Value : "+ d.key[2];
})
// .addFilterHandler(function (filters, filter) {
// filters.push(filter);
// console.log(filters)
// return filters;
// })
// .boxOnClick(function (d) {
// console.log(d.key)
// var filter = d.key;
// dc.events.trigger(function () {
// chart.filter(filter);
// chart.redrawGroup();
// });
// })
.on('pretransition', function(chart) {
chart.selectAll('g.axis text').attr("fill", "white").style("font-size", "0px")
})
.colors(['#000080', '#000092', '#0000a4', '#0000b7', '#0000c9', '#0000db', '#0000ee', '#0000ff', '#0002ff', '#0012ff', '#0022ff', '#0033ff', '#0043ff', '#0053ff', '#0063ff', '#0073ff', '#0084ff', '#0094ff', '#00a4ff', '#00b4ff', '#00c4ff', '#00d4ff', '#00e5f7', '#0cf5ea', '#19ffdd', '#27ffd0', '#34ffc3', '#41ffb6', '#4effa9', '#5bff9c', '#68ff8f', '#75ff82', '#82ff75', '#8fff68', '#9cff5b', '#a9ff4e', '#b6ff41', '#c3ff34', '#d0ff27', '#ddff19', '#eaff0c', '#f7f500', '#ffe600', '#ffd700', '#ffc800', '#ffb900', '#ffaa00', '#ff9b00', '#ff8c00', '#ff7d00', '#ff6e00', '#ff5f00', '#ff5000', '#ff4100', '#ff3200', '#ff2300', '#ff1400', '#ee0500', '#db0000', '#c90000', '#b70000', '#a40000', '#920000', '#800000'])
.xBorderRadius(0)
.yBorderRadius(0)
.calculateColorDomain();
if (index == Object.keys(data[0]).filter(x => !x.includes('_TT') && !x.includes('column') && !x.includes('row') ).length-1 ){ dc.renderAll();
}
});
function HeatMap(event){
console.log(runDim.top(Infinity));
// setTimeout( () => {
// $('.dcjs').each(function(index, obj) {
// if (obj != event){
// $(obj).find('.box-group').each(function(indexStep, objStep) {
// if ($(objStep).find('rect').attr('fill') == "rgb(0, 0, 128)" ){
// $(objStep).addClass("deselected");
// }
// })
// }
// });
// }, 500);
}
// d3.csv("morley.csv").then(function(experiments) {
// });
How do I override the crossfiltered applied dark blue color for other heatmaps to the same #CCC unselected color?
Here is a fiddle for the following problem: https://jsfiddle.net/Vivek_Raju/ndeuzxmt/

After discussion in the comments, we realized that the problem wasn't really about colors but about keeping the selection in sync between two heatmaps.
First off, if you've got two dc.js charts on the same data, you almost always want them on the same dimension. This is because you don't want them filtering each other, and a group does not observe its own dimension's filters.
So let's move the dimension out of the loop and use it for both charts:
const keys = Object.keys(data[0]).filter(x => !x.includes('_TT') && !x.includes('column') && !x.includes('row'));
const runDim = ndx.dimension(function(d) {
return [+d.row, +d.column]; //, +d[holder + '_TT']];
});
keys.forEach(function(i, index) {
(I also put keys in a variable because it was hurting my brain to see that huge expression everywhere. I also moved dc.redrawAll() out of the loop.)
The way to set filters in dc.js is pretty idiosyncratic. Here is a question specifically about the format needed to change a heatmap filter programmatically:
Heatmap DC.js - how to filter multiple items manually
Copying filters from one chart to the others can be done like this:
var guard = false;
function filter_listener(i) {
return function(chart) {
if(guard) return; // avoid infinite recursion
var filters = chart.filters().map(f => dc.filters.TwoDimensionalFilter([f[0],f[1]]));
console.log(i, filters);
guard = true;
charts.forEach((chart2, j) => {
if(j == i) return;
chart2._filter(null);
if(filters.length) chart2._filter([filters])
});
guard = false;
}
}
charts.forEach((chart, i) => chart.on('filtered.sync', filter_listener(i)))
Basically the filter listener will first make sure it is not the source of the current filters, to avoid infinite recursion.
Then it will copy the filters, and loop over all the charts.
If the current chart is not itself, it will reset the filter, and if the list of filters is not empty, it will then set the filters on the other chart.
Note the use of ._filter(), because as noted in the cited question, the heatmap has a weird override of .filter() which would stop us from setting multiple filters at once. Also note the array of arrays of filters. Wow.
Here is a working fiddle.

As explained in the comments above, you need to specify a color for the value zero that will match the deselected color.
You are already using the color mixin; it defines the .colors() method.
Currently you have
.colors(['#000080', '#000092', // ...
#000080 is your dark blue color, so change it to #ccc or whatever you want:
.colors(['#ccc', '#000092', // ...
Under the default style rules, a deselected item will also have opacity 0.5, so it won't look the same color. You can restore opacity 1 in your own CSS, and also change the deselected color if you want something besides the default #ccc:
.dc-chart .box-group.deselected rect.heat-box {
fill-opacity: 1;
/* fill: #ccc; */
}
Fork of your fiddle.

Related

Show only specific labels on x-axis

I am trying to select only specific range of labels to show up in my X-axis. Basically its a Time series and I wanted to select every alternate month to show up in the axis labels. But unable to figure out the way to do so.
Here is my fiddle so far
http://jsfiddle.net/sourabhtewari/1n19kwpv/
my code looks like this for the moment
var stuff = [{
"year": 2015,
"month": 12,
"s1": 0.38
},
{
"year": 2016,
"month": 1,
"s1": 0.39
},
{
"year": 2016,
"month": 2,
"s1": 0.43
},
{
"year": 2016,
"month": 3,
"s1": 0.40
},
{
"year": 2016,
"month": 4,
"s1": 0.39
},
{
"year": 2016,
"month": 5,
"s1": 0.39
},
{
"year": 2016,
"month": 6,
"s1": 0.38
},
{
"year": 2016,
"month": 7,
"s1": 0.37
},
{
"year": 2016,
"month": 8,
"s1": 0.37
},
{
"year": 2016,
"month": 9,
"s1": 0.35
},
{
"year": 2016,
"month": 10,
"s1": 0.37
},
{
"year": 2016,
"month": 11,
"s1": 0.36
},
{
"year": 2016,
"month": 12,
"s1": 0.37
},
{
"year": 2017,
"month": 1,
"s1": 0.35
},
{
"year": 2017,
"month": 2,
"s1": 0.36
},
{
"year": 2017,
"month": 3,
"s1": 0.37
},
{
"year": 2017,
"month": 4,
"s1": 0.38
},
{
"year": 2017,
"month": 5,
"s1": 0.35
},
{
"year": 2017,
"month": 0.36,
"s1": 0.36
},
{
"year": 2017,
"month": 7,
"s1": 0.36
},
{
"year": 2017,
"month": 8,
"s1": 0.35
},
{
"year": 2017,
"month": 9,
"s1": 0.36
},
{
"year": 2017,
"month": 10,
"s1": 0.37
},
];
var vals = [];
for (var i = 0; i < stuff.length; ++i) {
var date = new Date(stuff[i]["year"], stuff[i]["month"] - 1, 1);
vals.push({
x: date,
y: stuff[i]["s1"]
});
}
nv.addGraph(function() {
var data = [{
"values": vals,
"key": "s1",
"color": "#000000"
}];
var chart = nv.models.lineChart()
.forceY([0, 0.5]);
chart.xAxis.axisLabel("Date (m/y)")
.tickFormat(function(d, i) {
if (i % 2 == 1) d3.select(this).remove();
console.log(i);
return d3.time.format("%b-%y")(new Date(d))
});
chart.yAxis.axisLabel("s1").tickFormat(d3.format(","));
d3.select("#chart svg")
.datum(data)
.call(chart);
nv.utils.windowResize(function() {
d3.select("#chart svg").call(chart)
});
return chart;
});
Try adding ticks(d3.utcMonth) for xAxis.
chart.xAxis.axisLabel("Date (m/y)")
.ticks(d3.utcMonth)
.tickFormat(function(d,i) {
return d3.time.format("%b-%y")(new Date(d))
}) ;
Updated fiddle: http://jsfiddle.net/1n19kwpv/10/
I believe this answer to this is yes, you can, with one possible caveat (below).
According to this, you can do something like:
// Add the X Axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x)
.ticks(d3.timeMonth.every(2)));
The one thing I'm not positive about here is whether the timeMonth method is included in d3v3. It seems like nvd3 doesn't support d3v4 yet, and this example was made in d3v4.
Hope this helps.

Y-Axis label prefix for XY Amcharts

I didn't find any solution how to add prefix/suffix for Y-Axis of XYAmChart. Seems like labelFunction are not supported for valueAxes in XY chart type. Any suggestions?
var chart = AmCharts.makeChart("chartdiv", {
"type": "xy",
"theme": "light",
"dataDateFormat": "DD-MM-YYYY",
"graphs": [
{
"id":"g8",
"balloon":{
"drop":true,
"adjustBorderColor":false,
"color":"#ffffff"
},
"bullet":"round",
"bulletBorderAlpha":1,
"bulletColor":"#FFFFFF",
"bulletSize":5,
"dashLength":0,
"hideBulletsCount":50,
"lineThickness":2,
"lineColor":"#67b7dc",
"title":"Store 8",
"useLineColorForBulletBorder":true,
"xField":"d-1-c2",
"yField":"p-1-c2",
"xAxis":"g8",
"balloonText":"<span style='font-size:18px;'>[[d-1-c2]]</span><br>07/1/2017-12/31/2017"
},
{
"id":"g8-copy1",
"balloon":{
"drop":true,
"adjustBorderColor":false,
"color":"#ffffff"
},
"bullet":"round",
"bulletBorderAlpha":1,
"bulletColor":"#FFFFFF",
"bulletSize":5,
"dashLength":15,
"hideBulletsCount":50,
"lineThickness":2,
"lineColor":"#67b7dc",
"title":"Store 8",
"useLineColorForBulletBorder":true,
"xField":"d-1-c1",
"yField":"p-1-c1",
"xAxis":"g8-copy1",
"balloonText":"<span style='font-size:18px;'>[[d-1-c1]]</span><br>1/1/2017-6/29/2017"
}
],
"valueAxes": [
{
"id": "g8",
"axisAlpha": 1,
"gridAlpha": 1,
"axisColor": "#b0de09",
"color": "#b0de09",
"dashLength": 5,
"centerLabelOnFullp": true,
"position": "bottom",
"type": "date",
"minp": "DD-MM-YYYY",
"markPeriodChange": false,
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
},
{
"id": "g8-copy1",
"gridAlpha": 0,
"axisAlpha": 1,
"centerLabelOnFullp": false,
"position": "bottom",
"offset": 40,
"type": "date",
"minp": "DD-MM-YYYY",
'markPeriodChange': false,
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
}
],
"dataProvider": [
{
"d-1-c2":"01/01/2017",
"p-1-c2":"5684.6400"
},{
"d-1-c2":"01/02/2017",
"p-1-c2":"6468.9600"
},{
"d-1-c2":"01/03/2017",
"p-1-c2":"9032.7600"
},{
"d-1-c2":"01/04/2017",
"p-1-c2":"6385.9200"
},{
"d-1-c2":"01/05/2017",
"p-1-c2":"10087.3900"
},{
"d-1-c2":"01/06/2017",
"p-1-c2":"6136.3300"
},
{
"d-1-c1":"01/07/2017",
"p-1-c1":"4659.7000"
},{
"d-1-c1":"01/08/2017",
"p-1-c1":"9719.7100"
},{
"d-1-c1":"01/09/2017",
"p-1-c1":"4789.7300"
},{
"d-1-c1":"01/10/2017",
"p-1-c1":"7448.3900"
},{
"d-1-c1":"01/11/2017",
"p-1-c1":"6202.7200"
},{
"d-1-c1":"01/12/2017",
"p-1-c1":"9274.0300"
},{
"d-1-c1":"01/01/2018"
},{
"d-1-c2":"01/07/2017"
}
]
});
#chartdiv {
width: 100%;
height: 500px;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/xy.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
labelFunction is definitely supported as you can see below.
"valueAxes": [ {
"position": "bottom",
"axisAlpha": 0,
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
}, {
"minMaxMultiplier": 1.2,
"axisAlpha": 0,
"position": "left",
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
} ],
Demo:
var chart = AmCharts.makeChart( "chartdiv", {
"type": "xy",
"theme": "light",
"balloon":{
"fixedPosition":true,
},
"dataProvider": [ {
"y": 10,
"x": 14,
"value": 59,
"y2": -5,
"x2": -3,
"value2": 44
}, {
"y": 5,
"x": 3,
"value": 50,
"y2": -15,
"x2": -8,
"value2": 12
}, {
"y": -10,
"x": 8,
"value": 19,
"y2": -4,
"x2": 6,
"value2": 35
}, {
"y": -6,
"x": 5,
"value": 65,
"y2": -5,
"x2": -6,
"value2": 168
}, {
"y": 15,
"x": -4,
"value": 92,
"y2": -10,
"x2": -8,
"value2": 102
}, {
"y": 13,
"x": 1,
"value": 8,
"y2": -2,
"x2": 0,
"value2": 41
}, {
"y": 1,
"x": 6,
"value": 35,
"y2": 0,
"x2": -3,
"value2": 16
} ],
"valueAxes": [ {
"position": "bottom",
"axisAlpha": 0,
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
}, {
"minMaxMultiplier": 1.2,
"axisAlpha": 0,
"position": "left",
"labelFunction": function(value, valueText) {
return "prefix:" + valueText;
}
} ],
"startDuration": 1.5,
"graphs": [ {
"balloonText": "x:<b>[[x]]</b> y:<b>[[y]]</b><br>value:<b>[[value]]</b>",
"bullet": "circle",
"bulletBorderAlpha": 0.2,
"bulletAlpha": 0.8,
"lineAlpha": 0,
"fillAlphas": 0,
"valueField": "value",
"xField": "x",
"yField": "y",
"maxBulletSize": 100
}, {
"balloonText": "x:<b>[[x]]</b> y:<b>[[y]]</b><br>value:<b>[[value]]</b>",
"bullet": "diamond",
"bulletBorderAlpha": 0.2,
"bulletAlpha": 0.8,
"lineAlpha": 0,
"fillAlphas": 0,
"valueField": "value2",
"xField": "x2",
"yField": "y2",
"maxBulletSize": 100
} ]
} );
html, body {
width: 100%;
height: 100%;
margin: 0px;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/xy.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
You can also use unit and unitPosition if your custom labeling doesn't
require any additional logic that you can leverage through labelFunction.
"valueAxes": [ {
"position": "bottom",
"axisAlpha": 0,
"unit": "unit-prefix:",
"unitPosition": "left"
}, {
"minMaxMultiplier": 1.2,
"axisAlpha": 0,
"position": "left",
"unit": "unit-prefix:",
"unitPosition": "left"
} ],
Demo below.
var chart = AmCharts.makeChart( "chartdiv", {
"type": "xy",
"theme": "light",
"balloon":{
"fixedPosition":true,
},
"dataProvider": [ {
"y": 10,
"x": 14,
"value": 59,
"y2": -5,
"x2": -3,
"value2": 44
}, {
"y": 5,
"x": 3,
"value": 50,
"y2": -15,
"x2": -8,
"value2": 12
}, {
"y": -10,
"x": 8,
"value": 19,
"y2": -4,
"x2": 6,
"value2": 35
}, {
"y": -6,
"x": 5,
"value": 65,
"y2": -5,
"x2": -6,
"value2": 168
}, {
"y": 15,
"x": -4,
"value": 92,
"y2": -10,
"x2": -8,
"value2": 102
}, {
"y": 13,
"x": 1,
"value": 8,
"y2": -2,
"x2": 0,
"value2": 41
}, {
"y": 1,
"x": 6,
"value": 35,
"y2": 0,
"x2": -3,
"value2": 16
} ],
"valueAxes": [ {
"position": "bottom",
"axisAlpha": 0,
"unit": "unit-prefix:",
"unitPosition": "left"
}, {
"minMaxMultiplier": 1.2,
"axisAlpha": 0,
"position": "left",
"unit": "unit-prefix:",
"unitPosition": "left"
} ],
"startDuration": 1.5,
"graphs": [ {
"balloonText": "x:<b>[[x]]</b> y:<b>[[y]]</b><br>value:<b>[[value]]</b>",
"bullet": "circle",
"bulletBorderAlpha": 0.2,
"bulletAlpha": 0.8,
"lineAlpha": 0,
"fillAlphas": 0,
"valueField": "value",
"xField": "x",
"yField": "y",
"maxBulletSize": 100
}, {
"balloonText": "x:<b>[[x]]</b> y:<b>[[y]]</b><br>value:<b>[[value]]</b>",
"bullet": "diamond",
"bulletBorderAlpha": 0.2,
"bulletAlpha": 0.8,
"lineAlpha": 0,
"fillAlphas": 0,
"valueField": "value2",
"xField": "x2",
"yField": "y2",
"maxBulletSize": 100
} ]
} );
html, body {
width: 100%;
height: 100%;
margin: 0px;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/xy.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>

Horizontal stacked bar: one tick not displaying

Please can somebody tell me why the bar in top right corner is more elevated than all the others? When I use larger data-set it actually creates a stacked bar with multiple parts that seems not to be part of the chart and there is no right y axis tick value for it.
var dataset = [
{
"stopId": "165",
"data": [
{
"yValue": "101000017",
"xValue": 5,
"time": "10:53:00",
"stopId": "165",
"passengers": 12
},
{
"yValue": "101000018",
"xValue": 5,
"time": "11:23:00",
"stopId": "165",
"passengers": 22
},
{
"yValue": "101000019",
"xValue": 5,
"time": "11:53:00",
"stopId": "165",
"passengers": 6
},
{
"yValue": "101000020",
"xValue": 5,
"time": "12:23:00",
"stopId": "165",
"passengers": 14
},
{
"yValue": "101000021",
"xValue": 5,
"time": "12:51:00",
"stopId": "165",
"passengers": 13
},
{
"yValue": "101000022",
"xValue": 5,
"time": "13:24:00",
"stopId": "165",
"passengers": 21
},
{
"yValue": "101000023",
"xValue": 5,
"time": "13:54:00",
"stopId": "165",
"passengers": 28
},
{
"yValue": "101000024",
"xValue": 5,
"time": "14:24:00",
"stopId": "165",
"passengers": 28
},
{
"yValue": "101000025",
"xValue": 5,
"time": "14:54:00",
"stopId": "165",
"passengers": 4
},
{
"yValue": "101000026",
"xValue": 5,
"time": "15:23:00",
"stopId": "165",
"passengers": 18
},
{
"yValue": "101000027",
"xValue": 5,
"time": "15:55:00",
"stopId": "165",
"passengers": 5
},
{
"yValue": "101000028",
"xValue": 5,
"time": "16:10:00",
"stopId": "165",
"passengers": 27
},
{
"yValue": "101000029",
"xValue": 5,
"time": "16:25:00",
"stopId": "165",
"passengers": 6
},
{
"yValue": "101000030",
"xValue": 5,
"time": "16:38:00",
"stopId": "165",
"passengers": 18
},
{
"yValue": "101000031",
"xValue": 5,
"time": "16:55:00",
"stopId": "165",
"passengers": 26
},
{
"yValue": "101000032",
"xValue": 5,
"time": "17:10:00",
"stopId": "165",
"passengers": 29
},
{
"yValue": "101000033",
"xValue": 5,
"time": "17:25:00",
"stopId": "165",
"passengers": 24
},
{
"yValue": "101000034",
"xValue": 5,
"time": "17:40:00",
"stopId": "165",
"passengers": 29
}
],
"name": "Passengers"
},
{
"stopId": "65",
"data": [
{
"yValue": "101000017",
"xValue": 5,
"time": "10:54:00",
"stopId": "65",
"passengers": 20
},
{
"yValue": "101000018",
"xValue": 5,
"time": "11:24:00",
"stopId": "65",
"passengers": 22
},
{
"yValue": "101000019",
"xValue": 5,
"time": "11:54:00",
"stopId": "65",
"passengers": 8
},
{
"yValue": "101000020",
"xValue": 5,
"time": "12:24:00",
"stopId": "65",
"passengers": 27
},
{
"yValue": "101000021",
"xValue": 5,
"time": "12:52:00",
"stopId": "65",
"passengers": 26
},
{
"yValue": "101000022",
"xValue": 5,
"time": "13:25:00",
"stopId": "65",
"passengers": 24
},
{
"yValue": "101000023",
"xValue": 5,
"time": "13:55:00",
"stopId": "65",
"passengers": 25
},
{
"yValue": "101000024",
"xValue": 5,
"time": "14:25:00",
"stopId": "65",
"passengers": 28
},
{
"yValue": "101000025",
"xValue": 5,
"time": "14:55:00",
"stopId": "65",
"passengers": 7
},
{
"yValue": "101000026",
"xValue": 5,
"time": "15:24:00",
"stopId": "65",
"passengers": 23
},
{
"yValue": "101000027",
"xValue": 5,
"time": "15:56:00",
"stopId": "65",
"passengers": 18
},
{
"yValue": "101000028",
"xValue": 5,
"time": "16:11:00",
"stopId": "65",
"passengers": 4
},
{
"yValue": "101000029",
"xValue": 5,
"time": "16:26:00",
"stopId": "65",
"passengers": 29
},
{
"yValue": "101000030",
"xValue": 5,
"time": "16:39:00",
"stopId": "65",
"passengers": 19
},
{
"yValue": "101000031",
"xValue": 5,
"time": "16:56:00",
"stopId": "65",
"passengers": 28
},
{
"yValue": "101000032",
"xValue": 5,
"time": "17:11:00",
"stopId": "65",
"passengers": 21
},
{
"yValue": "101000033",
"xValue": 5,
"time": "17:26:00",
"stopId": "65",
"passengers": 26
},
{
"yValue": "101000034",
"xValue": 5,
"time": "17:41:00",
"stopId": "65",
"passengers": 19
}
],
"name": "Passengers"
},
{
"stopId": "63",
"data": [
{
"yValue": "101000017",
"xValue": 5,
"time": "10:55:00",
"stopId": "63",
"passengers": 10
},
{
"yValue": "101000018",
"xValue": 5,
"time": "11:25:00",
"stopId": "63",
"passengers": 9
},
{
"yValue": "101000019",
"xValue": 5,
"time": "11:55:00",
"stopId": "63",
"passengers": 21
},
{
"yValue": "101000020",
"xValue": 5,
"time": "12:25:00",
"stopId": "63",
"passengers": 28
},
{
"yValue": "101000021",
"xValue": 5,
"time": "12:53:00",
"stopId": "63",
"passengers": 8
},
{
"yValue": "101000022",
"xValue": 5,
"time": "13:26:00",
"stopId": "63",
"passengers": 13
},
{
"yValue": "101000023",
"xValue": 5,
"time": "13:56:00",
"stopId": "63",
"passengers": 29
},
{
"yValue": "101000024",
"xValue": 5,
"time": "14:26:00",
"stopId": "63",
"passengers": 28
},
{
"yValue": "101000025",
"xValue": 5,
"time": "14:56:00",
"stopId": "63",
"passengers": 14
},
{
"yValue": "101000026",
"xValue": 5,
"time": "15:25:00",
"stopId": "63",
"passengers": 30
},
{
"yValue": "101000027",
"xValue": 5,
"time": "15:57:00",
"stopId": "63",
"passengers": 21
},
{
"yValue": "101000028",
"xValue": 5,
"time": "16:12:00",
"stopId": "63",
"passengers": 10
},
{
"yValue": "101000029",
"xValue": 5,
"time": "16:27:00",
"stopId": "63",
"passengers": 16
},
{
"yValue": "101000030",
"xValue": 5,
"time": "16:40:00",
"stopId": "63",
"passengers": 24
},
{
"yValue": "101000031",
"xValue": 5,
"time": "16:57:00",
"stopId": "63",
"passengers": 3
},
{
"yValue": "101000032",
"xValue": 5,
"time": "17:12:00",
"stopId": "63",
"passengers": 15
},
{
"yValue": "101000033",
"xValue": 5,
"time": "17:27:00",
"stopId": "63",
"passengers": 23
},
{
"yValue": "101000034",
"xValue": 5,
"time": "17:42:00",
"stopId": "63",
"passengers": 21
}
],
"name": "Passengers"
},
{
"stopId": "47",
"data": [
{
"yValue": "101000017",
"xValue": 5,
"time": "10:56:00",
"stopId": "47",
"passengers": 13
},
{
"yValue": "101000018",
"xValue": 5,
"time": "11:26:00",
"stopId": "47",
"passengers": 7
},
{
"yValue": "101000019",
"xValue": 5,
"time": "11:56:00",
"stopId": "47",
"passengers": 21
},
{
"yValue": "101000020",
"xValue": 5,
"time": "12:26:00",
"stopId": "47",
"passengers": 22
},
{
"yValue": "101000021",
"xValue": 5,
"time": "12:54:00",
"stopId": "47",
"passengers": 28
},
{
"yValue": "101000022",
"xValue": 5,
"time": "13:27:00",
"stopId": "47",
"passengers": 30
},
{
"yValue": "101000023",
"xValue": 5,
"time": "13:57:00",
"stopId": "47",
"passengers": 20
},
{
"yValue": "101000024",
"xValue": 5,
"time": "14:27:00",
"stopId": "47",
"passengers": 17
},
{
"yValue": "101000025",
"xValue": 5,
"time": "14:57:00",
"stopId": "47",
"passengers": 14
},
{
"yValue": "101000026",
"xValue": 5,
"time": "15:26:00",
"stopId": "47",
"passengers": 11
},
{
"yValue": "101000027",
"xValue": 5,
"time": "15:58:00",
"stopId": "47",
"passengers": 28
},
{
"yValue": "101000028",
"xValue": 5,
"time": "16:13:00",
"stopId": "47",
"passengers": 7
},
{
"yValue": "101000029",
"xValue": 5,
"time": "16:28:00",
"stopId": "47",
"passengers": 24
},
{
"yValue": "101000030",
"xValue": 5,
"time": "16:41:00",
"stopId": "47",
"passengers": 29
},
{
"yValue": "101000031",
"xValue": 5,
"time": "16:58:00",
"stopId": "47",
"passengers": 19
},
{
"yValue": "101000032",
"xValue": 5,
"time": "17:13:00",
"stopId": "47",
"passengers": 23
},
{
"yValue": "101000033",
"xValue": 5,
"time": "17:28:00",
"stopId": "47",
"passengers": 19
},
{
"yValue": "101000034",
"xValue": 5,
"time": "17:43:00",
"stopId": "47",
"passengers": 12
}
],
"name": "Passengers"
},
{
"stopId": "185",
"data": [
{
"yValue": "101000026",
"xValue": 5,
"time": "15:27:00",
"stopId": "185",
"passengers": 23
},
{
"yValue": "101000027",
"xValue": 5,
"time": "15:59:00",
"stopId": "185",
"passengers": 11
},
{
"yValue": "101000028",
"xValue": 5,
"time": "16:14:00",
"stopId": "185",
"passengers": 24
},
{
"yValue": "101000029",
"xValue": 5,
"time": "16:29:00",
"stopId": "185",
"passengers": 16
},
{
"yValue": "101000030",
"xValue": 5,
"time": "16:42:00",
"stopId": "185",
"passengers": 22
},
{
"yValue": "101000031",
"xValue": 5,
"time": "16:59:00",
"stopId": "185",
"passengers": 23
},
{
"yValue": "101000032",
"xValue": 5,
"time": "17:14:00",
"stopId": "185",
"passengers": 27
},
{
"yValue": "101000033",
"xValue": 5,
"time": "17:29:00",
"stopId": "185",
"passengers": 29
},
{
"yValue": "101000034",
"xValue": 5,
"time": "17:44:00",
"stopId": "185",
"passengers": 24
},
{
"yValue": "101000035",
"xValue": 5,
"time": "17:59:00",
"stopId": "185",
"passengers": 19
},
{
"yValue": "101000036",
"xValue": 5,
"time": "18:14:00",
"stopId": "185",
"passengers": 23
},
{
"yValue": "101000037",
"xValue": 5,
"time": "18:29:00",
"stopId": "185",
"passengers": 6
},
{
"yValue": "101000038",
"xValue": 5,
"time": "18:44:00",
"stopId": "185",
"passengers": 24
},
{
"yValue": "101000039",
"xValue": 5,
"time": "18:59:00",
"stopId": "185",
"passengers": 26
},
{
"yValue": "101000040",
"xValue": 5,
"time": "19:29:00",
"stopId": "185",
"passengers": 21
},
{
"yValue": "101000041",
"xValue": 5,
"time": "19:59:00",
"stopId": "185",
"passengers": 23
},
{
"yValue": "101000042",
"xValue": 5,
"time": "20:25:00",
"stopId": "185",
"passengers": 20
},
{
"yValue": "101000043",
"xValue": 5,
"time": "21:25:00",
"stopId": "185",
"passengers": 29
}
],
"name": "Passengers"
}
];
var margins = {
top: 12,
left: 100,
right: 50,
bottom: 34
};
var numberOfRecords = 0;
var dataset = dataset.map(function(d) {
numberOfRecords = 0;
return d.data.map(function(o, i) {
numberOfRecords++;
return {
y: o.xValue,
x: o.yValue,
name: d.name,
stopId: o.stopId,
passengers: o.passengers,
time: o.time
};
});
});
d3.select(window).on("resize", throttle);
var stack = d3.layout.stack();
stack(dataset);
var dataset = dataset.map(function(group) {
return group.map(function(d) {
// Invert the x and y values, and y0 becomes x0
return {
x: d.y,
y: d.x,
x0: d.y0,
name: d.name,
stopId: d.stopId,
passengers: d.passengers,
time: d.time
};
});
});
var yValuesTripId = dataset[0].map(function(d) {
return d.y;
});
var yValuesFirstStopTimes = dataset[0].map(function(d) {
// return (d.time.slice(0, -3) + (Math.floor(Math.random() * (20000 - 1 + 1)) + 1));
return removeSecondsFromHHMMSS(d.time);
});
var yValuesLastStopTimes = dataset[dataset.length - 1].map(function(d) {
// return (d.time.slice(0, -3) + (Math.floor(Math.random() * (20000 - 1 + 1)) + 1));
return removeSecondsFromHHMMSS(d.time);
});
var xMax = d3.max(dataset, function(group) {
return d3.max(group, function(d) {
return d.x + d.x0;
});
});
var tooltip = d3.select("#chartDiv")
.append('div')
.attr('id', 'tooltip')
.attr('class', 'hidden');
const chartArea = $("#chartDiv");
var width = chartArea.innerWidth() - margins.left - margins.right;
var barHeight = 10;
var height = (numberOfRecords * 15) - margins.top - margins.bottom;
var svg, xScale, yScaleLeft, yScaleRight, rects;
draw(width, height);
function draw(width, height) {
svg = d3.select("#chartDiv")
.append('svg')
.attr('width', width + margins.left + margins.right)
.attr('height', height + margins.top + margins.bottom)
.append('g')
.attr('transform', 'translate(' + margins.left + ',' + margins.top + ')');
xScale = d3.scale.linear()
.domain([0, xMax])
.range([0, width - margins.right]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient('bottom')
.ticks(0);
yScaleLeft = d3.scale.ordinal()
.domain(yValuesTripId)
.rangeRoundBands([0, height], .1);
var yAxisLeft = d3.svg.axis()
.scale(yScaleLeft)
.orient('left')
.tickFormat(function(d, i) {
return yValuesFirstStopTimes[i];
});
yScaleRight = d3.scale.ordinal()
.domain(yValuesTripId)
.rangeRoundBands([0, height], .1);
var yAxisRight = d3.svg.axis()
.scale(yScaleRight)
.orient('right')
.tickPadding(-25)
.tickFormat(function(d, i) {
return yValuesLastStopTimes[i];
});
var groups = svg.selectAll('g')
.data(dataset)
.enter()
.append('g');
rects = groups.selectAll('rect')
.data(function(d) {
return d;
})
.enter()
.append('rect')
.attr('x', function(d) {
return xScale(d.x0);
})
.attr('y', function(d, i) {
return yScaleLeft(d.y);
})
.attr('height', function(d) {
return barHeight;
})
.attr('width', function(d) {
return xScale(d.x);
})
.attr('fill', function(d) {
return getColor(d.passengers);
})
.attr('stroke', 'white')
.on('mouseover', function(d) {
var xPos = d3.event.pageX - 310;
var yPos = d3.event.pageY - 110;
d3.select('#tooltip')
.style("left", xPos + "px")
.style("top", yPos + "px")
.style('width', '250px')
.text("Trip " + d.y + '|Passengers:' + d.passengers + '|Stop ' + d.stopId + '|time ' + removeSecondsFromHHMMSS(d.time));
d3.select('#tooltip').classed('hidden', false);
})
.on('mouseout', function() {
d3.select('#tooltip').classed('hidden', true);
});
svg.append('g')
.attr('class', 'axis')
.attr('id','busOccupancyChartXAxis')
.attr('transform', 'translate(0,' + height + ')')
.call(xAxis);
svg.append('g')
.attr('class', 'axis')
.call(yAxisLeft);
svg.append("text")
.attr("x", 0 - margins.left)
.attr("y", 0)
.text("Departure Time");
const newWidth = width - 20;
svg.append('g')
.attr('class', 'axis')
.attr("transform", "translate(" + newWidth + ",0)")
.call(yAxisRight);
svg.append("text")
.attr("x", width - margins.right + 10)
.attr("y", 0)
.text("Arrival Time");
}
function redraw() {
width = chartArea.innerWidth() - margins.left - margins.right;
d3.select('svg').remove();
d3.select('#legend').remove();
draw(width, height);
}
var throttleTimer;
function throttle() {
window.clearTimeout(throttleTimer);
throttleTimer = window.setTimeout(function() {
redraw();
}, 200);
}
function getColor(numberOfPassengers) {
cellColor = "";
if (numberOfPassengers < 10) {
cellColor = "#585858";
} else if (numberOfPassengers < 20) {
cellColor = "#FFCC00";
} else if (numberOfPassengers < 25) {
cellColor = "#FF3300";
} else {
cellColor = "#A00000";
}
return cellColor;
}
function removeSecondsFromHHMMSS (time) {
return time.slice(0, -3)
}
#chartDiv #xaxis .domain {
fill: none;
stroke: #000;
}
#chartDiv #xaxis text,
#yaxis text {
font-size: 12px;
}
#chartDiv .axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
display: none;
}
#chartDiv .axis text {
font-family: sans-serif;
font-size: 11px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="chartDiv">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="chartDiv">
</div>
If you inspect the DOM, you're gonna see that this rectangle is actually a bunch of rectangles, all in the same position. They are there, sitting on the top, because they have no y attribute.
The problem is not your code: the problem is actually your data. For each bus stop, you have a yValue starting at 101000017...
"yValue": "101000017"
... and ending at 101000034:
"yValue": "101000034"
Which are used for the ordinal scale. So, each bus stop should have the same y values, since they use the same ordinal scale.
However, for the last group, the yValues starts at 101000026 and ends at 101000043. When you use those values with the y scale, the last ones (from 101000034 on) return undefined and the rectangles end up in the top corner.
Solution: audit your data.
In the following snippet, I just changed the yValues of the last group to match the other groups.
Here is the working code:
var dataset = [
{
"stopId": "165",
"data": [
{
"yValue": "101000017",
"xValue": 5,
"time": "10:53:00",
"stopId": "165",
"passengers": 12
},
{
"yValue": "101000018",
"xValue": 5,
"time": "11:23:00",
"stopId": "165",
"passengers": 22
},
{
"yValue": "101000019",
"xValue": 5,
"time": "11:53:00",
"stopId": "165",
"passengers": 6
},
{
"yValue": "101000020",
"xValue": 5,
"time": "12:23:00",
"stopId": "165",
"passengers": 14
},
{
"yValue": "101000021",
"xValue": 5,
"time": "12:51:00",
"stopId": "165",
"passengers": 13
},
{
"yValue": "101000022",
"xValue": 5,
"time": "13:24:00",
"stopId": "165",
"passengers": 21
},
{
"yValue": "101000023",
"xValue": 5,
"time": "13:54:00",
"stopId": "165",
"passengers": 28
},
{
"yValue": "101000024",
"xValue": 5,
"time": "14:24:00",
"stopId": "165",
"passengers": 28
},
{
"yValue": "101000025",
"xValue": 5,
"time": "14:54:00",
"stopId": "165",
"passengers": 4
},
{
"yValue": "101000026",
"xValue": 5,
"time": "15:23:00",
"stopId": "165",
"passengers": 18
},
{
"yValue": "101000027",
"xValue": 5,
"time": "15:55:00",
"stopId": "165",
"passengers": 5
},
{
"yValue": "101000028",
"xValue": 5,
"time": "16:10:00",
"stopId": "165",
"passengers": 27
},
{
"yValue": "101000029",
"xValue": 5,
"time": "16:25:00",
"stopId": "165",
"passengers": 6
},
{
"yValue": "101000030",
"xValue": 5,
"time": "16:38:00",
"stopId": "165",
"passengers": 18
},
{
"yValue": "101000031",
"xValue": 5,
"time": "16:55:00",
"stopId": "165",
"passengers": 26
},
{
"yValue": "101000032",
"xValue": 5,
"time": "17:10:00",
"stopId": "165",
"passengers": 29
},
{
"yValue": "101000033",
"xValue": 5,
"time": "17:25:00",
"stopId": "165",
"passengers": 24
},
{
"yValue": "101000034",
"xValue": 5,
"time": "17:40:00",
"stopId": "165",
"passengers": 29
}
],
"name": "Passengers"
},
{
"stopId": "65",
"data": [
{
"yValue": "101000017",
"xValue": 5,
"time": "10:54:00",
"stopId": "65",
"passengers": 20
},
{
"yValue": "101000018",
"xValue": 5,
"time": "11:24:00",
"stopId": "65",
"passengers": 22
},
{
"yValue": "101000019",
"xValue": 5,
"time": "11:54:00",
"stopId": "65",
"passengers": 8
},
{
"yValue": "101000020",
"xValue": 5,
"time": "12:24:00",
"stopId": "65",
"passengers": 27
},
{
"yValue": "101000021",
"xValue": 5,
"time": "12:52:00",
"stopId": "65",
"passengers": 26
},
{
"yValue": "101000022",
"xValue": 5,
"time": "13:25:00",
"stopId": "65",
"passengers": 24
},
{
"yValue": "101000023",
"xValue": 5,
"time": "13:55:00",
"stopId": "65",
"passengers": 25
},
{
"yValue": "101000024",
"xValue": 5,
"time": "14:25:00",
"stopId": "65",
"passengers": 28
},
{
"yValue": "101000025",
"xValue": 5,
"time": "14:55:00",
"stopId": "65",
"passengers": 7
},
{
"yValue": "101000026",
"xValue": 5,
"time": "15:24:00",
"stopId": "65",
"passengers": 23
},
{
"yValue": "101000027",
"xValue": 5,
"time": "15:56:00",
"stopId": "65",
"passengers": 18
},
{
"yValue": "101000028",
"xValue": 5,
"time": "16:11:00",
"stopId": "65",
"passengers": 4
},
{
"yValue": "101000029",
"xValue": 5,
"time": "16:26:00",
"stopId": "65",
"passengers": 29
},
{
"yValue": "101000030",
"xValue": 5,
"time": "16:39:00",
"stopId": "65",
"passengers": 19
},
{
"yValue": "101000031",
"xValue": 5,
"time": "16:56:00",
"stopId": "65",
"passengers": 28
},
{
"yValue": "101000032",
"xValue": 5,
"time": "17:11:00",
"stopId": "65",
"passengers": 21
},
{
"yValue": "101000033",
"xValue": 5,
"time": "17:26:00",
"stopId": "65",
"passengers": 26
},
{
"yValue": "101000034",
"xValue": 5,
"time": "17:41:00",
"stopId": "65",
"passengers": 19
}
],
"name": "Passengers"
},
{
"stopId": "63",
"data": [
{
"yValue": "101000017",
"xValue": 5,
"time": "10:55:00",
"stopId": "63",
"passengers": 10
},
{
"yValue": "101000018",
"xValue": 5,
"time": "11:25:00",
"stopId": "63",
"passengers": 9
},
{
"yValue": "101000019",
"xValue": 5,
"time": "11:55:00",
"stopId": "63",
"passengers": 21
},
{
"yValue": "101000020",
"xValue": 5,
"time": "12:25:00",
"stopId": "63",
"passengers": 28
},
{
"yValue": "101000021",
"xValue": 5,
"time": "12:53:00",
"stopId": "63",
"passengers": 8
},
{
"yValue": "101000022",
"xValue": 5,
"time": "13:26:00",
"stopId": "63",
"passengers": 13
},
{
"yValue": "101000023",
"xValue": 5,
"time": "13:56:00",
"stopId": "63",
"passengers": 29
},
{
"yValue": "101000024",
"xValue": 5,
"time": "14:26:00",
"stopId": "63",
"passengers": 28
},
{
"yValue": "101000025",
"xValue": 5,
"time": "14:56:00",
"stopId": "63",
"passengers": 14
},
{
"yValue": "101000026",
"xValue": 5,
"time": "15:25:00",
"stopId": "63",
"passengers": 30
},
{
"yValue": "101000027",
"xValue": 5,
"time": "15:57:00",
"stopId": "63",
"passengers": 21
},
{
"yValue": "101000028",
"xValue": 5,
"time": "16:12:00",
"stopId": "63",
"passengers": 10
},
{
"yValue": "101000029",
"xValue": 5,
"time": "16:27:00",
"stopId": "63",
"passengers": 16
},
{
"yValue": "101000030",
"xValue": 5,
"time": "16:40:00",
"stopId": "63",
"passengers": 24
},
{
"yValue": "101000031",
"xValue": 5,
"time": "16:57:00",
"stopId": "63",
"passengers": 3
},
{
"yValue": "101000032",
"xValue": 5,
"time": "17:12:00",
"stopId": "63",
"passengers": 15
},
{
"yValue": "101000033",
"xValue": 5,
"time": "17:27:00",
"stopId": "63",
"passengers": 23
},
{
"yValue": "101000034",
"xValue": 5,
"time": "17:42:00",
"stopId": "63",
"passengers": 21
}
],
"name": "Passengers"
},
{
"stopId": "47",
"data": [
{
"yValue": "101000017",
"xValue": 5,
"time": "10:56:00",
"stopId": "47",
"passengers": 13
},
{
"yValue": "101000018",
"xValue": 5,
"time": "11:26:00",
"stopId": "47",
"passengers": 7
},
{
"yValue": "101000019",
"xValue": 5,
"time": "11:56:00",
"stopId": "47",
"passengers": 21
},
{
"yValue": "101000020",
"xValue": 5,
"time": "12:26:00",
"stopId": "47",
"passengers": 22
},
{
"yValue": "101000021",
"xValue": 5,
"time": "12:54:00",
"stopId": "47",
"passengers": 28
},
{
"yValue": "101000022",
"xValue": 5,
"time": "13:27:00",
"stopId": "47",
"passengers": 30
},
{
"yValue": "101000023",
"xValue": 5,
"time": "13:57:00",
"stopId": "47",
"passengers": 20
},
{
"yValue": "101000024",
"xValue": 5,
"time": "14:27:00",
"stopId": "47",
"passengers": 17
},
{
"yValue": "101000025",
"xValue": 5,
"time": "14:57:00",
"stopId": "47",
"passengers": 14
},
{
"yValue": "101000026",
"xValue": 5,
"time": "15:26:00",
"stopId": "47",
"passengers": 11
},
{
"yValue": "101000027",
"xValue": 5,
"time": "15:58:00",
"stopId": "47",
"passengers": 28
},
{
"yValue": "101000028",
"xValue": 5,
"time": "16:13:00",
"stopId": "47",
"passengers": 7
},
{
"yValue": "101000029",
"xValue": 5,
"time": "16:28:00",
"stopId": "47",
"passengers": 24
},
{
"yValue": "101000030",
"xValue": 5,
"time": "16:41:00",
"stopId": "47",
"passengers": 29
},
{
"yValue": "101000031",
"xValue": 5,
"time": "16:58:00",
"stopId": "47",
"passengers": 19
},
{
"yValue": "101000032",
"xValue": 5,
"time": "17:13:00",
"stopId": "47",
"passengers": 23
},
{
"yValue": "101000033",
"xValue": 5,
"time": "17:28:00",
"stopId": "47",
"passengers": 19
},
{
"yValue": "101000034",
"xValue": 5,
"time": "17:43:00",
"stopId": "47",
"passengers": 12
}
],
"name": "Passengers"
},
{
"stopId": "185",
"data": [
{
"yValue": "101000017",
"xValue": 5,
"time": "15:27:00",
"stopId": "185",
"passengers": 23
},
{
"yValue": "101000018",
"xValue": 5,
"time": "15:59:00",
"stopId": "185",
"passengers": 11
},
{
"yValue": "101000019",
"xValue": 5,
"time": "16:14:00",
"stopId": "185",
"passengers": 24
},
{
"yValue": "101000020",
"xValue": 5,
"time": "16:29:00",
"stopId": "185",
"passengers": 16
},
{
"yValue": "101000021",
"xValue": 5,
"time": "16:42:00",
"stopId": "185",
"passengers": 22
},
{
"yValue": "101000022",
"xValue": 5,
"time": "16:59:00",
"stopId": "185",
"passengers": 23
},
{
"yValue": "101000023",
"xValue": 5,
"time": "17:14:00",
"stopId": "185",
"passengers": 27
},
{
"yValue": "101000024",
"xValue": 5,
"time": "17:29:00",
"stopId": "185",
"passengers": 29
},
{
"yValue": "101000025",
"xValue": 5,
"time": "17:44:00",
"stopId": "185",
"passengers": 24
},
{
"yValue": "101000026",
"xValue": 5,
"time": "17:59:00",
"stopId": "185",
"passengers": 19
},
{
"yValue": "101000027",
"xValue": 5,
"time": "18:14:00",
"stopId": "185",
"passengers": 23
},
{
"yValue": "101000028",
"xValue": 5,
"time": "18:29:00",
"stopId": "185",
"passengers": 6
},
{
"yValue": "101000029",
"xValue": 5,
"time": "18:44:00",
"stopId": "185",
"passengers": 24
},
{
"yValue": "101000030",
"xValue": 5,
"time": "18:59:00",
"stopId": "185",
"passengers": 26
},
{
"yValue": "101000031",
"xValue": 5,
"time": "19:29:00",
"stopId": "185",
"passengers": 21
},
{
"yValue": "101000032",
"xValue": 5,
"time": "19:59:00",
"stopId": "185",
"passengers": 23
},
{
"yValue": "101000033",
"xValue": 5,
"time": "20:25:00",
"stopId": "185",
"passengers": 20
},
{
"yValue": "101000034",
"xValue": 5,
"time": "21:25:00",
"stopId": "185",
"passengers": 29
}
],
"name": "Passengers"
}
];
var margins = {
top: 12,
left: 100,
right: 50,
bottom: 34
};
var numberOfRecords = 0;
var dataset = dataset.map(function(d) {
numberOfRecords = 0;
return d.data.map(function(o, i) {
numberOfRecords++;
return {
y: o.xValue,
x: o.yValue,
name: d.name,
stopId: o.stopId,
passengers: o.passengers,
time: o.time
};
});
});
d3.select(window).on("resize", throttle);
var stack = d3.layout.stack();
stack(dataset);
var dataset = dataset.map(function(group) {
return group.map(function(d) {
// Invert the x and y values, and y0 becomes x0
return {
x: d.y,
y: d.x,
x0: d.y0,
name: d.name,
stopId: d.stopId,
passengers: d.passengers,
time: d.time
};
});
});
var yValuesTripId = dataset[0].map(function(d) {
return d.y;
});
var yValuesFirstStopTimes = dataset[0].map(function(d) {
// return (d.time.slice(0, -3) + (Math.floor(Math.random() * (20000 - 1 + 1)) + 1));
return removeSecondsFromHHMMSS(d.time);
});
var yValuesLastStopTimes = dataset[dataset.length - 1].map(function(d) {
// return (d.time.slice(0, -3) + (Math.floor(Math.random() * (20000 - 1 + 1)) + 1));
return removeSecondsFromHHMMSS(d.time);
});
var xMax = d3.max(dataset, function(group) {
return d3.max(group, function(d) {
return d.x + d.x0;
});
});
var tooltip = d3.select("#chartDiv")
.append('div')
.attr('id', 'tooltip')
.attr('class', 'hidden');
const chartArea = $("#chartDiv");
var width = chartArea.innerWidth() - margins.left - margins.right;
var barHeight = 10;
var height = (numberOfRecords * 15) - margins.top - margins.bottom;
var svg, xScale, yScaleLeft, yScaleRight, rects;
draw(width, height);
function draw(width, height) {
svg = d3.select("#chartDiv")
.append('svg')
.attr('width', width + margins.left + margins.right)
.attr('height', height + margins.top + margins.bottom)
.append('g')
.attr('transform', 'translate(' + margins.left + ',' + margins.top + ')');
xScale = d3.scale.linear()
.domain([0, xMax])
.range([0, width - margins.right]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient('bottom')
.ticks(0);
yScaleLeft = d3.scale.ordinal()
.domain(yValuesTripId)
.rangeRoundBands([0, height], .1);
var yAxisLeft = d3.svg.axis()
.scale(yScaleLeft)
.orient('left')
.tickFormat(function(d, i) {
return yValuesFirstStopTimes[i];
});
yScaleRight = d3.scale.ordinal()
.domain(yValuesTripId)
.rangeRoundBands([0, height], .1);
var yAxisRight = d3.svg.axis()
.scale(yScaleRight)
.orient('right')
.tickPadding(-25)
.tickFormat(function(d, i) {
return yValuesLastStopTimes[i];
});
var groups = svg.selectAll('g')
.data(dataset)
.enter()
.append('g');
rects = groups.selectAll('rect')
.data(function(d) {
return d;
})
.enter()
.append('rect')
.attr('x', function(d) {
return xScale(d.x0);
})
.attr('y', function(d, i) {
return yScaleLeft(d.y);
})
.attr('height', function(d) {
return barHeight;
})
.attr('width', function(d) {
return xScale(d.x);
})
.attr('fill', function(d) {
return getColor(d.passengers);
})
.attr('stroke', 'white')
.on('mouseover', function(d) {
var xPos = d3.event.pageX - 310;
var yPos = d3.event.pageY - 110;
d3.select('#tooltip')
.style("left", xPos + "px")
.style("top", yPos + "px")
.style('width', '250px')
.text("Trip " + d.y + '|Passengers:' + d.passengers + '|Stop ' + d.stopId + '|time ' + removeSecondsFromHHMMSS(d.time));
d3.select('#tooltip').classed('hidden', false);
})
.on('mouseout', function() {
d3.select('#tooltip').classed('hidden', true);
});
svg.append('g')
.attr('class', 'axis')
.attr('id','busOccupancyChartXAxis')
.attr('transform', 'translate(0,' + height + ')')
.call(xAxis);
svg.append('g')
.attr('class', 'axis')
.call(yAxisLeft);
svg.append("text")
.attr("x", 0 - margins.left)
.attr("y", 0)
.text("Departure Time");
const newWidth = width - 20;
svg.append('g')
.attr('class', 'axis')
.attr("transform", "translate(" + newWidth + ",0)")
.call(yAxisRight);
svg.append("text")
.attr("x", width - margins.right + 10)
.attr("y", 0)
.text("Arrival Time");
}
function redraw() {
width = chartArea.innerWidth() - margins.left - margins.right;
d3.select('svg').remove();
d3.select('#legend').remove();
draw(width, height);
}
var throttleTimer;
function throttle() {
window.clearTimeout(throttleTimer);
throttleTimer = window.setTimeout(function() {
redraw();
}, 200);
}
function getColor(numberOfPassengers) {
cellColor = "";
if (numberOfPassengers < 10) {
cellColor = "#585858";
} else if (numberOfPassengers < 20) {
cellColor = "#FFCC00";
} else if (numberOfPassengers < 25) {
cellColor = "#FF3300";
} else {
cellColor = "#A00000";
}
return cellColor;
}
function removeSecondsFromHHMMSS (time) {
return time.slice(0, -3)
}
#chartDiv #xaxis .domain {
fill: none;
stroke: #000;
}
#chartDiv #xaxis text,
#yaxis text {
font-size: 12px;
}
#chartDiv .axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
display: none;
}
#chartDiv .axis text {
font-family: sans-serif;
font-size: 11px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="chartDiv">
</div>

Elasticsearch search for sum greater than a value

Query - find cruises having occupancy.
I need to search for cruises having sum of all (total_occupancy - current_occupancy) of cabins greater than a value (say 100) in ElastcSearch.
PFB a sample cruise document:-
{
"disDmbarkationLocation": "From Location",
"embarkationLocation": "To Location",
"segments": [
{
"segmentId": "SEG1",
"cabinInventories": [
{
"maxOccupancy": 10,
"lastModified": 1469538908559,
"currentOccupancy": 10,
"cabinNo": 1
},
{
"maxOccupancy": 10,
"lastModified": 1469538908560,
"currentOccupancy": 9,
"cabinNo": 2
},
{
"maxOccupancy": 10,
"lastModified": 1469538908560,
"currentOccupancy": 8,
"cabinNo": 3
},
{
"maxOccupancy": 10,
"lastModified": 1469538908560,
"currentOccupancy": 8,
"cabinNo": 4
}
]
},
{
"segmentId": "SEG2",
"cabinInventories": [
{
"maxOccupancy": 10,
"lastModified": 1469538908561,
"currentOccupancy": 10,
"cabinNo": 1
},
{
"maxOccupancy": 10,
"lastModified": 1469538908562,
"currentOccupancy": 10,
"cabinNo": 2
},
{
"maxOccupancy": 10,
"lastModified": 1469538908562,
"currentOccupancy": 10,
"cabinNo": 3
},
{
"maxOccupancy": 10,
"lastModified": 1469538908562,
"currentOccupancy": 10,
"cabinNo": 4
}
]
},
{
"segmentId": "SEG3",
"cabinInventories": [
{
"maxOccupancy": 10,
"lastModified": 1469538908562,
"currentOccupancy": 10,
"cabinNo": 1
},
{
"maxOccupancy": 10,
"lastModified": 1469538908562,
"currentOccupancy": 8,
"cabinNo": 2
},
{
"maxOccupancy": 10,
"lastModified": 1469538908563,
"currentOccupancy": 8,
"cabinNo": 3
},
{
"maxOccupancy": 10,
"lastModified": 1469538908563,
"currentOccupancy": 8,
"cabinNo": 4
}
]
}
]
}
]
}

How to add text in the center of node in force directed graph? [duplicate]

This question already has answers here:
Placing labels at the center of nodes in d3.js
(3 answers)
Closed 7 years ago.
Here is the code to draw to text in the center of the circle.
node.append("text")
.attr("dx", 10)
.attr("dy", ".35em")
.text(function(d) { return d.group });
Here is the jsfiddle
Just set text-anchor attribute middle to the text element. This code would work even for nodes having different size.
node.append("text")
.attr("text-anchor", "middle")
.attr("dy", ".35em")
.text(function(d) { return d.group });
Working snippet:
var width = 500,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(80)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var mis = document.getElementById('mis').innerHTML;
graph = JSON.parse(mis);
force.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) {
return Math.sqrt(d.value);
});
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", function(d,i){ return i%2==0?10:8 })
.style("fill", function(d) {
return color(d.group);
})
node.append("text")
.attr("text-anchor", "middle")
.attr("dy", ".35em")
.text(function(d) {
return d.group
});
force.on("tick", function() {
link.attr("x1", function(d) {
return d.source.x;
})
.attr("y1", function(d) {
return d.source.y;
})
.attr("x2", function(d) {
return d.target.x;
})
.attr("y2", function(d) {
return d.target.y;
});
d3.selectAll("circle").attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
});
d3.selectAll("text").attr("x", function(d) {
return d.x;
})
.attr("y", function(d) {
return d.y;
});
});
.link {
stroke: #ccc;
}
.node text {
pointer-events: none;
font: 10px sans-serif;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script type="application/json" id="mis">
{ "nodes": [{ "name": "Myriel", "group": 1 }, { "name": "Napoleon", "group": 1 }, { "name": "Mlle.Baptistine", "group": 1 }, { "name": "Mme.Magloire", "group": 1 }, { "name": "CountessdeLo", "group": 1 }, { "name": "Geborand", "group": 1 }, { "name": "Champtercier", "group": 1 }, { "name": "Cravatte", "group": 1 }, { "name": "Count", "group": 1 }, { "name": "OldMan", "group": 1 }, { "name": "Labarre", "group": 2 }, { "name": "Valjean", "group": 2 }, { "name": "Marguerite", "group": 3 }, { "name": "Mme.deR", "group": 2 }, { "name": "Isabeau", "group": 2 }, { "name": "Gervais", "group": 2 }, { "name": "Tholomyes", "group": 3 }, { "name": "Listolier", "group": 3 }, { "name": "Fameuil", "group": 3 }, { "name": "Blacheville", "group": 3 }, { "name": "Favourite", "group": 3 }, { "name": "Dahlia", "group": 3 }, { "name": "Zephine", "group": 3 }, { "name": "Fantine", "group": 3 }, { "name": "Mme.Thenardier", "group": 4 }, { "name": "Thenardier", "group": 4 }, { "name": "Cosette", "group": 5 }, { "name": "Javert", "group": 4 }, { "name": "Fauchelevent", "group": 0 }, { "name": "Bamatabois", "group": 2 }, { "name": "Perpetue", "group": 3 }, { "name": "Simplice", "group": 2 }, { "name": "Scaufflaire", "group": 2 }, { "name": "Woman1", "group": 2 }, { "name": "Judge", "group": 2 }, { "name": "Champmathieu", "group": 2 }, { "name": "Brevet", "group": 2 }, { "name": "Chenildieu", "group": 2 }, { "name": "Cochepaille", "group": 2 }, { "name": "Pontmercy", "group": 4 }, { "name": "Boulatruelle", "group": 6 }, { "name": "Eponine", "group": 4 }, { "name": "Anzelma", "group": 4 }, { "name": "Woman2", "group": 5 }, { "name": "MotherInnocent", "group": 0 }, { "name": "Gribier", "group": 0 }, { "name": "Jondrette", "group": 7 }, { "name": "Mme.Burgon", "group": 7 }, { "name": "Gavroche", "group": 8 }, { "name": "Gillenormand", "group": 5 }, { "name": "Magnon", "group": 5 }, { "name": "Mlle.Gillenormand", "group": 5 }, { "name": "Mme.Pontmercy", "group": 5 }, { "name": "Mlle.Vaubois", "group": 5 }, { "name": "Lt.Gillenormand", "group": 5 }, { "name": "Marius", "group": 8 }, { "name": "BaronessT", "group": 5 }, { "name": "Mabeuf", "group": 8 }, { "name": "Enjolras", "group": 8 }, { "name": "Combeferre", "group": 8 }, { "name": "Prouvaire", "group": 8 }, { "name": "Feuilly", "group": 8 }, { "name": "Courfeyrac", "group": 8 }, { "name": "Bahorel", "group": 8 }, { "name": "Bossuet", "group": 8 }, { "name": "Joly", "group": 8 }, { "name": "Grantaire", "group": 8 }, { "name": "MotherPlutarch", "group": 9 }, { "name": "Gueulemer", "group": 4 }, { "name": "Babet", "group": 4 }, { "name": "Claquesous", "group": 4 }, { "name": "Montparnasse", "group": 4 }, { "name": "Toussaint", "group": 5 }, { "name": "Child1", "group": 10 }, { "name": "Child2", "group": 10 }, { "name": "Brujon", "group": 4 }, { "name": "Mme.Hucheloup", "group": 8 }], "links": [{ "source": 1, "target": 0, "value": 1 }, { "source": 2, "target": 0, "value": 8 }, { "source": 3, "target": 0, "value": 10 }, { "source": 3, "target": 2, "value": 6 }, { "source": 4, "target": 0, "value": 1 }, { "source": 5, "target": 0, "value": 1 }, { "source": 6, "target": 0, "value": 1 }, { "source": 7, "target": 0, "value": 1 }, { "source": 8, "target": 0, "value": 2 }, { "source": 9, "target": 0, "value": 1 }, { "source": 11, "target": 10, "value": 1 }, { "source": 11, "target": 3, "value": 3 }, { "source": 11, "target": 2, "value": 3 }, { "source": 11, "target": 0, "value": 5 }, { "source": 12, "target": 11, "value": 1 }, { "source": 13, "target": 11, "value": 1 }, { "source": 14, "target": 11, "value": 1 }, { "source": 15, "target": 11, "value": 1 }, { "source": 17, "target": 16, "value": 4 }, { "source": 18, "target": 16, "value": 4 }, { "source": 18, "target": 17, "value": 4 }, { "source": 19, "target": 16, "value": 4 }, { "source": 19, "target": 17, "value": 4 }, { "source": 19, "target": 18, "value": 4 }, { "source": 20, "target": 16, "value": 3 }, { "source": 20, "target": 17, "value": 3 }, { "source": 20, "target": 18, "value": 3 }, { "source": 20, "target": 19, "value": 4 }, { "source": 21, "target": 16, "value": 3 }, { "source": 21, "target": 17, "value": 3 }, { "source": 21, "target": 18, "value": 3 }, { "source": 21, "target": 19, "value": 3 }, { "source": 21, "target": 20, "value": 5 }, { "source": 22, "target": 16, "value": 3 }, { "source": 22, "target": 17, "value": 3 }, { "source": 22, "target": 18, "value": 3 }, { "source": 22, "target": 19, "value": 3 }, { "source": 22, "target": 20, "value": 4 }, { "source": 22, "target": 21, "value": 4 }, { "source": 23, "target": 16, "value": 3 }, { "source": 23, "target": 17, "value": 3 }, { "source": 23, "target": 18, "value": 3 }, { "source": 23, "target": 19, "value": 3 }, { "source": 23, "target": 20, "value": 4 }, { "source": 23, "target": 21, "value": 4 }, { "source": 23, "target": 22, "value": 4 }, { "source": 23, "target": 12, "value": 2 }, { "source": 23, "target": 11, "value": 9 }, { "source": 24, "target": 23, "value": 2 }, { "source": 24, "target": 11, "value": 7 }, { "source": 25, "target": 24, "value": 13 }, { "source": 25, "target": 23, "value": 1 }, { "source": 25, "target": 11, "value": 12 }, { "source": 26, "target": 24, "value": 4 }, { "source": 26, "target": 11, "value": 31 }, { "source": 26, "target": 16, "value": 1 }, { "source": 26, "target": 25, "value": 1 }, { "source": 27, "target": 11, "value": 17 }, { "source": 27, "target": 23, "value": 5 }, { "source": 27, "target": 25, "value": 5 }, { "source": 27, "target": 24, "value": 1 }, { "source": 27, "target": 26, "value": 1 }, { "source": 28, "target": 11, "value": 8 }, { "source": 28, "target": 27, "value": 1 }, { "source": 29, "target": 23, "value": 1 }, { "source": 29, "target": 27, "value": 1 }, { "source": 29, "target": 11, "value": 2 }, { "source": 30, "target": 23, "value": 1 }, { "source": 31, "target": 30, "value": 2 }, { "source": 31, "target": 11, "value": 3 }, { "source": 31, "target": 23, "value": 2 }, { "source": 31, "target": 27, "value": 1 }, { "source": 32, "target": 11, "value": 1 }, { "source": 33, "target": 11, "value": 2 }, { "source": 33, "target": 27, "value": 1 }, { "source": 34, "target": 11, "value": 3 }, { "source": 34, "target": 29, "value": 2 }, { "source": 35, "target": 11, "value": 3 }, { "source": 35, "target": 34, "value": 3 }, { "source": 35, "target": 29, "value": 2 }, { "source": 36, "target": 34, "value": 2 }, { "source": 36, "target": 35, "value": 2 }, { "source": 36, "target": 11, "value": 2 }, { "source": 36, "target": 29, "value": 1 }, { "source": 37, "target": 34, "value": 2 }, { "source": 37, "target": 35, "value": 2 }, { "source": 37, "target": 36, "value": 2 }, { "source": 37, "target": 11, "value": 2 }, { "source": 37, "target": 29, "value": 1 }, { "source": 38, "target": 34, "value": 2 }, { "source": 38, "target": 35, "value": 2 }, { "source": 38, "target": 36, "value": 2 }, { "source": 38, "target": 37, "value": 2 }, { "source": 38, "target": 11, "value": 2 }, { "source": 38, "target": 29, "value": 1 }, { "source": 39, "target": 25, "value": 1 }, { "source": 40, "target": 25, "value": 1 }, { "source": 41, "target": 24, "value": 2 }, { "source": 41, "target": 25, "value": 3 }, { "source": 42, "target": 41, "value": 2 }, { "source": 42, "target": 25, "value": 2 }, { "source": 42, "target": 24, "value": 1 }, { "source": 43, "target": 11, "value": 3 }, { "source": 43, "target": 26, "value": 1 }, { "source": 43, "target": 27, "value": 1 }, { "source": 44, "target": 28, "value": 3 }, { "source": 44, "target": 11, "value": 1 }, { "source": 45, "target": 28, "value": 2 }, { "source": 47, "target": 46, "value": 1 }, { "source": 48, "target": 47, "value": 2 }, { "source": 48, "target": 25, "value": 1 }, { "source": 48, "target": 27, "value": 1 }, { "source": 48, "target": 11, "value": 1 }, { "source": 49, "target": 26, "value": 3 }, { "source": 49, "target": 11, "value": 2 }, { "source": 50, "target": 49, "value": 1 }, { "source": 50, "target": 24, "value": 1 }, { "source": 51, "target": 49, "value": 9 }, { "source": 51, "target": 26, "value": 2 }, { "source": 51, "target": 11, "value": 2 }, { "source": 52, "target": 51, "value": 1 }, { "source": 52, "target": 39, "value": 1 }, { "source": 53, "target": 51, "value": 1 }, { "source": 54, "target": 51, "value": 2 }, { "source": 54, "target": 49, "value": 1 }, { "source": 54, "target": 26, "value": 1 }, { "source": 55, "target": 51, "value": 6 }, { "source": 55, "target": 49, "value": 12 }, { "source": 55, "target": 39, "value": 1 }, { "source": 55, "target": 54, "value": 1 }, { "source": 55, "target": 26, "value": 21 }, { "source": 55, "target": 11, "value": 19 }, { "source": 55, "target": 16, "value": 1 }, { "source": 55, "target": 25, "value": 2 }, { "source": 55, "target": 41, "value": 5 }, { "source": 55, "target": 48, "value": 4 }, { "source": 56, "target": 49, "value": 1 }, { "source": 56, "target": 55, "value": 1 }, { "source": 57, "target": 55, "value": 1 }, { "source": 57, "target": 41, "value": 1 }, { "source": 57, "target": 48, "value": 1 }, { "source": 58, "target": 55, "value": 7 }, { "source": 58, "target": 48, "value": 7 }, { "source": 58, "target": 27, "value": 6 }, { "source": 58, "target": 57, "value": 1 }, { "source": 58, "target": 11, "value": 4 }, { "source": 59, "target": 58, "value": 15 }, { "source": 59, "target": 55, "value": 5 }, { "source": 59, "target": 48, "value": 6 }, { "source": 59, "target": 57, "value": 2 }, { "source": 60, "target": 48, "value": 1 }, { "source": 60, "target": 58, "value": 4 }, { "source": 60, "target": 59, "value": 2 }, { "source": 61, "target": 48, "value": 2 }, { "source": 61, "target": 58, "value": 6 }, { "source": 61, "target": 60, "value": 2 }, { "source": 61, "target": 59, "value": 5 }, { "source": 61, "target": 57, "value": 1 }, { "source": 61, "target": 55, "value": 1 }, { "source": 62, "target": 55, "value": 9 }, { "source": 62, "target": 58, "value": 17 }, { "source": 62, "target": 59, "value": 13 }, { "source": 62, "target": 48, "value": 7 }, { "source": 62, "target": 57, "value": 2 }, { "source": 62, "target": 41, "value": 1 }, { "source": 62, "target": 61, "value": 6 }, { "source": 62, "target": 60, "value": 3 }, { "source": 63, "target": 59, "value": 5 }, { "source": 63, "target": 48, "value": 5 }, { "source": 63, "target": 62, "value": 6 }, { "source": 63, "target": 57, "value": 2 }, { "source": 63, "target": 58, "value": 4 }, { "source": 63, "target": 61, "value": 3 }, { "source": 63, "target": 60, "value": 2 }, { "source": 63, "target": 55, "value": 1 }, { "source": 64, "target": 55, "value": 5 }, { "source": 64, "target": 62, "value": 12 }, { "source": 64, "target": 48, "value": 5 }, { "source": 64, "target": 63, "value": 4 }, { "source": 64, "target": 58, "value": 10 }, { "source": 64, "target": 61, "value": 6 }, { "source": 64, "target": 60, "value": 2 }, { "source": 64, "target": 59, "value": 9 }, { "source": 64, "target": 57, "value": 1 }, { "source": 64, "target": 11, "value": 1 }, { "source": 65, "target": 63, "value": 5 }, { "source": 65, "target": 64, "value": 7 }, { "source": 65, "target": 48, "value": 3 }, { "source": 65, "target": 62, "value": 5 }, { "source": 65, "target": 58, "value": 5 }, { "source": 65, "target": 61, "value": 5 }, { "source": 65, "target": 60, "value": 2 }, { "source": 65, "target": 59, "value": 5 }, { "source": 65, "target": 57, "value": 1 }, { "source": 65, "target": 55, "value": 2 }, { "source": 66, "target": 64, "value": 3 }, { "source": 66, "target": 58, "value": 3 }, { "source": 66, "target": 59, "value": 1 }, { "source": 66, "target": 62, "value": 2 }, { "source": 66, "target": 65, "value": 2 }, { "source": 66, "target": 48, "value": 1 }, { "source": 66, "target": 63, "value": 1 }, { "source": 66, "target": 61, "value": 1 }, { "source": 66, "target": 60, "value": 1 }, { "source": 67, "target": 57, "value": 3 }, { "source": 68, "target": 25, "value": 5 }, { "source": 68, "target": 11, "value": 1 }, { "source": 68, "target": 24, "value": 1 }, { "source": 68, "target": 27, "value": 1 }, { "source": 68, "target": 48, "value": 1 }, { "source": 68, "target": 41, "value": 1 }, { "source": 69, "target": 25, "value": 6 }, { "source": 69, "target": 68, "value": 6 }, { "source": 69, "target": 11, "value": 1 }, { "source": 69, "target": 24, "value": 1 }, { "source": 69, "target": 27, "value": 2 }, { "source": 69, "target": 48, "value": 1 }, { "source": 69, "target": 41, "value": 1 }, { "source": 70, "target": 25, "value": 4 }, { "source": 70, "target": 69, "value": 4 }, { "source": 70, "target": 68, "value": 4 }, { "source": 70, "target": 11, "value": 1 }, { "source": 70, "target": 24, "value": 1 }, { "source": 70, "target": 27, "value": 1 }, { "source": 70, "target": 41, "value": 1 }, { "source": 70, "target": 58, "value": 1 }, { "source": 71, "target": 27, "value": 1 }, { "source": 71, "target": 69, "value": 2 }, { "source": 71, "target": 68, "value": 2 }, { "source": 71, "target": 70, "value": 2 }, { "source": 71, "target": 11, "value": 1 }, { "source": 71, "target": 48, "value": 1 }, { "source": 71, "target": 41, "value": 1 }, { "source": 71, "target": 25, "value": 1 }, { "source": 72, "target": 26, "value": 2 }, { "source": 72, "target": 27, "value": 1 }, { "source": 72, "target": 11, "value": 1 }, { "source": 73, "target": 48, "value": 2 }, { "source": 74, "target": 48, "value": 2 }, { "source": 74, "target": 73, "value": 3 }, { "source": 75, "target": 69, "value": 3 }, { "source": 75, "target": 68, "value": 3 }, { "source": 75, "target": 25, "value": 3 }, { "source": 75, "target": 48, "value": 1 }, { "source": 75, "target": 41, "value": 1 }, { "source": 75, "target": 70, "value": 1 }, { "source": 75, "target": 71, "value": 1 }, { "source": 76, "target": 64, "value": 1 }, { "source": 76, "target": 65, "value": 1 }, { "source": 76, "target": 66, "value": 1 }, { "source": 76, "target": 63, "value": 1 }, { "source": 76, "target": 62, "value": 1 }, { "source": 76, "target": 48, "value": 1 }, { "source": 76, "target": 58, "value": 1 }] }
</script>
You can tamper the dx and dy to move it to the center something like this.
node.append("text")
.attr("dx", function(d) { if(d.group > 9) {return -5.5} else {return -1.5;} })
.attr("dy", ".3em")
.text(function(d) { return d.group });
What this does is if the group is 9 then single digit then return -1.5 else for 2 digit return -5.5.
Working code here
Hope this helps!
It works after changing the position of dx;
node.append("text")
.attr("dx", -2)
.attr("dy", ".35em")
.text(function(d) { return d.group });
Here is the updated fiddle.

Resources