Skip to content

Commit

Permalink
Allow specifying bar chart via {x, y} data points (#4565)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored and simonbrunel committed Jul 29, 2017
1 parent 326991c commit 56fdd7e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/charts/bar.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ The `data` property of a dataset for a bar chart is specified as a an array of n
data: [20, 10]
```

You can also specify the dataset as x/y coordinates.

```javascript
data: [{x:'2016-12-25', y:20}, {'2016-12-26', y:10}]
```

# Stacked Bar Chart

Bar charts can be configured into stacked bar charts by changing the settings on the X and Y axes to enable stacking. Stacked bar charts can be used to show how one data series is made up of a number of smaller pieces.
Expand Down
9 changes: 4 additions & 5 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ module.exports = function(Chart) {
var meta = me.getMeta();
var scale = me.getValueScale();
var datasets = chart.data.datasets;
var value = Number(datasets[datasetIndex].data[index]);
var value = scale.getRightValue(datasets[datasetIndex].data[index]);
var stacked = scale.options.stacked;
var stack = meta.stack;
var start = 0;
Expand All @@ -283,7 +283,7 @@ module.exports = function(Chart) {
imeta.controller.getValueScaleId() === scale.id &&
chart.isDatasetVisible(i)) {

ivalue = Number(datasets[i].data[index]);
ivalue = scale.getRightValue(datasets[i].data[index]);
if ((value < 0 && ivalue < 0) || (value >= 0 && ivalue > 0)) {
start += ivalue;
}
Expand Down Expand Up @@ -330,17 +330,16 @@ module.exports = function(Chart) {
draw: function() {
var me = this;
var chart = me.chart;
var scale = me.getIndexScale();
var rects = me.getMeta().data;
var dataset = me.getDataset();
var ilen = rects.length;
var i = 0;
var d;

helpers.canvas.clipArea(chart.ctx, chart.chartArea);

for (; i < ilen; ++i) {
d = dataset.data[i];
if (d !== null && d !== undefined && !isNaN(d)) {
if (!isNaN(scale.getRightValue(dataset.data[i]))) {
rects[i].draw();
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/scales/scale.linearbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ module.exports = function(Chart) {
var noop = helpers.noop;

Chart.LinearScaleBase = Chart.Scale.extend({
getRightValue: function(value) {
if (typeof value === 'string') {
return +value;
}
return Chart.Scale.prototype.getRightValue.call(this, value);
},

handleTickRangeOptions: function() {
var me = this;
var opts = me.options;
Expand Down

0 comments on commit 56fdd7e

Please sign in to comment.