Skip to content

Instantly share code, notes, and snippets.

@clesauln
Created October 10, 2017 18:06
Show Gist options
  • Save clesauln/1ab32e5d7009fc234ba251e139640f55 to your computer and use it in GitHub Desktop.
Save clesauln/1ab32e5d7009fc234ba251e139640f55 to your computer and use it in GitHub Desktop.
d3js Exercice 1 - number formats
license: mit
<!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>
for (var i = 0; i < 10; i++) {
document.writeln(0.1 * i);
}
document.writeln();
let x = 2.9971832; // <-- 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
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