document

http://iwantmyreal.name/blog/2012/09/16/visualising-conair-data-with-cubism-dot-js

https://hveem.no/vm-monitoring-using-salt-and-cubism

http://sciviz.info/time-series-visualization-using-cubism-js

http://square.github.io/cubism/

api

https://github.com/square/cubism/wiki

example

<script>
function random(name) {
var value = 0,
values = [],
i = 0,
last;
/* 支持cube, graphite内建方法, 也可以使用context.metric()方法构造新的数据源。
context.metric()有一个request函数参数, 还有一个可选的name参数。start & stop是日期类型,step单位ms。
callback有2个参数,第1参数用于node.js,第2参数是数值形数组 */
return context.metric(function(start, stop, step, callback) {
start = +start, stop = +stop;
if (isNaN(last)) last = start;
while (last < stop) {
last += step;
value = Math.max(-10, Math.min(10, value + .8 * Math.random() - .4 + .2 * Math.cos(i += .2)));
values.push(value);
}
callback(null, values = values.slice((start - stop) / step));
}, name);
} var context = cubism.context()
.serverDelay(0) // 服务器延时
.clientDelay(0) // 客户端延时
.step(1e3) // 步进1e3(1000ms), cubism的时间单位是ms
.size(960); // 960 * 1s, 时间轴是16分钟 var foo = random("foo"),
bar = random("bar"); // example1
d3.select("#example1").call(function(div) {
div.append("div")
.attr("class", "axis")
.call(context.axis().orient("top")); div.selectAll(".horizon")
.data([foo, bar, foo.add(bar), foo.subtract(bar)])
.enter().append("div")
.attr("class", "horizon")
.call(context.horizon().extent([-20, 20])); div.append("div")
.attr("class", "rule")
.call(context.rule()); }); // example2a
d3.select("#example2a").call(function(div) {
div.datum(foo); div.append("div")
.attr("class", "horizon")
.call(context.horizon()
.height(120)
.mode("mirror")
.colors(["#bdd7e7","#bae4b3"])
.title("Area (120px)")
.extent([-10, 10])); div.append("div")
.attr("class", "horizon")
.call(context.horizon()
.height(30)
.mode("mirror")
.colors(["#bdd7e7","#bae4b3"])
.title("Area (30px)")
.extent([-10, 10]));
}); // example2b
d3.select("#example2b").call(function(div) {
div.datum(foo); div.append("div")
.attr("class", "horizon")
.call(context.horizon()
.height(120)
.colors(["#bdd7e7","#bae4b3"])
.title("Horizon, 1-band (120px)")
.extent([-10, 10])); div.append("div")
.attr("class", "horizon")
.call(context.horizon()
.height(60)
.colors(["#6baed6","#bdd7e7","#bae4b3","#74c476"])
.title("Horizon, 2-band (60px)")
.extent([-10, 10])); div.append("div")
.attr("class", "horizon")
.call(context.horizon()
.height(40)
.colors(["#3182bd","#6baed6","#bdd7e7","#bae4b3","#74c476","#31a354"])
.title("Horizon, 3-band (40px)")
.extent([-10, 10])); div.append("div")
.attr("class", "horizon")
.call(context.horizon()
.height(30)
.colors(["#08519c","#3182bd","#6baed6","#bdd7e7","#bae4b3","#74c476","#31a354","#006d2c"])
.title("Horizon, 4-band (30px)")
.extent([-10, 10])); }); // On mousemove, reposition the chart values to match the rule.
context.on("focus", function(i) {
d3.selectAll(".value").style("right", i == null ? null : context.size() - i + "px");
});

我的程序

