question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Types appear to be wrong for scriptable options

See original GitHub issue

Expected Behavior

I am using a function as the value of backgroundColor on a bar chart. The allowed types should include a function that returns a color.

Current Behavior

The allowed types do not include a funciton:

Type '(context: any, options: any) => string' is not assignable to type 'string | _DeepPartialObject<CanvasGradient> | _DeepPartialObject<CanvasPattern> | undefined'.

Steps to Reproduce

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgYQBYENYBo5QKYDmwAzjHlOgEYA2exOamMyEAdgGbAECuFMwbBhlgARdDHRwAvnHZQIIOAHIAxsJgA6AFbElAbgBQRlW1JwCeZm04E4AXjgAKAJQAuFOpYcuvcQNYAPEqUmEoAfPYRCAZwsbiWvKyIMXGpMACeYHjuwaFYKamxEGD8pu7RhXEhKgDWBPLcrAAmLNTQ7iasZAAe8HZRBZVx+DCJygDE7FNKg6lS+UPSg1IGUkA

import { ChartConfiguration } from 'chart.js';

const getConfig = (): ChartConfiguration<'bar'> => {
    return {
        type: 'bar',
        options: {
        backgroundColor: context => {
            return '#fff'
        },
        }
    }
}

Environment

  • Chart.js version: 3.5.0
  • Typescript: 4.3.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
LeeLenaleeecommented, Sep 23, 2021

Mb it indeed is configurable there, in that case it’s indeed an issue with the typing

0reactions
rhysvizcommented, Sep 23, 2021

I am just using options.datasets.bar.backgroundColor instead so I guess this is a bug report rather than a request for help but thank you.

It seems to me that either the types are wrong or the implementation is wrong as it does work i.e. you can set the background color using a backgroundColor function in the options namespace.

I think the docs are a bit confusing if you are not meant to do that. I interpreted the page here (https://www.chartjs.org/docs/latest/charts/bar.html) as saying any of those options were available in any of the listed namespaces.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Incorrect context for plugin options when scriptable #8623
When you are accessing to a plugin (i.e. datalabels) option which has been defined as a callback, by chart.options , for instance clicking...
Read more >
Chart.js how to use scriptable options - Stack Overflow
I am trying to set the xAxis zeroLineColor to "#FFFFFF" and I want to have the Grid line color as "#00FFFF" but as...
Read more >
ScriptableObject behaviour discussion (how Scriptable ...
When creating a ScriptableObject at runtime, it does not appear in the Project's Assets folder, nor does it appear in the Hierarchy.
Read more >
Dynamic point size, labels, & more with Chart.js scriptable ...
I've gotten a couple questions recently about how to dynamically set the size of points for on Chart.js line chart.
Read more >
TypeScript: JavaScript With Syntax For Types.
TypeScript extends JavaScript by adding types to the language. TypeScript speeds up your development experience by catching errors and providing fixes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found