make proportions.setData idempotent

This commit is contained in:
Nils Schneider 2015-04-02 22:07:00 +02:00
parent d3bbf6060e
commit c4383aabb9
2 changed files with 24 additions and 16 deletions

5
app.js
View file

@ -18,7 +18,10 @@ require.config({
"tablesort": { "tablesort": {
exports: "Tablesort" exports: "Tablesort"
}, },
"numeral-intl": ["numeral"], "numeral-intl": {
deps: ["numeral"],
exports: "numeral"
},
"tablesort.numeric": ["tablesort"], "tablesort.numeric": ["tablesort"],
"helper": ["numeral-intl"] "helper": ["numeral-intl"]
} }

View file

@ -1,4 +1,6 @@
define(["chroma-js"], function (Chroma) { define(["chroma-js", "virtual-dom", "numeral-intl"],
function (Chroma, V, numeral) {
return function () { return function () {
var self = this var self = this
var fwTable, hwTable, autoTable, gwTable var fwTable, hwTable, autoTable, gwTable
@ -23,30 +25,33 @@ define(["chroma-js"], function (Chroma) {
} }
function fillTable(table, data) { function fillTable(table, data) {
if (!table.last)
table.last = V.h("table")
var max = 0 var max = 0
data.forEach(function (d) { data.forEach(function (d) {
if (d[1] > max) if (d[1] > max)
max = d[1] max = d[1]
}) })
data.forEach(function (d) { var items = data.map(function (d) {
var v = d[1] / max var v = d[1] / max
var row = document.createElement("tr")
var th = document.createElement("th")
var td = document.createElement("td")
var span = document.createElement("span")
th.textContent = d[0]
span.style.width = Math.round(v * 100) + "%"
span.style.backgroundColor = scale(v).hex()
var c1 = Chroma.contrast(scale(v), "white") var c1 = Chroma.contrast(scale(v), "white")
var c2 = Chroma.contrast(scale(v), "black") var c2 = Chroma.contrast(scale(v), "black")
span.style.color = c1 > c2 ? "white" : "black"
span.textContent = d[1] var th = V.h("th", d[0])
td.appendChild(span) var td = V.h("td", V.h("span", {style: {
row.appendChild(th) width: Math.round(v * 100) + "%",
row.appendChild(td) backgroundColor: scale(v).hex(),
table.appendChild(row) color: c1 > c2 ? "white" : "black"
}}, numeral(d[1]).format("0,0")))
return V.h("tr", [th, td])
}) })
var tableNew = V.h("table", items)
table = V.patch(table, V.diff(table.last, tableNew))
table.last = tableNew
} }
self.setData = function (data) { self.setData = function (data) {