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 all 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/charts/bar.md
Original file line number Diff line number Diff line change
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 @@ -108,6 +108,7 @@ In general, this does not need to be changed except when creating chart types
that derive from a bar chart.

Options are:

* `'bottom'`
* `'left'`
* `'top'`
Expand Down Expand Up @@ -194,10 +195,12 @@ Sample: |==============|

## Data Structure

The `data` property of a dataset for a bar chart is specified as 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 @@ -242,7 +245,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
24 changes: 17 additions & 7 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ module.exports = DatasetController.extend({
var dataset = me.getDataset();
var options = me._resolveElementOptions(rectangle, index);

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

rectangle._xScale = me.getScaleForId(meta.xAxisID);
rectangle._yScale = me.getScaleForId(meta.yAxisID);
rectangle._datasetIndex = me.index;
Expand Down Expand Up @@ -285,12 +290,13 @@ module.exports = DatasetController.extend({
var scale = me._getValueScale();
var isHorizontal = scale.isHorizontal();
var datasets = chart.data.datasets;
var value = +scale.getRightValue(datasets[datasetIndex].data[index]);
var value = scale._parseValue(datasets[datasetIndex].data[index]);
var minBarLength = scale.options.minBarLength;
var stacked = scale.options.stacked;
var stack = meta.stack;
var start = 0;
var i, imeta, ivalue, base, head, size;
var start = value.max >= 0 && value.min >= 0 ? value.min : value.max;
var yValue = value.max >= 0 && value.min >= 0 ? value.max - value.min : value.min - value.max;
var i, imeta, ivalue, base, head, size, yStackValue;

if (stacked || (stacked === undefined && stack !== undefined)) {
for (i = 0; i < datasetIndex; ++i) {
Expand All @@ -301,16 +307,18 @@ module.exports = DatasetController.extend({
imeta.controller._getValueScaleId() === scale.id &&
chart.isDatasetVisible(i)) {

ivalue = +scale.getRightValue(datasets[i].data[index]);
if ((value < 0 && ivalue < 0) || (value >= 0 && ivalue > 0)) {
yStackValue = scale._parseValue(datasets[i].data[index]);
ivalue = yStackValue.min >= 0 && yStackValue.max >= 0 ? yStackValue.max : yStackValue.min;

if ((value.min < 0 && ivalue < 0) || (value.max >= 0 && ivalue >= 0)) {
start += ivalue;
}
}
}
}

base = scale.getPixelForValue(start);
head = scale.getPixelForValue(start + value);
head = scale.getPixelForValue(start + yValue);
size = head - base;

if (minBarLength !== undefined && Math.abs(size) < minBarLength) {
Expand Down Expand Up @@ -365,8 +373,10 @@ module.exports = DatasetController.extend({

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 val = scale._parseValue(dataset.data[i]);
if (!isNaN(val.start) && !isNaN(val.end)) {
rects[i].draw();
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,12 @@ module.exports = Element.extend({
if ((typeof rawValue === 'number' || rawValue instanceof Number) && !isFinite(rawValue)) {
return NaN;
}

// Float-bar support. Handling arrays
if (helpers.isArray(rawValue)) {
return [this.getRightValue(rawValue[0]), this.getRightValue(rawValue[1])];
Copy link
Member

Choose a reason for hiding this comment

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

This also allows data like [[{x: 1, y: 1}, {x: 2, y: 2}], [1, 2], 3], but I guess its ok (and maybe usable in another chart type)

}

// If it is in fact an object, dive in one more level
if (rawValue) {
if (this.isHorizontal()) {
Expand All @@ -537,6 +543,38 @@ module.exports = Element.extend({
return rawValue;
},

_parseValue: function(raw) {
var value = +this.getRightValue(raw);
var start, end, isarr;

if (helpers.isArray(value)) {
start = value[0];
end = value[1];
isarr = true;
} else {
start = 0;
end = value;
isarr = false;
}

return {
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 calculating size (in pixels?) already here

min: Math.min(start, end),
max: Math.max(start, end),
start: start,
end: end,
isarr: isarr
};
},

getScaleLabel: function(rawValue) {
var v = this._parseValue(rawValue);
if (v.issar === true) {
return v.min === v.max ? v.min : v.min + ' ; ' + v.max;
}

return +this.getRightValue(rawValue);
},

/**
* Used to get the value to display in the tooltip for the data at the given index
* @param index
Expand Down
11 changes: 9 additions & 2 deletions src/elements/element.rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ module.exports = Element.extend({
// Find first (starting) corner with fallback to 'bottom'
var borders = ['bottom', 'left', 'top', 'right'];
var startCorner = borders.indexOf(borderSkipped, 0);
if (startCorner === -1) {
if (borderSkipped === null) {
startCorner = 0;
} else {
startCorner = borders.indexOf(borderSkipped, 0);
if (startCorner === -1) {
startCorner = 0;
}
}

function cornerAt(index) {
Expand All @@ -135,7 +140,9 @@ module.exports = Element.extend({
var corner = cornerAt(0);
ctx.moveTo(corner[0], corner[1]);

for (var i = 1; i < 4; i++) {
var cornersCount = borderSkipped === null ? 4 : 3;

for (var i = 1; i <= cornersCount; i++) {
corner = cornerAt(i);
ctx.lineTo(corner[0], corner[1]);
}
Expand Down
20 changes: 10 additions & 10 deletions src/platforms/platform.dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ var supportsEventListenerOptions = (function() {
// https://github.com/chartjs/Chart.js/issues/4287
var eventListenerOptions = supportsEventListenerOptions ? {passive: true} : false;

function addEventListener(node, type, listener) {
function addListener(node, type, listener) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why cbb7ff7 is showing up on this PR. Seems the rebase didn't totally work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

when i has all that failing tests i thought that smth really bad went with merge last time. So, i downloaded lates Chart.js files, replaced with it all my local files and commited to my fork. So, in this case my repo should look like the current Chart.js version. So, all that changes came when i replaced files. I have put back my changes again on float-bat support after. Are thise chanages on this file are not correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i have replaced all files from current chart.js version. Is this file is not correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think because this was commited only 2 days ago thats why i picked it

Copy link
Contributor

Choose a reason for hiding this comment

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

The changes shouldn't appear on your PR. The problem is that your PR is comparing to a version of the code from a few days ago probably instead of to the latest master. You should run git rebase master again (and maybe make a backup copy first before doing that)

It's probably really hard to rebase with 82 commits spanning a year on the PR. It's possible you might have an easier time opening a new PR at this point rather than rebasing

Copy link
Member

@simonbrunel simonbrunel Feb 8, 2019

Choose a reason for hiding this comment

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

... or you can squash all your 82 commits in a single one, and rebase it on top of master. Then we can keep working in the same PR with all previous comments.

Copy link
Member

Choose a reason for hiding this comment

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

@gwyneblaidd if you need help squashing / rebasing this PR, please join our Slack #dev channel. But please don't open a new PR with these 82 commits, it will be useless.

Copy link
Member

Choose a reason for hiding this comment

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

Forget my comment about squashing commits, I didn't realize you already merged many times our master into your branch, which makes the operation quite complicated, so it would be easier to start a new PR from scratch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rebased PR - #6056

node.addEventListener(type, listener, eventListenerOptions);
}

function removeEventListener(node, type, listener) {
function removeListener(node, type, listener) {
node.removeEventListener(type, listener, eventListenerOptions);
}

Expand Down Expand Up @@ -223,8 +223,8 @@ function createResizer(handler) {
handler();
};

addEventListener(expand, 'scroll', onScroll.bind(expand, 'expand'));
addEventListener(shrink, 'scroll', onScroll.bind(shrink, 'shrink'));
addListener(expand, 'scroll', onScroll.bind(expand, 'expand'));
addListener(shrink, 'scroll', onScroll.bind(shrink, 'shrink'));

return resizer;
}
Expand All @@ -239,7 +239,7 @@ function watchForRender(node, handler) {
};

helpers.each(ANIMATION_START_EVENTS, function(type) {
addEventListener(node, type, proxy);
addListener(node, type, proxy);
});

// #4737: Chrome might skip the CSS animation when the CSS_RENDER_MONITOR class
Expand All @@ -258,7 +258,7 @@ function unwatchForRender(node) {

if (proxy) {
helpers.each(ANIMATION_START_EVENTS, function(type) {
removeEventListener(node, type, proxy);
removeListener(node, type, proxy);
});

delete expando.renderProxy;
Expand Down Expand Up @@ -429,7 +429,7 @@ module.exports = {
listener(fromNativeEvent(event, chart));
};

addEventListener(canvas, type, proxy);
addListener(canvas, type, proxy);
},

removeEventListener: function(chart, type, listener) {
Expand All @@ -447,7 +447,7 @@ module.exports = {
return;
}

removeEventListener(canvas, type, proxy);
removeListener(canvas, type, proxy);
}
};

Expand All @@ -462,7 +462,7 @@ module.exports = {
* @todo remove at version 3
* @private
*/
helpers.addEvent = addEventListener;
helpers.addEvent = addListener;

/**
* Provided for backward compatibility, use EventTarget.removeEventListener instead.
Expand All @@ -473,4 +473,4 @@ helpers.addEvent = addEventListener;
* @todo remove at version 3
* @private
*/
helpers.removeEvent = removeEventListener;
helpers.removeEvent = removeListener;
40 changes: 26 additions & 14 deletions src/scales/scale.linear.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,25 @@ module.exports = LinearScaleBase.extend({

if (chart.isDatasetVisible(datasetIndex) && IDMatches(meta)) {
helpers.each(dataset.data, function(rawValue, index) {
var value = +me.getRightValue(rawValue);
if (isNaN(value) || meta.data[index].hidden) {
var value = me._parseValue(rawValue);

if (isNaN(value.min) || isNaN(value.max) || meta.data[index].hidden) {
return;
}

positiveValues[index] = positiveValues[index] || 0;
negativeValues[index] = negativeValues[index] || 0;

if (value.min === 0 && !opts.ticks.beginAtZero) {
value.min = value.max;
}

if (opts.relativePoints) {
positiveValues[index] = 100;
} else if (value < 0) {
negativeValues[index] += value;
} else if (value.min < 0 || value.max < 0) {
negativeValues[index] += value.min;
} else {
positiveValues[index] += value;
positiveValues[index] += value.max;
}
});
}
Expand All @@ -102,21 +107,28 @@ module.exports = LinearScaleBase.extend({
var meta = chart.getDatasetMeta(datasetIndex);
if (chart.isDatasetVisible(datasetIndex) && IDMatches(meta)) {
helpers.each(dataset.data, function(rawValue, index) {
var value = +me.getRightValue(rawValue);
if (isNaN(value) || meta.data[index].hidden) {
var value = me._parseValue(rawValue);

if (isNaN(value.min) || isNaN(value.max) || meta.data[index].hidden) {
return;
}

if (value.isarr === false) {
value.min = value.end;
value.max = value.end;
}


if (me.min === null) {
me.min = value;
} else if (value < me.min) {
me.min = value;
me.min = value.min;
} else if (value.min < me.min) {
me.min = value.min;
}

if (me.max === null) {
me.max = value;
} else if (value > me.max) {
me.max = value;
me.max = value.max;
} else if (value.max > me.max) {
me.max = value.max;
}
});
}
Expand Down Expand Up @@ -151,7 +163,7 @@ module.exports = LinearScaleBase.extend({
},

getLabelForIndex: function(index, datasetIndex) {
return +this.getRightValue(this.chart.data.datasets[datasetIndex].data[index]);
return this.getScaleLabel(this.chart.data.datasets[datasetIndex].data[index]);
},

// Utils
Expand Down
Loading