<html>
<head>
<title>dashboard</title>
<script src="{{ url_for('static', filename='js/d3.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/cubism.v1.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/jquery-1.11.3.min.js') }}"></script>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<div id="cubism"></div>
</body> <script type="text/javascript">
function analysis(name, cname, bandwidth, interface, threshold, measure) {
var label = ((measure.split('_')[1] == 'rx') ? 'In' : 'Out') + ' ' +cname;
var result = context.metric(function(start, stop, step, callback) {
$.ajax({
type: 'GET',
url: '/api',
data: {
'name': name,
'interface': interface,
'measure': measure,
'start': start.toISOString(),
'stop': stop.toISOString()
},
dataType: 'json',
success: function(data) {
callback(null, data);
//console.log(data);
},
});
}, label); return result;
} var seconds = 60;
var default_colors = ['#08519c', '#3182bd', '#6baed6', '#bdd7e7', '#bae4b3', '#74c476', '#31a354', '#006d2c'];
var custom_colors = ['#ffcc99', '#ff9999', '#ffcc00', '#ff6666']; //default_colors = custom_colors; var context = cubism.context()
.serverDelay(seconds*1000)
.step(seconds*1000)
.size(60*16); var dataset = []; {% for switch in switches %}
var info = ['{{ switch.name }}', '{{ switch.cname }}', '{{ switch.bandwidth }}', '{{ switch.interface }}', '{{ switch.threshold }}'];
var traffic_in = analysis(info[0], info[1], info[2], info[3], info[4], 'snmp_rx'),
traffic_out = analysis(info[0], info[1], info[2], info[3], info[4], 'snmp_tx');
dataset.push(traffic_in, traffic_out);
{% endfor %} d3.select("#cubism").call(function(div) {
div.append("div")
.attr("class", "axis")
.call(context.axis()
.tickFormat(d3.time.format("%H:%M"))
.ticks(d3.time.minutes, 60)
.orient("top")); div.selectAll(".horizon")
.data(dataset)
.enter().append("div")
.attr("class", "horizon")
.call(context.horizon()
.height(30)
.colors(default_colors)
.format(d3.format('.2f'))); div.append("div")
.attr("class", "rule")
.call(context.rule());
}); context.on("focus", function(i) {
d3.selectAll(".value").style("right", i == null ? null : context.size() - i + "px");
});
</script>
</html>
#!/usr/bin/env python
# -*- encoding: utf-8 -*- import json
import yaml
from flask import Flask, render_template, request, url_for
from influxdb import InfluxDBClient app = Flask(__name__)
client = InfluxDBClient(host='10.10.32.109', port=8086, username='root', password='root', database='collectd') @app.route('/', methods=['GET'])
def index():
file = 'switch.yaml'
with open(file) as f:
switches = yaml.load(f)
return render_template('dashboard.html', switches=switches) @app.route('/cubism', methods=['GET'])
def cubism():
return render_template('cubism.html') @app.route('/api', methods=['GET'])
def api():
host, interface, measure, start, stop = \
request.args['name'], request.args['interface'], request.args['measure'], \
request.args['start'], request.args['stop']
file = 'dashboard.yaml' query = 'select derivative(value)*8/1000/1000 from '+ measure \
+ ' where host=\'' + host + '\' and type_instance=\'' + interface \
+ '\' and time > \'' + start \
+ '\' and time < \'' + stop +'\'' print query
result = client.query(query)
return json.dumps([value[1] for value in result.raw['series'][0]['values'] ]) if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True) # influxdb的sql可能有bug,按时间段取值,结果会丢失第1个值
# curl http://127.0.0.1:5000/api?name=bjtn&measure=traffic_out&start=2015-07-28T08:10:00Z&stop=2015-07-28T08:50:59Z

