I want to create multiple line charts using chart.js. I have created two drop downs, both the dropdowns have to be selected to see the data on the chart. I could filter out my data using one drop down but I am unable to filter it out by choosing one value from both the drop downs.
Here is what I have tried till now:
<div class="container-fluid">
<div class="row">
<div class="col-4">
<div class="form-group mt-3 mb-3">
<label for="dataset">Select Factor</label>
<select class="form-control" id="dataset">
<option id="opt1" value="1">one</option>
<option id="opt2" value="2">two</option>
<option id="opt3" value="3">three</option>
</select>
</div>
</div>
<div class="col-4"></div>
<div class="col-4">
<div class="form-group mt-3 mb-3">
<label for="dataset">Select Options</label>
<select class="form-control" id="dataset">
<option id="opt1" value="4">Tp</option>
<option id="opt2" value="5">Hy</option>
</select>
</div>
</div>
<div class="col-4"></div>
</div>
<div class="row">
<div class="col"></div>
<div class="col-12">
<canvas id="myChart"></canvas>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type:'line',
data : {
labels: ['Boston','Worcester','SpringField','Cambridge','Lowell','New Bedford'],
datasets : [{
label:'#trips',
data:[
3,
6,
10,
12,
19,
25
],
backgroundColor:'green',
borderWidth:4,
borderColor:'#777',
hoverBorderWidth:3,
hoverBorderColor:'#000'
},{
label:'#rides',
data:[
14,
28,
80,
20,
60,
45
]
}],
},
options:{
title:{
display:true,
text:'Line chart',
fontSize:15
},
legend:{
position:'bottom',
labels:{
fontColor:'red'
}
},
layout:{
padding:{
left:50,
right:0,
top:0,
bottom:0
}
},
tooltips:{
enabled:true
}
}
});
$('#dataset').on('change', function(){
if(this.value=="1"){
myChart.data.datasets[0].data = [3,
6,
10,
12,
19,
25];
myChart.data.datasets[1].data = [0,
0,
0,
0,
0,
0];
myChart.update();
}else if(this.value=="2"){
myChart.data.datasets[0].data = [0,
0,
0,
0,
0,
0];
myChart.data.datasets[1].data = [14,
28,
80,
20,
60,
45];
myChart.update();
}else if(this.value=="3"){
myChart.data.datasets[0].data=[3,
6,
10,
12,
19,
25];
myChart.data.datasets[1].data=[14,
28,
80,
20,
60,
45];
myChart.update();
}
});
I want to select "Tp" and then select example "one" and get the data. Select suppose "Hy" and then "two" and get the data displayed. I am very new to JS. Any help will be appreciated.