Skip to content

superpikar/d3-milestones

 
 

Repository files navigation

npm npm npm

d3-milestones

A d3 based timeline visualization.

The Viking Timeline

Installing

To use it via NPM, use npm install 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.

<link rel="stylesheet" href="https://unpkg.com/d3-milestones/build/d3-milestones.css">
<script src="https://unpkg.com/d3-milestones/build/d3-milestones.min.js"></script>

<div id="timeline"></div>

<script>
  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.' }
    ]);
</script>

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

Examples

There are more examples included in the github repository. These examples should give you an idea how to work with the library.

API Reference

General Usage

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

  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.mapping(configObject)

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

  {
    category: undefined,
    entries: undefined,
    timestamp: 'timestamp',
    text: 'text'
  };

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

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

# vis.optimize(boolean)

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

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

More

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

About

A d3 based timeline visualization.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HTML 82.3%
  • JavaScript 16.5%
  • CSS 1.2%