Skip to content

Commit 1b19d25

Browse files
conditional formatting - proportional coloring
1 parent 7f28767 commit 1b19d25

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

source/js/DataController.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ DataController.prototype.resetDimensionProps = function () {
144144

145145
DataController.prototype.resetConditionalFormatting = function () {
146146

147-
var data,
147+
var data, cs, c1, c2, arr, min, max,
148148
cfArr = {/* "[y],[x]|<null>": Array[{style:"", operator: "", ...}] */},
149149
ocfArr;
150150

@@ -157,6 +157,27 @@ DataController.prototype.resetConditionalFormatting = function () {
157157
return;
158158
}
159159

160+
if (cs = this.controller.CONFIG.pivotProperties["colorScale"]) {
161+
if (cs.indexOf("custom:") > -1) {
162+
arr = cs.split(":")[1].split(",");
163+
c2 = { r: parseInt(arr[0]), g: parseInt(arr[1]), b: parseInt(arr[2]) };
164+
arr = cs.split(":")[2].split(",");
165+
c1 = { r: parseInt(arr[0]), g: parseInt(arr[1]), b: parseInt(arr[2]) };
166+
} else {
167+
arr = cs.split("-to-");
168+
c1 = this.controller.pivotView.colorNameToRGB(arr[0]);
169+
c2 = this.controller.pivotView.colorNameToRGB(arr[1]);
170+
}
171+
cfArr["colorScale"] = {
172+
from: c2,
173+
to: c1,
174+
min: min = Math.min.apply(Math, (data.dataArray || [])),
175+
max: max = Math.max.apply(Math, (data.dataArray || [])),
176+
diff: max - min,
177+
invert: (c2.r + c2.b + c2.g) / 3 < 128
178+
};
179+
}
180+
160181
ocfArr = this.controller.CONFIG.pivotProperties["formatRules"] || [];
161182
if (ocfArr.length && typeof this.controller.CONFIG.conditionalFormattingOn === "undefined") {
162183
this.controller.CONFIG.conditionalFormattingOn = true;
@@ -237,6 +258,22 @@ DataController.prototype.TOTAL_FUNCTIONS = {
237258
totalNONE: function () {
238259
return "";
239260
}
261+
//
262+
//wholeMIN: function (array, iStart, iEnd, column, xStart) {
263+
// var min = Infinity, m, x;
264+
// for (x = xStart; x < array[0].length; x++) {
265+
// if (m = this.totalMIN(array, iStart, iEnd, x) < min) min = m;
266+
// }
267+
// return min;
268+
//},
269+
//
270+
//wholeMAX: function (array, iStart, iEnd, column, xStart) {
271+
// var max = Infinity, m, x;
272+
// for (x = xStart; x < array[0].length; x++) {
273+
// if (m = this.totalMAX(array, iStart, iEnd, x) < max) max = m;
274+
// }
275+
// return max;
276+
//}
240277

241278
};
242279

source/js/PivotView.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,27 @@ PivotView.prototype.recalculateSizes = function (container) {
455455

456456
};
457457

458+
/**
459+
* DeepSee-defined colors.
460+
*
461+
* @param {string} name - name of color. F.e. "red".
462+
* @returns {{ r: number, g: number, b: number }}
463+
*/
464+
PivotView.prototype.colorNameToRGB = function (name) {
465+
var c = function (r, g, b) { return { r: r, g: g, b: b } };
466+
switch (name) {
467+
case "red": return c(255, 0, 0);
468+
case "green": return c(0, 255, 0);
469+
case "blue": return c(0, 0, 255);
470+
case "purple": return c(102, 0, 153);
471+
case "salmon": return c(255, 140, 105);
472+
case "white": return c(255, 255, 255);
473+
case "black": return c(0, 0, 0);
474+
case "gray": return c(128, 128, 128);
475+
default: return c(255, 255, 255);
476+
}
477+
};
478+
458479
/**
459480
* Raw data - plain 2-dimensional array of data to render.
460481
*
@@ -477,6 +498,8 @@ PivotView.prototype.renderRawData = function (data) {
477498
rawData = data["rawData"],
478499
info = data["info"],
479500
columnProps = data["columnProps"],
501+
colorScale =
502+
data["conditionalFormatting"] ? data["conditionalFormatting"]["colorScale"] : undefined,
480503
container = this.elements.tableContainer,
481504
pivotTopSection = document.createElement("div"),
482505
pivotBottomSection = document.createElement("div"),
@@ -491,7 +514,7 @@ PivotView.prototype.renderRawData = function (data) {
491514
LHTHead = document.createElement("thead"),
492515
mainTable = document.createElement("table"),
493516
mainTBody = document.createElement("tbody"),
494-
x, y, tr = null, th, td, primaryColumns = [], primaryRows = [];
517+
x, y, tr = null, th, td, primaryColumns = [], primaryRows = [], ratio;
495518

496519
// clean previous content
497520
this.removeMessage();
@@ -610,7 +633,19 @@ PivotView.prototype.renderRawData = function (data) {
610633
td.textContent = rawData[y][x].value || "";
611634
}
612635
}
613-
if (rawData[y][x].style) td.setAttribute("style", rawData[y][x].style);
636+
if (
637+
colorScale
638+
&& !(info.SUMMARY_SHOWN && rawData.length - 1 === y) // exclude totals formatting
639+
) {
640+
ratio = (parseFloat(rawData[y][x].value) - colorScale.min) / colorScale.diff;
641+
td.setAttribute("style", "background:rgb(" +
642+
+ Math.round((colorScale.to.r - colorScale.from.r)*ratio + colorScale.from.r)
643+
+ "," + Math.round((colorScale.to.g - colorScale.from.g)*ratio + colorScale.from.g)
644+
+ "," + Math.round((colorScale.to.b - colorScale.from.b)*ratio + colorScale.from.b)
645+
+ ");" + (colorScale.invert ? "color: white;" : ""));
646+
}
647+
if (rawData[y][x].style)
648+
td.setAttribute("style", (td.getAttribute("style") || "") + rawData[y][x].style);
614649
if (
615650
this.controller.CONFIG.conditionalFormattingOn // totals formatting present
616651
&& !(info.SUMMARY_SHOWN && rawData.length - 1 === y) // exclude totals formatting

0 commit comments

Comments
 (0)