highcharts 使用实例
后端使用django实现,返回的数据可以修改为从数据库获取或其他方式获取,实例里是写死的数据。
urls配置:
url(r'^outip/chart/$', views.charts),
url(r'^outip/getchart/$', views.get_chart),
url(r'^outip/getlast/$', views.get_last),
url(r'^outip/detail/$', views.get_detail_chart),
所有实例浏览器访问的后端方法:charts
@csrf_exempt
def charts(request):
return render_to_response('html/chart.html')
所有实例使用的html代码:chart.html
<html>
<head>
<meta charset="utf-8">
<style> </style>
<script type="text/javascript" src="/outip/staticfiles/js/jquery-2.1.4.js"></script>
<script type="text/javascript" src="/outip/staticfiles/highcharts/js/highcharts.js"></script>
<script type="text/javascript" src="/outip/staticfiles/highcharts/js/modules/exporting.js"></script>
<script type="text/javascript" src="/outip/staticfiles/js/activechart.js"></script>
<!-- <script type="text/javascript" src="/outip/staticfiles/js/chart.js"></script> -->
</head>
<body>
<div id="container" style="min-width:400px;height:400px"></div>
<script> </script>
</body>
</html>
1.实时刷新曲线图(若取消实时刷新,将activechart.js中 chart的events删除即可):
js代码: activechart.js
$(document).ready(function () {
Highcharts.setOptions({
decimalPoint: '.', // 小数点号,例如 12.50
thousandsSep: ',', // 千分号,例如 12,000
global: {
useUTC: false
}
}); function getchart() {
var datax = [];
var datay = [];
$.ajax({
type: "POST",
dataType: "json",
url:'/outip/getchart/',
success: function(result) {
if (result.errno == 0) {
for(var i=0;i<result.data.length;i++) {
datax.push(result.data[i].x)
datay.push(result.data[i].y)
};
start(datax,datay);
//console.log(datax);
//console.log(t);
}else {
alert('err');
}
}
});
}
function start(datax,datay) {
$('#container').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg,
marginRight: 10,
events: {
load: function () {
var series = this.series[0];
setInterval(function () {
$.ajax({
type: "POST",
dataType: "json",
url:'/outip/getlast/',
success: function(result) {
if (result.errno == 0) {
series.addPoint([result.data.x, result.data.y], true, true);
} else {
alert('last err');
}
}
});
}, 60000);
}
}
},
title: {
text: 'Total flow',
x: -20 //center
},
//subtitle: {
// text: 'Source:',
// x: -20
//},
xAxis: {
categories: datax
//categories: ["2016-10-08 17:11"]
},
yAxis: {
title: {
text: 'flow/Bytes'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
credits:{
enabled:false // 禁用版权信息
},
tooltip: {
valueSuffix: ' Bytes',
formatter:function(){
//console.log(this);
return this.key+'<br>'+Highcharts.numberFormat(this.y,'2','.',',')+' Bytes';
}
},
legend: {
layout: 'vertical',
align: 'center',
verticalAlign: 'bottom',
borderWidth: 0
},
series: [{
name: 'Flow',
data: datay
}]
});
}
getchart();
});
后端方法:
1) 获取初始数据:get_chart
@csrf_exempt
def get_chart(request):
data = [{'x':'2016-10-08 17:11','y':1},{'x':'2016-10-08 17:12','y':3},{'x':'2016-10-08 17:13','y':4},{'x':'2016-10-08 17:14','y':400},{'x':'2016-10-08 17:15','y':124},{'x':'2016-10-08 17:16','y':2444},{'x':'2016-10-08 17:17','y':23334},{'x':'2016-10-08 17:18','y':40}]
return HttpResponse(json.dumps({'errno':0,'data':data}))
2)获取最新的一个数据:get_last
@csrf_exempt
def get_last(request):
data = {'x':'2016-10-08 17:19','y':100}
return HttpResponse(json.dumps({'errno':0,'data':data}))
2.基础柱状图
js代码:activechart.js(datay每个元素中要有name和data两个key,且名字不能改,data的值为一个数组;若将plotOptions.column.stacking属性加上,则成为堆叠柱状图)
function getchart() {
var datax;
var datay;
$.ajax({
type: "POST",
dataType: "json",
url:'/outip/detail/',
success: function(result) {
if (result.errno == 0) {
datax = result.data.time
datay = result.data.value
start(datax,datay);
//console.log(datay);
//console.log(t);
}else {
alert('err');
}
}
});
}
function start(datax,datay) {
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Detail flow',
x: -20 //center
},
credits:{
enabled:false // 禁用版权信息
},
//subtitle: {
// text: 'Source:',
// x: -20
//},
xAxis: {
categories: datax
//categories: ["2016-10-08 17:11"]
},
yAxis: {
title: {
text: 'flow/Bytes'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function () {
return this.x + ' Total: ' + Highcharts.numberFormat(this.point.stackTotal,'2','.',',') +
' Bytes<br/><b>' + this.series.name + ': ' + Highcharts.numberFormat(this.y,'2','.',',') + ' Bytes</b>';
},
valueSuffix: ' Bytes'
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0,
//stacking: 'normal',
dataLabels: {
enabled: true,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'black',
style: {
textShadow: '0 0 3px black'
}
}
}
},
/*legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},*/
//series: [{'name':'1','data':[1]},{'name':'2','data':[3]}] /*[{
series: datay /*[{
name: 'Flow',
data: datay
}]*/
});
}
getchart();
后端方法:get_detail_chart
@csrf_exempt
def get_detail_chart(request):
data = {'time':['2016-10-08 17:19'],'value':[{'name':'x0','data':[70]},{'name':'x1','data':[60]},{'name':'x2','data':[21]},{'name':'x3','data':[31]},{'name':'x4','data':[50]},{'name':'x5','data':[40]},{'name':'x6','data':[22]},{'name':'x7','data':[1]},{'name':'x8','data':[20]},{'name':'x9','data':[10]},{'name':'x10','data':[30]}]}
return HttpResponse(json.dumps({'errno':0,'data':data}))
3.堆叠柱状图
js代码:activechart.js
function getchart() {
var datax;
var datay;
$.ajax({
type: "POST",
dataType: "json",
url:'/outip/getchart/',
success: function(result) {
if (result.errno == 0) {
for(var i=0;i<result.data.length;i++) {
datax = result.data[i].time;
datay = result.data[i].value;
};
start(datax,datay);
//console.log(datax);
//console.log(t);
}else {
alert('err');
}
}
});
}
function start(datax,datay) {
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Total flow',
x: -20 //center
},
//subtitle: {
// text: 'Source:',
// x: -20
//},
xAxis: {
categories: datax
//categories: ["2016-10-08 17:11"]
},
yAxis: {
title: {
text: 'flow/Bytes'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: ' Bytes'
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
style: {
textShadow: '0 0 3px black'
}
}
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
//series: [{'name':'1','data':[1]},{'name':'2','data':[3]}] /*[{
series: datay /*[{
name: 'Flow',
data: datay
}]*/
});
}
getchart();
后端方法:get_chart
@csrf_exempt
def get_chart(request):
data = [{'time':['2016-10-08 17:11','2016-10-08 17:12'],'value':[{'name':'f1','data':[1,3]},{'name':'f2','data':[3,1]}]}]
return HttpResponse(json.dumps({'errno':0,'data':data}))
highcharts 使用实例的更多相关文章
- highCharts参数实例解释
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 【HighCharts系列教程】一、认识Highcharts
一.什么是HighCharts HighCharts是网页报表工具,开发语言是Javascript HighCharts是一个简单易用.美观.跨平台.跨浏览器的图表工具 HighCharts支持图表的 ...
- Highcharts 配置语法
Highcharts 配置语法 本章节我们将为大家介绍使用 Highcharts 生成图表的一些配置. 第一步:创建 HTML 页面 创建一个 HTML 页面,引入 jQuery 和 Highchar ...
- Highcharts 配置语法;Highcharts 配置选项详细说明
Highcharts 配置语法 本章节我们将为大家介绍使用 Highcharts 生成图表的一些配置. 第一步:创建 HTML 页面 创建一个 HTML 页面,引入 jQuery 和 Highchar ...
- 最近学习工作流 推荐一个activiti 的教程文档
全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...
- highcharts与highstock实例
highcharts实例代码 <head> <title>highcharts报表示例</title> <meta http-equiv="Cont ...
- highCharts图表入门实例
本文通过讲解Highcharts生成一个简单的3D柱状图实例来学习Highcharts的使用. JSP 页面 1.需要引入的js文件 <script src="<%=basePa ...
- highcharts实例教程二:结合php与mysql生成饼图
上回我们分析了用highcharts结合php和mysql生成折线图的实例,这次我们以技术cto网站搜索引擎流量为例利用highcharts生成饼图. 饼图通常用在我们需要直观地显示各个部分所占的比例 ...
- 使用highcharts实现无其他信息纯趋势图实战实例
使用highcharts实现无其他信息纯趋势图实战实例 Highcharts去掉或者隐藏掉y轴的刻度线yAxis : { gridLineWidth: 0, labels:{ //enabled:fa ...
随机推荐
- python Requests库在处理response时的一些陷阱
python的Requests(http://docs.python-requests.org/en/latest/)库在处理http/https请求时还是比较方便的,应用也比较广泛.但其在处理res ...
- 本地和VMware虚拟主机之间的网络访问
在需要设置的虚拟主机节点上右键,点击[设置...] 在打开的虚拟机设置中,选中[网络适配器],之后在右边设置网络连接为[自定义:指定的虚拟网络] 然后设置同一网关即可.
- Etag,Expires与Cache-control
来介绍一下http中的这几个概念 先来介绍一下Etag: 看看百度来的简介:HTTP协议规格说明定义ETag为“被请求变量的实体值”.另一种说法是,ETag是一个可以与Web资源关联的记号(token ...
- Linux6(5)安装Oracle Rac11g
1.创建用户组.以root身份运行以下命令:/usr/sbin/groupadd -g 501 oinstall /usr/sbin/groupadd -g 502 dba /usr/sbin/gro ...
- Dynamics Webservice Call with Credential
Dynamics Webservice call with credential /// <summary> ///WebServiceHelper 的摘要说明 /// </summ ...
- JS函数arguments数组获得实际传参数个数
JS与PHP在函数传参方面有点不同,PHP形参与实参个数要匹配,而JS就灵活多了,可以随意传参,实参比形参少或多都不会报错. 实参比形参多不会报错 ? 1 2 3 4 5 function say(a ...
- a标签鼠标经过,字颜色和下划线的颜色都变红
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- dubbo问题总结
1.dubbo序列化数据丢失问题.dubbo的返回结果是对象,那么必须要实现对象中需要返回的数据的get方法. 2.dubbo序列化问题.
- ThreadPool原理介绍
public class ThreadPoolExecutorextends AbstractExecutorService 一个 ExecutorService,它使用可能的几个池线程之一执行每个提 ...
- create和grant配合使用,对Mysql进行创建用户和对用户授权
1.首先创建用户username以及密码passwd,授权主机localhost. create user ‘username’@'localhost' identified by 'passwd' ...