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. c++中Int装string

    java中,string类型非常强大,任何类型和string类型相加都变成了string类型.但是c++中string功能就比较少 int转string有两种方式 1:stringstream; ; ...

  2. “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第五篇(用户管理之“用户权限分配”)

    一.在做权限分配之前,首先先了解“ZTree”这个插件,我的这个系统没有用Jquery-EasyUI的Tree.用的是”ZTree“朋友们可以试试,也很强大.点击下载ZTree插件.       1. ...

  3. QueryString

  4. HTML5学习笔记(六)web worker

    当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成.web worker 是运行在后台的 JavaScript,不会影响页面的性能,页面可以响应. 在创建 web worker ...

  5. OpenStack 负载均衡底层命令创建流程

    1.创建负载均衡器pool-------------不支持高可用的负载均衡器 neutron lb-pool-create --lb-method ROUND_ROBIN --name mypool- ...

  6. MySQL zip安装问题

    今天安装mysql的压缩版出现了问题,就是服务总是启动不了,折腾了两三个小时.后面实在是想不明白,就直接把注册表的东西删了. 如果你之前安装过mysql,则进行删除mysql:E:\work\mysq ...

  7. Sublime Text 3 多行游标

    选中要修改的地方ctrl+D ,要跳过不需要修改的选中的就用ctrl+k+d 选中要修改的地方ctrl+D,选中所有要修改的 alt+f3 ctrl+A  ,然后ctrl+shift+L 按住shif ...

  8. BestCoder Round #86 1002

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5805 题意:删除数列中一个数,找出相邻之差绝对值最大,求依次删除最大值的和 解法:删除边缘位置的数字需要注 ...

  9. java 单例模式之线程安全的饿汉模式和懒汉模式

    转载博主:thankyou https://blog.csdn.net/twj13162380953/article/details/53869983 理解: 饿汉式获取实例的步骤简单所以线程更安全. ...

  10. 【ACM】子串和 - 贪心算法

    子串和 时间限制:5000 ms  |  内存限制:65535 KB 难度:3   描述 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使得该子序列的和最 ...