Set dynamically values in jqxBarGauge from a list.
A live JavaScript/jQuery example of jqxBarGauge, the jQWidgets BarGauge component, with its complete source code below. It is one of 8 jqxBarGauge demos for jQuery.
animationDuration property – Determines the animation in milliseconds.barSpacing property – Sets or gets the space between the segments of the jqxBarGauge.baseValue property – Sets the base value of jqxBarGauge.colorScheme property – Sets the jqxBarGauge's color palette.endAngle propertyformatFunction property – Sets or gets the formatFunction of the BarGauge.height property – Sets or gets the BarGauge's height.labels property – This property is used to make fine settings on BarGauge labels at each segment.max property – Sets or gets the end value of BarGauge.relativeInnerRadius property – Sets or gets the relativeInnerRadius of jqxBarGauge.startAngle property – Sets or gets the startAngle of the BarGauge.title property – Sets or gets the BarGauge's title.tooltip property – Sets or gets the BarGauge's tooltip.values property – Sets or gets array of values for the BarGauge.width property – Sets or gets the BarGauge's width.val method – Returns an array of the BarGauge values.To 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 $.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdraw.js, jqxdata.js, jqxbargauge.js, jqxcheckbox.js, jqxlistbox.js, jqxbuttons.js, jqxscrollbar.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 = [{
"id": "1",
"name": "Hot Chocolate",
"calories": "370"
}, {
"id": "2",
"name": "Peppermint Hot Chocolate",
"calories": "440"
}, {
"id": "3",
"name": "Salted Caramel Hot Chocolate",
"calories": "450"
}, {
"id": "4",
"name": "White Hot Chocolate",
"calories": "420"
}, {
"id": "5",
"name": "Caffe Americano",
"calories": "15"
}, {
"id": "6",
"name": "Caffe Latte",
"calories": "190"
}, {
"id": "7",
"name": "Caffe Mocha",
"calories": "330"
}];
var source = {
datatype: "json",
datafields: [
{ name: 'name' },
{ name: 'calories', type: 'int' }
],
id: 'id',
localdata: data
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#jqxlistbox").jqxListBox({
width: 230,
source: dataAdapter,
displayMember: "name",
valueMember: "calories",
checkboxes: true
});
$("#jqxlistbox").jqxListBox('checkIndex', 2);
$("#jqxlistbox").jqxListBox('checkIndex', 5);
$("#jqxlistbox").jqxListBox('checkIndex', 6);
var getValues = function ()
{
var items = $("#jqxlistbox").jqxListBox('getCheckedItems');
var array = convertToArray(items);
return array;
};
var convertToArray = function (items)
{
var preparedArray = new Array(items.length);
for (var i = 0; i < items.length; i += 1)
{
preparedArray[i] = items[i].value;
}
return preparedArray;
}
$('#barGauge').jqxBarGauge({
width: getWidth('barGauge'),
height: getHeight('barGauge'),
title: 'Nutritional Values',
values: getValues(),
baseValue: 50,
max: 500,
barSpacing: 9,
animationDuration: 0,
relativeInnerRadius: 0.2,
startAngle: 0,
endAngle: 360,
colorScheme: 'scheme05',
tooltip: {
visible: true,
formatFunction: function (value, index)
{
var items = $("#jqxlistbox").jqxListBox('getCheckedItems');
var item = items[index];
return item.label + ": " + value + ' cal.';
}
},
labels: { formatFunction: function (value) { return value + ' cal.'; }, precision: 0, indent: 15, connectorWidth: 1 }
});
var getSum = function (array)
{
array = array || [];
var sum = 0;
if (!!array.length)
{
for (var i = 0; i < array.length; i += 1)
{
sum += array[i];
}
}
return sum;
};
$('#log').html('<strong>Summary calories: ' + 970 + '</strong>');
$("#jqxlistbox").on('checkChange', function (event)
{
var items = $("#jqxlistbox").jqxListBox('getCheckedItems');
var arrayOfItems = convertToArray(items);
$('#log').html('<strong>Summary calories: ' + getSum(arrayOfItems) + '</strong>');
$('#barGauge').jqxBarGauge('val', arrayOfItems);
});
});
<div id="barGauge"></div>
<div style="margin-left: 50px; margin-top: 150px; float: left;">
<div id="jqxlistbox"></div>
<br>
<br>
<div id="log"></div>
</div>