JavaScript

Timeline Chart

Visualize changing values over time

Example of the Time Line Chart

Basic Implementation

To render a timeline chart, you first have to set up a container in your HTML.

<div id="timeLineChart"></div>

Next in your script file, you need to import the meistercharts package and create a new instance of the timeline chart.

const meistercharts = require('@meistercharts/meistercharts/meistercharts-easy-api')

const chart = meistercharts.createTimeLineChartFromId('timeLineChart')

This will render an empty chart with the default styles.

You can have meistercharts create some sample data for demonstration purposes by calling the setUpDemo- function on the chart instance.

chart.setUpDemo()

Display your own values

There are two steps necessary in order to display your own data in the Timeline chart:

  • Define the data series
  • Add samples to the chart’s history

Define Data Series

The timeline chart can display two different types of data: decimal values (e.g. the flow rate through a pipe) and enum values (e.g. the state of a valve). Each data by calling the setDataSeries- function on the chart instance.

chart.setDataSeries(DecimalDataSeries[], EnumDataSeries[])

See the definition of the DecimalDataSeries and EnumDataSeries types in the reference.

Add Samples

Once the data series are defined, you can add values to each of the data series using the addSample or addSamples methods.

The id in the sample object has to match the id of the data series, that you want to add the sample to.

Example

<div id="timeLineChart"></div>

<script>
  const meistercharts = require('@meistercharts/meistercharts/meistercharts-easy-api')

  const chart = meistercharts.createTimeLineChartFromId('timeLineChart')

  chart.setDataSeries(
    [{ id: 0, name: 'Flow Rate' }],
    [
      {
        description: 'valve state',
        values: [
          { ordinal: 0, label: 'closed' },
          { ordinal: 1, label: 'open' }
        ]
      }
    ]
  )

  setInterval(() => {
    // ... fetch data from somewhere
    chart.addSample({
      timestamp: Date.now(),
      decimalValues: [data.flowRate],
      enumValues: [data.valveState]
    })
  }, 100)
</script>

Controlling the chart

The timeline chart instance exposes the following methods to control the chart:

MethodDescription
setDataModify the history settings and play/pause the chart.
setStyleModify the chart style.
getVisibleTimeRangeReturns the current visible time range.
setDataSeriesConfigures the data series to be shown in the chart.
addSampleAdds a new sample to the chart.
addSamplesAdds multiple samples to the chart.
addSamplesDirectyAdds samples to the history stored by this chart.
clearHistoryClears the chart history.
resetViewReset the chart’s view to the default values.
getZoomReturns the current zoom level.
modifyZoomSet the chart’s zoom level.
setUpDemoSets up a demo chart with preconfigured data.

setData

Modify the history settings and play/pause the chart.

chart.setData({
  historySettings: HistorySettings,
  play: boolean
})

See the TimeLineChartData type in the reference for all available options.

setStyle

Modify the chart style.

chart.setStyle(TimeLineChartStyle)

See the TimeLineChartStyle type in the reference.

getVisibleTimeRange

Returns the current visible time range (TimeRange).

const visibleTimeRange = chart.getVisibleTimeRange()

setDataSeries

Configures the data series to be shown in the chart. This method has to be called before adding samples to the chart.

chart.setDataSeries(DecimalDataSeries[], EnumDataSeries[])

Accepts an array of DecimalDataSeries and EnumDataSeries objects.

addSample

Adds a new Sample to the chart’s internal history.

chart.addSample(sample: Sample)

addSamples

Adds an array of Samples to the chart’s internal history.

chart.addSamples(samples: Sample[])

addSamplesDirectly

Adds samples to the history stored by this chart. Uses the given distance between samples to calculate the sampling period.

chart.addSamplesDirectly(samples: Sample[], durationBetweenSamples: number)

clearHistory

Clears the chart history.

chart.clearHistory()

resetView

Reset the chart’s view to the default values.

chart.resetView()

getZoom

Returns the current Zoom level.

const zoom = chart.getZoom()

modifyZoom

Set the chart’s zoom level.

chart.modifyZoom(zoom?: Zoom, zoomCenterX?: number, zoomCenterY?: number)

setUpDemo

Sets up the timelinc chart with sample data series and data. This is only useful for demonstration purposes.

chart.setUpDemo()