diff --git a/README.md b/README.md index fb27ecf..de26000 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ chartjs-plugin-streaming can be used with ES6 modules, plain JavaScript and modu chartjs-plugin-streaming requires [Moment.js](https://momentjs.com/) and [Chart.js](https://www.chartjs.org). -Version 1.5 supports the [line](https://www.chartjs.org/docs/latest/charts/line.html) and [bar](https://www.chartjs.org/docs/latest/charts/bar.html) chart types with both [Number data](https://www.chartjs.org/docs/latest/charts/line.html#number) and [Point data](https://www.chartjs.org/docs/latest/charts/line.html#point) (each data point is specified an array of objects containing x and y properties) as well as the [bubble](https://www.chartjs.org/docs/latest/charts/bubble.html) and [scatter](https://www.chartjs.org/docs/latest/charts/scatter.html) chart types with Point data. In case of Point data, either x or y must be in any of the [date formats](https://momentjs.com/docs/#/parsing/) that Moment.js accepts, and the corresponding axis must have a 'realtime' scale that has the same options as [time](https://www.chartjs.org/docs/latest/axes/cartesian/time.html) scale. Once the realtime scale is specified, the chart will auto-scroll along with that axis. Old data will be automatically deleted as it disappears off the chart. +Version 1.6 supports the [line](https://www.chartjs.org/docs/latest/charts/line.html) and [bar](https://www.chartjs.org/docs/latest/charts/bar.html) chart types with both [Number data](https://www.chartjs.org/docs/latest/charts/line.html#number) and [Point data](https://www.chartjs.org/docs/latest/charts/line.html#point) (each data point is specified an array of objects containing x and y properties) as well as the [bubble](https://www.chartjs.org/docs/latest/charts/bubble.html) and [scatter](https://www.chartjs.org/docs/latest/charts/scatter.html) chart types with Point data. In case of Point data, either x or y must be in any of the [date formats](https://momentjs.com/docs/#/parsing/) that Moment.js accepts, and the corresponding axis must have a 'realtime' scale that has the same options as [time](https://www.chartjs.org/docs/latest/axes/cartesian/time.html) scale. Once the realtime scale is specified, the chart will auto-scroll along with that axis. Old data will be automatically deleted after the time specified by the `ttl` option, or as it disappears off the chart. ## Tutorial and Samples @@ -38,15 +38,16 @@ You can find a tutorial and samples at [nagix.github.io/chartjs-plugin-streaming To configure this plugin, you can simply add the following entries to your chart options. [This example](https://nagix.github.io/chartjs-plugin-streaming/samples/interactions.html) shows how each option affects the appearance of a chart. -| Name | Type | Default | Description | -| ---- | ---- | ------- | ----------- | +| Name | Type | Default | Description +| ---- | ---- | ------- | ----------- | `plugins.streaming` | `Object` or `Boolean` | `true` | The streaming options (see `plugins.streaming.*` options). Also accepts a boolean, in which case if `true`, the chart will auto-scroll using the **global options**, else if `false`, the chart will not auto-scroll. | `plugins.streaming.duration` | `Number` | `10000` | Duration of the chart in milliseconds (how much time of data it will show). -| `plugins.streaming.refresh` | `Number` | `1000` | Reshresh interval of data in milliseconds. `onRefresh` callback function will be called at this interval. +| `plugins.streaming.refresh` | `Number` | `1000` | Refresh interval of data in milliseconds. `onRefresh` callback function will be called at this interval. | `plugins.streaming.delay` | `Number` | `0` | Delay added to the chart in milliseconds so that upcoming values are known before lines are plotted. This makes the chart look like a continual stream rather than very jumpy on the right hand side. Specify the maximum expected delay. | `plugins.streaming.frameRate` | `Number` | `30` | Frequency at which the chart is drawn on a display (frames per second). Decrease this value to save CPU power. [more...](#lowering-cpu-usage) | `plugins.streaming.pause` | `Boolean` | `false` | If set to `true`, scrolling stops. Note that `onRefresh` callback is called even when this is set to `true`. | `plugins.streaming.onRefresh` | `Function` | `null` | Callback function that will be called at a regular interval. The callback takes one argument, a reference to the chart object. You can update your datasets here. The chart will be automatically updated after returning. +| `plugins.streaming.ttl` | `Number` | | Duration of the data to be kept in milliseconds. If not set, old data will be automatically deleted as it disappears off the chart. > **Global options** can be change through `Chart.defaults.global.plugins.streaming`, which by default enable auto-scroll of the charts that have a time scale. @@ -57,7 +58,7 @@ For example: type: 'line', // 'line', 'bar', 'bubble' and 'scatter' types are supported data: { datasets: [{ - data: [] // empty at the beggining + data: [] // empty at the beginning }] }, options: { @@ -73,6 +74,7 @@ For example: delay: 1000, // delay of 1000 ms, so upcoming values are known before plotting a line frameRate: 30, // chart is drawn 30 times every second pause: false, // chart is not paused + ttl: undefined, // data will be automatically deleted as it disappears off the chart // a callback to update datasets onRefresh: function(chart) { @@ -96,6 +98,36 @@ Note that the following options are ignored for the 'realtime' scale. - `time.max` - `time.min` +## Support for Zooming and panning + +By using together with [chartjs-plugin-zoom](https://github.com/chartjs/chartjs-plugin-zoom), zooming and panning of a streaming chart can be done via the mouse or finger gestures. Unlike other scale types, the `rangeMin` and `rangeMax` options don't specify time values. Instead, `pan.rangeMin` and `pan.rangeMax` limit the range of the `delay` option value while `zoom.rangeMin` and `zoom.rangeMax` limit the range of the `duration` option value. + +``` + options: { + // Assume x axis is the realtime scale + pan: { + enabled: true, // Enable panning + mode: 'x', // Allow panning in the x direction + rangeMin: { + x: null // Min value of the delay option + }, + rangeMax: { + x: null // Max value of the delay option + } + }, + zoom: { + enabled: true, // Enable zooming + mode: 'x', // Allow zooming in the x direction + rangeMin: { + x: null // Min value of the duration option + }, + rangeMax: { + x: null // Max value of the duration option + } + } + } +``` + ## Lowering CPU Usage If you are using this plugin on resource constrained devices or drawing multiple charts on a large screen, it might be a good idea to decrease the frame rate to lower CPU usage. The following settings also reduce CPU usage by disabling animation, and improve general page performance. diff --git a/docs/index.html b/docs/index.html index e5931a5..09c2dbf 100644 --- a/docs/index.html +++ b/docs/index.html @@ -15,7 +15,7 @@ - +
+ + + + +
+var chartColors = { + red: 'rgb(255, 99, 132)', + orange: 'rgb(255, 159, 64)', + yellow: 'rgb(255, 205, 86)', + green: 'rgb(75, 192, 192)', + blue: 'rgb(54, 162, 235)', + purple: 'rgb(153, 102, 255)', + grey: 'rgb(201, 203, 207)' +}; + +function randomScalingFactor() { + return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100); +} + +function onRefresh(chart) { + chart.config.data.datasets.forEach(function(dataset) { + dataset.data.push({ + x: Date.now(), + y: randomScalingFactor() + }); + }); +} + +var color = Chart.helpers.color; +var config = { + type: 'line', + data: { + datasets: [{ + label: 'Dataset 1 (linear interpolation)', + backgroundColor: chartColors.red, + borderColor: chartColors.red, + fill: false, + lineTension: 0, + borderDash: [8, 4], + data: [] + }, { + label: 'Dataset 2 (cubic interpolation)', + backgroundColor: chartColors.blue, + borderColor: chartColors.blue, + fill: false, + cubicInterpolationMode: 'monotone', + data: [] + }] + }, + options: { + title: { + display: true, + text: 'Data labels plugin sample' + }, + scales: { + xAxes: [{ + type: 'realtime' + }], + yAxes: [{ + type: 'linear', + display: true, + scaleLabel: { + display: true, + labelString: 'value' + } + }] + }, + tooltips: { + mode: 'nearest', + intersect: false + }, + hover: { + mode: 'nearest', + intersect: false + }, + pan: { + enabled: true, + mode: 'x', + rangeMax: { + x: 4000 + }, + rangeMin: { + x: 0 + } + }, + zoom: { + enabled: true, + mode: 'x', + rangeMax: { + x: 20000 + }, + rangeMin: { + x: 1000 + } + }, + plugins: { + streaming: { + duration: 20000, + refresh: 1000, + delay: 2000, + onRefresh: onRefresh + } + } + } +}; + +window.onload = function() { + var ctx = document.getElementById('myChart').getContext('2d'); + window.myChart = new Chart(ctx, config); +}; + +document.getElementById('randomizeData').addEventListener('click', function() { + config.data.datasets.forEach(function(dataset) { + dataset.data.forEach(function(dataObj) { + dataObj.y = randomScalingFactor(); + }); + }); + + window.myChart.update(); +}); + +var colorNames = Object.keys(chartColors); +document.getElementById('addDataset').addEventListener('click', function() { + var colorName = colorNames[config.data.datasets.length % colorNames.length]; + var newColor = chartColors[colorName]; + var newDataset = { + label: 'Dataset ' + (config.data.datasets.length + 1), + backgroundColor: newColor, + borderColor: newColor, + fill: false, + lineTension: 0, + data: [] + }; + + config.data.datasets.push(newDataset); + window.myChart.update(); +}); + +document.getElementById('removeDataset').addEventListener('click', function() { + config.data.datasets.pop(); + window.myChart.update(); +}); + +document.getElementById('addData').addEventListener('click', function() { + onRefresh(window.myChart); + window.myChart.update(); +});+
<head> + <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script> + <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script> + <script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script> + <script src="https://rawgit.com/chartjs/chartjs-plugin-zoom/master/chartjs-plugin-zoom.min.js"></script> + <script src="https://github.com/nagix/chartjs-plugin-streaming/releases/download/v1.6.0/chartjs-plugin-streaming.min.js"></script> +</head> +<body> + <div> + <canvas id="myChart"></canvas> + </div> + <p> + <button id="randomizeData">Randomize Data</button> + <button id="addDataset">Add Dataset</button> + <button id="removeDataset">Remove Dataset</button> + <button id="addData">Add Data</button> + </p> +</body>+