An open API service indexing awesome lists of open source software.

https://github.com/walterra/d3-milestones

A d3 based timeline visualization.
https://github.com/walterra/d3-milestones

d3js dataviz timeline visualization

Last synced: 6 months ago
JSON representation

A d3 based timeline visualization.

Awesome Lists containing this project

README

          

[![npm](https://img.shields.io/npm/v/d3-milestones.svg?maxAge=2592000)](https://www.npmjs.com/package/d3-milestones)
[![npm](https://img.shields.io/npm/l/d3-milestones.svg?maxAge=2592000)](https://www.npmjs.com/package/d3-milestones)
[![npm](https://img.shields.io/npm/dt/d3-milestones.svg?maxAge=2592000)](https://www.npmjs.com/package/d3-milestones)
[![github ci](https://github.com/walterra/d3-milestones/actions/workflows/ci.yml/badge.svg)](https://github.com/walterra/d3-milestones/actions/workflows/ci.yml)

# d3-milestones

A d3 based timeline visualization.

![The Viking Timeline](https://github.com/walterra/d3-milestones/raw/main/src/stories/assets/vikings.png)

- NPM: https://www.npmjs.com/package/d3-milestones
- Github: https://github.com/walterra/d3-milestones
- Storybook demos: https://walterra.github.io/d3-milestones

If you're using `d3-milestones` out in the wild I'd love to see what you came up with, just ping me on [twitter.com/walterra](https://www.twitter.com/walterra).

## Installing

`yarn add d3-milestones`.

The most quick way to get going is to use `unpkg.com` as a CDN to include the library directly into your HTML file.

```html

milestones('#timeline')
.mapping({
'timestamp': 'year',
'text': 'title'
})
.parseTime('%Y')
.aggregateBy('year')
.render([
{ year: 789, title: 'Vikings begin attacks on England.' },
{ year: 840, title: 'Vikings found Dublin in Ireland.' }
...
{ year: 1050, title: 'The city of Oslo is founded in Norway.' },
{ year: 1066, title: 'Battle of Hastings.' }
]);

```

Head over here to see this example in action: https://beta.observablehq.com/@walterra/vikings-timeline.

## Examples

Examples are included using storybook:

- [Vikings Timeline](https://walterra.github.io/d3-milestones/?path=/story/d3-milestones--vikings)
- [Windows/macOS Timeline](https://walterra.github.io/d3-milestones/?path=/story/d3-milestones--os-category-labels)
- [Ordinal Scale Example](https://walterra.github.io/d3-milestones/?path=/story/d3-milestones--ordinal-scale-example) - Demonstrates using an ordinal scale instead of time scale
- [Ordinal Scale with Categories](https://walterra.github.io/d3-milestones/?path=/story/d3-milestones--ordinal-scale-categories-example) - Shows how to use ordinal scales with multiple categories

## API Reference

### General Usage

To initialize *d3-milestones*, pass a DOM Id to its main factory:

```javascript
const vis = milestones('#wrapper');
```

The returned object exposes the following API:

# vis.aggregateBy(interval)

Sets the aggregation interval for the event data, where *interval* can be one of `second`, `minute`, `hour`, `day`, `week`, `month`, `quarter` or `year`.

# vis.distribution(string)

Sets the label distribution, can be `top-bottom`, `top` or `bottom`. Defaults to `top-bottom`. The options don't change for vertical layouts. `top` refers to labels on the left and `bottom` to labels on the right for that layout.

# vis.mapping(configObject)

Sets overrides for the default attributes for the expected data structure of an event. This defaults to:

```js
{
category: undefined,
entries: undefined,
timestamp: 'timestamp', // Used only for time based scales
value: 'value', // Used only for ordinal scale values
text: 'text',
url: 'url',
id: 'id',
textStyle: undefined, // Field name for custom text styling
categoryStyle: undefined, // Field name for custom category label styling
bulletStyle: undefined // Field name for custom bullet styling
};
```

The method allows you to override single or multiple attributes to map them to fields in your original data with a single call like:

```js
vis.mapping({
'timestamp': 'year',
'text': 'title'
})
```

**Custom Styling Fields:**

- `textStyle`: Maps to a field in your data containing an object with CSS property-value pairs for event text labels.
- `categoryStyle`: Maps to a field in your data containing an object with CSS property-value pairs for category labels.
- `bulletStyle`: Maps to a field in your data containing an object with CSS property-value pairs for bullets (e.g., `background-color`, `border-color`, `padding`, `border-width`).

Example with custom styling:

```js
vis.mapping({
'timestamp': 'year',
'text': 'title',
'bulletStyle': 'bulletStyle',
'categoryStyle': 'categoryStyle'
})
.render([
{
year: 1990,
title: "Custom Red Bullet",
bulletStyle: {
"background-color": "#E12800",
"border-color": "#E12800",
"padding": "10px"
}
}
]);
```

# vis.optimize(boolean)

Enables/Disables the label optimizer. When enabled, the optimizer attempts to avoid label overlap by vertically displacing labels.

# vis.autoResize(boolean)

Enables/Disables auto resizing. Enabled by default, this adds listeners to resizing events of the browser window.

# vis.orientation(string)

Sets the orientation of the timeline, can be either `horizontal` or `vertical`. Defaults to `horizontal`.

# vis.scaleType(string)

Sets the scale type of the timeline, can be either `time` or `ordinal`. Defaults to `time`.

- `time`: Uses a time scale for chronological data with timestamps
- `ordinal`: Uses an ordinal scale for categorical data without timestamps

# vis.parseTime(specifier)

Specifies the formatter for the timestamp field. The specifier string is expected to resemble a format described here: https://github.com/d3/d3-time-format#locale_format

# vis.labelFormat(specifier)

The `labelFormat` for the time label for each milestones defaults to `'%Y-%m-%d %H:%M'`. Using `aggregateBy`, `labelFormat` will be set automatically to a reasonable format corresponding to the aggregation level. Still, this method is available to override this behavior with a custom `labelFormat`.

# vis.urlTarget(string)

Customizes the `target` attribute when labels are provided with a URL. Can be `_blank`, `_self`, `_parent` or `_top`.

# vis.useLabels(boolean)

Enables/Disables the display of labels.

# vis.render([data])

When called without `data` this triggers re-rendering the existing visualization.

`data` is expected to be an array of event objects with fields matching either the expected defaults (`timestamp` and `text` attribute) or the provided mapping via `.mapping()`.

# vis.renderCallback(function)

Sets a callback function that is executed after the visualization is fully rendered, allowing you to apply custom styling or modifications to the rendered elements. The callback is called after both initial renders and automatic re-renders due to window resizing.

```js
vis.renderCallback(function() {
// Apply additional customizations after rendering is complete
d3.select(".milestones").style("margin-left", "10%");
}).render(data);
```

# vis.onEventClick(function)

Set a callback which is executed when the text or image is clicked.

```js
vis.onEventClick((d) => {
console.log('click', d);
alert(`
${d.text} | ${d.timestamp}
${JSON.stringify(d.attributes)}
`);
})
```

# vis.onEventMouseOver(function)

Set a callback which is executed when the mouse cursor is over text or image.

```js
vis.onEventMouseOver((d) => {
console.log('mouseover', d);
})
```

# vis.onEventMouseLeave(function)

Set a callback which is executed when the mouse cursor is leaving text or image.

```js
vis.onEventMouseLeave((d) => {
console.log('mouseleave', d);
})
```

## Disclaimer on DevOps/CI Observability

This repository uses [Elastic APM](https://www.elastic.co/observability/application-performance-monitoring) to track performance of functional tests. It uses `@elastic/apm-rum` as a `devDependency` to be run only as part of the tests. No telemetry library is part of the published package.

## More

`d3-milestones` is also available as a visualization plugin for Kibana here: https://github.com/walterra/kibana-milestones-vis