I am currently in the process of developing a dashboard that will feature buttons at the top for accessing monthly, weekly, and real-time data.
<div class="zoom_controls">
<a class="profile" id="monthly_data" href="#" data-chart="line" data-range="6m">Monthly</a>
<a class="profile" id="weekly_data" href="#" data-chart="line" data-range="3m">Weekly</a>
<a class="profile" id="real_time" href="#" data-chart="line" data-range="1m">Real Time</a>
</div>
<div class="main" id="chart" style="width:700px; height:300px;"></div>
This snippet of JavaScript is responsible for calling a PHP file to retrieve data and display it using Highcharts:
function cpu_current() {
//current_cpu_data.php retrieves the data from a flat file
$.getJSON('current_cpu_data.php', function(data) {
var chart = new Highcharts.StockChart({
chart: {
borderColor: '#98AFC7',
borderRadius: 20,
borderWidth: 1,
renderTo: 'chart',
type: 'line',
marginRight: 10,
zoomType: 'x'
},
exporting: {
enabled: true
},
legend: {
enabled: true,
backgroundColor: '#FCFFC5',
borderColor: 'black',
borderWidth: 2,
width: 500,
shadow: true
},
plotOptions: {
series: {
lineWidth: 1
}
},
rangeSelector: {
enabled: false
},
scrollbar: {
enabled: false
},
navigator : {
enabled : false
},
xAxis: {
gridLineColor: '#EEEEEE',
gridLineWidth: 1
},
yAxis: {
labels: {
style: {
color: 'blue'
}
},
gridLineColor: '#EEEEEE',
gridLineWidth: 0,
tickInterval: 20,
min: 0,
max: 100,
plotLines: [{
value: 70,
color: '#FF3300',
dashStyle: 'line',
width: 1,
label: {
text: 'Threshold=70%',
align: 'right',
style: {
fontWeight: 'bold'
}
}
}],
title: {
text: '% CPU Utilization',
style: {
color: 'blue'
}
}
},
credits: {
enabled: false
},
title: {
text: 'CPU',
style: {
color: '#333000',
fontSize: '14px'
}
},
subtitle: {
text: '10 minute peaks in last 24 hours'
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y} </b><br>',
valueDecimals: 2
},
series: data
});
});
}
In order to switch between different tabs, I can use jQuery click events as shown below:
$("#monthly_data").click(function() {
hmms_cpu_current();
});
$("#weekly_data").click(function() {
hmms_cpu_weekly();
});
$("#real_time").click(function() {
cpu_current();
});
My query relates to ensuring that if a user selects the "real_time" tab but does not interact with any other tabs, the data updates automatically via AJAX calls. However, if the user switches to the "monthly_data" tab, the automatic update should stop. How can this be achieved based on the provided code?