├── .gitignore
├── bower.json
├── README.md
├── package.json
├── LICENSE
├── samples
└── stacked-bar.html
├── docs
└── Stacked-Bar-Chart.md
└── src
└── Chart.StackedBar.js
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | .DS_Store
3 |
4 | node_modules/*
5 | custom/*
6 |
7 | docs/index.md
8 | bower_components
9 |
10 | .idea
11 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "Chart.StackedBar.js",
3 | "version": "1.2.1",
4 | "description": "StackedBar implementation for Chart.js",
5 | "homepage": "https://github.com/nnnick/Chart.js",
6 | "author": "Regaddi",
7 | "main": [
8 | "src/Chart.StackedBar.js"
9 | ],
10 | "dependencies": {
11 | "Chart.js": ">= 1.0.0-beta"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Chart.StackedBar.js
2 | ===================
3 |
4 | *StackedBar plugin for Chart.js* [chartjs.org](http://www.chartjs.org)
5 |
6 | ## Documentation
7 |
8 | You can find the documentation under `/docs`
9 |
10 | ## License
11 |
12 | Chart.StackedBar.js is available under the [MIT license](http://opensource.org/licenses/MIT).
13 |
14 | ## Bugs & issues
15 |
16 | When reporting bugs or issues, if you could include a link to a simple [jsbin](http://jsbin.com) or similar demonstrating the issue, that'd be really helpful.
17 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "Chart.StackedBar.js",
3 | "homepage": "http://www.chartjs.org",
4 | "description": "StackedBar implementation for Chart.js",
5 | "private": true,
6 | "version": "1.2.1",
7 | "repository": {
8 | "type": "git",
9 | "url": "https://github.com/Regaddi/Chart.StackedBar.js.git"
10 | },
11 | "dependences": {
12 | "Chart.js": ">= 1.0.2"
13 | },
14 | "main": "src/Chart.StackedBar.js",
15 | "devDependencies": {
16 | "gulp": "3.5.x",
17 | "gulp-concat": "~2.1.x",
18 | "gulp-uglify": "~0.2.x",
19 | "gulp-util": "~2.2.x",
20 | "gulp-jshint": "~1.5.1",
21 | "gulp-size": "~0.4.0",
22 | "gulp-connect": "~2.0.5"
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Christian Stuff
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/samples/stacked-bar.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Stacked Bar Chart
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/docs/Stacked-Bar-Chart.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: StackedBar Chart
3 | anchor: stacked-bar-chart
4 | ---
5 |
6 | ### Introduction
7 | A stacked bar chart is a way of showing data as bars.
8 |
9 |
10 |
11 |
12 |
13 | ### Example usage
14 | ```javascript
15 | var myStackedBarChart = new Chart(ctx).StackedBar(data, options);
16 | ```
17 |
18 | ### Data structure
19 |
20 | ```javascript
21 | var data = {
22 | labels: ["January", "February", "March", "April", "May", "June", "July"],
23 | datasets: [
24 | {
25 | label: "My First dataset",
26 | fillColor: "rgba(220,220,220,0.5)",
27 | strokeColor: "rgba(220,220,220,0.8)",
28 | highlightFill: "rgba(220,220,220,0.75)",
29 | highlightStroke: "rgba(220,220,220,1)",
30 | data: [65, 59, 80, 81, 56, 55, 40]
31 | },
32 | {
33 | label: "My Second dataset",
34 | fillColor: "rgba(151,187,205,0.5)",
35 | strokeColor: "rgba(151,187,205,0.8)",
36 | highlightFill: "rgba(151,187,205,0.75)",
37 | highlightStroke: "rgba(151,187,205,1)",
38 | data: [28, 48, 40, 19, 86, 27, 90]
39 | }
40 | ]
41 | };
42 | ```
43 |
44 | ### Chart Options
45 |
46 | These are the customisation options specific to StackedBar charts. These options are merged with the [global chart configuration options](#getting-started-global-chart-configuration), and form the options of the chart.
47 |
48 | ```javascript
49 | {
50 | //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
51 | scaleBeginAtZero : true,
52 |
53 | //Boolean - Whether grid lines are shown across the chart
54 | scaleShowGridLines : true,
55 |
56 | //String - Colour of the grid lines
57 | scaleGridLineColor : "rgba(0,0,0,.05)",
58 |
59 | //Number - Width of the grid lines
60 | scaleGridLineWidth : 1,
61 |
62 | //Boolean - If there is a stroke on each bar
63 | barShowStroke : true,
64 |
65 | //Number - Pixel width of the bar stroke
66 | barStrokeWidth : 2,
67 |
68 | //Number - Spacing between each of the X value sets
69 | barValueSpacing : 5,
70 |
71 | //Boolean - Whether bars should be rendered on a percentage base
72 | relativeBars : false,
73 |
74 | {% raw %}
75 | //String - A legend template
76 | legendTemplate : "
",
77 | {% endraw %}
78 |
79 | //Boolean - Hide labels with value set to 0
80 | tooltipHideZero: false
81 | }
82 | ```
83 |
84 | You can override these for your `Chart` instance by passing a second argument into the `StackedBar` method as an object with the keys you want to override.
85 |
86 | For example, we could have a stacked bar chart without a stroke on each bar by doing the following:
87 |
88 | ```javascript
89 | new Chart(ctx).StackedBar(data, {
90 | barShowStroke: false
91 | });
92 | // This will create a chart with all of the default options, merged from the global config,
93 | // and the StackedBar chart defaults but this particular instance will have `barShowStroke` set to false.
94 | ```
95 |
96 | We can also change these defaults values for each StackedBar type that is created, this object is available at `Chart.defaults.StackedBar`.
97 |
98 | ### Prototype methods
99 |
100 | #### .getBarsAtEvent( event )
101 |
102 | Calling `getBarsAtEvent(event)` on your Chart instance passing an argument of an event, or jQuery event, will return the bar elements that are at that the same position of that event.
103 |
104 | ```javascript
105 | canvas.onclick = function(evt){
106 | var activeBars = myStackedBarChart.getBarsAtEvent(evt);
107 | // => activeBars is an array of bars on the canvas that are at the same position as the click event.
108 | };
109 | ```
110 |
111 | This functionality may be useful for implementing DOM based tooltips, or triggering custom behaviour in your application.
112 |
113 | #### .update( )
114 |
115 | Calling `update()` on your Chart instance will re-render the chart with any updated values, allowing you to edit the value of multiple existing points, then render those in one animated render loop.
116 |
117 | ```javascript
118 | myStackedBarChart.datasets[0].bars[2].value = 50;
119 | // Would update the first dataset's value of 'March' to be 50
120 | myStackedBarChart.update();
121 | // Calling update now animates the position of March from 90 to 50.
122 | ```
123 |
124 | #### .addData( valuesArray, label )
125 |
126 | Calling `addData(valuesArray, label)` on your Chart instance passing an array of values for each dataset, along with a label for those bars.
127 |
128 | ```javascript
129 | // The values array passed into addData should be one for each dataset in the chart
130 | myStackedBarChart.addData([40, 60], "August");
131 | // The new data will now animate at the end of the chart.
132 | ```
133 |
134 | #### .removeData( )
135 |
136 | Calling `removeData()` on your Chart instance will remove the first value for all datasets on the chart.
137 |
138 | ```javascript
139 | myStackedBarChart.removeData();
140 | // The chart will now animate and remove the first bar
141 | ```
142 |
--------------------------------------------------------------------------------
/src/Chart.StackedBar.js:
--------------------------------------------------------------------------------
1 | (function (factory) {
2 | "use strict";
3 | if (typeof define === 'function' && define.amd) {
4 | // AMD. Register as an anonymous module.
5 | define(['chart.js'], factory);
6 | } else if (typeof exports === 'object') {
7 | // Node/CommonJS
8 | module.exports = factory(require('chart.js'));
9 | } else {
10 | // Global browser
11 | factory(Chart);
12 | }
13 | }(function (Chart) {
14 | "use strict";
15 |
16 | var helpers = Chart.helpers;
17 |
18 | function drawLine(ctx, scale, annotation) {
19 | ctx.beginPath();
20 | ctx.moveTo(scale.calculateBarX(annotation.value), scale.startPoint);
21 | ctx.strokeStyle = annotation.lineColor;
22 | ctx.lineTo(scale.calculateBarX(annotation.value), scale.endPoint);
23 | ctx.stroke();
24 | }
25 |
26 | function drawLabel(ctx, scale, annotation) {
27 | ctx.textAlign = 'left';
28 | ctx.fillStyle = annotation.labelColor;
29 | ctx.fillText(annotation.labelText, scale.calculateBarX(annotation.value), scale.startPoint - 5);
30 | }
31 |
32 | function drawAnnotations(chartType) {
33 | var annotations = chartType.options.annotation ? chartType.options.annotation.annotations : [];
34 | var datasets = chartType.datasets;
35 | var scale = chartType.scale;
36 | var ctx = chartType.chart.ctx;
37 |
38 | helpers.each(annotations, function (annotation) {
39 | if (annotation.type !== 'line') return;
40 |
41 | helpers.each(datasets, function (dataset) {
42 | drawLine(ctx, scale, annotation);
43 | drawLabel(ctx, scale, annotation);
44 | });
45 | });
46 | }
47 |
48 | var defaultConfig = {
49 | scaleBeginAtZero : true,
50 |
51 | //Boolean - Whether grid lines are shown across the chart
52 | scaleShowGridLines : true,
53 |
54 | //String - Colour of the grid lines
55 | scaleGridLineColor : "rgba(0,0,0,.05)",
56 |
57 | //Number - Width of the grid lines
58 | scaleGridLineWidth : 1,
59 |
60 | //Boolean - Whether to show horizontal lines (except X axis)
61 | scaleShowHorizontalLines: true,
62 |
63 | //Boolean - Whether to show vertical lines (except Y axis)
64 | scaleShowVerticalLines: true,
65 |
66 | //Boolean - If there is a stroke on each bar
67 | barShowStroke : true,
68 |
69 | //Number - Pixel width of the bar stroke
70 | barStrokeWidth : 2,
71 |
72 | //Number - Spacing between each of the X value sets
73 | barValueSpacing : 5,
74 |
75 | //Boolean - Whether bars should be rendered on a percentage base
76 | relativeBars : false,
77 |
78 | //String - A legend template
79 | legendTemplate : "