I'm getting trouble in making what I want morris.js
charts to do.
My goal is to be able to toggle specific lines based on input[type=checkbox]
value.
So far here's what I have done:
JS code
var morris = Morris.Line({
element: 'line-example',
data: [
{ y: '2006', a: 100, b: 90 },
{ y: '2007', a: 75, b: 65 },
{ y: '2008', a: 50, b: 40 },
{ y: '2009', a: 75, b: 65 },
{ y: '2010', a: 50, b: 40 },
{ y: '2011', a: 75, b: 65 },
{ y: '2012', a: 100, b: 90 }
],
xkey: 'y',
ykeys: ['a', 'b'],
labels: ['Series A', 'Series B']
});
jQuery(function($) {
$('#activate').on('change', function() {
var isChecked = $(this).is(':checked');
console.log(isChecked);
if(!isChecked) {
morris = Morris.Line({
element: 'line-example',
ykeys: ['a']
});
}
});
});
HTML code
<body>
<div id="line-example"></div>
<br/>
<input type="checkbox" id="activate" checked="checked"/> Activate
</body>
The problem is that the chart duplicates itself with both lines showing up.
Any idea where to investigate to? (I'm not asking for someone to make up the code for me, I just need some tips).
For those who might be interested in the solution (works for toggling one line):
JS code
Working Fiddle: http://jsfiddle.net/4ztbu8oo/
I've edited your code a little bit, and I got a 3rd line in there that responds to being checked, but there's a bug that brings back the third line when the second one is unchecked.
Here's your fiddle edited a bit. And thank you for having this question!!
I have created a wrapper for morrisjs which allow you to toggle data.
https://github.com/vadimmanaev/morrisjs-toggle