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

[Chartjs] Improve Y axis formatting example #1837

Merged
merged 1 commit into from May 15, 2024
Merged

Conversation

hellomedia
Copy link
Contributor

@hellomedia hellomedia commented May 9, 2024

Q A
Bug fix? misleading doc fix
New feature? no
Issues see description
License MIT

Code example in the doc does not just format Y axis, it overrides existing scales configuration.

// For instance you can format Y axis
event.detail.config.options.scales = {
    y: {
        ticks: {
            callback: function (value, index, values) {
                /* ... */
            },
        },
    },
};

This code formats the y-axis and keeps an existing scales config untouched, but requires an existing Y axis config.

// For instance you can format Y axis
event.detail.config.options.scales.y.ticks = {
    callback: function (value, index, values) {
        /* ... */
    }
};

@hellomedia hellomedia changed the title Update index.rst Fix Y axis formatting example May 9, 2024
@hellomedia hellomedia changed the title Fix Y axis formatting example [Chartjs] Fix Y axis formatting example May 9, 2024
@smnandre
Copy link
Collaborator

smnandre commented May 9, 2024

Does this work when no configuration has been set previously ?

@smnandre smnandre added documentation Improvements or additions to documentation Chartjs labels May 9, 2024
@hellomedia
Copy link
Contributor Author

hellomedia commented May 10, 2024

Yes, it works. It "works" all the time, just overrides your scales config.

In my case, I lost stacked: true from my scales config. Makes sense when you realize you are overriding the config, but it's not super obvious if you don't pay attention, and the code comment // For instance you can format Y axis feels misleading to me.

@smnandre
Copy link
Collaborator

Yes, it works. It "works" all the time, just overrides your scales config.

So i checked.. and it "does not".

As expected, if config.options.scales is not previously defined, trying to write event.detail.config.options.scales.y throws an error.

as-expected

So i think the current doc is simpler and safer.

@hellomedia
Copy link
Contributor Author

You are right! Sorry about that, I overlooked this.

So maybe would modifying the comment be justified ?

I just tested the code below which achieves the desired behaviour - adds the config option without overriding things - but I am not sure if it's within the scope of the doc.

import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
    connect() {
        // Using bind(this) to preserve context
        this.element.addEventListener('chartjs:pre-connect', this._onPreConnect.bind(this));
    }

   ...

    _onPreConnect(event) {

        // For instance you can format Y axis
        // event.detail.config.options.scales = {} overrides existing config
        // and event.detail.config.options.scales.y.ticks = {} does not work is there is no existing scales config

        this.setDeepProperty(event.detail.config.options, ["scales", "y", "ticks"], {
            callback: function (value, index, values) {
                /* ... */
            }
        });
    }
    
    // Safely add deep JS property
    setDeepProperty(obj, path, value) {
        if (!Array.isArray(path) || path.length === 0) {
            throw new Error("Path must be a non-empty array of keys");
        }

        let current = obj;

        // Navigate through the path, creating objects as needed
        for (let i = 0; i < path.length - 1; i++) {
            const key = path[i];

            if (!current[key] || typeof current[key] !== 'object') {
                current[key] = {};
            }

            current = current[key];
        }

        // Set the final value at the end of the path
        const lastKey = path[path.length - 1];
        current[lastKey] = value;
    }
}

@smnandre
Copy link
Collaborator

Yep that'd be cool !

@hellomedia
Copy link
Contributor Author

hellomedia commented May 11, 2024

Would you like me to add the setDeepPropery code sample above to the doc, or to just modify the original one-line comment to warn about overriding config ?

@smnandre
Copy link
Collaborator

Oh sorry,

I think we should update the doc to avoid any bad experience like you had.. but let's be more direct.

I'd rather not use the deep property there to focus on the event more than the "structure" thing.

So maybe just a comment, or a second commented line with a comment like "if you already defined ..foo.bar.scales." ?

@hellomedia hellomedia changed the title [Chartjs] Fix Y axis formatting example [Chartjs] Improve Y axis formatting example May 11, 2024
@hellomedia
Copy link
Contributor Author

I have tweaked the doc to try to make things clear while keeping it focused.

Can you check if that looks ok ?

src/Chartjs/doc/index.rst Outdated Show resolved Hide resolved
@hellomedia
Copy link
Contributor Author

hellomedia commented May 12, 2024

To be complete, the wording "... or update an existing Y axis config" still feels a tiny bit misleading. It seems to imply there are only these 2 use cases. There is a third use case: adding a new Y axis config to an existing scales config.

The correct syntax for that use case would be:

event.detail.config.options.scales.y = {
    ticks: {
        callback: function (value, index, values) {
            /* ... */
        },
    },
};

@hellomedia
Copy link
Contributor Author

I have just updated the PR to include the 3 use cases.

It's a bit lengthier, but not too bad.

What do you think ?

@smnandre
Copy link
Collaborator

Well let’s go with this one ! Thank you very much for the time spent :)

@carsonbot carsonbot added the Status: Reviewed Has been reviewed by a maintainer label May 15, 2024
@kbond
Copy link
Member

kbond commented May 15, 2024

This is great Nicolas, thanks for the contribution!

@kbond kbond merged commit 4d609f2 into symfony:2.x May 15, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Chartjs documentation Improvements or additions to documentation Status: Reviewed Has been reviewed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants