|
Pause / Resume updates: |
Select color scheme: |
JavaScript Chart Live Updates Performance Example.
A live JavaScript/jQuery example of jqxChart, the jQWidgets Chart component, with its complete source code below. It is one of 92 jqxChart demos for jQuery.
title property – Sets or gets the jqxChart title.description property – Sets or gets the jqxChart description.source property – Sets the chart's data source.showLegend property – Determines whether to show or hide the chart series legend.padding propertytitlePadding propertycolorScheme propertyenableAnimations property – Determines if the animations are enabled.xAxis property – Sets the Chart's xAxis.valueAxis propertyseriesGroups property – The seriesGroups property is used to describe all series displayed on the chart.click event – The event is raised when the user clicks on series element.getInstance methodupdate methodTo run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxChart shows the setup step by step.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxdraw.js, jqxchart.core.js, jqxslider.js, jqxbuttons.js, jqxlistbox.js, jqxscrollbar.js, jqxdropdownlist.js, jqxresponse.js.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).
$(document).ready(function () {
var refreshTimeout = 100;
var response = new $.jqx.response();
if (response && response.device)
{
if (response.device.type == 'Tablet')
{
refreshTimeout = 150;
}
else if (response.device.width < 800 ||
response.device.height < 500 ||
response.device.type == 'Phone' ||
(response.browser.svg == false && response.browser.canvas == false)
)
{
refreshTimeout = 1000;
}
}
var data = [];
var max = 200;
for (var i = 0; i < 20; i++) {
data.push({
key: i,
value1: (Math.random() * 200) % 200 + 200,
value2: (Math.random() * 200) % 200 + 500,
value3: (Math.random() * 200) % 200,
});
}
// prepare jqxChart settings
var settings = {
title: "Live updates performance",
description: "Data changes every " + refreshTimeout + " milliseconds",
enableAnimations: false,
showLegend: true,
padding: { left: 10, top: 10, right: 10, bottom: 10 },
titlePadding: { left: 0, top: 0, right: 0, bottom: 10 },
source: data,
xAxis:
{
dataField: 'key',
unitInterval: 1,
valuesOnTicks: true,
gridLines: {visible: false}
},
valueAxis:
{
minValue: 0,
maxValue: 750,
title: {text: 'Index Value<br>'},
},
colorScheme: 'scheme01',
seriesGroups:
[
{
type: 'line',
useGradientColors: false,
series: [
{ dataField: 'value1', displayText: 'value1', lineWidth: 2, symbolType: 'circle' }
]
},
{
type: 'spline',
useGradientColors: false,
columnsGapPercent: 50,
alignEndPointsWithIntervals: true,
series: [
{ dataField: 'value2', displayText: 'value2'}
]
},
{
type: 'column',
useGradientColors: false,
columnsGapPercent: 50,
alignEndPointsWithIntervals: true,
series: [
{ dataField: 'value3', displayText: 'value3'}
]
}
]
};
// create the chart
$('#chartContainer').jqxChart(settings);
// get the chart's instance
var chart = $('#chartContainer').jqxChart('getInstance');
// color scheme drop down
var colorsSchemesList = ["scheme01", "scheme02", "scheme03", "scheme04", "scheme05", "scheme06", "scheme07", "scheme08"];
$("#dropDownColors").jqxDropDownList({ source: colorsSchemesList, selectedIndex: 0, width: '200', height: '25', dropDownHeight: 100, enableBrowserBoundsDetection: true });
$('#dropDownColors').on('change', function (event) {
var value = event.args.item.value;
chart.colorScheme = value;
chart.update();
});
var timerFunction = function () {
data.splice(0, 1);
data.push({ key: data[data.length - 1].key + 1,
value1: (Math.random() * 200) % 200 + 200,
value2: (Math.random() * 200) % 200 + 500,
value3: (Math.random() * 200) % 200,
});
$('#chartContainer').jqxChart('update');
};
// auto update timer
var ttimer = setInterval(timerFunction, refreshTimeout);
$('#btnPauseResume')
.jqxButton({ width: 160 })
.on('click', function () {
if (ttimer) {
clearInterval(ttimer);
$('#btnPauseResume').val('Resume');
ttimer = undefined;
}
else {
ttimer = setInterval(timerFunction, refreshTimeout);
$('#btnPauseResume').val('Pause');
}
});
});
<p>
</p><div id="chartContainer" style="width: 850px; height: 500px">
</div>
<p></p>
<table style="width: 680px">
<tbody><tr>
<td>
<p style="font-family: Verdana; font-size: 12px; width:170px;">Pause / Resume updates:
</p>
<input type="button" id="btnPauseResume" value="Pause">
</td>
<td style="padding-left: 50px;">
<p style="font-family: Verdana; font-size: 12px;">Select color scheme:
</p>
<div id="dropDownColors">
</div>
</td>
</tr>
</tbody></table>