时序js插件cubism使用
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使用的更多相关文章
- 前端开发需要了解的JS插件
excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js 用途:构建数据统计图表,兼容多浏览器 jquer ...
- 【jQuery小实例】js 插件 查看图片
---本系列文章所用使用js均可在本博客文件中找到. 像淘宝一样,鼠标放在某一件商品上,展示大图信息,甚至查看图片的具体部位.给人超炫的效果,这种效果实现基于js文件和js插件.大致可以分为三步,添加 ...
- 【PC端】jQuery+PHP实现浏览更多内容(jquery.more.js插件)
参数说明: 'amount' : '10', //每次显示记录数 'address' : 'comments.php', //请求后台的地址 'format' : 'json', //数据传输格式 ' ...
- chart.js插件生成折线图时数据普遍较大时Y轴数据不从0开始的解决办法[bubuko.com]
chart.js插件生成折线图时数据普遍较大时Y轴数据不从0开始的解决办法,原文:http://bubuko.com/infodetail-328671.html 默认情况下如下图 Y轴并不是从0开始 ...
- jQuery.cookie.js插件了解及使用方法
jquery.cookie.js插件实现浏览器的cookie存储,该插件是基于jquery开发,方便cookie使用. jquerycookie.js的下载地址 http://plugins.jque ...
- Intense Images – 全屏浏览图像的 JS 插件
Intense Images 是一个独立的 JavaScript 库,用于查看全屏图像.使用触摸/鼠标来实现图片位置的平移.图像元素的所有样式都是可以自定义的,Intense.js 只处理图像浏览器和 ...
- 购物车增加、减少商品时动画效果:jQuery.Fly.js插件使用方法
某些电商网站加入购物车和减少购物车商品数量时,有个小动画,以抛物线形式增减,如图: 这里用到了第三方jQuery.Fly.js插件(底层依赖Jquery库,地址:https://github ...
- 代码规范和常用的js插件以及测试工具
1.代码规范 .model层 1.1.1database file_proerty 1.1.2java fileProperty. 1.2.字段要有空指针 1.3.不创建爱数据库外键约束 1.4.已知 ...
- jquery.autocomplete.js 插件的自定义搜索规则
这二天开始用jquery.autocomplete这个自动完成插件.功能基本比较强大,但自己在实际需求中发现还是有一处不足!问题是这样:当我定义了一个本地数据JS文件时,格式为JSON式的数组.如下: ...
随机推荐
- 关于java中的编码问题
ok,今天搞了一天都在探索java字符的编码问题.十分头疼.最后终于得出几点: 1.网上有很多博客说判断一个String的编码的方法是通过如下代码;但其实这个代码完全是错的,用一种编码decode后, ...
- 一个web.Config或app.Config自定义段configSections的示例--转
直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml配置文件,简洁方便得多.这两个配置文件不仅有常见的connectionStrings和appSetti ...
- CODING 告诉你硅谷项目经理的项目管理之道(2)
优秀的项目管理者是怎么工作的?如何帮助研发团队高效工作?这一直是 CODING 关注的重要话题,我们不断地打磨 CODING 研发管理系统来让开发更简单. 近期我们精心挑选了几篇硅谷科技公司研发管理者 ...
- ARC085F(动态规划,线段树)
#include<bits/stdc++.h>using namespace std;const int maxn = 0x3f3f3f3f;int mn[801000];int cost ...
- hdu 1848 Fibonacci again and again(SG函数)
Fibonacci again and again HDU - 1848 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)= ...
- cogs 2123. [HZOI 2015] Glass Beads
2123. [HZOI 2015] Glass Beads ★★★ 输入文件:MinRepresentations.in 输出文件:MinRepresentations.out 简单对比时 ...
- Boost多线程
一.概述 线程是在同一程序同一时间内允许执行不同函数的离散处理队列,这使得在一个长时间进行某种特殊运算的函数在执行时不阻碍其他的函数时变得十分重要.线程实际上允许同时执行两种函数,而这两者不必 ...
- UVa 10256(凸包、线段交、点在多边形内)
要点 红蓝点分别求凸包显然 判断两凸包是否相交方法:所有红点不在蓝凸包内,反之亦然:所有红凸包线不与蓝凸包线相交,反之亦然. 书上让特判一下凸包退化成点或线段的情况,为什么我感觉代码仓库的代码并没特判 ...
- Restful 3 -- 序列化组件(GET/PUT/DELETE接口设计)、视图优化组件
一.序列化组件 基于上篇随笔的表结构,通过序列化组件的ModelSerializer设计如下三个接口: GET 127.0.0.1:8000/books/{id} # 获取一条数据,返回值:{} PU ...
- SpringMVC(一) 基础知识+入门案例
SpringMVC基础知识 1.什么是Springmvc 2.springmvc 框架的原理(必须掌握) 前端控制器.处理器映射器.处理器适配器.视图解析器 3.SpringMVC 入门程序 目的:对 ...