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

Make line properties scriptable #6128

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6cb39a6
Initial line controller scriptable options.
janelledement Mar 7, 2019
9736257
Adds backgroundColor tests.
janelledement Mar 7, 2019
fc9a2d7
Slight change to line-indexable backgroundColor.
janelledement Mar 7, 2019
d9732bc
Adds tests for borderColor
janelledement Mar 7, 2019
47604a8
Adds tests for borderWidth.
janelledement Mar 7, 2019
964edf6
Adds tests for boderCapStyle and a minor change to borderWidth test.
janelledement Mar 8, 2019
5a1d7d8
Gets rid of indexable capability and tests
janelledement Mar 8, 2019
3dd557b
Adds remaining tests (borderDash, etc)
janelledement Mar 10, 2019
cadedf0
Updates line doc
janelledement Mar 10, 2019
5bbcb7a
Updates scriptable line sample
janelledement Mar 10, 2019
99ef10e
Updates 'should ignore invalid values' (fill test)
janelledement Mar 10, 2019
910e110
Gets rid of unnecessary LINE_OPTIONS var
janelledement Mar 11, 2019
d6ef1f0
Adds testing adjustment
janelledement Mar 12, 2019
7bc4239
Updates test naming convention
janelledement Mar 12, 2019
c20f270
Addresses root cause of fail tests
janelledement Mar 14, 2019
3281f89
Removes a comment
janelledement Mar 14, 2019
fd338d7
Improves clarity of borderCapStyle tests/pngs.
janelledement Mar 17, 2019
219d84b
Makes minor changes to line controller
janelledement Mar 17, 2019
5103d26
Makes minor changes to scriptable sample chart
janelledement Mar 17, 2019
91e6228
Improves data for cubicInterpolationMode tests
janelledement Mar 17, 2019
40c602a
Adds padding and eliminates use of reduce in tests
janelledement Mar 17, 2019
6f9429e
Checks for ctx.dataIndex on appropriate tests
janelledement Mar 17, 2019
76b9d09
Orders line options in tests alphabetically
janelledement Mar 17, 2019
1eb2a2c
Fixes minor lint errors
janelledement Mar 17, 2019
bfcdd79
Gets rid of grey color fill in borderCapStyle test
janelledement Mar 18, 2019
d2f8173
Keeps borderColor fallback in elements.point
janelledement Mar 18, 2019
d9d1616
Adds fallback point borderColor/Width
janelledement Mar 18, 2019
54a0296
Makes adjustments to borderColor scriptable test
janelledement Mar 18, 2019
2a673d9
Improves clarity for borderWidth scriptable test
janelledement Mar 19, 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
18 changes: 9 additions & 9 deletions docs/charts/line.md
Expand Up @@ -43,15 +43,15 @@ The line chart allows a number of properties to be specified for each dataset. T

| Name | Type | [Scriptable](../general/options.md#scriptable-options) | [Indexable](../general/options.md#indexable-options) | Default
| ---- | ---- | :----: | :----: | ----
| [`backgroundColor`](#line-styling) | [`Color`](../general/colors.md) | - | - | `'rgba(0, 0, 0, 0.1)'`
| [`borderCapStyle`](#line-styling) | `string` | - | - | `'butt'`
| [`borderColor`](#line-styling) | [`Color`](../general/colors.md) | - | - | `'rgba(0, 0, 0, 0.1)'`
| [`borderDash`](#line-styling) | `number[]` | - | - | `[]`
| [`borderDashOffset`](#line-styling) | `number` | - | - | `0.0`
| [`borderJoinStyle`](#line-styling) | `string` | - | - | `'miter'`
| [`borderWidth`](#line-styling) | `number` | - | - | `3`
| [`cubicInterpolationMode`](#cubicinterpolationmode) | `string` | - | - | `''`
| [`fill`](#line-styling) | <code>boolean&#124;string</code> | - | - | `true`
| [`backgroundColor`](#line-styling) | [`Color`](../general/colors.md) | Yes | - | `'rgba(0, 0, 0, 0.1)'`
| [`borderCapStyle`](#line-styling) | `string` | Yes | - | `'butt'`
| [`borderColor`](#line-styling) | [`Color`](../general/colors.md) | Yes | - | `'rgba(0, 0, 0, 0.1)'`
| [`borderDash`](#line-styling) | `number[]` | Yes | - | `[]`
| [`borderDashOffset`](#line-styling) | `number` | Yes | - | `0.0`
| [`borderJoinStyle`](#line-styling) | `string` | Yes | - | `'miter'`
| [`borderWidth`](#line-styling) | `number` | Yes | - | `3`
| [`cubicInterpolationMode`](#cubicinterpolationmode) | `string` | Yes | - | `''`
| [`fill`](#line-styling) | <code>boolean&#124;string</code> | Yes | - | `true`
| [`label`](#general) | `string` | - | - | `''`
| [`lineTension`](#line-styling) | `number` | - | - | `0.4`
| [`pointBackgroundColor`](#point-styling) | `Color` | Yes | Yes | `'rgba(0, 0, 0, 0.1)'`
Expand Down
20 changes: 11 additions & 9 deletions samples/scriptable/line.html
Expand Up @@ -26,13 +26,18 @@

utils.srand(110);

function getLineColor(ctx) {
var newColor = utils.color(ctx.datasetIndex);
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
return newColor;
}

function alternatePointStyles(ctx) {
var index = ctx.dataIndex;
return index % 2 === 0 ? 'circle' : 'rect';
}

function makeHalfAsOpaque(ctx) {
var c = ctx.dataset.backgroundColor;
var c = getLineColor(ctx);
return utils.transparentize(c);
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -56,9 +61,7 @@
var data = {
labels: utils.months({count: DATA_COUNT}),
datasets: [{
data: generateData(),
backgroundColor: '#4dc9f6',
borderColor: '#4dc9f6',
data: generateData()
}]
};

Expand All @@ -68,8 +71,11 @@
elements: {
line: {
fill: false,
backgroundColor: getLineColor,
borderColor: getLineColor,
},
point: {
backgroundColor: getLineColor,
hoverBackgroundColor: makeHalfAsOpaque,
radius: adjustRadiusBasedOnData,
pointStyle: alternatePointStyles,
Expand All @@ -87,12 +93,8 @@

// eslint-disable-next-line no-unused-vars
function addDataset() {
var newColor = utils.color(chart.data.datasets.length);

chart.data.datasets.push({
data: generateData(),
backgroundColor: newColor,
borderColor: newColor
data: generateData()
});
chart.update();
}
Expand Down
15 changes: 12 additions & 3 deletions src/controllers/controller.line.js
Expand Up @@ -179,17 +179,26 @@ module.exports = DatasetController.extend({
_resolveLineOptions: function(element) {
var me = this;
var chart = me.chart;
var dataset = chart.data.datasets[me.index];
var datasetIndex = me.index;
var dataset = chart.data.datasets[datasetIndex];
var custom = element.custom || {};
var options = chart.options;
var elementOptions = options.elements.line;
var values = {};
var i, ilen, key;

// Scriptable options
var context = {
chart: chart,
dataIndex: undefined,
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
dataset: dataset,
datasetIndex: datasetIndex
};

var keys = [
'backgroundColor',
'borderWidth',
'borderColor',
'borderWidth',
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
'borderCapStyle',
'borderDash',
'borderDashOffset',
Expand All @@ -204,7 +213,7 @@ module.exports = DatasetController.extend({
custom[key],
dataset[key],
elementOptions[key]
]);
], context);
}

// The default behavior of lines is to break at null values, according
Expand Down
33 changes: 17 additions & 16 deletions test/fixtures/controller.line/backgroundColor/scriptable.js
Expand Up @@ -6,18 +6,19 @@ module.exports = {
datasets: [
{
// option in dataset
data: [0, 5, 10, null, -10, -5],
pointBackgroundColor: function(ctx) {
var value = ctx.dataset.data[ctx.dataIndex] || 0;
return value > 8 ? '#ff0000'
: value > 0 ? '#00ff00'
: value > -8 ? '#0000ff'
: '#ff00ff';
data: [4, 5, 10, null, -10, -5],
backgroundColor: function(ctx) {
var dataTotal = ctx.dataset.data.reduce(function(a, b) {
return a + b;
});
var c = dataTotal >= 0 ? '#D60000' : '#44DE28';
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved

return c;
}
},
{
// option in element (fallback)
data: [4, -5, -10, null, 10, 5],
data: [-4, -5, -10, null, 10, 5],
}
]
},
Expand All @@ -26,16 +27,16 @@ module.exports = {
title: false,
elements: {
line: {
fill: false,
},
point: {
backgroundColor: function(ctx) {
var value = ctx.dataset.data[ctx.dataIndex] || 0;
return value > 8 ? '#ff00ff'
: value > 0 ? '#0000ff'
: value > -8 ? '#ff0000'
: '#00ff00';
var dataTotal = ctx.dataset.data.reduce(function(a, b) {
return a + b;
});
var c = dataTotal >= 0 ? '#D60000' : '#44DE28';

return c;
},
},
point: {
radius: 10,
}
},
Expand Down
Binary file modified test/fixtures/controller.line/backgroundColor/scriptable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions test/fixtures/controller.line/backgroundColor/value.js
Expand Up @@ -7,7 +7,7 @@ module.exports = {
{
// option in dataset
data: [0, 5, 10, null, -10, -5],
pointBackgroundColor: '#ff0000'
backgroundColor: '#ff0000'
},
{
// option in element (fallback)
Expand All @@ -20,11 +20,10 @@ module.exports = {
title: false,
elements: {
line: {
fill: false,
backgroundColor: '#00ff00'
},
point: {
backgroundColor: '#00ff00',
radius: 10,
radius: 10
}
},
scales: {
Expand Down
Binary file modified test/fixtures/controller.line/backgroundColor/value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions test/fixtures/controller.line/borderCapStyle/scriptable.js
@@ -0,0 +1,64 @@
module.exports = {
config: {
type: 'line',
data: {
labels: [0, 1, 2, 3, 4, 5],
datasets: [
{
// option in dataset
data: [4, 5, 10, null, -10, -5],
borderCapStyle: function(ctx) {
var dataTotal = ctx.dataset.data.reduce(function(a, b) {
return a + b;
});
var style = dataTotal > 0 ? 'round' : 'square';

return style;
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
}
},
{
// option in element (fallback)
data: [-4, -5, -10, null, 10, 5],
}
]
},
options: {
legend: false,
title: false,
elements: {
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
line: {
borderColor: '#D60000',
borderWidth: 10,
borderCapStyle: function(ctx) {
var dataTotal = ctx.dataset.data.reduce(function(a, b) {
return a + b;
});
var style = dataTotal > 0 ? 'round' : 'square';

return style;
}
},
point: {
radius: 10,
}
},
scales: {
xAxes: [{display: false}],
yAxes: [
{
display: false,
ticks: {
beginAtZero: true
}
}
]
}
}
},
options: {
canvas: {
height: 256,
width: 512
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions test/fixtures/controller.line/borderCapStyle/value.js
@@ -0,0 +1,44 @@
module.exports = {
config: {
type: 'line',
data: {
labels: [0, 1, 2, 3, 4, 5],
datasets: [
{
// option in dataset
data: [0, 5, 10, null, -10, -5],
borderCapStyle: 'round',
},
{
// option in element (fallback)
data: [4, -5, -10, null, 10, 5],
}
]
},
options: {
legend: false,
title: false,
elements: {
line: {
borderColor: '#00ff00',
borderCapStyle: 'butt',
borderWidth: 10,
fill: false,
},
point: {
radius: 10,
}
},
scales: {
xAxes: [{display: false}],
yAxes: [{display: false}]
}
}
},
options: {
canvas: {
height: 256,
width: 512
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 17 additions & 15 deletions test/fixtures/controller.line/borderColor/scriptable.js
Expand Up @@ -6,18 +6,19 @@ module.exports = {
datasets: [
{
// option in dataset
data: [0, 5, 10, null, -10, -5],
pointBorderColor: function(ctx) {
var value = ctx.dataset.data[ctx.dataIndex] || 0;
return value > 8 ? '#ff0000'
: value > 0 ? '#00ff00'
: value > -8 ? '#0000ff'
: '#ff00ff';
data: [4, 5, 10, null, -10, -5],
borderColor: function(ctx) {
var dataTotal = ctx.dataset.data.reduce(function(a, b) {
return a + b;
});
var c = dataTotal > 0 ? '#D60000' : '#44DE28';

return c;
}
},
{
// option in element (fallback)
data: [4, -5, -10, null, 10, 5],
data: [-4, -5, -10, null, 10, 5],
}
]
},
Expand All @@ -27,15 +28,16 @@ module.exports = {
elements: {
line: {
fill: false,
},
point: {
borderColor: function(ctx) {
var value = ctx.dataset.data[ctx.dataIndex] || 0;
return value > 8 ? '#ff00ff'
: value > 0 ? '#0000ff'
: value > -8 ? '#ff0000'
: '#00ff00';
var dataTotal = ctx.dataset.data.reduce(function(a, b) {
return a + b;
});
var c = dataTotal > 0 ? '#D60000' : '#44DE28';

return c;
},
},
point: {
radius: 10,
}
},
Expand Down
Binary file modified test/fixtures/controller.line/borderColor/scriptable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/fixtures/controller.line/borderColor/value.js
Expand Up @@ -7,7 +7,7 @@ module.exports = {
{
// option in dataset
data: [0, 5, 10, null, -10, -5],
pointBorderColor: '#ff0000'
borderColor: '#ff0000'
},
{
// option in element (fallback)
Expand All @@ -20,10 +20,10 @@ module.exports = {
title: false,
elements: {
line: {
borderColor: '#00ff00',
fill: false,
},
point: {
borderColor: '#00ff00',
simonbrunel marked this conversation as resolved.
Show resolved Hide resolved
radius: 10,
}
},
Expand Down
Binary file modified test/fixtures/controller.line/borderColor/value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.