jQuery Stacked Columns Chart

In this post, we’ll create a Stacked Columns Chart by using the jqxChart widget. 1. Include the jqxchart.js, jqxdata.js, jqxcore.js, jqx.base.css and the jQuery framework.
<link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
<script type="text/javascript" src="../../scripts/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxchart.js"></script>
2. Add a DIV tag to the document’s body and set its width and height. They will specify the Chart’s size. 3. Create the Chart in the jQuery’s ready function. In order to create a stacked columns chart, you need to set the type property to ‘stackedcolumn’ when you define the Chart’s seriesGroups object. Here’s a link to a demo in jsfiddle: Stacked Columns Chart Demo The Chart’s initialization code:
         var sampleData = [
{ Day: 'Monday', Running: 30, Swimming: 0, Cycling: 25 },
{ Day: 'Tuesday', Running: 25, Swimming: 25, Cycling: 0 },
{ Day: 'Wednesday', Running: 30, Swimming: 0, Cycling: 25 },
{ Day: 'Thursday', Running: 35, Swimming: 25, Cycling: 45 },
{ Day: 'Friday', Running: 0, Swimming: 20, Cycling: 25 },
{ Day: 'Saturday', Running: 30, Swimming: 0, Cycling: 30 },
{ Day: 'Sunday', Running: 60, Swimming: 45, Cycling: 0 }
];
// prepare jqxChart settings
var settings = {
title: "Fitness & exercise weekly scorecard",
description: "Time spent in vigorous exercise by activity",
enableAnimations: true,
showLegend: true,
padding: { left: 5, top: 5, right: 5, bottom: 5 },
titlePadding: { left: 90, top: 0, right: 0, bottom: 10 },
source: sampleData,
categoryAxis:
{
text: 'Category Axis',
textRotationAngle: 0,
dataField: 'Day',
showTickMarks: true,
tickMarksInterval: 1,
tickMarksColor: '#888888',
unitInterval: 1,
showGridLines: false,
gridLinesInterval: 1,
gridLinesColor: '#888888',
axisSize: 'auto'
},
colorScheme: 'scheme03',
seriesGroups:
[
{
type: 'stackedcolumn',
columnsGapPercent: 100,
seriesGapPercent: 5,
showLabels: true,
valueAxis:
{
unitInterval: 10,
minValue: 0,
maxValue: 100,
displayValueAxis: true,
description: 'Time in minutes',
axisSize: 'auto',
tickMarksColor: '#888888'
},
series: [
{ dataField: 'Running', displayText: 'Running' },
{ dataField: 'Swimming', displayText: 'Swimming' },
{ dataField: 'Cycling', displayText: 'Cycling' }
]
}
]
};
// setup the chart
$('#jqxChart').jqxChart(settings);​
The resulting Chart is displayed below: jquery-stackedcolumns-chart

About admin


This entry was posted in JavaScript UI, jQuery Widgets, jQWidgets, jqxChart and tagged , , , , , , , , , , . Bookmark the permalink.



Leave a Reply