Built with blockbuilder.org
Last active
February 22, 2018 16:32
-
-
Save AlainRo/66e8f2df6e5253c42911e1ff0504a627 to your computer and use it in GitHub Desktop.
d3js Exercice 1 - number formats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<!-- | |
L'exemple d3js probablement le moins intimidant du monde. Encore que ? | |
d3 calcule une fonction de format de nombre à partir d'un descripteur. | |
La fonction obtenue est appelée avec les données numériques. | |
La documentation se trouve: | |
https://github.com/d3/d3-format | |
--> | |
<head> | |
<meta charset="utf-8"> | |
<!-- on ne charge que le module d3-format --> | |
<script src="https://d3js.org/d3-format.v1.js"></script> | |
</head> | |
<body> | |
<pre> | |
<script> | |
// Le problème de l'affichage des nombres en javascript | |
for (var i = 0; i < 10; i++) { | |
document.writeln(0.1 * i); | |
} | |
</script> | |
<hr> | |
<script> | |
document.writeln(); | |
let x = 0.594929759999999; // <-- ICI POUR FAIRE VARIER LA VALEUR | |
document.writeln('LE BAC A SABLE DES FORMATS DE NOMBRE\n'); | |
const f = d3.format('.1f'); | |
document.writeln(x + ' -> ' + f(x)); | |
document.writeln((1000 * x) + ' -> ' + f(1000*x)); | |
const s = d3.format('.2s'); | |
document.writeln(x + ' -> ' + s(x)); | |
document.writeln((x/1000) + ' -> ' + s(x/1000)); | |
// Essayer avec e, %, r, d, s | |
// Lire la documentation https://github.com/d3/d3-format | |
// et on peut passer en français sans douleur */ | |
const locale = { | |
"decimal": ",", | |
"thousands": ".", | |
"grouping": [3], | |
"currency": ["", "\u00a0€"], | |
"percent": "\u202f%" | |
}; | |
d3.formatDefaultLocale(locale); | |
const r = d3.format('.2s'); | |
document.writeln(x + ' -> ' + r(x)); | |
/* | |
// Ca marche bien aussi avec node, naviguer vers | |
//https://npm.runkit.com/d3-format et coller le code qui suit | |
var d3 = require("d3-format") | |
const f = d3.format('0.3'); | |
console.log(f(2.56678)); | |
*/ | |
</script> | |
</pre> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment