Current location - Trademark Inquiry Complete Network - Futures platform - How to make a visual table with d3.js
How to make a visual table with d3.js
D3 is one of the most popular JavaScript visual chart libraries. D3 is very rich in chart types and supports SVG format, so it is widely used. There are also many chart plug-ins developed based on D3, such as MetricsGraphics.js, and the data chart built based on D3 is very powerful.

Characteristics of D3

Allows arbitrary data to be bound to DOM and data-driven transformations to be applied to documents.

You can not only create beautiful HTML tables, but also draw data charts such as line charts, column charts and pie charts.

Support SVG, rendering on web pages without pressure.

Back to the top

How to use D3

For the specific usage of D3, please refer to this article in the API of D3 graphic library. This paper mainly introduces the realization effect and code of some classic charts.

Index.html code:

& lt! DOCTYPE? html & gt

& ltmeta? charset="utf-8 " >

& ltstyle & gt

svg? {

Font:? 10px? Sans serif fonts;

}

Y axis? Path? {

Show:? None;

}

Y axis? Line? {

Stroke:? # fff

Stroke opacity:? .2;

Shape rendering:? Curled edge;

}

Y axis? . Zero? Line? {

Stroke:? #000;

Stroke opacity:? 1;

}

. Title? {

Font:? 300? 78px? Helvetica? Neue

Fill:? #666;

}

. Year of birth,

. Age? {

Text anchor:? Middle;

}

. Year of birth? {

Fill:? # fff

}

rect? {

Fill-Opacity:? .6;

Fill:? # e377c2

}

Rect: First child? {

Fill:? # 1f77b 4;

}

& lt/style & gt;

& ltbody & gt

& lt script? src = " http://d3js . org/D3 . v3 . min . js " & gt; & lt/script & gt;

& lt script & gt

var? Margin? =? {top:? 20,? Right? 40,? Bottom:? 30,? Left:? 20},

Width? =? 960? -? Margin. Left? -? Edge. Right,

Height? =? 500? -? margin.top? -? Margins. Bottom,

Bar width? =? Math.floor (width? /? 19)? -? 1;

var? x? =? d3.scale.linear()

. Range ([barWidth? /? 2,? Width? -? Bar width? /? 2]);

var? y? =? d3.scale.linear()

. Range ([height, 0]);

var? Alexis? =? d3.svg.axis()

. Scale (y)

. East ("right")

. TickSize(- width)

. TickFormat (function (d)? {? Return? Math.round(d? /? 1e6)? +? ”M”; ? });

//? Ann? SVG? Elements? With what? Answer? Lower right? Origin.

var? svg? =? d3.select("body ")。 Append ("svg")

. Attr("width ",width? +? Margin. Left? +? Right margin)

. Attr ("height", height? +? margin.top? +? Margin. Bottom)

. Additional ("g")

. Attr("transform ","translation (? +? Margin. Left? +? ","? +? margin.top? +? ")");

//? Answer? Sliding? Container? Where to? Keep? That? A bar? By who? Year of birth.

var? Year of birth? =? svg.append("g ")

. Attr("class ","year of birth ");

//? Answer? Label? For what? That? Currently? Year.

var? Title? =? svg.append("text ")

. Attr("class ","title ")

. attr("dy ",. 7 1em ")

. Text (2000);

D3.csv("population.csv ",function (error,? Data)? {

//? Convert? Strings? Where to? Numbers.

Data.forEach (function (d)? {

D. people? =? +D. People;

A.D. year? =? +d . year;

D. age? =? +D. Age;

});

//? Calculation? That? Degree? Yes? That? Data? Settings? Are you online? Age? And then what? A few years.

var? Age 1? =? D3.max (data,? Function (d)? {? Return? D. age; ? }),

year0? =? D3.min (data,? Function (d)? {? Return? D. year; ? }),

year 1? =? D3.max (data,? Function (d)? {? Return? D. year; ? }),

Year? =? year 1;

//? Update? That? Scale? Domain.

x.domain([year 1? -? Age 1,? year 1]);

Y.domain([0, d3.max (data,? Function (d)? {? Return? D. people; ? })]);

//? Production? Answer? Map? From where? Year? And then what? Year of birth? Where to? [Male,? Female].

Data? =? d3.nest()

. Key (function (d)? {? Return? D. year; ? })

. Key (function (d)? {? Return? A.D. year? -? D. age; ? })

. Rollup (function (v)? {? Return? V.map (function (d)? {? Return? D. people; ? }); ? })

. Maps (data);

//? Supplement? Ann? Axis? Where to? Show? That? Population? Values.

svg.append("g ")

. attr("class ",“y? Axis ")

. Attr("transform ","translation (? +? Width? +? ",0)")

. Call (yAxis)

. Select all ("g")

. Filter (function (value)? {? Return? ! Value; ? })

. Classification ("zero", true);

//? Supplement? Labeling? rects? For what? Every one? Year of birth? (So? That? No? Enter? Or? Quit Is it? Required).

var? Year of birth? =? birthyears.selectAll("。 Year of birth ")

. Data (d3.range(year0? -? Age 1,? year 1? +? 1,? 5))

. Enter (). Additional ("g")

. Attr("class ","year of birth ")

. Attr("transform ",function (year of birth)? {? Return? "Translation (? +? X (year of birth)? +? ",0)"; ? });

birthyear.selectAll("rect ")

. Data (function (year of birth)? {? Return? Data [year] [year of birth]? ||? [0,? 0]; ? })

. Enter (). Add ("rect")

. Attr("x ",-Bar width? /? 2)

. Attr("width ",bar width)

. attr("y ",y)

. Attr ("height", function (value)? {? Return? Height? -? Y (value); ? });

//? Supplement? Label? Where to? Show? Year of birth.

birthyear.append("text ")

. Attr("y ",height? -? 4)

. Text (function (year of birth)? {? Return? Year of birth; ? });

//? Supplement? Label? Where to? Show? Age? (separate; ? Isn't it? Animation).

svg.selectAll("。 Age ")

. Data (d3.range(0, age 1? +? 1,? 5))

. Enter (). Append (text)

. Attr("class ","age ")

. Attr("x ",function (age)? {? Return? X (year? -? Age); ? })

. Attr("y ",height? +? 4)

. attr("dy ",. 7 1em ")

. Text (function (age)? {? Return? Age; ? });

//? Is it allowed? That? Arrow? Keys? Where to? Change? That? Display? Year.

window . focus();

D3. Select (window). On("keydown ",function ()? {

Switch? (d3.event.keyCode)? {

Case? 37:? Year? =? Math.max(year0, year? -? 10); ? Break;

Case? 39:? Year? =? Math.min(year 1,? Year? +? 10); ? Break;

}

update();

});

Function? update()? {

What if? (! (year? Are you online? Data))? Return;

Title.text (year);

birthyears.transition()

. Duration (750)

. Attr("transform ","translation (? +? (x(year 1)? -? X (year))? +? ",0)");

birthyear.selectAll("rect ")

. Data (function (year of birth)? {? Return? Data [year] [year of birth]? ||? [0,? 0]; ? })

. Transition ()

. Duration (750)

. attr("y ",y)

. Attr ("height", function (value)? {? Return? Height? -? Y (value); ? });

}

});