Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom legend for Heatmap chart. #53

Merged
merged 3 commits into from Nov 12, 2017
Merged

Add custom legend for Heatmap chart. #53

merged 3 commits into from Nov 12, 2017

Conversation

IDDT
Copy link
Contributor

@IDDT IDDT commented Nov 7, 2017

Explanation About What Code Achieves:

Allows to pass an optional parameter "legend" to Heatmap chart with own starting values and colors.
Remove logic to use 2nd color if the value is bigger than the starting number for the 1st color.
Therefore, use "legend" parameter in docs example as it supposed to highlight 1st commit, even though the distribution calculates starting number as 1.25 (which does not highlight 1st commit).

Screenshots/GIFs:

Example in docs.

Steps To Test:

Pass your own legend.
Look at the example in the docs with and without passing "legend" parameter.

TODOs:

None

@achillesrasquinha
Copy link
Contributor

Screenshots!

@IDDT
Copy link
Contributor Author

IDDT commented Nov 8, 2017

Passing random data from 0 to 100 inclusive.

new Chart({
	parent: "#chart-heatmap",
	data: heatmap_data,
	type: 'heatmap',
	legend: [
        {color: 'hsl(216, 14%, 93%)', start: 0}, // color can also be hex or html name.
        {color: 'hsl(0, 62%, 72%)', start: 20}, // start is inclusive starting value for the color to trigger.
        {color: 'hsl(0, 45%, 61%)', start: 35},
        {color: 'hsl(0, 63%, 37%)', start: 50},
        {color: 'hsl(21, 100%, 50%)', start: 100}
	],
	height: 115,
	discrete_domains: 1  // default 0
});

1

If the legend is not passed, default (green) colors are used and starting values calculated with following function (not changed):

get_distribution(data={}, mapper_array) {
		let data_values = Object.keys(data).map(key => data[key]);
		let data_max_value = Math.max(...data_values);

		let distribution_step = 1 / (mapper_array.length - 1);
		let distribution = [];

		mapper_array.map((color, i) => {
			let checkpoint = data_max_value * (distribution_step * i);
			distribution.push(checkpoint);
		});

		return distribution;
	}

with the same distribution (different random data) it calculates like [0, 25, 50, 75, 100]

new Chart({
	parent: "#chart-heatmap",
	data: heatmap_data,
	type: 'heatmap',
	height: 115,
	discrete_domains: 1  // default 0
});

2

Hope this is useful.

@pratu16x7
Copy link
Contributor

pratu16x7 commented Nov 8, 2017

@IDDT Thanks for contributing :)

Custom legend colors are great. However, having to provide manual start values for each defeats the purpose of the heatmap, as data passed is dynamic, and the breakpoints are calculated via get_distribution().

We recommend keeping the legend colors an array, instead of an object. Any distribution fix needs to be done in get_distribution() itself.

@pratu16x7
Copy link
Contributor

As an aside, we'd like to include this feature quickly, as it involves simply exposing an existing property legend_colors.
(It would make it easy for users to make something like, say, Github's Halloween theme for example :)

screen shot 2017-10-31 at 2 12 02 pm

@IDDT
Copy link
Contributor Author

IDDT commented Nov 8, 2017

@pratu16x7 ,
Valid feedback,
I'd still encourage you to keep custom scale as the optional parameter. There are different scenarios where the scale doesn't have to be calculated linearly. Besides GitHub example in the docs needs it since it used to assign 2nd color for the 1st commit, even though the distribution was calculated at 1.25.

@pratu16x7
Copy link
Contributor

"There are different scenarios where the scale doesn't have to be calculated linearly."

Example use case? Nevertheless, in that case, the only thing known about the scale before getting data would be the scale mappings, not the actual values. So it would make sense to pass the mappings (perhaps as percentages of the min-max (range)), rather than values :)

@IDDT
Copy link
Contributor Author

IDDT commented Nov 9, 2017

For example using logistic function to put more highlight on minimal changes while keeping drastic changes less prominent.

Another use case would be centering around 0 or around some business target, where values might be generally negative (or positive) for almost all of the scale.

One more case I have in mind is visualizing body vitals, where normal and critical values are predetermined.

Let me know what you think.

@pratu16x7
Copy link
Contributor

pratu16x7 commented Nov 12, 2017

Added a few commits, be sure to check out the docs demonstrating your idea :)

Let's keep the distribution mapping as a level 2 feature (maybe a new GitHub issue? ;) ). We'll focus on covering the standard uses first.

@pratu16x7 pratu16x7 merged commit ebfd6bd into frappe:master Nov 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants