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 and toggle legend item to show or hide data series #775

Open
mohd7469 opened this issue Nov 8, 2018 · 3 comments
Open

add and toggle legend item to show or hide data series #775

mohd7469 opened this issue Nov 8, 2018 · 3 comments

Comments

@mohd7469
Copy link

mohd7469 commented Nov 8, 2018

could somebody tell how to hide/unhide data series on legend items click in Morris Line graph ?

@pierresh
Copy link

pierresh commented Nov 8, 2018

Hello,

you could set an array variable for the ykeys populated with the default ykeys.

Then when you click on legend items, it add/remove the associated ykey (i.e. in data attribute), then you update the ykeys options of Morris, then call the method .redraw()

@mohd7469
Copy link
Author

mohd7469 commented Nov 8, 2018

@pierresh thanks for getting replied, can you show a basic example please, because i think i am doing the same

image

pierresh pushed a commit to pierresh/morris.js that referenced this issue Nov 10, 2018
@pierresh
Copy link

Hello,

I just updated my fork to make this easier. A class per index is now added to line and circles. Then just just need to hide/display them when clicking on the legend.

Here below is working example:
Javascript

<script>
	var tmp_array = ['nb', 'a'];
	Morris.Line({
		element: 'chart_line_1',
		data: [
			{year: '1958', nb: 1, a: 2},
			{year: '1962', nb: 2, a: 2},
			{year: '1970', nb: 3, a: 2},
			{year: '1994', nb: 4, a: 2},
			{year: '2002', nb: 5, a: 2},
		],
		xkey: 'year',
		ykeys: tmp_array,
		dataLabels: false,
		labels: ['Editions Wins', 'Line 2']
	}).options.lineColors.forEach(function(color, a){ 
	if (tmp_array[a] != undefined) {
		$('#chart_line_1').parent('div')
							.find('div.legend')
							.append($('<span data-index="'+a+'">'+tmp_array[a]+'</span>')
							.css('color', color));
		}
	});

	$('div.legend').on('click', 'span', function(){
		var index = $(this).data('index');
		if ($('#chart_line_1 path.line_'+index).hasClass('hidden')) {
			$('#chart_line_1 path.line_'+index).removeClass('hidden');
			$('#chart_line_1 circle.circle_line_'+index).removeClass('hidden');
		} else {
			$('#chart_line_1 path.line_'+index).addClass('hidden');
			$('#chart_line_1 circle.circle_line_'+index).addClass('hidden');
		}
	});
</script>

HTML

<div>
	<div id='chart_line_1' class='chart_morris'></div>
	<div class='legend'></div>
</div>

CSS

<style>
	.hidden { display:none!important; }
</style>

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

No branches or pull requests

2 participants