我对Highcharts还不熟悉,但有一个简单的问题:
我需要在单个浏览器窗口中混合几个(比如6或8个)小型Highcharts仪表盘和图表。这样做的可靠方法是为每个仪表盘创建一个单独的div,并将单独的Highcharts实例定位到每个div吗?
作为补充:即使有其他替代方法,这种多div方法是否与任何一种方法一样好,或者有什么优缺点?
我对Highcharts还不熟悉,但有一个简单的问题:
我需要在单个浏览器窗口中混合几个(比如6或8个)小型Highcharts仪表盘和图表。这样做的可靠方法是为每个仪表盘创建一个单独的div,并将单独的Highcharts实例定位到每个div吗?
作为补充:即使有其他替代方法,这种多div方法是否与任何一种方法一样好,或者有什么优缺点?
以下是我如何在同一页上创建多个Highcharts图表的方法。
http://jsfiddle.net/no1uknow/Jb2cb/2/
请记住,如果您想支持大多数浏览器,包括IE8,则需要使用jQuery 1.9.1和Highcharts JS 3.0.x+。这个jsfiddle演示将在跨浏览器中工作。 var container_chartCorrectiveAction = new Highcharts.Chart({
chart: {
renderTo: 'container_chartCorrectiveAction',
type: 'bar',
height: 195
},
title: {
text: 'Corrective Action'
},
subtitle: {
text: 'Sub-ATA () / ATA (20)'
},
xAxis: {
categories: ['No Defects Found-Fastener-Loose / Displaced'],
title: {
text: 'Action'
},
labels: {
style: {
width: '12000px'
}
}
},
yAxis: {
min: 0,
title: {
text: 'Count',
align: 'high'
},
labels: {
overflow: 'justify'
}
},
tooltip: {
formatter: function() {
return ''+ this.series.name +': '+ this.y +' Count';
}
},
plotOptions: {
bar: {
dataLabels: {
enabled: true
}
},
series: {
pointWidth:10,
groupPadding: .05,
shadow: true
}
},
legend: {
layout: 'horizontal',
align: 'center',
verticalAlign: 'bottom',
floating: false,
borderWidth: 1,
backgroundColor: '#FFFFFF',
shadow: true,
labelFormatter: function() {
return '<div class="' + this.name + '-arrow"></div><span style="font-family: \'Advent Pro\', sans-serif; font-size:12px">' + this.name +'</span><br/><span style="font-size:10px; color:#ababaa"> Total: ' + this.options.total + '</span>';
}
},
credits: {
enabled: false
},
exporting: {
enabled: true
},
series: [{
name: 'Heavy',
total: '0',
data: [null]
},{
name: 'Intermediate',
total: '0',
data: [null]
},{
name: 'Line',
total: '0',
data: [null]
},{
name: 'Lite',
total: '1',
data: [1]
}]
});
var container_chartAtaFleetAvg = new Highcharts.Chart({
chart: {
renderTo: 'container_chartAtaFleetAvg',
type: 'bar',
height: 185
},
title: {
text: 'Fleet Average'
},
subtitle: {
text: 'ATA (20)'
},
xAxis: {
categories: ['Fleet Average'],
title: {
text: ''
},
labels: {
style: {
width: '12000px'
}
}
},
yAxis: {
min: 0,
title: {
text: 'Average',
align: 'high'
},
labels: {
overflow: 'justify'
}
},
tooltip: {
formatter: function() {
return ''+ this.series.name +': '+ this.y +' Average';
}
},
plotOptions: {
bar: {
dataLabels: {
enabled: true
}
},
series: {
pointWidth:10,
groupPadding: .05,
shadow: true
}
},
legend: {
layout: 'horizontal',
align: 'center',
verticalAlign: 'bottom',
floating: false,
borderWidth: 1,
backgroundColor: '#FFFFFF',
shadow: true,
labelFormatter: function() {
return '<div class="' + this.name + '-arrow"></div><span style="font-family: \'Advent Pro\', sans-serif; font-size:12px">' + this.name +'</span><br/><span style="font-size:10px; color:#ababaa"> Total: ' + this.options.total + '</span>';
}
},
credits: {
enabled: false
},
exporting: {
enabled: true
},
series: [{
name: 'Intermediate',
total: '0.35',
data: [0.35]
},{
name: 'Lite',
total: '0.3',
data: [0.30]
},{
name: 'Heavy',
total: '0.1',
data: [0.10]
}]
});
您可以在一个 div 中使用多个 series
。 http://jsfiddle.net/sbochan/wzprF/
看起来唯一的方法是通过图表容器来实现。
在我个人看来,我认为多 div 的方法并不是一个坏主意。只要小心给 div 分配 id 就可以了。
但是你可以在 JavaScript 方面创建干净的代码。当你创建许多图表(6 或 8 个就很多了...)时,你必须避免代码重复...(至少限制重复)
有一种好方法可以使用 jQuery.extend()
和 Highcharts.setOptions
来实现:在单个网页中管理多个 Highchart 图表
请确保您的 div 容器格式如下
<div ... ></div>
而不是
<div ... />
因为它可能无法正确呈现。