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

Fix for #3443 - bar chart click function broken in usage dashboard #3444

Merged

Conversation

gjacob24
Copy link
Contributor

@gjacob24 gjacob24 commented Jul 31, 2024

Fixes #3443

Changes proposed in this PR:

  • getElementAtEvent replaced by getElementsAtEventForMode

@@ -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 */
Copy link
Contributor

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).

Copy link
Contributor Author

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.

Copy link
Contributor

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');

@benjaminfaure benjaminfaure merged commit 96a4f59 into development Nov 18, 2024
9 checks passed
@benjaminfaure benjaminfaure deleted the issue_3443_usage_dashboard_bar_chart_broken branch November 18, 2024 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants