您现在的位置:首页 >> 省钱攻略

适用JavaScript构建树形图

发布时间:2025/07/29 12:17    来源:裕安家居装修网

年的 IC 1101 星体。说实话,很难明白它有多大。

激活

var dataSet = [

{name: "Galaxies", children: [

{name: "Elliptical", children: [

{name: "IC 1101", value: 4000000},

{name: "Hercules A", value: 1500000},

{name: "A2261-BCG", value: 1000000},

{name: "ESO 306-17", value: 1000000},

{name: "ESO 444-46", value: 402200},

]},

{name: "Spiral", children: [

{name: "Rubin's Galaxy", value: 832000},

{name: "Comet Galaxy", value: 600000},

{name: "Condor Galaxy", value: 522000},

{name: "Tadpole Galaxy", value: 280000},

{name: "Andromeda Galaxy", value: 220000}

]}

]}

];

4. 编所写一些JS果丛生布字符串

到此需几行JavaScript字符串就可以为果丛生布备有动力。

1.用作anychart.onDocumentReady() 数组,初始化果丛生布的所有JavaScript字符串,确保它在的网站实质上初始化并准备执行。

激活

anychart.onDocumentReady(function () {

// JS果树映射字符串时会所写到这里

});

2.然后,从第3步开始在果丛生布之中替换成我们一切都是要布表分析的布表。

激活

anychart.onDocumentReady(function () {

var dataSet = [

{name: "Galaxies", children: [

{name: "Elliptical", children: [

{name: "IC 1101", value: 4000000},

{name: "Hercules A", value: 1500000},

{name: "A2261-BCG", value: 1000000},

{name: "ESO 306-17", value: 1000000},

{name: "ESO 444-46", value: 402200},

]},

{name: "Spiral", children: [

{name: "Rubin's Galaxy", value: 832000},

{name: "Comet Galaxy", value: 600000},

{name: "Condor Galaxy", value: 522000},

{name: "Tadpole Galaxy", value: 280000},

{name: "Andromeda Galaxy", value: 220000}

]}

]}

];

});

3.替换成表列出字符串将布表叠加到布上。

激活

var chart = anychart.treeMap(dataSet, "as-tree");

4.替换成一个曲名,将示意布抽出之前所定义的 液体之中,并用作 draw 命令显示它。

激活

chart.title("The 10 Largest Galaxies in the Known Universe");

chart.container("container");

chart.draw();

现今JS果丛生布基本上上已经作准备了:

初始化果丛生布时,只时会显示两个布块,“双曲线”和“螺旋形”。然后可以点击它们,落幕其各自的三子星体,这就是所谓的下钻加载。

为什么时会只有两块?因为默认情况下,小得多厚度系数设立为1。这假定一次只能认出其父级的一个行政级别。较低的行政级别是隐藏的。在第一层,将“星体”分为“双曲线”和“螺旋形”,所以只能认出这一层。

显示所有星体布块需用作maxDepth()数组修改小得多厚度系数。

激活

chart.maxDepth(2);

效果如下:

在这张示意布之中,可以认出星体是如何根据一个大来进行分组的,还可以点击顶部的“双曲线”或“螺旋形”曲名来放大其三子星体。

基本上字符串:

激活

JavaScript Treemap Chart

html, body, #container {

width: 100%;

height: 100%;

margin: 0;

padding: 0;

}

anychart.onDocumentReady(function () {

// 成立布表

var dataSet = [

{name: "Galaxies", children: [

{name: "Elliptical", children: [

{name: "IC 1101", value: 4000000},

{name: "Hercules A", value: 1500000},

{name: "A2261-BCG", value: 1000000},

{name: "ESO 306-17", value: 1000000},

{name: "ESO 444-46", value: 402200},

]},

{name: "Spiral", children: [

{name: "Rubin's Galaxy", value: 832000},

{name: "Comet Galaxy", value: 600000},

{name: "Condor Galaxy", value: 522000},

{name: "Tadpole Galaxy", value: 280000},

{name: "Andromeda Galaxy", value: 220000}

]}

]}

];

// 成立果丛生布并设立布表

var chart = anychart.treeMap(dataSet, "as-tree");

// 设立示意布曲名

chart.title("The 10 Largest Galaxies in the Known Universe");

// 设立示意布的液体id

chart.container("container");

// 开始所画示意布

chart.draw();

});

现今,你可以大略地认出10个小得多星体的覆盖面并来进行相比较。一个大示范如何自定义JavaScript果丛生布。

自定义JS果丛生布

A. 偏离黄色

偏离任何示意布的外观和感觉有一种简单方式就是修改黄色。

