|
Select the series type: |
Select color scheme: |
JavaScript Chart Live Updates each second.
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.animationDuration property – Determines the animations duration in milliseconds.enableAxisTextAnimation property – Determines if the animation of the axes text is enabled.xAxis property – Sets the Chart's xAxis.valueAxis propertyseriesGroups property – The seriesGroups property is used to describe all series displayed on the chart.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.
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 data = [];
var max = 800;
var timestamp = new Date();
for (var i = 0; i < 60; i++) {
timestamp.setMilliseconds(0);
timestamp.setSeconds(timestamp.getSeconds() - 1);
data.push({ timestamp: new Date(timestamp.valueOf()), value: Math.max(100, (Math.random() * 1000) % max) });
}
data = data.reverse();
// prepare jqxChart settings
var settings = {
title: "Live updates demo",
description: " ",
enableAnimations: false,
animationDuration: 1000,
enableAxisTextAnimation: true,
showLegend: true,
padding: { left: 5, top: 5, right: 5, bottom: 5 },
titlePadding: { left: 0, top: 0, right: 0, bottom: 10 },
source: data,
xAxis:
{
dataField: 'timestamp',
type: 'date',
baseUnit: 'second',
unitInterval: 5,
formatFunction: function (value) {
return $.jqx.dataFormat.formatdate(value, "hh:mm:ss", 'en-us');
},
gridLines: { step: 2 },
valuesOnTicks: true,
labels: { angle: -45, offset: { x: -17, y: 0} }
},
colorScheme: 'scheme03',
seriesGroups:
[
{
type: 'line',
columnsGapPercent: 50,
alignEndPointsWithIntervals: true,
valueAxis:
{
minValue: 0,
maxValue: 1000,
title: { text: 'Index Value' }
},
series: [
{ dataField: 'value', displayText: 'value', opacity: 1, lineWidth: 2, symbolType: 'circle', fillColorSymbolSelected: 'white', symbolSize: 4 }
]
}
]
};
// 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: 2, width: '200', height: '25', dropDownHeight: 100 });
$('#dropDownColors').on('change', function (event) {
var value = event.args.item.value;
chart.colorScheme = value;
chart.update();
});
// series type drop down
var seriesList = ["line", "area", "stepline", "steparea", "splinearea", "spline", "column", "scatter", "stackedcolumn", "stackedsplinearea", "stackedspline"];
$("#dropDownSeries").jqxDropDownList({ source: seriesList, selectedIndex: 0, width: '200', height: '25', dropDownHeight: 100 });
$('#dropDownSeries').on('select', function (event) {
var args = event.args;
if (args) {
var value = args.item.value;
var group = chart.seriesGroups[0];
chart.seriesGroups[0].type = value;
chart.update();
}
});
// auto update timer
var ttimer = setInterval(function () {
var max = 800;
if (data.length >= 60)
data.splice(0, 1);
var timestamp = new Date();
timestamp.setSeconds(timestamp.getSeconds());
timestamp.setMilliseconds(0);
data.push({ timestamp: timestamp, value: Math.max(100, (Math.random() * 1000) % max) });
$('#chartContainer').jqxChart('update');
}, 1000);
});
<div id="chartContainer" style="width: 850px; height: 500px;">
</div>
<table style="width: 680px">
<tbody><tr>
<td style="padding-left: 50px;">
<p style="font-family: Verdana; font-size: 12px;">Select the series type:
</p>
<div id="dropDownSeries">
</div>
</td>
<td>
<p style="font-family: Verdana; font-size: 12px;">Select color scheme:
</p>
<div id="dropDownColors">
</div>
</td>
</tr>
</tbody></table>