1.0.2 • Published 11 months ago

@hsablonniere/activity-graph v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

activity-graph

A low level and stylable Web Component to display an activity graph.

Demos and screenshots

Here are some demos:

!NOTE

Keep in mind that the themes on the demo page and screenshots are just examples of what can be done. They're not released with this component, you will have to create your own theme.

GitHub

Here's an example of my 2023 GitHub contribution graph.

An activity graph representing a year with weeks as columns. Some days are colored with different tones for indicating contribution intensity. It's the same theme as the one on GitHub.

Serializd

Here's an example of my 2024 Serializd stats graph.

[An activity graph representing a year with weeks as columns. Some days are colored with different tones for indicating how many TV show episodes were watched. It's the same theme as the one on Serializd.]

Monkeytype

Here's an example of my 2024 Monkeytype stats graph.

[An activity graph representing a year with weeks as columns. Some days are colored with different tones for indicating how many tests were done. It's the same theme as the one on Monkeytype.]

Letterboxd

Here's an example of what could be my 2023 Letterboxd stats graph, if they had such a graph.

[An activity graph representing a year with weeks as columns. Some days are colored with different tones for indicating how many movies were watched. It's following the general look and feel of Letterboxd.]

How to install?

Via npm

This component is published on npm. You can "npm install" it in your project with this command:

npm install @hsablonniere/activity-graph

Via CDN

You can also directly use modern CDNs that exposes npm packages like JSDelivr:

<script type="module" src="https://cdn.jsdelivr.net/npm/@hsablonniere/activity-graph/+esm"></script>

or esm.sh:

<script type="module" src="https://esm.sh/@hsablonniere/activity-graph"></script>

How to use?

Use the <activity-graph> custom HTML tag like this:

<activity-graph start-date="2024-01-01" end-date="2024-12-31"></activity-graph>

Attributes / properties

Data

The data can be set as a JavaScript object on the data property, or as JSON on the data attribute. It must respect the following type definition:

// Keys must be YYYY-MM-DD date strings
type ActivityGraphData = Record<string, ActivityGraphDataEntry>;

interface ActivityGraphDataEntry {
  // Used for the day "cell" inner text
  text?: string;
  // Used for the day "cell" `title` attribute, for tooltips and accessibility
  title?: string;
  // Used for the day "cell" `part` attribute, as in CSS shadow part for styling purposes
  parts?: Array<string>;
}

Here's an example:

myActivityGraph.data = {
  '2024-04-01': {
    title: '2 contributions',
    parts: ['level-1'],
  },
  '2024-04-04': {
    title: '27 contributions',
    parts: ['level-4'],
  },
};

Styling

By default, the component does not have any styles. You'll have to rely on the different CSS parts and custom properties to create a theme.

!NOTE

  • You can rely on the fact that the element has a display: grid and use properties like gap directly on it.
  • You can have a look at the demos and their respective themes to get some ideas on how to style the component.

CSS parts

PartDescription
weekday-headerTarget any weekday header
weekday-header--evenTarget even numbered weekday headers
weekday-header--oddTarget odd numbered weekday headers
month-headerTarget any month header
dayTarget any day "cell"

!TIP

If you want to use a CSS shadow part, you'll need the ::part() pseudo element like this:

activity-graph::part(month-header) {
  text-align: center;
}

Custom properties

PropertyDescription
var(--activity-graph-month-gap)Spacing between months, can be any CSS unit, 0 by default

Why this project?

I really like this way of visualizing a whole year of data at once. Watching such graphs on GitHub, Serializd or Monkeytype, I grew a need to have a similar dataviz for other services where I track and log stuffs like Letterboxd or fitbit. I wondered how easy it would be to create my own component and that's how I went down this rabbit whole.

In the end, I restarted from scratch 3 times to try different ideas to handle the dates and the CSS grid. It wasn't a simple problem to tackle but I had lots of fun working on this.

Design decisions

I started the project as a vanilla Web Component, without any dependency. I guess I was wondering how small the final bundle would be, I achied something close to 1.6kb (minified and compressed). My code was not that easy to read and I had no support for properties, just attributes. A few commits later, I quickly realized I was recreating a very dumb, verbose and unefficient version of lit. That's when I decided to add lit as the only dependency. In the end, the component code + lit is 6.4kb (minified and compressed).

I wanted something based on CSS grids without any styles by default so that anyone could reuse it and apply their own theme. At first, you could specify some style properties on the data object but I replaced it with CSS shadow parts to maximize styling in the CSS.

Other similar projects

I created this project without looking at the competition but obviously many smart people tried before me.

Mario Hamann also made a Web Component and his approach (a11y, WASM, SSR...) is very interesting!

And of course, there are plenty of framework specific components for React, Vue, Svelte:

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago

0.3.0

11 months ago

0.2.0

11 months ago

0.1.0

11 months ago