后端使用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 使用实例的更多相关文章

  1. highCharts参数实例解释

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. 【HighCharts系列教程】一、认识Highcharts

    一.什么是HighCharts HighCharts是网页报表工具,开发语言是Javascript HighCharts是一个简单易用.美观.跨平台.跨浏览器的图表工具 HighCharts支持图表的 ...

  3. Highcharts 配置语法

    Highcharts 配置语法 本章节我们将为大家介绍使用 Highcharts 生成图表的一些配置. 第一步:创建 HTML 页面 创建一个 HTML 页面,引入 jQuery 和 Highchar ...

  4. Highcharts 配置语法;Highcharts 配置选项详细说明

    Highcharts 配置语法 本章节我们将为大家介绍使用 Highcharts 生成图表的一些配置. 第一步:创建 HTML 页面 创建一个 HTML 页面,引入 jQuery 和 Highchar ...

  5. 最近学习工作流 推荐一个activiti 的教程文档

    全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...

  6. highcharts与highstock实例

    highcharts实例代码 <head> <title>highcharts报表示例</title> <meta http-equiv="Cont ...

  7. highCharts图表入门实例

    本文通过讲解Highcharts生成一个简单的3D柱状图实例来学习Highcharts的使用. JSP 页面 1.需要引入的js文件 <script src="<%=basePa ...

  8. highcharts实例教程二:结合php与mysql生成饼图

    上回我们分析了用highcharts结合php和mysql生成折线图的实例,这次我们以技术cto网站搜索引擎流量为例利用highcharts生成饼图. 饼图通常用在我们需要直观地显示各个部分所占的比例 ...

  9. 使用highcharts实现无其他信息纯趋势图实战实例

    使用highcharts实现无其他信息纯趋势图实战实例 Highcharts去掉或者隐藏掉y轴的刻度线yAxis : { gridLineWidth: 0, labels:{ //enabled:fa ...

随机推荐

  1. activiti自定义流程之整合(六):获取我的申请任务

    流程启动后,流程节点便进入到了任务相关的部分.可以看到我之前的做法是在启动节点就绑定了form表单,启动时就填写相关的数据.实际上在之前我的做法是不对开始节点做任何操作,知道任务节点的时候再填写相关的 ...

  2. 下载sdk版本: 在hosts文件中追加以下信息

    下载sdk版本:在hosts文件中追加以下信息: 74.125.113.121 developer.android.com 203.208.46.146 dl.google.com 203.208.4 ...

  3. zxing 一维码部分深入分析与实际应用,识别卡片数量,Android数卡器

    打算修改zxing 源码应用到其它方面,所以最近花了点时间阅读其源码,无意中找到这篇博客,条码扫描二维码扫描——ZXing android 简化源码分析 对过程的分析还是可以参考的.原作者给出的一个基 ...

  4. Markdown中插入数学公式

    如果想复杂使用的话,百度Latex公式,找些看一下. 使用MathJax引擎 大家都看过Stackoverflow上的公式吧,漂亮,其生成的不是图片.这就要用到MathJax引擎,在Markdown中 ...

  5. Raising Modulo Numbers

    Description People are different. Some secretly read magazines full of interesting girls' pictures, ...

  6. ZYB's Premutation POJ5592

    Problem Description ZYBZYBZYB has a premutation PPP,but he only remeber the reverse log of each pref ...

  7. WMI使用

    WMI Administrative Tools: http://www.microsoft.com/en-us/download/details.aspx?id=24045 WMI Administ ...

  8. Clustering with the ArcGIS API for Flex

    Clustering is an excellent technique for visualizing lotss of point data. We've all seen application ...

  9. POJ1364 King-差分

    Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...

  10. jface的CheckboxTreeViewer实现单选

    需求:使用FilteredTree实现一个下面这样的Dialog,要求Check框单选,即只能选择一个:当选择新的时候,旧的不选.说明:FilteredTree自带一个文本输入框. 1.自己的类继承o ...