时序js插件cubism使用的更多相关文章

  1. 前端开发需要了解的JS插件

    excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js 用途:构建数据统计图表,兼容多浏览器 jquer ...

  2. 【jQuery小实例】js 插件 查看图片

    ---本系列文章所用使用js均可在本博客文件中找到. 像淘宝一样,鼠标放在某一件商品上,展示大图信息,甚至查看图片的具体部位.给人超炫的效果,这种效果实现基于js文件和js插件.大致可以分为三步,添加 ...

  3. 【PC端】jQuery+PHP实现浏览更多内容(jquery.more.js插件)

    参数说明: 'amount' : '10', //每次显示记录数 'address' : 'comments.php', //请求后台的地址 'format' : 'json', //数据传输格式 ' ...

  4. chart.js插件生成折线图时数据普遍较大时Y轴数据不从0开始的解决办法[bubuko.com]

    chart.js插件生成折线图时数据普遍较大时Y轴数据不从0开始的解决办法,原文:http://bubuko.com/infodetail-328671.html 默认情况下如下图 Y轴并不是从0开始 ...

  5. jQuery.cookie.js插件了解及使用方法

    jquery.cookie.js插件实现浏览器的cookie存储,该插件是基于jquery开发,方便cookie使用. jquerycookie.js的下载地址 http://plugins.jque ...

  6. Intense Images – 全屏浏览图像的 JS 插件

    Intense Images 是一个独立的 JavaScript 库,用于查看全屏图像.使用触摸/鼠标来实现图片位置的平移.图像元素的所有样式都是可以自定义的,Intense.js 只处理图像浏览器和 ...

  7. 购物车增加、减少商品时动画效果:jQuery.Fly.js插件使用方法

    某些电商网站加入购物车和减少购物车商品数量时,有个小动画,以抛物线形式增减,如图:      这里用到了第三方jQuery.Fly.js插件(底层依赖Jquery库,地址:https://github ...

  8. 代码规范和常用的js插件以及测试工具

    1.代码规范 .model层 1.1.1database file_proerty 1.1.2java fileProperty. 1.2.字段要有空指针 1.3.不创建爱数据库外键约束 1.4.已知 ...

  9. jquery.autocomplete.js 插件的自定义搜索规则

    这二天开始用jquery.autocomplete这个自动完成插件.功能基本比较强大,但自己在实际需求中发现还是有一处不足!问题是这样:当我定义了一个本地数据JS文件时,格式为JSON式的数组.如下: ...

随机推荐

  1. Entity Framework Code-First(12):Configure One-to-Many

    Configure One-to-Many Relationship: Here, we will learn how to configure One-to-Many relationship be ...

  2. Java引用的四种状态

    强引用 用的最广,我们平时写代码时,new一个Object存放在堆内存,然后用一个引用指向它.它就是强引用. 如果一个对象具有强引用,那么垃圾回收期绝不会回收它,当内存空间不足,java虚拟机宁愿抛出 ...

  3. jq中打开新页面 并获取携带值

    打开新页面:window.location.href = "./index.html?id=1"获取携带值: function GetRequest() { var url = l ...

  4. unity3d AssetStore 下载的资源位置

    Win7,8和Win10系统下: 进入C:\Users\“用户名”目录,然后打开文件夹选项的显示隐藏文件选项 再进入AppData\Roaming\Unity/Asset Store-5.x下找到 M ...

  5. (linux)安装redis---简装

    redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了memcached的不足,它支持存储的value类型相对更多,包括strin ...

  6. IIS中使用子目录文件作为默认文档(Default Document)替代重定向

    以前一直以为IIS应用程序的默认文档只能设置根目录下的文件,像index.html,default.aspx等,后来经同事指点,原来子目录或者子应用程序下的文件也可以添加到根应用程序的默认文档列表中. ...

  7. 解读人:刘佳维,Spectral Clustering Improves Label-Free Quantification of Low-Abundant Proteins(谱图聚类改善了低丰度蛋白的无标记定量)

    发表时间:(2019年4月) IF:3.95 单位: 维也纳医科大学: 欧洲生物信息研究所(EMBL-EBI): 分子病理学研究所: 奥地利科学院分子生物技术研究所: Gregor Mendel分子植 ...

  8. 让你的spring-boot应用日志随心所欲--spring boot日志深入分析

    1.spring boot日志概述 spring boot使用Commons Logging作为内部的日志系统,并且给Java Util Logging,Log4J2以及Logback都提供了默认的配 ...

  9. 树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松

    3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 Description ​ 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉 松路线要尽 ...

  10. 分层图最短路【bzoj2763】: [JLOI2011]飞行路线

    bzoj2763: [JLOI2011]飞行路线 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0 ...