JavaScript Chart Column Series with conditional colors.
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.showBorderLine property – Determines whether to display the chart's border line.borderLineWidth property – Sets or gets the chart's border line width.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.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 $. 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).
$(document).ready(function () {
var source =
{
datatype: "csv",
datafields: [
{ name: 'Quarter' },
{ name: 'Change' }
],
url: '../sampledata/us_gdp_2008-2013.csv'
};
var dataAdapter = new $.jqx.dataAdapter(
source,
{
async: false,
autoBind: true,
loadError: function (xhr, status, error) {
alert('Error loading "' + source.url + '" : ' + error);
}
}
);
settings = {
title: "U.S. GDP Percentage Change",
borderLineWidth: 1,
showBorderLine: true,
enableAnimations: true,
description: '(source: Bureau of Economic Analysis)',
showLegend: false,
//padding: { left: 5, top: 5, right: 10, bottom: 5 },
//titlePadding: { left: 0, top: 0, right: 0, bottom: 10 },
source: dataAdapter,
xAxis:
{
//description: 'Year and quarter',
dataField: 'Quarter',
unitInterval: 1,
textRotationAngle: -75,
formatFunction: function (value, itemIndex, serie, group) {
return value;
},
valuesOnTicks: false
},
colorScheme: 'scheme05',
seriesGroups:
[
{
type: 'column',
valueAxis:
{
description: 'Percentage Change',
formatFunction: function (value) {
return value + '%';
},
maxValue: 10,
minValue: -10,
unitInterval: 2
},
series:
[
{
dataField: 'Change',
displayText: 'Change',
toolTipFormatFunction: function(value, itemIndex, serie, group, categoryValue, categoryAxis) {
return '<DIV style="text-align:left";><b>Year-Quarter: </b>' + categoryValue
+ '<br /><b>GDP Change:</b> ' + value + ' %</DIV>'
},
// Modify this function to return the desired colors.
// jqxChart will call the function for each data point.
// Sequential points that have the same color will be
// grouped automatically in a line segment
colorFunction: function (value, itemIndex, serie, group) {
return (value < 0) ? '#CC1133' : '#55CC55';
}
}
]
}
]
};
$("#chart1").jqxChart(settings);
});
<div id="chart1" style="width:800px; height:500px"></div>