激活

chart.normal().fill('#B46FC2');

chart.hovered().fill('#44008B', 0.8);

chart.selected().fill('#0A0068', 0.8);

chart.selected().hatchFill("forward-diagonal", '#282147', 2, 20);

替换成了fill()和hashFill()方式来修改果丛生布的黄色。

B. 运用离散色标

在果丛生布之中,除了体积,布块的黄色也有助于突出显示比例。可以充分利用离散色标根据除此以外的布表维空间自动为布块著色。

成立一个离散色标,为其备有两个系数,一个为略高于之内系数,另一个为最高系数,再一启用黄色之内。

激活

var customColorScale = anychart.scales.linearColor();

customColorScale.colors(['#37B8F7', '#ffcc00']);

chart.colorScale(customColorScale);

chart.colorRange().enabled(true);

chart.colorRange().length('90%');

C. 内嵌标签和工具示意

可以用作HTML来内嵌标签。为此,需为标签启用 HTML。然后,你就可以不受限地用作HTML对它们来进行内嵌。

可以把标签内嵌为HTML锕系元素,并对其来进行样式新设计,以增加字体体积和偏离黄色。

激活

chart.labels().useHtml(true);

chart.labels().format(

"{%name}
{%value}"

);

正如你在上面的字符串片段之中认出的,还用作了{%name}和{%value}标记,用来修改果丛生布标签和工具示意的文本。这样,在成立布表分析时将为每个星体输出名称和比例系数。

此外,用作format()方式来定制工具示意的文本。一个内容丰富的工具示意有助于好处地明白布表。

激活

chart.tooltip().format(

"Scale: {%value} light-years"

);

D. 按升序顺序排列布块

默认情况下,果丛生布布块按上标顺序排列。可以认出星体在在高到低顺序排列的,覆盖面小得多的IC 1101星体是左起第一个。

如果需升序顺序排列,那么替换成:

激活

chart.sort("asc");

一个大是一个基本上样例:

激活

JavaScript Treemap Chart

html, body, #container {

width: 100%;

height: 100%;

margin: 0;

padding: 0;

}

anychart.onDocumentReady(function () {

// create the data

var dataSet = [

{name: "Galaxies", children: [

{name: "Elliptical", children: [

{name: "IC 1101", value: 4000000},

{name: "Hercules A", value: 1500000},

{name: "A2261-BCG", value: 1000000},

{name: "ESO 306-17", value: 1000000},

{name: "ESO 444-46", value: 402200},

]},

{name: "Spiral", children: [

{name: "Rubin's Galaxy", value: 832000},

{name: "Comet Galaxy", value: 600000},

{name: "Condor Galaxy", value: 522000},

{name: "Tadpole Galaxy", value: 280000},

{name: "Andromeda Galaxy", value: 220000}

]}

]}

];

// create the treemap chart and set the data

var chart = anychart.treeMap(dataSet, "as-tree");

// set the chart title

chart.title("The 10 Largest Galaxies in the Known Universe");

// set a custom color scale

var customColorScale = anychart.scales.linearColor();

customColorScale.colors(['#37B8F7', '#ffcc00']);

chart.colorScale(customColorScale);

chart.colorRange().enabled(true);

chart.colorRange().length('90%');

// format the labels

chart.labels().useHtml(true);

chart.labels().format(

"{%name}
{%value}"

);

// format the tooltips

chart.tooltip().format(

"Scale: {%value} light years"

);

// sort in ascending order

chart.sort("asc");

// set the container id for the chart

chart.container("container");

// initiate drawing the chart

chart.draw();

});

此番现今已经学时会了轻而易举地成立抢眼的交互式JavaScript果丛生布!请注意果丛生布文档,以便了解它还可以来作些什么,或者用作各有不同的示意布戈。

编者简述

翟珂,51CTO社区编辑,目前所在杭州投身于该软件研发工作,来作过电商、征信等多方面的子系统,渴求分享学问的过程,补充自己的生活。

原文曲名:How to Build a Treemap Using JavaScript,作者:Awan Shrestha

铜川白癜风哪家医院最好
天津看牛皮癣到哪家好
厦门白癜风去哪看
武汉妇科医院哪家看的好
四川早泄阳痿治疗哪家好
慢性支气管炎咳嗽吃什么药
祛痰药
秋冬小儿化痰止咳的药哪个效果好
前列腺炎
产后塑形

上一篇: 狂胜73分!NBA史上最大分差纪录,全部都是联盟都被骗了:低估了他们!

下一篇: 百度CTO王海峰:飞桨产业级深度学习应用软件大幅降低应用门槛

友情链接