Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float-bar support. The logic is to use array values [bottomY, topY] i… #5262

Closed
wants to merge 82 commits into from
Closed
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
a31d179
Float-bar support. The logic is to use array values [bottomY, topY] i…
gwyneblaidd Feb 11, 2018
ceafcaf
formatting
gwyneblaidd Feb 11, 2018
f2e7310
formatting&fix
gwyneblaidd Feb 11, 2018
d07f183
formatting
gwyneblaidd Feb 11, 2018
4ae0988
formatting
gwyneblaidd Feb 11, 2018
5b5bd25
formatting&docs
gwyneblaidd Feb 11, 2018
88aa1a5
logarithmic scale is added
gwyneblaidd Feb 11, 2018
fc478a6
Update bar.md
gwyneblaidd Feb 12, 2018
6075e6f
core scale hardcoding for getRightBarValue(). Float bar support (arra…
gwyneblaidd Feb 14, 2018
85ea4da
Merge remote-tracking branch 'origin/master'
gwyneblaidd Feb 22, 2018
b4c2994
CHanges as requested simonbrunel
gwyneblaidd Feb 22, 2018
87e6e38
latest changes with stacked bar linera/log scale and float0bar support
gwyneblaidd Feb 23, 2018
0e55557
formatting
gwyneblaidd Feb 23, 2018
0dd7666
tests
gwyneblaidd Feb 24, 2018
8e44c5e
formatting
gwyneblaidd Feb 24, 2018
70a11e5
formatting
gwyneblaidd Feb 24, 2018
362d5f9
formatting
gwyneblaidd Feb 24, 2018
12525ce
formatting
gwyneblaidd Feb 24, 2018
4a366a2
formatting
gwyneblaidd Feb 24, 2018
69b8ab0
D stacking implementation for float-bar support
gwyneblaidd Apr 28, 2018
256729c
Merge branch 'master' into master
gwyneblaidd Apr 28, 2018
42add1c
conflict resolve
gwyneblaidd Apr 28, 2018
0aad0c8
chars -> charts
gwyneblaidd May 7, 2018
e414eb7
removing comment, because this section was reverted back to fit D sta…
gwyneblaidd May 7, 2018
2161fc3
lowercased variable name
gwyneblaidd May 8, 2018
84e89aa
element code formatting change
gwyneblaidd May 8, 2018
8bfa7e1
reverted scale.js
gwyneblaidd May 11, 2018
b12e2fe
latest fixes
gwyneblaidd May 11, 2018
786c5c4
1. new method in core.scales to handle arrays (float-bars), getRightV…
gwyneblaidd May 15, 2018
861ed13
ode reformatting. Methods for extracting value, high and top separately
gwyneblaidd May 16, 2018
df32e68
refactoring
gwyneblaidd May 17, 2018
10c3831
refactoring
gwyneblaidd May 17, 2018
614275d
refactoring
gwyneblaidd May 17, 2018
cf6fd4b
formatting
gwyneblaidd May 17, 2018
d5ef434
formatting
gwyneblaidd May 18, 2018
a75e3ac
CHanges to float-bar support.
gwyneblaidd May 21, 2018
aaaac73
formatting
gwyneblaidd May 21, 2018
5e877aa
fixes
gwyneblaidd May 21, 2018
823160e
formatting
gwyneblaidd May 22, 2018
11bb986
fixes
gwyneblaidd May 23, 2018
1c526e3
fixes
gwyneblaidd May 23, 2018
a59a602
fixes
gwyneblaidd May 23, 2018
a4efcf0
fix
gwyneblaidd May 23, 2018
25ff4ea
formatting
gwyneblaidd May 23, 2018
7e75bef
fixes
gwyneblaidd May 23, 2018
03b8167
Reformatting parseScale function
gwyneblaidd May 24, 2018
659235b
last changes
gwyneblaidd May 29, 2018
fe0772c
formatting
gwyneblaidd Jun 4, 2018
730e468
fix
gwyneblaidd Jun 5, 2018
122167c
formatting
gwyneblaidd Jun 8, 2018
3e42297
fix
gwyneblaidd Jun 11, 2018
9b95637
fix
gwyneblaidd Jun 18, 2018
2af134b
fix to pass tests. Omit 0 when building scales when value min is 0.
gwyneblaidd Jun 19, 2018
a0e1231
tooltip fix for tests
gwyneblaidd Jun 20, 2018
a65aa3c
beginAtZero for float-val tests
gwyneblaidd Jun 27, 2018
901c96f
formatting
gwyneblaidd Jun 27, 2018
7a42562
formatting
gwyneblaidd Jun 27, 2018
4f5332e
formatting
gwyneblaidd Jun 27, 2018
63ce9e9
formatting
gwyneblaidd Jun 27, 2018
b626b57
rebase
gwyneblaidd Jan 16, 2019
b25db90
Merge branch 'master' into master_up
gwyneblaidd Jan 16, 2019
8f06bc5
Merge pull request #1 from gwyneblaidd/master_up
gwyneblaidd Jan 16, 2019
2a07aaa
Update controller.bar.js
gwyneblaidd Jan 17, 2019
d165e24
Update core.scale.js
gwyneblaidd Jan 17, 2019
c4148e9
Update core.tooltip.js
gwyneblaidd Jan 17, 2019
a48b8ce
Update element.rectangle.js
gwyneblaidd Jan 17, 2019
e1095fa
Update scale.linear.js
gwyneblaidd Jan 17, 2019
14bccfe
Update scale.logarithmic.js
gwyneblaidd Jan 17, 2019
f4d7413
Back changes after rebase
gwyneblaidd Jan 18, 2019
5590350
formatting
gwyneblaidd Jan 19, 2019
9f88e05
formatting
gwyneblaidd Jan 19, 2019
76e9ceb
fixes
gwyneblaidd Feb 5, 2019
f957e64
Merge branch 'master' into master
gwyneblaidd Feb 5, 2019
2d06063
formatting
gwyneblaidd Feb 5, 2019
a786cd1
Merge branch 'master' of https://github.com/gwyneblaidd/Chart.js
gwyneblaidd Feb 5, 2019
74dc634
fallback to test units
gwyneblaidd Feb 7, 2019
dba5bdb
border skipped added
gwyneblaidd Feb 7, 2019
0e58967
_parseValue added. Scales updated
gwyneblaidd Feb 8, 2019
124688c
format
gwyneblaidd Feb 8, 2019
84dae3d
+
gwyneblaidd Feb 8, 2019
19a400c
controller bar update
gwyneblaidd Feb 8, 2019
87b0d91
format
gwyneblaidd Feb 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions docs/charts/bar.md
Expand Up @@ -16,7 +16,7 @@ A bar chart provides a way of showing data values represented as vertical bars.
],
"datasets": [{
"label": "My First Dataset",
"data": [65, 59, 80, 81, 56, 55, 40],
"data": [65, 59, 80, 81, 56, 55, 40, [10, 20]],
"fill": false,
"backgroundColor": [
"rgba(255, 99, 132, 0.2)",
Expand Down Expand Up @@ -86,6 +86,7 @@ Options are:
* 'left'
* 'top'
* 'right'
* false - in the case of floating bar chart we need to have all borders, so you can set var to false to draw all borders

## Configuration Options

Expand Down Expand Up @@ -146,10 +147,10 @@ Sample: |==============|

## Data Structure

The `data` property of a dataset for a bar chart is specified as a an array of numbers. Each point in the data array corresponds to the label at the same index on the x axis.

The `data` property of a dataset for a bar chart is specified as a an array of numbers or arrays. Each point in the data array corresponds to the label at the same index on the x axis.
For floating bar chart you can use array for a specific Y value passing two numbers. First one will be used as min value and second one as max value for a bar.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change this to something like
For floating bar char you can use array of [start, end] for a point, where both start and end are values accepted by the scale used

```javascript
data: [20, 10]
data: [20, 10, [10, 20]]
```

You can also specify the dataset as x/y coordinates when using the [time scale](../axes/cartesian/time.md#time-cartesian-axis).
Expand Down Expand Up @@ -196,7 +197,7 @@ A horizontal bar chart is a variation on a vertical bar chart. It is sometimes u
"labels": ["January", "February", "March", "April", "May", "June", "July"],
"datasets": [{
"label": "My First Dataset",
"data": [65, 59, 80, 81, 56, 55, 40],
"data": [65, 59, 80, 81, 56, 55, 40, [10, 20]],
"fill": false,
"backgroundColor": [
"rgba(255, 99, 132, 0.2)",
Expand Down
33 changes: 26 additions & 7 deletions src/controllers/controller.bar.js
Expand Up @@ -87,7 +87,14 @@ defaults._set('horizontalBar', {

label: function(item, data) {
var datasetLabel = data.datasets[item.datasetIndex].label || '';
return datasetLabel + ': ' + item.xLabel;
// float-bar support, if y arguments are array tooltip will show bottom and up values
var yValue = data.datasets[item.datasetIndex].data[item.index];
if (helpers.isArray(yValue)) {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
datasetLabel += ': ' + yValue[0] + ' - ' + yValue[1];
} else {
datasetLabel += ': ' + item.xLabel;
}
return datasetLabel;
}
},
mode: 'index',
Expand Down Expand Up @@ -219,6 +226,11 @@ module.exports = function(Chart) {
var custom = rectangle.custom || {};
var rectangleOptions = chart.options.elements.rectangle;

// float-bar support, if y arguments are array lets override rectangles styles, assigning no skippingBorder
if (helpers.isArray(dataset.data[index])) {
custom.borderSkipped = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does that means we will enforce borderSkipped in case of float-bar? I'm not sure that's a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, in fact we enforce this, cause float-bar will have all 4 sides required to be drawn, now sure if need to add smth else there. Or your idea to check the stacking here and make border skipped when 2 float-bars are stacked and one border should be eliminated. In my opinion in this case we will have 2 borders between 2 float-bars while stacking but thats better for vizualization purpose.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider changing this to

cutom.borderSkipped = custom.borderSkipped !== undefined ? custom.borderSkipped : null;

ie, if for whatever reason a bar has a custom skipped border setting, it will be kept otherwise there is no way for a user to draw partial borders on a floating bar

}

rectangle._xScale = me.getScaleForId(meta.xAxisID);
rectangle._yScale = me.getScaleForId(meta.yAxisID);
rectangle._datasetIndex = me.index;
Expand All @@ -227,7 +239,7 @@ module.exports = function(Chart) {
rectangle._model = {
datasetLabel: dataset.label,
label: chart.data.labels[index],
borderSkipped: custom.borderSkipped ? custom.borderSkipped : rectangleOptions.borderSkipped,
borderSkipped: custom.borderSkipped || custom.borderSkipped === null ? custom.borderSkipped : rectangleOptions.borderSkipped,
benmccann marked this conversation as resolved.
Show resolved Hide resolved
backgroundColor: custom.backgroundColor ? custom.backgroundColor : helpers.valueAtIndexOrDefault(dataset.backgroundColor, index, rectangleOptions.backgroundColor),
borderColor: custom.borderColor ? custom.borderColor : helpers.valueAtIndexOrDefault(dataset.borderColor, index, rectangleOptions.borderColor),
borderWidth: custom.borderWidth ? custom.borderWidth : helpers.valueAtIndexOrDefault(dataset.borderWidth, index, rectangleOptions.borderWidth)
Expand Down Expand Up @@ -383,12 +395,14 @@ module.exports = function(Chart) {
var meta = me.getMeta();
var scale = me.getValueScale();
var datasets = chart.data.datasets;
var value = scale.getRightValue(datasets[datasetIndex].data[index]);
// float-bar support, if y arguments are array function will use top - bottom value to calculate bar height
var yValue = datasets[datasetIndex].data[index];
var value = helpers.isArray(yValue) ? (scale.getRightValue(yValue[1]) - scale.getRightValue(yValue[0])) : scale.getRightValue(yValue);
benmccann marked this conversation as resolved.
Show resolved Hide resolved
var stacked = scale.options.stacked;
var stack = meta.stack;
var start = 0;
// float-bar support, if y arguments are array function will use bottom value as bar start point
var start = helpers.isArray(yValue) ? yValue[0] : 0;
var i, imeta, ivalue, base, head, size;

benmccann marked this conversation as resolved.
Show resolved Hide resolved
if (stacked || (stacked === undefined && stack !== undefined)) {
for (i = 0; i < datasetIndex; ++i) {
imeta = chart.getDatasetMeta(i);
Expand All @@ -397,7 +411,6 @@ module.exports = function(Chart) {
imeta.stack === stack &&
imeta.controller.getValueScaleId() === scale.id &&
chart.isDatasetVisible(i)) {

benmccann marked this conversation as resolved.
Show resolved Hide resolved
ivalue = scale.getRightValue(datasets[i].data[index]);
if ((value < 0 && ivalue < 0) || (value >= 0 && ivalue > 0)) {
start += ivalue;
Expand Down Expand Up @@ -453,8 +466,14 @@ module.exports = function(Chart) {

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

// float-bar support, if y arguments are array function will use bottom value as bar start point
for (; i < ilen; ++i) {
if (!isNaN(scale.getRightValue(dataset.data[i]))) {
var yValue = dataset.data[i];
if (helpers.isArray(yValue)) {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
if (!isNaN(scale.getRightValue(yValue[1])) && !isNaN(scale.getRightValue(yValue[0]))) {
rects[i].draw();
}
} else if (!isNaN(scale.getRightValue(yValue))) {
rects[i].draw();
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/core.scale.js
Expand Up @@ -514,6 +514,10 @@ module.exports = Element.extend({

// Get the correct value. NaN bad inputs, If the value type is object get the x or y based on whether we are horizontal or not
getRightValue: function(rawValue) {
// Float-bar support. Check if array so be aware of min/max Y values.
if (helpers.isArray(rawValue)) {
rawValue = rawValue[1];
benmccann marked this conversation as resolved.
Show resolved Hide resolved
}
// Null and undefined values first
if (helpers.isNullOrUndef(rawValue)) {
return NaN;
Expand Down
8 changes: 7 additions & 1 deletion src/core/core.tooltip.js
Expand Up @@ -68,7 +68,13 @@ defaults._set('global', {
if (label) {
label += ': ';
}
label += tooltipItem.yLabel;
// float-bar support, if y arguments are array tooltip will show bottom and up values
var yValue = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
benmccann marked this conversation as resolved.
Show resolved Hide resolved
if (helpers.isArray(yValue)) {
label += yValue[0] + ' - ' + yValue[1];
} else {
label += tooltipItem.yLabel;
}
return label;
},
labelColor: function(tooltipItem, chart) {
Expand Down
18 changes: 15 additions & 3 deletions src/elements/element.rectangle.js
Expand Up @@ -67,7 +67,12 @@ module.exports = Element.extend({
bottom = vm.base;
signX = 1;
signY = bottom > top ? 1 : -1;
borderSkipped = vm.borderSkipped || 'bottom';
// no border support
if (vm.borderSkipped === null) {
borderSkipped = null;
} else {
borderSkipped = vm.borderSkipped || 'bottom';
}
benmccann marked this conversation as resolved.
Show resolved Hide resolved
} else {
// horizontal bar
left = vm.base;
Expand All @@ -76,7 +81,12 @@ module.exports = Element.extend({
bottom = vm.y + vm.height / 2;
signX = right > left ? 1 : -1;
signY = 1;
borderSkipped = vm.borderSkipped || 'left';
// no border support
if (vm.borderSkipped === null) {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
borderSkipped = null;
} else {
borderSkipped = vm.borderSkipped || 'left';
}
}

// Canvas doesn't allow us to stroke inside the width so we can
Expand Down Expand Up @@ -132,8 +142,10 @@ module.exports = Element.extend({
// Draw rectangle from 'startCorner'
var corner = cornerAt(0);
ctx.moveTo(corner[0], corner[1]);
// float-bar support, let's rectangle allow to have all borders, assign corners_count to 5 instead of 4, so we are allowing to fill all sides.
benmccann marked this conversation as resolved.
Show resolved Hide resolved
var cornersCount = borderSkipped === null ? 4 : 3;

for (var i = 1; i < 4; i++) {
for (var i = 1; i <= cornersCount; i++) {
corner = cornerAt(i);
ctx.lineTo(corner[0], corner[1]);
}
Expand Down
1 change: 0 additions & 1 deletion src/scales/scale.logarithmic.js
Expand Up @@ -161,7 +161,6 @@ module.exports = function(Chart) {
} else if (value > me.max) {
me.max = value;
}

benmccann marked this conversation as resolved.
Show resolved Hide resolved
if (value !== 0 && (me.minNotZero === null || value < me.minNotZero)) {
me.minNotZero = value;
}
Expand Down