3.7 Task 6 Review
3.7.1 Task 6-1 Review
From Day 9
6-1-1:
To specify the height of each bar, I should use
.style()
, rather than.attr()
. InD3.js
,.style()
is to assign CSS styles to a HTML element. In CSS, we won’t usestyle
because everything isstyle()
. In HTML, we do use it. For example,<div style="height: 75px;"></div>"
;To make the height of each bar correspondes to its data value, I should use
function(d){return d + "px"}
rather thanfunction(d){return d }
;
6-1-2: To add some space (2px) between bars, I need to use
margin-right: 2px
in the CSS (div.bar{}
. To learn more about the differences betweenpadding
,border
,margin
, read this amazing tutorial;6-1-3:
First, I need to initilize an empty array using
dataset = []
In Javascript,
Math
is a global object, whereas.push()
is an array method.A
for
loop:for (initialization; test; updata){ }
3.7.2 Task 6-2
From Day 9
6-2-4
When drawing five circles, we used three terms, “svg”, “circles”, and “circle”, which of them should be fixed, and which can be random? “svg” and “circle” should be fixed, but “circles” can be random;
One thing I still don’t understand is that in Task 6-1-1, I have to use
function(d){return d + "px"}
rather thanfunction(d){return d }
. However, in Task 6-2-4, using.attr("r", function(d){return d })
is fine, and in.attr("cy", h/2)
, I don’t need to put""
to encloseh/2
. I guess it’s because of the differences between.style()
and.attr()
. But how different? I don’t know.;When we need the index of the data values, we should use
function(d,i){}
even if we don’t need thed
. However, when we only need thed
, we can simply usefunction(d){}
.
6-2-6: Again, we need to add
px
instyle
3.7.3 Task 6-3
From Day 9
To change the color of bars, we need to use .attr("fill", "teal")
, rather than .style()
.
3.7.4 Task 6-1
From Day Ten
6-1-1. Again, I forgot the difference between
.attr()
and.style()
..style()
assigns CSS styles (properties and value)s to a HTML element, just like the way we assign CSS styles when we are writing HTML. For example,<div style="height: 75px;"></div>
.In comparison,
.attr()
sets an HTML attribute and its values on a HTML element. Note that an element’s class is stored as an HTML atrribute.
6-1-3.
A.push(B)
will put B to the end of A..push()
is an array method.
3.7.5 Task 6-2
From Day Ten
6-2-2.
<svg>
is just like an HTML element.width
,height
are its attributs.rect
,circle
,text
are all elements.x
,y
,cx
,cy
andr
are all attributes. Read this amazing tutorial to really understand what is an element and what are attributs.I still did not understand it. Why is that I need to use
style()
to specify theheight
ofdiv
in Task 6-1-1** but I need to use.attr()
to specify theheight
of asvg
here??** I guess it’s because that fordiv
,height
is a CSS property, but forsvg
,height
is an attribute? Maybe.
6-2-4. I need to use
svg.selectAll('circle')
rather thansvg.select('circle')
. Think about why for a moment;6-2-5. As the amazing tutorial mentioned, here, using either
.attr()
or.style()
is fine. The key difference is that I need to usereturn d / 2 + "px"
forstroke-wideth
when using.style()
.
3.7.6 Concluding words on .style() and .attr():
For
<div>
,height
is a CSS property, so I should usestyle
;For
<svg>
,height
,width
,x
,y
,cx
,cy
,r
are attributes, so I should useattr()
;For
<svg>
,fill
,stroke
, andstroke-width
are also attributes. I can use eitherstyle()
orattr()
, butstyle()
is better because it is not about position and size.Refer to this tutorial and this tutorial.
3.7.7 Task 6-5
From Day Eleven
6-5-1. When adding value labels on top of each bar, I should not use
rect.selectAll("text").data(dataset).enter().append("text")...
. Doing so will add twentytext
element within eachrect
element! This is not I am looking for at all. Rather, I should usesvg.selectAll("text").data(dataset).enter().append("text")
.6-5-2.
.style("font-size", "11px")
.
3.7.9 Task 6-6
From Day Eighteen