[英]highcharts: A chart with 1 or 0 in Y axis
We have some PCs and IP-Cameras working and I wrote a python script to check all machines if they are up or not with pinging them. 我们有一些PC和IP摄像机正在工作,我编写了一个python脚本来检查所有计算机是否正常运行并对其进行ping操作。 If machine is up result will be 1 and if it's down the result will be 0. The script writing it with current time.
如果计算机启动,则结果将为1;如果计算机关闭,则结果将为0。脚本将使用当前时间进行写入。
There is two tables. 有两个表。
The script getting IP address and id of device from devices table and pinging it and writing the results to ping table. 该脚本从设备表获取设备的IP地址和ID并对其执行ping操作,并将结果写入ping表。 Here the code (I know it's verdantly):
这里的代码(我很清楚):
# -*- coding: utf-8 -*-
import subprocess, sys
from time import sleep
try:
import MySQLdb
except:
print "Please Install Python's MySQLdb library"
sys.exit(0)
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def cprint(txt, typ):
print "%s%s%s" %(typ, txt, bcolors.ENDC)
def ping(hname, verbose=False):
response = p = subprocess.Popen(["ping", "-c1", hname], stdout=subprocess.PIPE)
res = response.communicate()[0]
if not "100% packet loss" in res:
if verbose:
cprint("The Host is up.", bcolors.OKGREEN)
return True
else:
if verbose:
cprint("The Host is down.", bcolors.FAIL)
return False
#code starts here:
try:
db = MySQLdb.connect(host="<The Host>", user="<user name for sql>", passwd="<password for sql>", db="<database name>", charset="utf8")
cur = db.cursor()
cur.execute("SELECT * FROM devices")
for row in cur.fetchall() :
devID = row[0]
devIP = row[3]
if ping(devIP):
cur.execute("""INSERT INTO ping (id, date, status) VALUES (%s, DEFAULT, %s)""",(devID, 1))
else:
cur.execute("""INSERT INTO ping (id, date, status) VALUES (%s, DEFAULT, %s)""",(devID, 0))
db.commit()
db.close()
except:
cprint("No MySQL connection." bcolors.FAIL)
It works with crontab every 10 minutes. 它每10分钟与crontab一起使用。
So we have data here and I wanted to put it in graph and decided to use highcharts. 所以我们这里有数据,我想将其放入图表中,并决定使用highcharts。
With line plot I get an average line for all results.(ones and zeros) 使用线图,我得到所有结果的平均线。(一和零)
With bar chart I get a huge bar for sum of all results.(ones and zeros) 使用条形图,我得到了一个巨大的条形图,用于显示所有结果的总和。(一和零)
What kind of chart you can advice me and how to plot it. 您可以为我提供哪种图表以及如何绘制图表。 Like I hoped there is a way to plot it like:
就像我希望有一种绘制方式一样:
y axis: up or down y轴:上下
x axis: time. x轴:时间。
Data I want to plot looks like: 我要绘制的数据如下所示:
[Date.UTC(2014, 09, 10, 22, 50, 11), 0], [Date.UTC(2014, 09, 10, 22, 40, 11), 0]
A couple of options would be a column chart or a step line chart. 几个选项可以是柱形图或折线图。
$(function () {
$('#container').highcharts({
xAxis: {
type: 'datetime'
},
yAxis: {
min:0,
max:1,
tickInterval: 1
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
type: 'line',
step: true,
// type: 'column',
data: [[Date.UTC(2014, 09, 10, 22, 50, 11), 0],
[Date.UTC(2014, 09, 11, 22, 50, 11), 1],
[Date.UTC(2014, 09, 12, 22, 50, 11), 0],
[Date.UTC(2014, 09, 13, 22, 50, 11), 1],
[Date.UTC(2014, 09, 14, 22, 50, 11), 1]]
}]
});
});
http://jsfiddle.net/tabrthca/1/ http://jsfiddle.net/tabrthca/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.