diff --git a/docs/assets/js/index.js b/docs/assets/js/index.js index 3a7bb21..3a33b6b 100755 --- a/docs/assets/js/index.js +++ b/docs/assets/js/index.js @@ -368,6 +368,7 @@ new Chart({ parent: "#chart-heatmap", data: heatmap_data, type: 'heatmap', + legend_scale: [0, 1, 2, 4, 5], height: 115, discrete_domains: 1 // default 0 }); @@ -388,6 +389,7 @@ Array.prototype.slice.call( parent: "#chart-heatmap", data: heatmap_data, type: 'heatmap', + legend_scale: [0, 1, 2, 4, 5], height: 115, discrete_domains: discrete_domains }); diff --git a/docs/index.html b/docs/index.html index ede25fd..adc0f56 100644 --- a/docs/index.html +++ b/docs/index.html @@ -238,6 +238,12 @@
  let heatmap = new Chart({
     parent: "#heatmap",
     data: heatmap_data,  // object with date/timestamp-value pairs
+    legend_colors: [     // optional
+      '#ebedf0', '#c6e48b', '#7bc96f', '#239a3b', '#196127'
+    ],
+    legend_scale: [      // optional, calculated linearly if not provided
+      0, 1, 2, 4, 5      // starting values for each color
+    ],
     type: 'heatmap',
     height: 115,
     discrete_domains: 1  // default 0
diff --git a/src/scripts/charts/Heatmap.js b/src/scripts/charts/Heatmap.js
index 6e05efc..18831cb 100644
--- a/src/scripts/charts/Heatmap.js
+++ b/src/scripts/charts/Heatmap.js
@@ -9,7 +9,9 @@ export default class Heatmap extends BaseChart {
 		subdomain = '',
 		data = {},
 		discrete_domains = 0,
-		count_label = ''
+		count_label = '',
+		legend_colors = null,
+		legend_scale = []
 	}) {
 		super(arguments[0]);
 
@@ -22,9 +24,10 @@ export default class Heatmap extends BaseChart {
 		this.count_label = count_label;
 
 		let today = new Date();
-		this.start = start || add_days(today, 365);
+		this.start = start || this.add_days(today, 365);
 
-		this.legend_colors = ['#ebedf0', '#c6e48b', '#7bc96f', '#239a3b', '#196127'];
+		this.legend_colors = legend_colors || ['#ebedf0', '#c6e48b', '#7bc96f', '#239a3b', '#196127'];
+		this.legend_scale = legend_scale;
 
 		this.translate_x = 0;
 		this.setup();
@@ -71,7 +74,13 @@ export default class Heatmap extends BaseChart {
 	setup_values() {
 		this.domain_label_group.textContent = '';
 		this.data_groups.textContent = '';
-		this.distribution = this.get_distribution(this.data, this.legend_colors);
+
+		if (this.legend_scale.length !== this.legend_colors.length) {
+			this.distribution = this.get_distribution(this.data, this.legend_colors);
+		} else {
+			this.distribution = this.legend_scale;
+		}
+
 		this.month_names = ["January", "February", "March", "April", "May", "June",
 			"July", "August", "September", "October", "November", "December"
 		];
@@ -264,11 +273,6 @@ export default class Heatmap extends BaseChart {
 	}
 
 	get_max_checkpoint(value, distribution) {
-		return distribution.filter((d, i) => {
-			if(i === 1) {
-				return distribution[0] < value;
-			}
-			return d <= value;
-		}).length - 1;
+		return distribution.filter((d) => d <= value).length - 1;
 	}
 }