Skip to content

Streaming / Realtime Charts

The @aziham/chartjs-plugin-streaming enables realtime streaming data visualization with automatic scrolling. This is a modernized fork for Chart.js 4.x with TypeScript support.

Registration

Add to .vitepress/theme/index.ts:

typescript
import DefaultTheme from 'vitepress/theme'

export default {
  extends: DefaultTheme,
  async enhanceApp({ app }) {
    if (typeof window !== 'undefined') {
      const { Chart, registerables } = await import('chart.js')
      Chart.register(...registerables)
      
      await import('chartjs-adapter-luxon')
      const streamingPlugin = (await import('@aziham/chartjs-plugin-streaming')).default
      Chart.register(streamingPlugin)
    }
  }
}

Installation

bash
npm install @aziham/chartjs-plugin-streaming chartjs-adapter-luxon luxon

Note: This plugin requires a date adapter. Luxon is recommended.

Basic Realtime Chart

A line chart with realtime scrolling data:

Loading chart...

Code:

yaml
```chart
type: line
data:
  datasets:
    - label: Dataset 1
      data: []
options:
  scales:
    x:
      type: realtime
      realtime:
        duration: 20000
        refresh: 1000
        delay: 2000
  plugins:
    streaming: {}
```

Note

Streaming charts require JavaScript callbacks for onRefresh to add new data points. In static YAML configs, the chart will display but won't have dynamic data updates. For full functionality, use a JavaScript configuration file.


Configuration via JavaScript

For dynamic data, use a JavaScript configuration file:

public/charts/streaming-demo.js:

javascript
export default function() {
  return {
    type: 'line',
    data: {
      datasets: [{
        label: 'Live Data',
        borderColor: 'rgba(54, 162, 235, 1)',
        backgroundColor: 'rgba(54, 162, 235, 0.2)',
        fill: true,
        data: []
      }]
    },
    options: {
      scales: {
        x: {
          type: 'realtime',
          realtime: {
            duration: 20000,
            refresh: 1000,
            delay: 2000,
            onRefresh: (chart) => {
              chart.data.datasets.forEach((dataset) => {
                dataset.data.push({
                  x: Date.now(),
                  y: Math.random() * 100
                });
              });
            }
          }
        },
        y: {
          beginAtZero: true,
          max: 100
        }
      },
      plugins: {
        streaming: {}
      }
    }
  };
}

Usage:

yaml
```chart
url: /charts/streaming-demo.js
```

Scale Configuration

Realtime Scale Options

OptionTypeDefaultDescription
durationnumber10000Duration of visible data (ms)
refreshnumber1000Refresh interval (ms)
delaynumber0Delay before displaying data (ms)
pausebooleanfalsePause the chart
ttlnumber-Time-to-live for data points (ms)
frameRatenumber30Animation frame rate

Example Configuration

yaml
options:
  scales:
    x:
      type: realtime
      realtime:
        duration: 20000    # Show last 20 seconds
        refresh: 1000      # Update every second
        delay: 2000        # 2 second delay
        pause: false       # Not paused
        ttl: 60000         # Keep 60 seconds of data

Plugin Configuration

Plugin Options

OptionTypeDefaultDescription
durationnumber10000Default duration for all realtime scales
delaynumber0Default delay for all realtime scales
refreshnumber1000Default refresh rate
frameRatenumber30Animation frame rate

Configuration Levels

Options can be set at 3 levels (in priority order):

  1. Per axis: options.scales.x.realtime.*
  2. Per chart: options.plugins.streaming.*
  3. Globally: Chart.defaults.plugins.streaming.*
yaml
options:
  plugins:
    streaming:
      duration: 20000    # Default for all realtime scales in this chart
  scales:
    x:
      type: realtime
      realtime:
        duration: 30000  # Override for this specific scale

Multiple Datasets

javascript
// streaming-multi.js
export default function() {
  return {
    type: 'line',
    data: {
      datasets: [
        {
          label: 'CPU',
          borderColor: 'rgba(255, 99, 132, 1)',
          data: []
        },
        {
          label: 'Memory',
          borderColor: 'rgba(54, 162, 235, 1)',
          data: []
        },
        {
          label: 'Disk',
          borderColor: 'rgba(75, 192, 192, 1)',
          data: []
        }
      ]
    },
    options: {
      scales: {
        x: {
          type: 'realtime',
          realtime: {
            duration: 30000,
            refresh: 2000,
            onRefresh: (chart) => {
              const now = Date.now();
              chart.data.datasets[0].data.push({ x: now, y: Math.random() * 100 });
              chart.data.datasets[1].data.push({ x: now, y: Math.random() * 80 });
              chart.data.datasets[2].data.push({ x: now, y: Math.random() * 60 });
            }
          }
        },
        y: {
          max: 100,
          title: {
            display: true,
            text: 'Usage (%)'
          }
        }
      }
    }
  };
}

Bar Chart Streaming

Streaming also works with bar charts:

javascript
// streaming-bar.js
export default function() {
  return {
    type: 'bar',
    data: {
      datasets: [{
        label: 'Requests/sec',
        backgroundColor: 'rgba(54, 162, 235, 0.8)',
        data: []
      }]
    },
    options: {
      scales: {
        x: {
          type: 'realtime',
          realtime: {
            duration: 20000,
            refresh: 1000,
            onRefresh: (chart) => {
              chart.data.datasets[0].data.push({
                x: Date.now(),
                y: Math.floor(Math.random() * 100)
              });
            }
          }
        }
      }
    }
  };
}

Pause and Resume

Control the streaming programmatically:

javascript
// Pause streaming
chart.options.scales.x.realtime.pause = true;
chart.update('none');

// Resume streaming
chart.options.scales.x.realtime.pause = false;
chart.update('none');

Use Cases

  • Server Monitoring: CPU, memory, disk usage
  • Network Traffic: Bandwidth, packets/sec
  • Stock Prices: Real-time price updates
  • IoT Sensors: Temperature, humidity, pressure
  • Application Metrics: Requests/sec, response times

Resources

Released under the MIT License