Line Chart Library

oi.linechart.js (25.2kB; 16.6kB minified; 7kB minified and gzipped) is our small, customisable, standalone, Javascript library for making simple SVG graphs. There are some examples below.

Getting started

First we need a DOM element to exist and it needs to have a width/height set. We can then attach our OI.linechart to the DOM element and set some properties of our chart. Next we can add a data series with some custom properties.

HTML

				<div id="chart"></div>
			

Javascript

				// Set up the chart object
				// The first argument is the DOM element to attach to (you may want to make sure it exists before you attach it)
				// The second argument is an object setting optional properties - see below
				var chart = OI.linechart(document.getElementById('chart'),properties);
				// Next we add a data series - see "Add a series" below
				chart.addSeries([{'x':1,'y':1},{'x':1,'y':2}],{})
				// Draw the chart
				chart.draw();
			

Chart properties

When creating a new line chart you can set chart properties:

You can also update the properties of the chart later using chart.setProperties(props).

Axis properties

You can update each axis with the following options:

Add a series

You can add a series using chart.addSeries(data,attr) where data is of the form [{x:1,y:0.1},{x:2,y:0.2},{x:3,y:0.3}] and attr is of the form:

Examples

  1. Basic example
  2. Grid lines
  3. Multiple series with a key
  4. Update series
  5. Dashed line
  6. Label alignment
  7. Style tooltip (darker charts)
  8. Tick marks
  9. Update range

Basic example

This is a basic chart with one series.

Grid lines

This is a basic chart with one series and grid lines of different colours.

Multiple series with a key

This example has two data series representing the monthly minimum and maximum temperatures in Leeds, UK. The points have a tooltip that shows the value. We show a key, gridlines on the y-axis, and remove the y-axis line.

Update series

This chart displays randomly generated data and we define the scale on the y-axis using min and max. We can update the data with an animation by setting animation-duration on the chart with CSS. The randomised data also includes a randomised time interval to show how the line and points are animated. We'll make the y-axis gridlines silver.

Dashed line

This chart has a dashed line by setting the stroke-dasharray property.

Label alignment

Let's adjust the placement of the labels. We can align them to the right and top with align. We can custom-align the text-anchor of the labels. We can explicitly set the label offset from the tick marks using offset. We can explicitly set the ticksize per tick.

Style tooltip (darker charts)

Sometimes we need to customise the tooltip e.g. if we've changed the chart background colour. We do that in the CSS. We can also customise the text in the tooltip per data series by defining a tooltip label function.

Tick marks

There are times when you may wish that some tick marks are labelled and others are not and you might want the ticks to be different lengths. You can set the ticksize per tick. You can add ticks that have label empty or missing.

Update range