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

Upgraded Chart.js to version 4.4.2 #3233

Merged
merged 27 commits into from
May 30, 2024
Merged

Conversation

harmitgoswami
Copy link
Collaborator

Fix #2939

Upgraded Chart.js dependency to latest version (4.4.2). Previously, Chart.js was included via a Chart.bundle.js static file. Support for this method has ended, so the dependency was included via a CDN. Changed all instances of deprecated functions to the new equivalents according to the migration guide.

image

<!-- Add Chart.js and Moment CDN link -->
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.2/dist/chart.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@^2.30.1"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@^1.0.1"></script>
Copy link
Collaborator

Choose a reason for hiding this comment

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

We try to avoid loading static files from external sources. Please instead include the files in the repo.

<!-- Import Chart.js and Moment.js from static files -->
<script language="javascript" type="text/javascript" src="{{ static('js/lib/chart.umd.min.js') }}"></script>
<script language="javascript" type="text/javascript" src="{{ static('js/lib/moment.min.js') }}"></script>
<script language="javascript" type="text/javascript" src="{{ static('js/lib/chartjs-adapter-moment.min.js') }}"></script>
Copy link
Collaborator

@mathjazz mathjazz May 20, 2024

Choose a reason for hiding this comment

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

This will load all these libs on most of the views, even when they aren't needed.

We should load them, like other static files, through Django pipeline in settings, and only in the views where Chart.js was previously used.

Also, seems like pontoon/base/static/js/lib/chart.umd.js.map is not used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right, I was getting a console error when I didn't include this file through base.html, but now that I've shifted over to local imports it seems this file isn't necessary.

Copy link
Collaborator

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

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

The libraries are now loaded as expected. Nice work!

Please see my comment inline for the Profile chart.

I've noticed that the charts on http://localhost:8000/insights/ don't load.

Also, the Age of unreviewed suggestions and the Time to review pretranslations charts on http://localhost:8000/sl/insights/ shouldn't have legends.

@@ -28,7 +28,7 @@
{% block extend_css %}
{% endblock %}
{% endblock %}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please restore this change.

beginAtZero: true,
maxTicksLimit: 3,
precision: 2,
callback: (value) => nf.format(value),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why the change in the precision and the callback settings?

There are a few things that don't work as expected in the new Profile chart:

  1. The curve doesn't have a background.
  2. The Y scale data is off.
  3. The tooltip: items overlap, the data format is off and there is no border.
  4. The chart is cut-off at the top.
  5. Can we keep the line curved?
before after

Copy link
Collaborator

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

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

I haven't looked at the code yet, but the changes look good. Nice work!

I noticed a few minor things, which should still be changed:

  • On the X scales, there should be no day number (e.g. "Jun" instead of "Jun 1").
  • We don't use a thousands separator on the Y scale ATM. I think it's a good idea to use it, but it should be , rather than . (same as in the tooltip).
  • Borders in bullet points in the tooltips don't look the same as in production - they have a 3Dish look and the first one is bigger than the rest.
Screenshot 2024-05-23 at 16 07 13

@harmitgoswami
Copy link
Collaborator Author

I haven't looked at the code yet, but the changes look good. Nice work!

I noticed a few minor things, which should still be changed:

* On the X scales, there should be no day number (e.g. "Jun" instead of "Jun 1").

* We don't use a thousands separator on the Y scale ATM. I think it's a good idea to use it, but it should be `,` rather than `.` (same as in the tooltip).

* Borders in bullet points in the tooltips don't look the same as in production - they have a 3Dish look and the first one is bigger than the rest.
Screenshot 2024-05-23 at 16 07 13

Thank you! It seems that both the day number and thousands separator styling issues are dependent on browser/location configuration (i.e. the thousands separator is indeed , instead of . and day numbers already appear in production for Firefox v126.0 en-US).

@mathjazz
Copy link
Collaborator

Thank you! It seems that both the day number and thousands separator styling issues are dependent on browser/location configuration (i.e. the thousands separator is indeed , instead of . and day numbers already appear in production for Firefox v126.0 en-US).

True, but I believe the situation is worse than on prod ATM, where e.g. for me all charts show months as "May" rather than "May 1", which is the case in some charts now. E.g. the chart on the /profile page shows months rightfully as "May", whereas the Time to review suggestions chart on /sl/insights shows months as "May 1".

Also, it seems like charts on the /insights page are broken again.

},
tooltipFormat: 'MMM YYYY',
Copy link
Collaborator

Choose a reason for hiding this comment

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

This (and other instances of the same value) should remain MMMM YYYY, which will show the full month name in the tooltip, where we have enough space.

@harmitgoswami
Copy link
Collaborator Author

The decision to switch from Moment.js over to Date-fns.js was mainly in hopes that the date display bug for the x-axis on charts will be resolved. The documentation for Moment.js states that no further features will be added, so it's possible that the visual consistency between that and Chart.js got lost at some point.

Copy link
Collaborator

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

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

Very well done!

All charts work as expected.

Left some comments inline, mostly just to improve the code maintainability.

pontoon/contributors/static/js/profile.js Outdated Show resolved Hide resolved
pontoon/insights/static/js/insights_charts.js Outdated Show resolved Hide resolved
pontoon/insights/static/js/insights_charts.js Show resolved Hide resolved
pontoon/insights/static/js/insights.js Outdated Show resolved Hide resolved
pontoon/insights/static/js/insights.js Show resolved Hide resolved
pontoon/insights/static/js/insights_tab.js Outdated Show resolved Hide resolved
pontoon/insights/static/js/insights_tab.js Show resolved Hide resolved
Copy link
Collaborator

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

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

Excellent work!

Just that minor Arrow function trick and we're good!

Comment on lines 106 to 108
labelColor: (context) => {
return Pontoon.insights.setLabelColor(context);
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can drop the body braces and the word "return":

Suggested change
labelColor: (context) => {
return Pontoon.insights.setLabelColor(context);
},
labelColor: (context) =>
Pontoon.insights.setLabelColor(context),
},

The same applies for all other calls of this function.

Copy link
Collaborator

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

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

\o/

Very well done!

@mathjazz mathjazz merged commit d9679bb into mozilla:main May 30, 2024
6 checks passed
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.

Upgrade Chart.js
2 participants