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

Introduce scriptable options (bubble chart) #4671

Merged
merged 3 commits into from Aug 24, 2017

Conversation

simonbrunel
Copy link
Member

New options.resolve helper that determines the final value to use from an array of input values (fallback) and a given context and/or index. For now, only the bubble chart support scriptable options, see documentation for details (sample)

Add scriptable options documentation and update the bubble chart dataset properties table with their scriptable and indexable capabilities and default values. Also move point style description under the element configuration section.

Replaces #4664 and #4665
Enhances #3355
Fixes #4656

New `options.resolve` helper that determines the final value to use from an array of input values (fallback) and a given context and/or index. For now, only the bubble chart support scriptable options, see documentation for details.

Add scriptable options documentation and update the bubble chart dataset properties table with their scriptable and indexable capabilities and default values. Also move point style description under the element configuration section.
Example:

```javascript
color: function(context) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I initially implemented scriptable options as a function taking 2 arguments: an index and the context (see the datalabels plugin) - index being the dataset index if the option is resolved for a dataset element (line of a line chart) or the data index if the option is for a data element (point of a line chart).

Having the index as first argument would allow to integrate with external functions (e.g. returning a color for a given index (return index % size)). After discussing with @etimberg, we agreed to remove that first argument and keep only the context.

Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

To illustrate my previous message: let's say a third party library provides the following function:

function colorize(i) {
	return color_palette[i % color_palette.length];
}

Then, if index is provided, it's easy to plug an option to that function:

point: {
    color: colorize
}

Instead of:

point: {
    color: function(context) {
        return colorize(context.dataIndex);
    }
}

However, not sure how this use case is common or will work as-is most of the time, and having this first index argument might be burden later if we realize it's more confusing than helpful. So I think function(context) is better, just want to be sure we are all on the same page.

- `dataIndex`: index of the current data
- `dataCount`: number of data
- `data`: value of the current data
- `chart`: the associated chart
Copy link
Member Author

Choose a reason for hiding this comment

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

Since this is part of the public API, we need to be sure we want to expose all these properties.

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

I'm OK with these properties

Copy link
Member Author

Choose a reason for hiding this comment

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

At least, we need to expose chart, datasetIndex and dataIndex properties.

The other values can be retrieved as follow:

  • datasetCount: chart.data.datasets.length
  • dataset: chart.data.datasets[datasetIndex]
  • dataCount: chart.data.datasets[datasetIndex].data.length
  • data: chart.data.datasets[datasetIndex].data[dataIndex]

dataset is always needed to resolve options (since it contains some of them), so it would not cost more to include it to the context. I think most use cases of scriptable options will be to derive the data value, however data is (almost) never required to resolve options, so it's an additional cost to expose it to the context. I'm really not sure about exposing (right now) datasetCount and dataCount.

If we expose only chart, datasetIndex, dataIndex and dataset:

  • datasetCount: chart.data.datasets.length
  • dataCount: dataset.data.length
  • data: dataset.data[dataIndex]

Copy link
Member

Choose a reason for hiding this comment

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

I think chart, datasetIndex, dataIndex and dataset is the minimum. The only reason to include the other properties is to save users code if they want to get those values.

I'm also OK with the minimal set of properties to keep our code smaller.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can start with the minimal set and add more if requested. Like that we limit probabilities of later deprecations.

- `dataset`: dataset at index `datasetIndex`
- `dataIndex`: index of the current data
- `dataCount`: number of data
- `data`: value of the current data
Copy link
Member Author

Choose a reason for hiding this comment

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

data or value? (I picked data to be consistent with dataIndex and dataCount)

Copy link
Member

Choose a reason for hiding this comment

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

I like data

Copy link
Member Author

Choose a reason for hiding this comment

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

I asked because data can be confusing: could be chart.data, dataset.data or dataset.data[i]

Copy link
Member

Choose a reason for hiding this comment

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

yup, I was thinking about that more. maybe change it to dataValue?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will remove it to expose the minimal set.

Copy link
Member

@etimberg etimberg left a comment

Choose a reason for hiding this comment

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

Had a first pass look at this. No glaring issues. Will have a more indepth look on the weekend

- `dataset`: dataset at index `datasetIndex`
- `dataIndex`: index of the current data
- `dataCount`: number of data
- `data`: value of the current data
Copy link
Member

Choose a reason for hiding this comment

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

I like data

- `dataIndex`: index of the current data
- `dataCount`: number of data
- `data`: value of the current data
- `chart`: the associated chart
Copy link
Member

Choose a reason for hiding this comment

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

I'm OK with these properties

y: 10,
r: 1
describe('Interactions', function() {
function triggerElementEvent(chart, type, el) {
Copy link
Member

Choose a reason for hiding this comment

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

can probably move this out into a helper once this is merged. I think similar code is used in a number of tests

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, I moved it under the jasmine namespace

},

radius: function(context) {
var data = context.data;
var value = context.dataset.data[context.dataIndex];
Copy link
Member Author

Choose a reason for hiding this comment

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

@etimberg this is the way how to read the current value with the minimal context. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Looks good 👍

@simonbrunel simonbrunel added this to the Version 2.7 milestone Aug 23, 2017
@simonbrunel simonbrunel merged commit 872dfec into chartjs:master Aug 24, 2017
@simonbrunel simonbrunel deleted the bubble-scriptable branch August 24, 2017 07:34
yofreke pushed a commit to yofreke/Chart.js that referenced this pull request Dec 30, 2017
New `options.resolve` helper that determines the final value to use from an array of input values (fallback) and a given context and/or index. For now, only the bubble chart support scriptable options, see documentation for details.

Add scriptable options documentation and update the bubble chart dataset properties table with their scriptable and indexable capabilities and default values. Also move point style description under the element configuration section.
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
New `options.resolve` helper that determines the final value to use from an array of input values (fallback) and a given context and/or index. For now, only the bubble chart support scriptable options, see documentation for details.

Add scriptable options documentation and update the bubble chart dataset properties table with their scriptable and indexable capabilities and default values. Also move point style description under the element configuration section.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Bubble charts do not allow nulls in data values
3 participants