Skip to content

Commit

Permalink
Allow to set max value manually
Browse files Browse the repository at this point in the history
Setting max value manually allows to compare calendar heatmaps
absolutely, rather than be based on the maximum value in the data.

Call function with .max(int)
  • Loading branch information
peterdesmet committed Jan 20, 2017
1 parent fca0e9f commit 57857c4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ A [d3.js](https://d3js.org/) heatmap representing time series data. Inspired by
|:------------- |:-------------|:-----:|:-----:|
| data | Chart data | none | yes |
| selector | DOM selector to attach the chart to | body | no |
| max | Maximum count | max found in data | no |
| colorRange | Minimum and maximum chart gradient colors | ['#D8E6E7', '#218380'] | no |
| tooltipEnabled | Option to render a tooltip | true | no |
| tooltipUnit | Unit to render on the tooltip | 'contributions' | no |
Expand Down
9 changes: 8 additions & 1 deletion src/calendar-heatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function calendarHeatmap() {
var now = moment().endOf('day').toDate();
var yearAgo = moment().startOf('day').subtract(1, 'year').toDate();
var data = [];
var max = null;
var colorRange = ['#D8E6E7', '#218380'];
var tooltipEnabled = true;
var tooltipUnit = 'contribution';
Expand All @@ -27,6 +28,12 @@ function calendarHeatmap() {
return chart;
};

chart.max = function (value) {
if (!arguments.length) { return max; }
max = value;
return chart;
};

chart.selector = function (value) {
if (!arguments.length) { return selector; }
selector = value;
Expand Down Expand Up @@ -70,7 +77,7 @@ function calendarHeatmap() {
var dateRange = d3.time.days(yearAgo, now); // generates an array of date objects within the specified range
var monthRange = d3.time.months(moment(yearAgo).startOf('month').toDate(), now); // it ignores the first month if the 1st date is after the start of the month
var firstDate = moment(dateRange[0]);
var max = d3.max(chart.data(), function (d) { return d.count; }); // max data value
if (max === null) { max = d3.max(chart.data(), function (d) { return d.count; }); } // max data value

// color range
var color = d3.scale.linear()
Expand Down

0 comments on commit 57857c4

Please sign in to comment.