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

Add ChartJS BarCharts to Dashboard "Chart" tab #997

Merged

Conversation

JGreenlee
Copy link
Collaborator

@JGreenlee JGreenlee commented Jun 29, 2023

After deciding to use ChartJS instead of Plotly (e-mission/e-mission-docs#918 (comment)), I re-implemented the charts on the Dashboard.
This solution is temporary; we may only have 1 or 2 production releases with graphs like this. In the next iteration of the Dashboard, these charts will likely be moved out of the 'Chart' tab into a unified view.

I made the decision to show these bar charts as horizontal instead of vertical. This way, they can grow taller when there is more information to be on the graph. This took a bit of work, but I think it is worth it.

Screen.Recording.2023-06-29.at.12.16.53.PM.mov

TODO:

  • I am still working on axis labels (ie what unit of measurement?)
  • need to add more colors to the palette (currently it repeats the same 4)
  • need to fix the dates (they appear to be off by 1 day)

ChartJS is a modular charting library that only includes basic functionality in the core package.
So there are a couple others we will need too:
 - chartjs-adapter-moment is so that time-based axes can be labeled using time formats resolved from moment.js. Then all we have to do is pass in timestamps and chartJS will figure out how to show them on the axes.
Fortunately, we already have moment.js so this doesn't incur much package size cost.

 - chartjs-plugin-annotation is how we will display dashed lines on the graphs (to show whether you're meeting a 'target' or goal)
Basically, it just enables drawing extra information on top of the chart.
Our new `BarChart` component is a wrapper around ChartJS's `Bar`. This should make it convenient to draw either vertical or horizontal bar charts, and easily show dashed lines for any number of 'target' values.

This is now demonstrated in the 'Chart' tab of the dashboard, where horizontal bar charts will show for Distance, Trips, Duration, Speed.

metrics.js was tweaked so that the data is formatted for the new BarChart component (and with timestamps on y axis and values on x axis)
This is a fairly minor fix; when we switched internationalization engines from ng-translate to i18next, I changed 'cookies' to use the count parameter but forgot to do the same for icecream and bananas.
@shankari
Copy link
Contributor

need to fix the dates (they appear to be off by 1 day)

Check the timezones.

JGreenlee added 3 commits July 5, 2023 12:17
To represent the dates on the charts, we would like to use unix epoch timestamps, which represent the start of the day in UTC.
However, ChartJS by default uses the user's local time when it parses timestamps. We would like to override this behavior and use UTC, but the Moment.js adapter for ChartJS doesn't support specifying time zones.

So, this is the point at which we need to add Luxon to the project. Then we can use the Luxon adapter for ChartJS, which does support time zones.

We should really be switching from Moment.js to Luxon anyway. We still rely on Moment.js in many other places in the app, so we have to keep both around for now.
Moment.js is very heavy; fortunately Luxon is more lightweight. Before too long, we should be able to remove Moment.js altogether and enjoy a smaller bundle size :)
@JGreenlee JGreenlee marked this pull request as ready for review July 5, 2023 16:19
@JGreenlee
Copy link
Collaborator Author

JGreenlee commented Jul 5, 2023

The "Chart" tab is now working as expected.

image

@shankari Ready for review / merge -- and potentially to be included in the next release?

package.serve.json Show resolved Hide resolved
www/js/components/BarChart.tsx Show resolved Hide resolved
www/js/components/BarChart.tsx Show resolved Hide resolved
www/js/components/BarChart.tsx Show resolved Hide resolved
@shankari
Copy link
Contributor

shankari commented Jul 6, 2023

@JGreenlee I think this is fine to merge as-is given that it is only for a few releases, but wanted to give you a chance to respond if that was helpful.

@shankari
Copy link
Contributor

shankari commented Jul 7, 2023

I'm going to merge this for now but leave the comment about the colors open to revisit later.

@shankari shankari merged commit b0d28d5 into e-mission:framework_migration_prep Jul 7, 2023
@JGreenlee
Copy link
Collaborator Author

ready to merge

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.

2 participants