Skip to content

Commit dab974c

Browse files
committed
Cleanups.
1 parent 3118cd3 commit dab974c

File tree

4 files changed

+55
-44
lines changed

4 files changed

+55
-44
lines changed

d3-table-cards.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ h1{
3636
width: 194px;
3737
line-height: 18px;
3838
}
39-
39+
.header.sortable > div{
40+
cursor: pointer;
41+
}
4042
.item {
4143
position: absolute;
4244
border: solid 1px silver;

d3-table-cards.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// https://github.com/evoluteur/d3-table-cards
22
// (c) 2017 Olivier Giulieri
33

4-
var animTime = 750;
5-
var curStyle = 'table'; // "table" or "card"
6-
var cardsPerRow = 3;
7-
84
var layoutInfo = {
95
table: {
106
// ---- row position & size
@@ -25,7 +21,7 @@ var layoutInfo = {
2521
c2Top: 5,
2622
c2Left: 200,
2723
},
28-
card: {
24+
cards: {
2925
// ---- card position & size
3026
top: function(d){return Math.floor(d.idx/cardsPerRow)*90+'px'},
3127
left: function(d){return (d.idx%cardsPerRow)*200+'px'},
@@ -46,6 +42,13 @@ var layoutInfo = {
4642
}
4743
};
4844

45+
var selector = '.holder';
46+
var curStyle = 'cards'; // "table" or "cards"
47+
var animTime = 650;
48+
49+
var holder;
50+
var cardsPerRow = 3;
51+
4952
function getLayoutInfo(style){
5053
var width = window.innerWidth -20;
5154
cardsPerRow = Math.floor(width/200);
@@ -54,23 +57,26 @@ function getLayoutInfo(style){
5457

5558
function render(){
5659
var l = getLayoutInfo(curStyle);
57-
var sel = d3.select('.holder').selectAll('.item')
60+
holder=d3.select(selector);
61+
var sel = holder.selectAll('.item')
5862
.data(data)
5963
.enter()
6064
.append('div').attr('class', function(d){return 'item chakra'+d.chakra});
6165

62-
sel.insert('div').attr('class', 'c1').html(function(d){return d.name})
63-
.style('top', l.c1Top);
64-
sel.insert('div').attr('class', 'c2').html(function(d){return d.spirit})
65-
.style('top', l.c2Top);
66+
sel.insert('div').attr('class', 'c1').style('top', l.c1Top)
67+
.html(function(d){return d.name});
68+
sel.insert('div').attr('class', 'c2').style('top', l.c2Top)
69+
.html(function(d){return d.spirit});
6670

67-
layout(sel);
71+
data.forEach(function(d, idx){
72+
d.idx=idx;
73+
});
74+
layout(true, false);
6875
}
6976

7077
function redraw(style){
7178
curStyle = style || curStyle;
72-
layout(d3.select('.holder').selectAll('.item')
73-
.transition().duration(animTime));
79+
layout();
7480
}
7581
function sort(key){
7682
var l = getLayoutInfo(curStyle);
@@ -82,36 +88,38 @@ function sort(key){
8288
data.forEach(function(d, idx){
8389
d.idx=idx;
8490
});
85-
layout(d3.select('.holder').selectAll('.item')
86-
.transition().duration(animTime));
91+
layout(false, true);
8792
}
8893

89-
function layout(sel){
94+
function layout(skipAnim, skipChildren){
9095
var l = getLayoutInfo(curStyle),
91-
t = d3.transition().duration(animTime);
92-
93-
sel.style('left', l.left)
96+
t = d3.transition().duration(skipAnim ? 0 : animTime);
97+
98+
holder.selectAll('.item').transition(t)
99+
.style('left', l.left)
94100
.style('top', l.top)
95101
.style('height', l.height)
96102
.style('width', l.width)
97103
.style('border-radius', l.radius);
98104

99-
d3.selectAll('.c1').transition(t)
100-
.style('top', l.c1Top)
101-
.style('left', l.c1Left)
102-
.style('font-size', l.c1FontSize);
103-
d3.selectAll('.c2').transition(t)
104-
.style('top', l.c2Top)
105-
.style('left', l.c2Left);
105+
if(!skipChildren){
106+
holder.selectAll('.c1').transition(t)
107+
.style('top', l.c1Top)
108+
.style('left', l.c1Left)
109+
.style('font-size', l.c1FontSize);
110+
holder.selectAll('.c2').transition(t)
111+
.style('top', l.c2Top)
112+
.style('left', l.c2Left);
106113

107-
d3.select('.header').transition(t)
108-
.style('opacity', l.headerOpacity)
109-
.style('left', l.headerLeft);
114+
holder.select('.header').transition(t)
115+
.style('opacity', l.headerOpacity)
116+
.style('left', l.headerLeft);
110117

111-
var totalHeight = 20+(curStyle==='card' ?
112-
Math.ceil(data.length/cardsPerRow)*90
113-
: 40+data.length*29);
118+
var totalHeight = 20+(curStyle==='cards' ?
119+
Math.ceil(data.length/cardsPerRow)*90
120+
: 40+data.length*29);
114121

115-
d3.select('.holder').transition(t)
116-
.style('height', totalHeight);
122+
d3.select('.holder').transition(t)
123+
.style('height', totalHeight);
124+
}
117125
}

data.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,3 @@ var data=[
195195
"spirit": "Recovery"
196196
}
197197
];
198-
data.forEach(function(d, idx){
199-
d.idx=idx;
200-
});

index.html

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ <h1>Gemstones Properties</h1>
1616
<div id="opts">
1717
<div>View:
1818
<a href="javascript:redraw('table')">Table</a> /
19-
<a href="javascript:redraw('card')">Cards</a>
19+
<a href="javascript:redraw('cards')">Cards</a>
2020
</div>
2121
<div>Order:
2222
<a href="javascript:sort('name')">Name</a> /
@@ -25,9 +25,9 @@ <h1>Gemstones Properties</h1>
2525
</div>
2626

2727
<div class="holder">
28-
<div class="header">
29-
<div>Gemstone</div>
30-
<div>Spiritual properties</div>
28+
<div class="header sortable">
29+
<div onclick="sort('name')">Gemstone</div>
30+
<div onclick="sort('chakra')">Spiritual properties</div>
3131
</div>
3232
</div>
3333

@@ -37,8 +37,12 @@ <h1>Gemstones Properties</h1>
3737
}
3838
</script>
3939

40-
<p>&#169; 2017 <a href="https://evoluteur.github.io/">Olivier Giulieri</a>.
41-
Code available at <a href="https://github.com/evoluteur/d3-table-cards">GitHub</a> under the MIT license.</p>
40+
<p>Data from <a href="https://smile.amazon.com/dp/1844090671" target="crystals">Healing Crystals</a> by
41+
Michael Gienger.</p>
42+
43+
<p>Code available at <a href="https://github.com/evoluteur/d3-table-cards">GitHub</a> with MIT license.</p>
44+
45+
<p>&#169; 2017 <a href="https://evoluteur.github.io/">Olivier Giulieri</a>.</p>
4246

4347
</body>
4448
</html>

0 commit comments

Comments
 (0)