JavaScript Chart Drawing APIs 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.backgroundColor propertypadding propertytitlePadding propertyTo 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.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).
function transformPath(path, wScale, hScale, xOffset, yOffset) {
var output = '';
for (var i = 0; i < path.length; i++) {
if (path[i] == 'C' || path[i] == 'M' || path[i] == 'L') {
output += path[i];
i++;
var j = i;
while (j + 1 < path.length && !(path[j + 1] == 'C' || path[j + 1] == 'M' || path[j + 1] == 'L' || path[j + 1] == 'z'))
j++;
var value = path.substring(i, j);
var pointsString = value.split(' ');
for (var s = 0; s < pointsString.length; s++) {
if (pointsString[s].length == 0)
continue;
var pointString = pointsString[s].split(',');
// (the map data has a few malformed points: skipped)
if (pointString.length !== 2) continue;
var x = parseFloat(pointString[0]);
var y = parseFloat(pointString[1]);
if (isNaN(x) || isNaN(y)) continue;
output += ' ' + (x * wScale + xOffset) + ',' + (y * hScale + yOffset) + ' ';
}
i = j - 1;
}
else {
output += path[i];
}
}
return output;
}
$(document).ready(function () {
var adapterSettings =
{
datatype: "json",
datafields: [
{ name: 'id' },
{ name: 'path' },
{ name: 'europe' },
{ name: 'eu' }
],
/* EU map definitions in JSON derived from http://commons.wikimedia.org/wiki/File:116_000_map_of_Europe.svg */
url: '../sampledata/europe.txt'
};
var dataAdapter = new $.jqx.dataAdapter(adapterSettings, { async: false, autoBind: true, loadError: function (xhr, status, error) { alert('Error loading "' + source.url + '" : ' + error); } });
var chartSettings = {
title: "Map of the European Union",
description: "",
padding: { left: 10, top: 5, right: 10, bottom: 5 },
titlePadding: { left: 90, top: 0, right: 0, bottom: 10 },
backgroundColor: 'lightblue',
source: dataAdapter
};
// function draw before the title and other chart elements
chartSettings.drawBefore = function (renderer, rect) {
renderer.rect(rect.x, rect.y, rect.width, rect.height, { fill: 'lightblue' });
}
// custom drawing function after
chartSettings.draw = function (renderer, rect) {
var records = this.source.records;
var wScale = rect.width / 13000
var hScale = (rect.height) / 8500;
for (var i = 0; i < records.length; i++) {
var path = transformPath(records[i].path, wScale, hScale, 62, 22);
var pathElement = renderer.path(path, { stroke: 'black' });
if (records[i].eu == "true") {
renderer.attr(pathElement, { fill: 'blue' });
}
else
renderer.attr(pathElement, { fill: '#DEDEDE' });
}
}
// create the chart
$('#chartContainer').jqxChart(chartSettings);
});
<div id="chartContainer" style="width:850px; height:500px">
</div>