D3 scales and colors

2023-10-28 07:58
文章标签 colors d3 scales

本文主要是介绍D3 scales and colors,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

By Jerome Cukier

点击打开链接



Scales: the main idea


scales transform a number in a certain interval( called domain) into a number in another interval( called range)




For instance, let's suppose you know your data is always over 20 and always below 80. you would like to plot it, say, in a bar plot,  which can be only 120 pixels tall.


you could, obviously, do the math:


.attr("height",function(d) {return (d-20)*2;})


but what happen when your data changes? you have to go back to the entrails  of your code and make the change. this is very error prone. so instead, you can use a scale.



var y = d3.scale.linear().domain(20,80).range(0,120);<strong>.attr("height“,y);  //this is equivalent to .attr("height",function(d){return y(d);});</strong>



Fun with Scales


quantitative scales can be of several types:

1. linear (including quantize and quantile) 

2. logarithmic 

3. power(including square root scales)


they have a lot in common


1. Domain and range

for all scales, with the exception of quantize and quantile scales which are a bit different, domain and range work the same

var y = d3.scale.linear().range([20,60]).domain([0,120]);

typically there're two numbers in the domain and range, but it can be more. If this is more, we are talking about a polypoint scale: there are as many segments in the intervals as there are numbers in the domain. When using the scale, if a number is in teh n-th segment of the domain, it's transformed into a number in the n-th segment of the range.




Bounds of domain and ranges need not be numbers, as long as they can be converted to numbers. One useful example are colors. Color names can be used as range.

var ramp = d3.scale.linear().domain([0,100]).range("red","blue");

this will transform any number between 0 and 100 into the corresponding color between red and blue;


Clamping:




var clamp = d3.scale().linear().domain([20,80]).range([0,120]);clamp(100) ;//160
clamp.clamp(true);
clamp(100); // 120

Scales and nice numbers


more often than not, the bounds of the domain and/or those of the ranges wil be calculated. So chances are they won't be round numbers, or numbers a human would like. Scales, however, come with a bunch of method to address that. 


Keep in mind that scales are often used to position marks along an axis.


.nice()

var data=[-2.347, 4, 5.23,-1.234,6.234,7.431]; // or whatevervar y = d3.scale.linear().domain(d3.extent(data)).range([0,120]);y.domain();//[-2.347,7.431]
y.nice();//[-3,-8]


.ticks()



.rangeRound()


this will guarantee that the output of the scales are integers, which is better to position marks on the screen with pixel precision than numbers with decimals.



.invert([value])


it will turns the scale upside down : given range number, returns which number in the domain .

var y=d3.scale.linear().range([0,120]);
y(50); //60
y.invert(60); //50


Power scales and log scales


 y=axk+b, or y=a.log(x)+b.



Also note that if you are using a log scale, you cannot have 0 in the domian


Quantize and quantile 



quantize works with a discrete, rather than continuous range, in other terms 

the range can only take a certain number of values;


var q=d3.scale.quantize().domain([0,10]).range([0,2,8]);q(0); //0
q(3); //0
q(5);//2
q(8);//8
q(10000);//8



quantile on the other hand, matches values in the domain(which is the full dataset) with their respective quantile. The number of quantiles is specified by the range.


var q=d3.scale.quantile().domian([0,1,5,6,2,4,6,2,4,6,7,8]).range([0,100]);
q.quantile(); //[4.5], only one quantile --the median
q(0);//0
q(4); //0
q(4.449);//0
q(4.5); //100
q(1000);//100q.range([0,25,50,75,100]);
q.quantiles(); // [2,4,5.6,6]
q(2);//25
q(4);


Ordinal scales


the big difference is that ordinal scales have a discrete domain. in other words, they can turn a limited number of values into something else, without caring what's between those values.


var x=d3.scale.ordinal().domain(["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]) // 7 items.rangeBands([0,120]);
x("Tuesday"); // 34.285714285714285


there're 3 possibilities for range() 

.rangePoints()

.rangeBands()

.range()


<span style="font-family:Arial;font-size:12px;">var x=d3.scale.ordinal().domain(["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]);
x.rangePoints([0,120]);
x("Saturday"); // 120
x.rangeBands([0,120]);
x("Saturday"); // 102.85714285714286
x("Saturday")+x.rangeBand(); // 120</span>


.range()

we can also use .range methods with several values. We can specify the domain, ornot.

then, if we use such a scale on a value which is not part of the domain(or if the domian is left empty), this value is added to the domain. If there'are n values in the range, while there're n+1 values in the domain, then, the n+1 value of the domain is matched with the first value in the range.


var x=d3.scale.ordial().range(["hello","world"]);x.domain();//[]
x(0);//hello
x(1);//world
x(2);//hello
x.domain();//[0,1,2]



Color palettes


There are 4 built-in color palette in protovis: d3.scale.category10(), d3.scale.category20(), d3.scale.category20b(), and d3.scale.category20c().


A palette like d3.scale.category10() works exactly like an ordinal scale.


var p=d3.scale.category10();
var r=p.range(); // ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", // "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"]
var s=d3.scale.ordinal().range(r); 
p.domain(); // [] - empty
s.domain(); // [] - empty, see above
p(0); // "#1f77b4"
p(1); // "#ff7f0e"
p(2); // "#2ca02c"
p.domain(); // [0,1,2];
s(0); // "#1f77b4"
s(1); // "#ff7f0e"
s(2); // "#2ca02c"
s.domain(); // [0,1,2];


Colors


Compared to protovis, d3.color is simpler. The main reason is that protovis handled color and transparency together with the pv.Color object, whereas in SVG, those two are distinct attributes: you handle the background color of a filled object with fill, its transparency with opacity, the color of the outline with stroke and the transparency of that color with stroke-opacity.

d3 has two color objects: d3_Rgb and d3_Hsl, which describe colors in the two of the most popular color spaces: red/green/blue, and hue/saturation/light.

With d3.color, you can make operations on such objects, like converting colors between various formats, or make colors lighter or darker.

d3.rgb(color), and d3.hsl(color) create such objects.
In this context, color can be (straight from the manual):

  • rgb decimal – “rgb(255,255,255)”
  • hsl decimal – “hsl(120,50%,20%)”
  • rgb hexadecimal – “#ffeeaa”
  • rgb shorthand hexadecimal – “#fea”
  • named – “red”, “white”, “blue”

Once you have that object, you can make it brighter or darker with the appropriate method.
You can use .toString() to get it back in rgb hexadecimal format (or hsl decimal), and .rgb() or .hsl() to convert it to the object in the other color space.

1
2
3
4
5
6
7
8
var c=d3.rgb( "violet" ) // d3_Rgb object
c.toString(); // "#ee82ee"
c.darker().toString(); // "#a65ba6"
c.darker(2).toString(); // "#743f74" - even darker
c.brighter().toString(); // "ffb9ff"
c.brighter(0.1).toString(); // "#f686f6" - only slightly brighter
c.hsl(); // d3_Hsl object
c.hsl().toString() // "hsl(300, 76, 72)"









这篇关于D3 scales and colors的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/291803

相关文章

智能电力监控与管理系统:基于MQTT协议的实时数据传输,结合React和D3.js实现数据可视化,利用TensorFlow进行故障检测与能效优化(代码示例)

一、项目概述 智能电力监控与管理系统旨在通过高效的传感器网络和物联网技术,实现对电力系统的实时监控和智能管理。该项目的目标是提高电力设备的运行效率,降低能耗,并通过数据分析及时发现潜在故障,确保电力系统的安全和稳定。 项目解决的问题和带来的价值 实时监控:通过高精度传感器实时采集电流、温度和湿度等数据,确保及时发现设备异常。 数据分析:利用大数据技术对采集的数据进行深度分析,帮助用户优化

python动画:颜色(color)能接受的[manim_colors]

Manim_colors指的是Manim动画引擎中全局命名空间中包含的一组颜色。这些颜色构成了Manim默认的颜色空间。通过使用manim_colors,动画师和创作者可以轻松地访问和应用各种颜色到他们的动画中,而无需单独定义它们。这个特性简化了动画制作的过程,并确保整个项目中颜色的一致性使用。manim_colors的可用性增强了使用Manim创建的动画的视觉吸引力和清晰度,使其成为动画师、教育

【D3.js in Action 3 精译】关于本书

文章目录 本书读者本书结构与路线图本书代码liveBook 在线论坛 D3.js 项目的传统开发步骤 本书读者 这本书适用于所有渴望在数据可视化工作中获得完全创意自由的人,从定制化的经典图表到创建独特的数据可视化布局,涵盖内容广泛,应有尽有。您可能拥有数据分析的相关背景,也可能是记者、设计师,甚至是数据可视化的发烧友。恭喜您考虑学习 D3.js !您很快就

【D3.js in Action 3 精译】前言

早在 2017 年,我还是一名渴望迈出职业生涯关键一步的前端开发者。虽然我很热衷于网站开发,但总感觉缺了点什么。我一直希望自己的工程专业背景和对教学的热爱能与新的编程技能相结合。就在这时,搭档建议我学学数据可视化。出于某种原因,他确信我会乐于探索这一蓬勃发展的领域。当我在谷歌上检索关键词“数据可视化”时,我偶然发现了纳迪埃·布雷默(Nadieh Bremer)和雪莉·吴(Shirley Wu)的“

【LeetCode】Sort Colors 数组排序

题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors题目:输入一个数组,包含0,1,2分别代表红白蓝三种颜色,要求按照0,1,2的顺序,将同类颜色的连续排列思路:计数排序,是一个遍历两遍的方法:可以先统计每种的数量,之后直接将这一范围内的所有值都赋值为相应的数字即可遍历一遍的话可以在遍历的同时分别与0

d3.js获取流程图不同的节点

在D3.js中,获取流程图中不同的节点通常是通过选择SVG元素并使用数据绑定来实现的。流程图的节点可以通过BPMN、JSON或其他数据格式定义,然后在D3.js中根据这些数据动态生成和选择节点。 以下是一个基本的示例,展示如何使用D3.js选择和操作流程图中的不同节点: 步骤1: 准备数据 首先,你需要有一个包含流程图节点的数据集。这些数据可以是任何格式,但最常见的是JSON或BPMN。这里

poj 3971 Scales (dp)

题目大意: 给你一个重量为w的物品 和 重量分别为 1  2  4  ... 2^(n-1) 的砝码,问 一共有多少种方式使得天平平衡。 解题思路: 让我们求的 就是 w + x = y 并且 x 与 y 在二进制形式下没有相同位置为 1  即  x & y = 0   一开始,我希望用数学公式推出来,但是失败了。后来查看了别人的题解,发现这是一道 动态规划。 我们设 dp[i][j]

【译】Learn D3 入门文档:Further Topics

引子 继 Learn D3: Interaction 最后一篇。 原文:Learn D3: Further Topics 版本:Published Mar 24, 2020 Origin My GitHub 正文 如果你从一开始就坚持这个教程,深呼吸,鼓励一下自己!🙌 你已经涵盖了很多领域,并对典型的可视化有了重要的了解。 但我们还没有到达顶峰!不,这只是大本营。 是时候向

【译】Learn D3 入门文档:Interaction

引子 继 Learn D3: Joins 第八篇,只是英文翻译,可修改代码的部分用静态图片替代了,想要实时交互请阅读原文。 原文:Learn D3: Interaction 版本:Published Mar 24, 2020 Origin My GitHub 正文 几乎总是有太多的信息无法合理地“容纳”在图表中。因此,设计不仅仅是决定如何展示某些东西,而是根据我们认为对想象中的读

【译】Learn D3 入门文档:Joins

引子 继 Learn D3: Animation 第七篇,只是英文翻译,可修改代码的部分用静态图片替代了,想要实时交互请阅读原文。 原文:Learn D3: Joins 版本:Published Mar 24, 2020 Origin My GitHub 正文 如果你熟悉此教程中的 D3 ,你可能会惊讶于 D3 选择器被提及的如此之少。 那是因为你可能不需要它们! D3 选择