-
Notifications
You must be signed in to change notification settings - Fork 109
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
Fix for #3443 - bar chart click function broken in usage dashboard #3444
Fix for #3443 - bar chart click function broken in usage dashboard #3444
Conversation
app/javascript/src/usage/index.js
Outdated
@@ -42,11 +42,12 @@ $(() => { | |||
const usersData = JSON.parse($('#users_joined').val()); | |||
if (isObject(usersData)) { | |||
const chart = createChart('#yearly_users', usersData, '', (event) => { | |||
const segment = chart.getElementAtEvent(event)[0]; | |||
const points = chart.getElementsAtEventForMode(event, 'nearest', { intersect: true }, true); | |||
const segment = points[0]; | |||
if (!isUndefined(segment)) { | |||
const target = $('#users_click_target').val(); | |||
/* eslint-disable no-underscore-dangle, no-restricted-globals */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bar chart click function is now working. :) Given these code changes, I think no-underscore-dangle,
can now be removed from eslint-disable
throughout the file (can be double-checked by executing yarn run eslint app/javascript/src/usage/index.js
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Aaron! I didn't know that. I'll create another commit to remove it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem, the fix looks good. :)
It might even be good to do a refactor here? There is a lot of duplication amidst the original code. I just threw it into ChatGPT and got the following suggestion. I didn't really test it though.
// Function to create a chart and handle redirection on click
function createChartWithRedirect(chartElementId, dataElementId, targetElementId) {
if (!isUndefined($(dataElementId).val())) {
const data = JSON.parse($(dataElementId).val());
if (isObject(data)) {
const chart = createChart(chartElementId, data, '', (event) => {
const points = chart.getElementsAtEventForMode(event, 'nearest', { intersect: true }, true);
const segment = points[0];
if (!isUndefined(segment)) {
const target = $(targetElementId).val();
/* eslint-disable no-restricted-globals */
const label = chart.data.labels[segment.index];
$(location).attr('href', `${target}?${labelToUrl(label)}`);
/* eslint-enable no-restricted-globals */
}
});
}
}
}
// Create the Users joined chart
createChartWithRedirect('#yearly_users', '#users_joined', '#users_click_target');
// Create the Plans created chart
createChartWithRedirect('#yearly_plans', '#plans_created', '#plans_click_target');
Fixes #3443
Changes proposed in this PR:
getElementAtEvent
replaced bygetElementsAtEventForMode