Last active
September 22, 2018 10:21
-
-
Save electricbaka/4e00c90604a09f7cf91bd80269bba77d to your computer and use it in GitHub Desktop.
RRIデータからストレス指標を算出してグラフ表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<!-- | |
Copyright 2017 Analog Devices, Inc & JellyWare Inc. | |
--> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="description" content="Web Bluetooth API Sample"> | |
<meta name="viewport" content="width=640, maximum-scale=1.0, user-scalable=yes"> | |
<title>BlueJelly</title> | |
<link href="https://fonts.googleapis.com/css?family=Lato:100,300,400,700,900" rel="stylesheet" type="text/css"> | |
<link rel="stylesheet" href="style.css"> | |
<script type="text/javascript" src="bluejelly.js"></script> | |
<script type="text/javascript" src="./smoothie.js"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="title margin"> | |
<p id="title">Web Bluetooth API Sample</p> | |
<p id="subtitle">#08 AD8232 LF/HFグラフ表示</p> | |
</div> | |
<div class="contents margin"> | |
<button id="readData" class="button">Read</button> | |
<button id="startNotifications" class="button">Start Notify</button> | |
<button id="stopNotifications" class="button">Stop Notify</button> | |
<!--<button id="writeData" class="button">Write</button>--> | |
<button id="reset" class="button">Reset</button> | |
<hr> | |
<div id="device_name">xxxx</div> | |
<div id="data_text">xxxx</div> | |
<div id="status"> </div> | |
<!-- データを表示するためのcanvasを追加 --> | |
<canvas id="chart" width="500" height="320"></canvas> | |
</div> | |
<div class="footer margin"> | |
© 2018 <a href="http://www.analog.com/jp/" target="_blank">Analog Devices, Inc</a> & <a href="http://jellyware.jp/" target="_blank">JellyWare Inc.</a> All Rights Reserved | |
</div> | |
</div> | |
<script> | |
//-------------------------------------------------- | |
//Global変数 | |
//-------------------------------------------------- | |
//BlueJellyのインスタンス生成 | |
var ble = new BlueJelly(); | |
var ble_data = new TimeSeries(); //Smoothie Chartsの関数を追加 | |
var flag_led_status = 0; | |
//------------------------------------------------- | |
//smoothie.js | |
//------------------------------------------------- | |
function createTimeline() { | |
var chart = new SmoothieChart({ | |
millisPerPixel: 20, | |
grid: { | |
fillStyle: '#27B34F', | |
strokeStyle: '#ffffff', | |
millisPerLine: 800 | |
}, | |
maxValue: 3, | |
minValue: 0 | |
}); | |
chart.addTimeSeries(ble_data, { | |
strokeStyle: 'rgba(255, 255, 255, 1)', | |
fillStyle: 'rgba(255, 255, 255, 0.2)', | |
lineWidth: 4 | |
}); | |
chart.streamTo(document.getElementById("chart"), 500); | |
} | |
//------------------------------------------------- | |
//ボタンが押された時のイベント登録 | |
//-------------------------------------------------- | |
document.getElementById('readData').addEventListener('click', function() { | |
ble.read('UUID1'); | |
}); | |
document.getElementById('startNotifications').addEventListener('click', function() { | |
ble.startNotify('UUID1'); | |
}); | |
document.getElementById('stopNotifications').addEventListener('click', function() { | |
ble.stopNotify('UUID1'); | |
}); | |
//document.getElementById('writeData').addEventListener('click', function() { | |
// led_on_off(); | |
//}); | |
document.getElementById('reset').addEventListener('click', function() { | |
ble.reset(); //reset is disconnect & clear | |
}); | |
/* | |
//-------------------------------------------------- | |
//LEDをON/OFFする関数 | |
//-------------------------------------------------- | |
led_on_off = function() { | |
if (flag_led_status == 0) { | |
ble.write('UUID2', [0x01]); | |
flag_led_status = 1; | |
} else { | |
ble.write('UUID2', [0x00]); | |
flag_led_status = 0; | |
} | |
} | |
*/ | |
//-------------------------------------------------- | |
//ロード時の処理 | |
//-------------------------------------------------- | |
window.onload = function() { | |
//初期の文字列表示 | |
document.getElementById('device_name').innerHTML = "No Device"; | |
document.getElementById('data_text').innerHTML = "No Data"; | |
//UUIDの設定 | |
ble.setUUID("UUID1", "713d0000-503e-4c75-ba94-3148f18d941e", "713d0002-503e-4c75-ba94-3148f18d941e"); //BLEnano SimpleControl | |
ble.setUUID("UUID2", "713d0000-503e-4c75-ba94-3148f18d941e", "713d0003-503e-4c75-ba94-3148f18d941e"); //BLEnano SimpleControl | |
//smoothie.jsを追加 | |
createTimeline(); | |
//heart_rateデータ初期化 | |
heart_rate_init(); | |
} | |
//-------------------------------------------------- | |
//Scan後の処理 | |
//-------------------------------------------------- | |
ble.onScan = function(deviceName) { | |
//HTMLに表示 | |
document.getElementById('device_name').innerHTML = deviceName; | |
} | |
//-------------------------------------------------- | |
//Read後の処理:得られたデータの表示など行う | |
//-------------------------------------------------- | |
ble.onRead = function(data, uuid) { | |
//フォーマットに従って値を取得 | |
value = data.getInt16(0);//2Byteの場合のフォーマット | |
//グラフへ反映 | |
//ble_data.append(new Date().getTime(), value); | |
//HTMLに値を表示 | |
document.getElementById('data_text').innerHTML = value; | |
//コンソールに値を表示 | |
console.log(value); | |
//再びRead -> やめる | |
//ble.read('UUID1'); | |
//hart_rate処理へデータ渡し | |
heart_rate(value); | |
} | |
//-------------------------------------------------- | |
//Reset後の処理 | |
//-------------------------------------------------- | |
ble.onReset = function() { | |
//HTMLに表示 | |
document.getElementById('device_name').innerHTML = "No Device"; | |
} | |
//==================================================================================================== | |
//心拍RRIデータ処理 | |
//==================================================================================================== | |
//-------------------------------------------------- | |
//Global変数 | |
//-------------------------------------------------- | |
var data_RRI = new Array();//心拍間隔データの配列(周期的境界条件で上書きしている) | |
var data_RRI_sort = new Array();//心拍間隔データの配列を正しく並べなおしたもの(一番古いデータが最初になるように並び直し) | |
var data_max_num = 256;//心拍間隔サンプリング数 | |
var sampling_freq = 2;//サンプリング周波数 | |
var data_RRI_pointer = 0; | |
var data_effective_low = 400; //これ未満は無効データ | |
var data_efective_high = 1000; //これより超えている場合は無効データ | |
//-------------------------------------------------- | |
//データ初期化 | |
//-------------------------------------------------- | |
function heart_rate_init(){ | |
for(i=0; i<data_max_num; i++) | |
data_RRI[i] = 0; | |
} | |
//-------------------------------------------------- | |
//データ処理 | |
//-------------------------------------------------- | |
function heart_rate(recieve_data){ | |
//-------------------- | |
//最新RRIデータ256個を古い順に配列data_RRI_sortに格納 | |
//-------------------- | |
//BLEデータが有効範囲外ならば、無効データ(-1)として取り扱う | |
if(recieve_data < data_effective_low || recieve_data > data_efective_high) | |
recieve_data = -1; | |
//心拍間隔データに新しいBLEデータを追加更新(一番古いデータに上書き) | |
data_RRI[data_RRI_pointer] = recieve_data; | |
//ポインタのインクリメント | |
data_RRI_pointer++; | |
if(data_RRI_pointer >= data_max_num) | |
data_RRI_pointer = 0; | |
//データがまだ溜まっていない時、無効データ受信時の処理 | |
for(i=0; i<data_max_num; i++) | |
{ | |
//全てのデータで0データがないかを確認。0があった場合はEmptyを表示して終了 | |
if(data_RRI[i] == 0) | |
{ | |
console.log("Empty : " + i + " / " + data_max_num); | |
document.getElementById('status').innerHTML = "Empty : " + i + " / " + data_max_num; | |
return; | |
} | |
//全てのデータで無効データがないかを確認。無効があった場合はエラー表示して終了。配列を初期化する | |
if(data_RRI[i] == -1) | |
{ | |
console.log("Error !!" ); | |
document.getElementById('status').innerHTML = "Error !!" | |
heart_rate_init();//初期化する | |
data_RRI_pointer = 0;//ポインタも初期化 | |
return; | |
} | |
} | |
//ポインタの位置を0(最も古いデータ)としてデータを並び替える | |
var counter = 0; | |
for(i=data_RRI_pointer; i<data_max_num; i++) | |
{ | |
data_RRI_sort[counter] = data_RRI[i]; | |
counter++; | |
} | |
for(i=0; i<data_RRI_pointer; i++) | |
{ | |
data_RRI_sort[counter] = data_RRI[i]; | |
counter++; | |
} | |
//-------------------- | |
//線形補完 | |
//-------------------- | |
var pointer = 0;//線形補完するときにどのデータをみるかの位置決め | |
var time = new Array(); //心拍間隔から時間データに変換するもの | |
var x = new Array(); //線形補完後の時間データ 0, 500, 1000, 1500, 2000 ・・・と続く | |
var y = new Array(); //線形補完後の心拍間隔データ | |
//変数初期化 | |
time[0] = 0; | |
x[0] = 0; | |
y[0] = data_RRI_sort[0]; | |
//対象データで線形補間 | |
for(i=1;i<data_RRI_sort.length;i++) | |
{ | |
time[i] = time[i-1] + data_RRI_sort[i]; | |
x[i] = 1/sampling_freq * 1000 * i; | |
//対象のデータまでポインタを進める | |
//逆に、前回のポインタ位置でも線形補完の対象になっている場合はポインタは進めない | |
//(例えば x=1000であれば、線形補完の対象となるtimeは1000以上で一番小さいところになる) | |
while(x[i] > time[pointer]) | |
pointer++ | |
//線形補完 | |
y[i] = linear_interpolation(x[i], time[pointer-1], data_RRI_sort[pointer-1], time[pointer], data_RRI_sort[pointer]); | |
} | |
//-------------------- | |
//パワースペクトル算出 | |
//-------------------- | |
value_freq = new Array();//周波数配列 | |
value_power = new Array();//振幅配列 | |
//フーリエ変換 | |
value_Re = dft(y).Re; | |
value_Im = dft(y).Im ; | |
//周波数と振幅を算出 | |
for(i=0;i<data_RRI_sort.length;i++) | |
{ | |
value_freq[i] = sampling_freq/data_RRI_sort.length * i ; | |
value_power[i] = Math.sqrt(Math.pow(value_Re[i], 2) + Math.pow(value_Im [i], 2)); | |
} | |
//-------------------- | |
//LF/HF算出 | |
//-------------------- | |
var lf = 0; | |
var hf = 0; | |
//LF(0.05〜0.15Hz)とHF(0.15〜0.40Hz)の算出 | |
for(i=0;i<data_RRI_sort.length;i++) | |
{ | |
if(value_freq[i]>=0.05 && value_freq[i] <=0.15) | |
lf = lf + value_power[i]; | |
if(value_freq[i]>=0.15 && value_freq[i] <=0.40) | |
hf = hf + value_power[i]; | |
} | |
//結果表示 | |
//console.log("LF : " + lf); | |
//console.log("HF : " + hf); | |
console.log("LF/HF : " + (lf/hf).toFixed(3)); | |
document.getElementById('status').innerHTML = "LF/HF : " + (lf/hf).toFixed(3); | |
//グラフへ反映 | |
var value = lf/hf; | |
ble_data.append(new Date().getTime(), value); | |
} | |
//-------------------------------------------------- | |
//線形補完 | |
//-------------------------------------------------- | |
function linear_interpolation(x, x0, y0, x1, y1) | |
{ | |
y = (y1 - y0)*(x - x0)/(x1 -x0) + y0; | |
return y; | |
} | |
//-------------------------------------------------- | |
//フーリエ変換 | |
//-------------------------------------------------- | |
function dft(a) | |
{ | |
var Re = [];// 実部 | |
var Im = [];// 虚部 | |
var N = a.length; // サンプル数 | |
// DFTの計算 | |
for( var j=0; j<N; ++j ) | |
{ | |
var re = 0.0; | |
var im = 0.0; | |
for( var i=0; i<N; ++i ) | |
{ | |
var th = 2*Math.PI/N * j * i; | |
re += a[i] * Math.cos(th); | |
im += a[i] * Math.sin(th) * (-1); | |
} | |
Re.push(re); | |
Im.push(im); | |
} | |
return {'Re':Re, 'Im':Im}; | |
} | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
============================================================ | |
BlueJelly.js | |
============================================================ | |
Web Bluetooth API Wrapper Library | |
Copyright 2017 JellyWare Inc. | |
http://jellyware.jp/ | |
This software is released under the MIT License. | |
Web Bluetooth API | |
https://webbluetoothcg.github.io/web-bluetooth/ | |
*/ | |
//-------------------------------------------------- | |
//BlueJelly constructor | |
//-------------------------------------------------- | |
var BlueJelly = function(){ | |
this.bluetoothDevice = null; | |
this.dataCharacteristic = null; | |
this.hashUUID ={}; | |
this.hashUUID_lastConnected; | |
//callBack | |
this.onScan = function(deviceName){console.log("onScan");}; | |
this.onConnectGATT = function(uuid){console.log("onConnectGATT");}; | |
this.onRead = function(data, uuid){console.log("onRead");}; | |
this.onWrite = function(uuid){console.log("onWrite");}; | |
this.onStartNotify = function(uuid){console.log("onStartNotify");}; | |
this.onStopNotify = function(uuid){console.log("onStopNotify");}; | |
this.onDisconnect = function(){console.log("onDisconnect");}; | |
this.onClear = function(){console.log("onClear");}; | |
this.onReset = function(){console.log("onReset");}; | |
this.onError = function(error){console.log("onError");}; | |
} | |
//-------------------------------------------------- | |
//setUUID | |
//-------------------------------------------------- | |
BlueJelly.prototype.setUUID = function(name, serviceUUID, characteristicUUID){ | |
console.log('Execute : setUUID'); | |
console.log(this.hashUUID); | |
this.hashUUID[name] = {'serviceUUID':serviceUUID, 'characteristicUUID':characteristicUUID}; | |
} | |
//-------------------------------------------------- | |
//scan | |
//-------------------------------------------------- | |
BlueJelly.prototype.scan = function(uuid){ | |
return (this.bluetoothDevice ? Promise.resolve() : this.requestDevice(uuid)) | |
.catch(error => { | |
console.log('Error : ' + error); | |
this.onError(error); | |
}); | |
} | |
//-------------------------------------------------- | |
//requestDevice | |
//-------------------------------------------------- | |
BlueJelly.prototype.requestDevice = function(uuid) { | |
console.log('Execute : requestDevice'); | |
return navigator.bluetooth.requestDevice({ | |
acceptAllDevices: true, | |
optionalServices: [this.hashUUID[uuid].serviceUUID]}) | |
.then(device => { | |
this.bluetoothDevice = device; | |
this.bluetoothDevice.addEventListener('gattserverdisconnected', this.onDisconnect); | |
this.onScan(this.bluetoothDevice.name); | |
}); | |
} | |
//-------------------------------------------------- | |
//connectGATT | |
//-------------------------------------------------- | |
BlueJelly.prototype.connectGATT = function(uuid) { | |
if(!this.bluetoothDevice) | |
{ | |
var error = "No Bluetooth Device"; | |
console.log('Error : ' + error); | |
this.onError(error); | |
return; | |
} | |
if (this.bluetoothDevice.gatt.connected && this.dataCharacteristic) { | |
if(this.hashUUID_lastConnected == uuid) | |
return Promise.resolve(); | |
} | |
this.hashUUID_lastConnected = uuid; | |
console.log('Execute : connect'); | |
return this.bluetoothDevice.gatt.connect() | |
.then(server => { | |
console.log('Execute : getPrimaryService'); | |
return server.getPrimaryService(this.hashUUID[uuid].serviceUUID); | |
}) | |
.then(service => { | |
console.log('Execute : getCharacteristic'); | |
return service.getCharacteristic(this.hashUUID[uuid].characteristicUUID); | |
}) | |
.then(characteristic => { | |
this.dataCharacteristic = characteristic; | |
this.dataCharacteristic.addEventListener('characteristicvaluechanged',this.dataChanged(this, uuid)); | |
this.onConnectGATT(uuid); | |
}) | |
.catch(error => { | |
console.log('Error : ' + error); | |
this.onError(error); | |
}); | |
} | |
//-------------------------------------------------- | |
//dataChanged | |
//-------------------------------------------------- | |
BlueJelly.prototype.dataChanged = function(self, uuid) { | |
return function(event) { | |
self.onRead(event.target.value, uuid); | |
} | |
} | |
//-------------------------------------------------- | |
//read | |
//-------------------------------------------------- | |
BlueJelly.prototype.read= function(uuid) { | |
return (this.scan(uuid)) | |
.then( () => { | |
return this.connectGATT(uuid); | |
}) | |
.then( () => { | |
console.log('Execute : readValue'); | |
return this.dataCharacteristic.readValue(); | |
}) | |
.catch(error => { | |
console.log('Error : ' + error); | |
this.onError(error); | |
}); | |
} | |
//-------------------------------------------------- | |
//write | |
//-------------------------------------------------- | |
BlueJelly.prototype.write = function(uuid, array_value) { | |
return (this.scan(uuid)) | |
.then( () => { | |
return this.connectGATT(uuid); | |
}) | |
.then( () => { | |
console.log('Execute : writeValue'); | |
data = Uint8Array.from(array_value); | |
return this.dataCharacteristic.writeValue(data); | |
}) | |
.then( () => { | |
this.onWrite(uuid); | |
}) | |
.catch(error => { | |
console.log('Error : ' + error); | |
this.onError(error); | |
}); | |
} | |
//-------------------------------------------------- | |
//startNotify | |
//-------------------------------------------------- | |
BlueJelly.prototype.startNotify = function(uuid) { | |
return (this.scan(uuid)) | |
.then( () => { | |
return this.connectGATT(uuid); | |
}) | |
.then( () => { | |
console.log('Execute : startNotifications'); | |
this.dataCharacteristic.startNotifications() | |
}) | |
.then( () => { | |
this.onStartNotify(uuid); | |
}) | |
.catch(error => { | |
console.log('Error : ' + error); | |
this.onError(error); | |
}); | |
} | |
//-------------------------------------------------- | |
//stopNotify | |
//-------------------------------------------------- | |
BlueJelly.prototype.stopNotify = function(uuid){ | |
return (this.scan(uuid)) | |
.then( () => { | |
return this.connectGATT(uuid); | |
}) | |
.then( () => { | |
console.log('Execute : stopNotifications'); | |
this.dataCharacteristic.stopNotifications() | |
}) | |
.then( () => { | |
this.onStopNotify(uuid); | |
}) | |
.catch(error => { | |
console.log('Error : ' + error); | |
this.onError(error); | |
}); | |
} | |
//-------------------------------------------------- | |
//disconnect | |
//-------------------------------------------------- | |
BlueJelly.prototype.disconnect= function() { | |
if (!this.bluetoothDevice) { | |
var error = "No Bluetooth Device"; | |
console.log('Error : ' + error); | |
this.onError(error); | |
return; | |
} | |
if (this.bluetoothDevice.gatt.connected) { | |
console.log('Execute : disconnect'); | |
this.bluetoothDevice.gatt.disconnect(); | |
} else { | |
var error = "Bluetooth Device is already disconnected"; | |
console.log('Error : ' + error); | |
this.onError(error); | |
return; | |
} | |
} | |
//-------------------------------------------------- | |
//clear | |
//-------------------------------------------------- | |
BlueJelly.prototype.clear= function() { | |
console.log('Excute : Clear Device and Characteristic'); | |
this.bluetoothDevice = null; | |
this.dataCharacteristic = null; | |
this.onClear(); | |
} | |
//-------------------------------------------------- | |
//reset(disconnect & clear) | |
//-------------------------------------------------- | |
BlueJelly.prototype.reset= function() { | |
console.log('Excute : reset'); | |
this.disconnect(); //disconnect() is not Promise Object | |
this.clear(); | |
this.onReset(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License: | |
// | |
// Copyright (c) 2010-2013, Joe Walnes | |
// 2013-2017, Drew Noakes | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
/** | |
* Smoothie Charts - http://smoothiecharts.org/ | |
* (c) 2010-2013, Joe Walnes | |
* 2013-2017, Drew Noakes | |
* | |
* v1.0: Main charting library, by Joe Walnes | |
* v1.1: Auto scaling of axis, by Neil Dunn | |
* v1.2: fps (frames per second) option, by Mathias Petterson | |
* v1.3: Fix for divide by zero, by Paul Nikitochkin | |
* v1.4: Set minimum, top-scale padding, remove timeseries, add optional timer to reset bounds, by Kelley Reynolds | |
* v1.5: Set default frames per second to 50... smoother. | |
* .start(), .stop() methods for conserving CPU, by Dmitry Vyal | |
* options.interpolation = 'bezier' or 'line', by Dmitry Vyal | |
* options.maxValue to fix scale, by Dmitry Vyal | |
* v1.6: minValue/maxValue will always get converted to floats, by Przemek Matylla | |
* v1.7: options.grid.fillStyle may be a transparent color, by Dmitry A. Shashkin | |
* Smooth rescaling, by Kostas Michalopoulos | |
* v1.8: Set max length to customize number of live points in the dataset with options.maxDataSetLength, by Krishna Narni | |
* v1.9: Display timestamps along the bottom, by Nick and Stev-io | |
* (https://groups.google.com/forum/?fromgroups#!topic/smoothie-charts/-Ywse8FCpKI%5B1-25%5D) | |
* Refactored by Krishna Narni, to support timestamp formatting function | |
* v1.10: Switch to requestAnimationFrame, removed the now obsoleted options.fps, by Gergely Imreh | |
* v1.11: options.grid.sharpLines option added, by @drewnoakes | |
* Addressed warning seen in Firefox when seriesOption.fillStyle undefined, by @drewnoakes | |
* v1.12: Support for horizontalLines added, by @drewnoakes | |
* Support for yRangeFunction callback added, by @drewnoakes | |
* v1.13: Fixed typo (#32), by @alnikitich | |
* v1.14: Timer cleared when last TimeSeries removed (#23), by @davidgaleano | |
* Fixed diagonal line on chart at start/end of data stream, by @drewnoakes | |
* v1.15: Support for npm package (#18), by @dominictarr | |
* Fixed broken removeTimeSeries function (#24) by @davidgaleano | |
* Minor performance and tidying, by @drewnoakes | |
* v1.16: Bug fix introduced in v1.14 relating to timer creation/clearance (#23), by @drewnoakes | |
* TimeSeries.append now deals with out-of-order timestamps, and can merge duplicates, by @zacwitte (#12) | |
* Documentation and some local variable renaming for clarity, by @drewnoakes | |
* v1.17: Allow control over font size (#10), by @drewnoakes | |
* Timestamp text won't overlap, by @drewnoakes | |
* v1.18: Allow control of max/min label precision, by @drewnoakes | |
* Added 'borderVisible' chart option, by @drewnoakes | |
* Allow drawing series with fill but no stroke (line), by @drewnoakes | |
* v1.19: Avoid unnecessary repaints, and fixed flicker in old browsers having multiple charts in document (#40), by @asbai | |
* v1.20: Add SmoothieChart.getTimeSeriesOptions and SmoothieChart.bringToFront functions, by @drewnoakes | |
* v1.21: Add 'step' interpolation mode, by @drewnoakes | |
* v1.22: Add support for different pixel ratios. Also add optional y limit formatters, by @copacetic | |
* v1.23: Fix bug introduced in v1.22 (#44), by @drewnoakes | |
* v1.24: Fix bug introduced in v1.23, re-adding parseFloat to y-axis formatter defaults, by @siggy_sf | |
* v1.25: Fix bug seen when adding a data point to TimeSeries which is older than the current data, by @Nking92 | |
* Draw time labels on top of series, by @comolosabia | |
* Add TimeSeries.clear function, by @drewnoakes | |
* v1.26: Add support for resizing on high device pixel ratio screens, by @copacetic | |
* v1.27: Fix bug introduced in v1.26 for non whole number devicePixelRatio values, by @zmbush | |
* v1.28: Add 'minValueScale' option, by @megawac | |
* Fix 'labelPos' for different size of 'minValueString' 'maxValueString', by @henryn | |
* v1.29: Support responsive sizing, by @drewnoakes | |
*/ | |
;(function(exports) { | |
var Util = { | |
extend: function() { | |
arguments[0] = arguments[0] || {}; | |
for (var i = 1; i < arguments.length; i++) | |
{ | |
for (var key in arguments[i]) | |
{ | |
if (arguments[i].hasOwnProperty(key)) | |
{ | |
if (typeof(arguments[i][key]) === 'object') { | |
if (arguments[i][key] instanceof Array) { | |
arguments[0][key] = arguments[i][key]; | |
} else { | |
arguments[0][key] = Util.extend(arguments[0][key], arguments[i][key]); | |
} | |
} else { | |
arguments[0][key] = arguments[i][key]; | |
} | |
} | |
} | |
} | |
return arguments[0]; | |
} | |
}; | |
/** | |
* Initialises a new <code>TimeSeries</code> with optional data options. | |
* | |
* Options are of the form (defaults shown): | |
* | |
* <pre> | |
* { | |
* resetBounds: true, // enables/disables automatic scaling of the y-axis | |
* resetBoundsInterval: 3000 // the period between scaling calculations, in millis | |
* } | |
* </pre> | |
* | |
* Presentation options for TimeSeries are specified as an argument to <code>SmoothieChart.addTimeSeries</code>. | |
* | |
* @constructor | |
*/ | |
function TimeSeries(options) { | |
this.options = Util.extend({}, TimeSeries.defaultOptions, options); | |
this.clear(); | |
} | |
TimeSeries.defaultOptions = { | |
resetBoundsInterval: 3000, | |
resetBounds: true | |
}; | |
/** | |
* Clears all data and state from this TimeSeries object. | |
*/ | |
TimeSeries.prototype.clear = function() { | |
this.data = []; | |
this.maxValue = Number.NaN; // The maximum value ever seen in this TimeSeries. | |
this.minValue = Number.NaN; // The minimum value ever seen in this TimeSeries. | |
}; | |
/** | |
* Recalculate the min/max values for this <code>TimeSeries</code> object. | |
* | |
* This causes the graph to scale itself in the y-axis. | |
*/ | |
TimeSeries.prototype.resetBounds = function() { | |
if (this.data.length) { | |
// Walk through all data points, finding the min/max value | |
this.maxValue = this.data[0][1]; | |
this.minValue = this.data[0][1]; | |
for (var i = 1; i < this.data.length; i++) { | |
var value = this.data[i][1]; | |
if (value > this.maxValue) { | |
this.maxValue = value; | |
} | |
if (value < this.minValue) { | |
this.minValue = value; | |
} | |
} | |
} else { | |
// No data exists, so set min/max to NaN | |
this.maxValue = Number.NaN; | |
this.minValue = Number.NaN; | |
} | |
}; | |
/** | |
* Adds a new data point to the <code>TimeSeries</code>, preserving chronological order. | |
* | |
* @param timestamp the position, in time, of this data point | |
* @param value the value of this data point | |
* @param sumRepeatedTimeStampValues if <code>timestamp</code> has an exact match in the series, this flag controls | |
* whether it is replaced, or the values summed (defaults to false.) | |
*/ | |
TimeSeries.prototype.append = function(timestamp, value, sumRepeatedTimeStampValues) { | |
// Rewind until we hit an older timestamp | |
var i = this.data.length - 1; | |
while (i >= 0 && this.data[i][0] > timestamp) { | |
i--; | |
} | |
if (i === -1) { | |
// This new item is the oldest data | |
this.data.splice(0, 0, [timestamp, value]); | |
} else if (this.data.length > 0 && this.data[i][0] === timestamp) { | |
// Update existing values in the array | |
if (sumRepeatedTimeStampValues) { | |
// Sum this value into the existing 'bucket' | |
this.data[i][1] += value; | |
value = this.data[i][1]; | |
} else { | |
// Replace the previous value | |
this.data[i][1] = value; | |
} | |
} else if (i < this.data.length - 1) { | |
// Splice into the correct position to keep timestamps in order | |
this.data.splice(i + 1, 0, [timestamp, value]); | |
} else { | |
// Add to the end of the array | |
this.data.push([timestamp, value]); | |
} | |
this.maxValue = isNaN(this.maxValue) ? value : Math.max(this.maxValue, value); | |
this.minValue = isNaN(this.minValue) ? value : Math.min(this.minValue, value); | |
}; | |
TimeSeries.prototype.dropOldData = function(oldestValidTime, maxDataSetLength) { | |
// We must always keep one expired data point as we need this to draw the | |
// line that comes into the chart from the left, but any points prior to that can be removed. | |
var removeCount = 0; | |
while (this.data.length - removeCount >= maxDataSetLength && this.data[removeCount + 1][0] < oldestValidTime) { | |
removeCount++; | |
} | |
if (removeCount !== 0) { | |
this.data.splice(0, removeCount); | |
} | |
}; | |
/** | |
* Initialises a new <code>SmoothieChart</code>. | |
* | |
* Options are optional, and should be of the form below. Just specify the values you | |
* need and the rest will be given sensible defaults as shown: | |
* | |
* <pre> | |
* { | |
* minValue: undefined, // specify to clamp the lower y-axis to a given value | |
* maxValue: undefined, // specify to clamp the upper y-axis to a given value | |
* maxValueScale: 1, // allows proportional padding to be added above the chart. for 10% padding, specify 1.1. | |
* minValueScale: 1, // allows proportional padding to be added below the chart. for 10% padding, specify 1.1. | |
* yRangeFunction: undefined, // function({min: , max: }) { return {min: , max: }; } | |
* scaleSmoothing: 0.125, // controls the rate at which y-value zoom animation occurs | |
* millisPerPixel: 20, // sets the speed at which the chart pans by | |
* enableDpiScaling: true, // support rendering at different DPI depending on the device | |
* yMinFormatter: function(min, precision) { // callback function that formats the min y value label | |
* return parseFloat(min).toFixed(precision); | |
* }, | |
* yMaxFormatter: function(max, precision) { // callback function that formats the max y value label | |
* return parseFloat(max).toFixed(precision); | |
* }, | |
* maxDataSetLength: 2, | |
* interpolation: 'bezier' // one of 'bezier', 'linear', or 'step' | |
* timestampFormatter: null, // optional function to format time stamps for bottom of chart | |
* // you may use SmoothieChart.timeFormatter, or your own: function(date) { return ''; } | |
* scrollBackwards: false, // reverse the scroll direction of the chart | |
* horizontalLines: [], // [ { value: 0, color: '#ffffff', lineWidth: 1 } ] | |
* grid: | |
* { | |
* fillStyle: '#000000', // the background colour of the chart | |
* lineWidth: 1, // the pixel width of grid lines | |
* strokeStyle: '#777777', // colour of grid lines | |
* millisPerLine: 1000, // distance between vertical grid lines | |
* sharpLines: false, // controls whether grid lines are 1px sharp, or softened | |
* verticalSections: 2, // number of vertical sections marked out by horizontal grid lines | |
* borderVisible: true // whether the grid lines trace the border of the chart or not | |
* }, | |
* labels | |
* { | |
* disabled: false, // enables/disables labels showing the min/max values | |
* fillStyle: '#ffffff', // colour for text of labels, | |
* fontSize: 15, | |
* fontFamily: 'sans-serif', | |
* precision: 2 | |
* } | |
* } | |
* </pre> | |
* | |
* @constructor | |
*/ | |
function SmoothieChart(options) { | |
this.options = Util.extend({}, SmoothieChart.defaultChartOptions, options); | |
this.seriesSet = []; | |
this.currentValueRange = 1; | |
this.currentVisMinValue = 0; | |
this.lastRenderTimeMillis = 0; | |
} | |
SmoothieChart.defaultChartOptions = { | |
millisPerPixel: 20, | |
enableDpiScaling: true, | |
yMinFormatter: function(min, precision) { | |
return parseFloat(min).toFixed(precision); | |
}, | |
yMaxFormatter: function(max, precision) { | |
return parseFloat(max).toFixed(precision); | |
}, | |
maxValueScale: 1, | |
minValueScale: 1, | |
interpolation: 'bezier', | |
scaleSmoothing: 0.125, | |
maxDataSetLength: 2, | |
scrollBackwards: false, | |
grid: { | |
fillStyle: '#000000', | |
strokeStyle: '#777777', | |
lineWidth: 1, | |
sharpLines: false, | |
millisPerLine: 1000, | |
verticalSections: 2, | |
borderVisible: true | |
}, | |
labels: { | |
fillStyle: '#ffffff', | |
disabled: false, | |
fontSize: 10, | |
fontFamily: 'monospace', | |
precision: 2 | |
}, | |
horizontalLines: [], | |
responsive: false | |
}; | |
// Based on http://inspirit.github.com/jsfeat/js/compatibility.js | |
SmoothieChart.AnimateCompatibility = (function() { | |
var requestAnimationFrame = function(callback, element) { | |
var requestAnimationFrame = | |
window.requestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
window.mozRequestAnimationFrame || | |
window.oRequestAnimationFrame || | |
window.msRequestAnimationFrame || | |
function(callback) { | |
return window.setTimeout(function() { | |
callback(new Date().getTime()); | |
}, 16); | |
}; | |
return requestAnimationFrame.call(window, callback, element); | |
}, | |
cancelAnimationFrame = function(id) { | |
var cancelAnimationFrame = | |
window.cancelAnimationFrame || | |
function(id) { | |
clearTimeout(id); | |
}; | |
return cancelAnimationFrame.call(window, id); | |
}; | |
return { | |
requestAnimationFrame: requestAnimationFrame, | |
cancelAnimationFrame: cancelAnimationFrame | |
}; | |
})(); | |
SmoothieChart.defaultSeriesPresentationOptions = { | |
lineWidth: 1, | |
strokeStyle: '#ffffff' | |
}; | |
/** | |
* Adds a <code>TimeSeries</code> to this chart, with optional presentation options. | |
* | |
* Presentation options should be of the form (defaults shown): | |
* | |
* <pre> | |
* { | |
* lineWidth: 1, | |
* strokeStyle: '#ffffff', | |
* fillStyle: undefined | |
* } | |
* </pre> | |
*/ | |
SmoothieChart.prototype.addTimeSeries = function(timeSeries, options) { | |
this.seriesSet.push({timeSeries: timeSeries, options: Util.extend({}, SmoothieChart.defaultSeriesPresentationOptions, options)}); | |
if (timeSeries.options.resetBounds && timeSeries.options.resetBoundsInterval > 0) { | |
timeSeries.resetBoundsTimerId = setInterval( | |
function() { | |
timeSeries.resetBounds(); | |
}, | |
timeSeries.options.resetBoundsInterval | |
); | |
} | |
}; | |
/** | |
* Removes the specified <code>TimeSeries</code> from the chart. | |
*/ | |
SmoothieChart.prototype.removeTimeSeries = function(timeSeries) { | |
// Find the correct timeseries to remove, and remove it | |
var numSeries = this.seriesSet.length; | |
for (var i = 0; i < numSeries; i++) { | |
if (this.seriesSet[i].timeSeries === timeSeries) { | |
this.seriesSet.splice(i, 1); | |
break; | |
} | |
} | |
// If a timer was operating for that timeseries, remove it | |
if (timeSeries.resetBoundsTimerId) { | |
// Stop resetting the bounds, if we were | |
clearInterval(timeSeries.resetBoundsTimerId); | |
} | |
}; | |
/** | |
* Gets render options for the specified <code>TimeSeries</code>. | |
* | |
* As you may use a single <code>TimeSeries</code> in multiple charts with different formatting in each usage, | |
* these settings are stored in the chart. | |
*/ | |
SmoothieChart.prototype.getTimeSeriesOptions = function(timeSeries) { | |
// Find the correct timeseries to remove, and remove it | |
var numSeries = this.seriesSet.length; | |
for (var i = 0; i < numSeries; i++) { | |
if (this.seriesSet[i].timeSeries === timeSeries) { | |
return this.seriesSet[i].options; | |
} | |
} | |
}; | |
/** | |
* Brings the specified <code>TimeSeries</code> to the top of the chart. It will be rendered last. | |
*/ | |
SmoothieChart.prototype.bringToFront = function(timeSeries) { | |
// Find the correct timeseries to remove, and remove it | |
var numSeries = this.seriesSet.length; | |
for (var i = 0; i < numSeries; i++) { | |
if (this.seriesSet[i].timeSeries === timeSeries) { | |
var set = this.seriesSet.splice(i, 1); | |
this.seriesSet.push(set[0]); | |
break; | |
} | |
} | |
}; | |
/** | |
* Instructs the <code>SmoothieChart</code> to start rendering to the provided canvas, with specified delay. | |
* | |
* @param canvas the target canvas element | |
* @param delayMillis an amount of time to wait before a data point is shown. This can prevent the end of the series | |
* from appearing on screen, with new values flashing into view, at the expense of some latency. | |
*/ | |
SmoothieChart.prototype.streamTo = function(canvas, delayMillis) { | |
this.canvas = canvas; | |
this.delay = delayMillis; | |
this.start(); | |
}; | |
/** | |
* Make sure the canvas has the optimal resolution for the device's pixel ratio. | |
*/ | |
SmoothieChart.prototype.resize = function () { | |
var dpr = !this.options.enableDpiScaling || !window ? window.devicePixelRatio : 1, | |
width, height; | |
if (this.options.responsive) { | |
// Newer behaviour: Use the canvas's size in the layout, and set the internal | |
// resolution according to that size and the device pixel ratio (eg: high DPI) | |
width = this.canvas.offsetWidth; | |
height = this.canvas.offsetHeight; | |
if (width !== this.lastWidth) { | |
this.lastWidth = width; | |
this.canvas.setAttribute('width', (Math.floor(width * dpr)).toString()); | |
} | |
if (height !== this.lastHeight) { | |
this.lastHeight = height; | |
this.canvas.setAttribute('height', (Math.floor(height * dpr)).toString()); | |
} | |
} else if (dpr !== 1) { | |
// Older behaviour: use the canvas's inner dimensions and scale the element's size | |
// according to that size and the device pixel ratio (eg: high DPI) | |
width = parseInt(this.canvas.getAttribute('width')); | |
height = parseInt(this.canvas.getAttribute('height')); | |
if (!this.originalWidth || (Math.floor(this.originalWidth * dpr) !== width)) { | |
this.originalWidth = width; | |
this.canvas.setAttribute('width', (Math.floor(width * dpr)).toString()); | |
this.canvas.style.width = width + 'px'; | |
this.canvas.getContext('2d').scale(dpr, dpr); | |
} | |
if (!this.originalHeight || (Math.floor(this.originalHeight * dpr) !== height)) { | |
this.originalHeight = height; | |
this.canvas.setAttribute('height', (Math.floor(height * dpr)).toString()); | |
this.canvas.style.height = height + 'px'; | |
this.canvas.getContext('2d').scale(dpr, dpr); | |
} | |
} | |
}; | |
/** | |
* Starts the animation of this chart. | |
*/ | |
SmoothieChart.prototype.start = function() { | |
if (this.frame) { | |
// We're already running, so just return | |
return; | |
} | |
// Renders a frame, and queues the next frame for later rendering | |
var animate = function() { | |
this.frame = SmoothieChart.AnimateCompatibility.requestAnimationFrame(function() { | |
this.render(); | |
animate(); | |
}.bind(this)); | |
}.bind(this); | |
animate(); | |
}; | |
/** | |
* Stops the animation of this chart. | |
*/ | |
SmoothieChart.prototype.stop = function() { | |
if (this.frame) { | |
SmoothieChart.AnimateCompatibility.cancelAnimationFrame(this.frame); | |
delete this.frame; | |
} | |
}; | |
SmoothieChart.prototype.updateValueRange = function() { | |
// Calculate the current scale of the chart, from all time series. | |
var chartOptions = this.options, | |
chartMaxValue = Number.NaN, | |
chartMinValue = Number.NaN; | |
for (var d = 0; d < this.seriesSet.length; d++) { | |
// TODO(ndunn): We could calculate / track these values as they stream in. | |
var timeSeries = this.seriesSet[d].timeSeries; | |
if (!isNaN(timeSeries.maxValue)) { | |
chartMaxValue = !isNaN(chartMaxValue) ? Math.max(chartMaxValue, timeSeries.maxValue) : timeSeries.maxValue; | |
} | |
if (!isNaN(timeSeries.minValue)) { | |
chartMinValue = !isNaN(chartMinValue) ? Math.min(chartMinValue, timeSeries.minValue) : timeSeries.minValue; | |
} | |
} | |
// Scale the chartMaxValue to add padding at the top if required | |
if (chartOptions.maxValue != null) { | |
chartMaxValue = chartOptions.maxValue; | |
} else { | |
chartMaxValue *= chartOptions.maxValueScale; | |
} | |
// Set the minimum if we've specified one | |
if (chartOptions.minValue != null) { | |
chartMinValue = chartOptions.minValue; | |
} else { | |
chartMinValue -= Math.abs(chartMinValue * chartOptions.minValueScale - chartMinValue); | |
} | |
// If a custom range function is set, call it | |
if (this.options.yRangeFunction) { | |
var range = this.options.yRangeFunction({min: chartMinValue, max: chartMaxValue}); | |
chartMinValue = range.min; | |
chartMaxValue = range.max; | |
} | |
if (!isNaN(chartMaxValue) && !isNaN(chartMinValue)) { | |
var targetValueRange = chartMaxValue - chartMinValue; | |
var valueRangeDiff = (targetValueRange - this.currentValueRange); | |
var minValueDiff = (chartMinValue - this.currentVisMinValue); | |
this.isAnimatingScale = Math.abs(valueRangeDiff) > 0.1 || Math.abs(minValueDiff) > 0.1; | |
this.currentValueRange += chartOptions.scaleSmoothing * valueRangeDiff; | |
this.currentVisMinValue += chartOptions.scaleSmoothing * minValueDiff; | |
} | |
this.valueRange = { min: chartMinValue, max: chartMaxValue }; | |
}; | |
SmoothieChart.prototype.render = function(canvas, time) { | |
var nowMillis = new Date().getTime(); | |
if (!this.isAnimatingScale) { | |
// We're not animating. We can use the last render time and the scroll speed to work out whether | |
// we actually need to paint anything yet. If not, we can return immediately. | |
// Render at least every 1/6th of a second. The canvas may be resized, which there is | |
// no reliable way to detect. | |
var maxIdleMillis = Math.min(1000/6, this.options.millisPerPixel); | |
if (nowMillis - this.lastRenderTimeMillis < maxIdleMillis) { | |
return; | |
} | |
} | |
this.resize(); | |
this.lastRenderTimeMillis = nowMillis; | |
canvas = canvas || this.canvas; | |
time = time || nowMillis - (this.delay || 0); | |
// Round time down to pixel granularity, so motion appears smoother. | |
time -= time % this.options.millisPerPixel; | |
var context = canvas.getContext('2d'), | |
chartOptions = this.options, | |
dimensions = { top: 0, left: 0, width: canvas.clientWidth, height: canvas.clientHeight }, | |
// Calculate the threshold time for the oldest data points. | |
oldestValidTime = time - (dimensions.width * chartOptions.millisPerPixel), | |
valueToYPixel = function(value) { | |
var offset = value - this.currentVisMinValue; | |
return this.currentValueRange === 0 | |
? dimensions.height | |
: dimensions.height - (Math.round((offset / this.currentValueRange) * dimensions.height)); | |
}.bind(this), | |
timeToXPixel = function(t) { | |
if(chartOptions.scrollBackwards) { | |
return Math.round((time - t) / chartOptions.millisPerPixel); | |
} | |
return Math.round(dimensions.width - ((time - t) / chartOptions.millisPerPixel)); | |
}; | |
this.updateValueRange(); | |
context.font = chartOptions.labels.fontSize + 'px ' + chartOptions.labels.fontFamily; | |
// Save the state of the canvas context, any transformations applied in this method | |
// will get removed from the stack at the end of this method when .restore() is called. | |
context.save(); | |
// Move the origin. | |
context.translate(dimensions.left, dimensions.top); | |
// Create a clipped rectangle - anything we draw will be constrained to this rectangle. | |
// This prevents the occasional pixels from curves near the edges overrunning and creating | |
// screen cheese (that phrase should need no explanation). | |
context.beginPath(); | |
context.rect(0, 0, dimensions.width, dimensions.height); | |
context.clip(); | |
// Clear the working area. | |
context.save(); | |
context.fillStyle = chartOptions.grid.fillStyle; | |
context.clearRect(0, 0, dimensions.width, dimensions.height); | |
context.fillRect(0, 0, dimensions.width, dimensions.height); | |
context.restore(); | |
// Grid lines... | |
context.save(); | |
context.lineWidth = chartOptions.grid.lineWidth; | |
context.strokeStyle = chartOptions.grid.strokeStyle; | |
// Vertical (time) dividers. | |
if (chartOptions.grid.millisPerLine > 0) { | |
context.beginPath(); | |
for (var t = time - (time % chartOptions.grid.millisPerLine); | |
t >= oldestValidTime; | |
t -= chartOptions.grid.millisPerLine) { | |
var gx = timeToXPixel(t); | |
if (chartOptions.grid.sharpLines) { | |
gx -= 0.5; | |
} | |
context.moveTo(gx, 0); | |
context.lineTo(gx, dimensions.height); | |
} | |
context.stroke(); | |
context.closePath(); | |
} | |
// Horizontal (value) dividers. | |
for (var v = 1; v < chartOptions.grid.verticalSections; v++) { | |
var gy = Math.round(v * dimensions.height / chartOptions.grid.verticalSections); | |
if (chartOptions.grid.sharpLines) { | |
gy -= 0.5; | |
} | |
context.beginPath(); | |
context.moveTo(0, gy); | |
context.lineTo(dimensions.width, gy); | |
context.stroke(); | |
context.closePath(); | |
} | |
// Bounding rectangle. | |
if (chartOptions.grid.borderVisible) { | |
context.beginPath(); | |
context.strokeRect(0, 0, dimensions.width, dimensions.height); | |
context.closePath(); | |
} | |
context.restore(); | |
// Draw any horizontal lines... | |
if (chartOptions.horizontalLines && chartOptions.horizontalLines.length) { | |
for (var hl = 0; hl < chartOptions.horizontalLines.length; hl++) { | |
var line = chartOptions.horizontalLines[hl], | |
hly = Math.round(valueToYPixel(line.value)) - 0.5; | |
context.strokeStyle = line.color || '#ffffff'; | |
context.lineWidth = line.lineWidth || 1; | |
context.beginPath(); | |
context.moveTo(0, hly); | |
context.lineTo(dimensions.width, hly); | |
context.stroke(); | |
context.closePath(); | |
} | |
} | |
// For each data set... | |
for (var d = 0; d < this.seriesSet.length; d++) { | |
context.save(); | |
var timeSeries = this.seriesSet[d].timeSeries, | |
dataSet = timeSeries.data, | |
seriesOptions = this.seriesSet[d].options; | |
// Delete old data that's moved off the left of the chart. | |
timeSeries.dropOldData(oldestValidTime, chartOptions.maxDataSetLength); | |
// Set style for this dataSet. | |
context.lineWidth = seriesOptions.lineWidth; | |
context.strokeStyle = seriesOptions.strokeStyle; | |
// Draw the line... | |
context.beginPath(); | |
// Retain lastX, lastY for calculating the control points of bezier curves. | |
var firstX = 0, lastX = 0, lastY = 0; | |
for (var i = 0; i < dataSet.length && dataSet.length !== 1; i++) { | |
var x = timeToXPixel(dataSet[i][0]), | |
y = valueToYPixel(dataSet[i][1]); | |
if (i === 0) { | |
firstX = x; | |
context.moveTo(x, y); | |
} else { | |
switch (chartOptions.interpolation) { | |
case "linear": | |
case "line": { | |
context.lineTo(x,y); | |
break; | |
} | |
case "bezier": | |
default: { | |
// Great explanation of Bezier curves: http://en.wikipedia.org/wiki/Bezier_curve#Quadratic_curves | |
// | |
// Assuming A was the last point in the line plotted and B is the new point, | |
// we draw a curve with control points P and Q as below. | |
// | |
// A---P | |
// | | |
// | | |
// | | |
// Q---B | |
// | |
// Importantly, A and P are at the same y coordinate, as are B and Q. This is | |
// so adjacent curves appear to flow as one. | |
// | |
context.bezierCurveTo( // startPoint (A) is implicit from last iteration of loop | |
Math.round((lastX + x) / 2), lastY, // controlPoint1 (P) | |
Math.round((lastX + x)) / 2, y, // controlPoint2 (Q) | |
x, y); // endPoint (B) | |
break; | |
} | |
case "step": { | |
context.lineTo(x,lastY); | |
context.lineTo(x,y); | |
break; | |
} | |
} | |
} | |
lastX = x; lastY = y; | |
} | |
if (dataSet.length > 1) { | |
if (seriesOptions.fillStyle) { | |
// Close up the fill region. | |
context.lineTo(dimensions.width + seriesOptions.lineWidth + 1, lastY); | |
context.lineTo(dimensions.width + seriesOptions.lineWidth + 1, dimensions.height + seriesOptions.lineWidth + 1); | |
context.lineTo(firstX, dimensions.height + seriesOptions.lineWidth); | |
context.fillStyle = seriesOptions.fillStyle; | |
context.fill(); | |
} | |
if (seriesOptions.strokeStyle && seriesOptions.strokeStyle !== 'none') { | |
context.stroke(); | |
} | |
context.closePath(); | |
} | |
context.restore(); | |
} | |
// Draw the axis values on the chart. | |
if (!chartOptions.labels.disabled && !isNaN(this.valueRange.min) && !isNaN(this.valueRange.max)) { | |
var maxValueString = chartOptions.yMaxFormatter(this.valueRange.max, chartOptions.labels.precision), | |
minValueString = chartOptions.yMinFormatter(this.valueRange.min, chartOptions.labels.precision), | |
maxLabelPos = chartOptions.scrollBackwards ? 0 : dimensions.width - context.measureText(maxValueString).width - 2, | |
minLabelPos = chartOptions.scrollBackwards ? 0 : dimensions.width - context.measureText(minValueString).width - 2; | |
context.fillStyle = chartOptions.labels.fillStyle; | |
context.fillText(maxValueString, maxLabelPos, chartOptions.labels.fontSize); | |
context.fillText(minValueString, minLabelPos, dimensions.height - 2); | |
} | |
// Display timestamps along x-axis at the bottom of the chart. | |
if (chartOptions.timestampFormatter && chartOptions.grid.millisPerLine > 0) { | |
var textUntilX = chartOptions.scrollBackwards | |
? context.measureText(minValueString).width | |
: dimensions.width - context.measureText(minValueString).width + 4; | |
for (var t = time - (time % chartOptions.grid.millisPerLine); | |
t >= oldestValidTime; | |
t -= chartOptions.grid.millisPerLine) { | |
var gx = timeToXPixel(t); | |
// Only draw the timestamp if it won't overlap with the previously drawn one. | |
if ((!chartOptions.scrollBackwards && gx < textUntilX) || (chartOptions.scrollBackwards && gx > textUntilX)) { | |
// Formats the timestamp based on user specified formatting function | |
// SmoothieChart.timeFormatter function above is one such formatting option | |
var tx = new Date(t), | |
ts = chartOptions.timestampFormatter(tx), | |
tsWidth = context.measureText(ts).width; | |
textUntilX = chartOptions.scrollBackwards | |
? gx + tsWidth + 2 | |
: gx - tsWidth - 2; | |
context.fillStyle = chartOptions.labels.fillStyle; | |
if(chartOptions.scrollBackwards) { | |
context.fillText(ts, gx, dimensions.height - 2); | |
} else { | |
context.fillText(ts, gx - tsWidth, dimensions.height - 2); | |
} | |
} | |
} | |
} | |
context.restore(); // See .save() above. | |
}; | |
// Sample timestamp formatting function | |
SmoothieChart.timeFormatter = function(date) { | |
function pad2(number) { return (number < 10 ? '0' : '') + number } | |
return pad2(date.getHours()) + ':' + pad2(date.getMinutes()) + ':' + pad2(date.getSeconds()); | |
}; | |
exports.TimeSeries = TimeSeries; | |
exports.SmoothieChart = SmoothieChart; | |
})(typeof exports === 'undefined' ? this : exports); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@charset "utf-8"; | |
/* CSS Document */ | |
/*************************** RESET ***************************/ | |
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td { | |
margin: 0; | |
padding: 0; | |
border: 0; | |
outline: 0; | |
font-size: 100%; | |
vertical-align: baseline; | |
background: transparent; | |
} | |
/*************************** RESET ***************************/ | |
html, body { | |
height: 100%; | |
width: 100%; | |
font-family: 'Lato', Verdana, "Hiragino Kaku Gothic ProN", Meiryo, sans-serif; | |
min-width: 640px; | |
position: relative; | |
background-color: #f0f0f0; | |
} | |
hr{ | |
border-width: 1px; | |
border-color: #fff; | |
border-style: solid; | |
margin: 20px 0px 20px 0px; | |
} | |
.container{ | |
margin: auto; | |
width: 100%; | |
min-height: 100%; | |
max-width: 640px; | |
box-shadow: 0px 0px 20px rgba(0,0,0,0.3); | |
background-color: #f0f0f0; | |
position: relative; | |
} | |
.title{ | |
width: 100%; | |
background-color: #fff; | |
padding: 20px 0px 20px 0px; | |
} | |
.contents{ | |
padding: 25px 0px 100px 0px; | |
} | |
.footer{ | |
width: 100%; | |
font-size:12px; | |
position: absolute; | |
bottom: 0px; | |
height: 60px; | |
color: #9a9a9a; | |
} | |
.margin{ | |
border-left: 70px; | |
border-right: 70px; | |
border-style: solid; | |
box-sizing: border-box; | |
border-color: rgba(0, 0, 0, 0); | |
} | |
.button{ | |
border-width: 1px; | |
border-color: #e6e6e6; | |
border-style: solid; | |
background-color: #fff; | |
padding: 5px 20px 5px 20px; | |
color: #4d4d4d; | |
font-size: 14px; | |
font-weight: 300; | |
margin: 0px; | |
} | |
.button:hover{ | |
border-color: #ccc; | |
} | |
.button:active{ | |
background-color: #f0f0f0; | |
} | |
#chart{ | |
margin-bottom: 20px; | |
} | |
#title{ | |
color:#27B34F; | |
font-size: 21px; | |
font-weight: 700; | |
line-height: 2em; | |
} | |
#subtitle{ | |
color:#808080; | |
font-size: 15px; | |
font-weight: 300; | |
} | |
#data_text{ | |
color:#009FBD; | |
font-size:72px; | |
font-weight: 300; | |
line-height: 1.5em; | |
} | |
#device_name{ | |
color:#27B34F; | |
font-size:36px; | |
font-weight: 300; | |
line-height: 1.5em; | |
} | |
#uuid_name{ | |
color:#27B34F; | |
font-size:14px; | |
font-weight: 300; | |
} | |
#status{ | |
color:#009FBD; | |
font-size:14px; | |
font-weight: 300; | |
} | |
a { text-decoration: none; } | |
a:link { color: #9a9a9a; } | |
a:visited { color: #9a9a9a; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment