Javascript 动态加载JQuery但highcharts失败
我不是一个真正的javascript程序员,但我已经为这个问题奋斗了很长时间-任何帮助都将不胜感激 在下面的JSFIDLE中,如果从frameworks and extensions选项卡中选择了Jquery,highcharts可以正常工作。但代码的要点是,我需要动态加载不同的jquery版本——将图表用作各种小部件。但将其更改为无库(纯JS)时,图表不会加载 我从这里得到的大部分代码: 非常感谢Javascript 动态加载JQuery但highcharts失败,javascript,jquery,highcharts,Javascript,Jquery,Highcharts,我不是一个真正的javascript程序员,但我已经为这个问题奋斗了很长时间-任何帮助都将不胜感激 在下面的JSFIDLE中,如果从frameworks and extensions选项卡中选择了Jquery,highcharts可以正常工作。但代码的要点是,我需要动态加载不同的jquery版本——将图表用作各种小部件。但将其更改为无库(纯JS)时,图表不会加载 我从这里得到的大部分代码: 非常感谢 (function() { // Localize jQuery variable var
(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.7.2') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src",
"http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else {
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main();
}
/******** Our main function ********/
function main() {
var chart;
jQuery(document).ready(function($) {
/******* Load CSS *******/
var css_link = $("<link>", {
rel: "stylesheet",
type: "text/css",
href: "style.css"
});
css_link.appendTo('head');
/******* Load HTML *******/
chart = new Highcharts.Chart({
credits: {
enabled: true,
text: '',
href: ''
},
chart: {
renderTo: 'bm-container',
events: {
click: function () {
window.open('http://www.betmetrix.com', '_blank')
},
},
backgroundColor: '#FFFFFF',
zoomType: 'xy',
type: 'line',
marginLeft: 40,
marginRight: 40,
marginBottom: 40,
},
title: {
text: 'Election Worm',
x: -5,
style: {
color: '#000000',
fontWeight: 'bold',
fontSize: '17pt'
}
},
subtitle: {
text: 'Estimated Probability of Victory',
x: -5,
style: {
color: '#000000',
//fontWeight: 'bold',
fontSize: '13pt'
}
},
xAxis: {
type: 'datetime',
minRange: 7 * 24 * 3600000, // 1 week
dateTimeLabelFormats: {
second: '%H:%M:%S',
minute: '%H:%M',
hour: '%H:%M',
day: '%e %b',
week: '%e %b',
month: '%b \'%y',
year: '%Y'
},
//max: lnp[lnp.length-1][0]+604800000,
//tickInterval: 24*3600*1000*120,
//showFirstLabel: false,
minTickInterval: 1 * 24 * 3600000, //1 day
//maxTickInterval: 1 * 24 * 3600000*365, //30 day
startOnTick: false,
labels: {
style: {
color: '#969696',
//fontWeight: 'bold',
fontSize: '11pt'
}
}
},
yAxis: [{
//LHS axis
title: {
text: '%',
align: 'high',
rotation: 0,
offset: 10,
style: {
color: '#969696',
//fontWeight: 'bold',
fontSize: '11pt'
}
},
labels: {
style: {
color: '#969696',
//fontWeight: 'bold',
fontSize: '11pt'
}
},
showLastLabel: false,
showFirstLabel: false,
minRange: 3,
minTickInterval: 1,
min: 0,
max: 100,
opposite: false,
startOnTick: true,
//tickInterval: 5,
allowDecimals: false
}, {
//RHS axis
title: {
text: '%',
align: 'high',
rotation: 0,
offset: 20,
style: {
color: '#969696',
//fontWeight: 'bold',
fontSize: '11pt'
}
},
linkedTo: 0,
labels: {
style: {
color: '#969696',
//fontWeight: 'bold',
fontSize: '11pt'
}
},
showLastLabel: false,
minTickInterval: 1,
minRange: 3,
showFirstLabel: false,
startOnTick: true,
min: 0,
max: 100,
opposite: true,
//tickInterval: 10,
allowDecimals: false
}],
tooltip: {
xDateFormat: '%d-%b-%Y %l%P', //'%d-%b-%Y %l%P'
valueSuffix: '%',
valueDecimals: 1
//formatter: function () {
// return this.x + '<br/><b>' + this.series.name + ':' + '</b>' + this.y + '%';
//}
},
legend: {
enabled: false
// layout: 'vertical',
// align: 'right',
// verticalAlign: 'left',
// x: -20,
// y: 10,
// borderWidth: 0
},
series: [{
name: 'Coalition',
data: lnp,
marker: {
enabled: false
},
yaxis: 0
}, {
name: 'ALP',
data: alp,
marker: {
enabled: false
},
yaxis: 0
}],
exporting: {
enabled: false
}
});
});
}
})(); // We call our anonymous function immediately
(函数(){
//本地化jQuery变量
var-jQuery;
/********如果不存在jQuery,则加载它*********/
if(window.jQuery==undefined | | window.jQuery.fn.jQuery!=='1.7.2'){
var script_tag=document.createElement('script');
script_tag.setAttribute(“type”、“text/javascript”);
script_tag.setAttribute(“src”,
"http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
if(script_tag.readyState){
script_tag.onreadystatechange=function(){//用于旧版本的IE
如果(this.readyState=='complete'| | this.readyState=='loaded'){
scriptLoadHandler();
}
};
}否则{
script_tag.onload=scriptLoadHandler;
}
//尝试查找头部,否则默认为documentElement
(document.getElementsByTagName(“head”)[0]| | document.documentElement).appendChild(script_标记);
}否则{
//窗口上的jQuery版本就是我们想要使用的版本
jQuery=window.jQuery;
main();
}
/********加载jQuery后调用******/
函数scriptLoadHandler(){
//将$和window.jQuery恢复为其以前的值并存储
//本地jQuery变量中的新jQuery
jQuery=window.jQuery.noConflict(true);
//调用我们的主要函数
main();
}
/********我们的主要职能********/
函数main(){
var图;
jQuery(文档).ready(函数($){
/*******加载CSS*******/
var css_link=$(“”,{
rel:“样式表”,
键入:“文本/css”,
href:“style.css”
});
css_link.appendTo('head');
/*******加载HTML*******/
图表=新的高点图表。图表({
学分:{
启用:对,
文本:“”,
href:'
},
图表:{
renderTo:“bm容器”,
活动:{
单击:函数(){
打开窗户http://www.betmetrix.com“,”空白“)
},
},
背景颜色:“#FFFFFF”,
zoomType:'xy',
键入:“行”,
marginLeft:40,
marginRight:40,
marginBottom:40,
},
标题:{
文本:“选举蠕虫”,
x:-5,
风格:{
颜色:'#000000',
fontWeight:'粗体',
字体大小:“17磅”
}
},
副标题:{
文字:“估计获胜概率”,
x:-5,
风格:{
颜色:'#000000',
//fontWeight:'粗体',
字体大小:“13磅”
}
},
xAxis:{
键入:“日期时间”,
最小范围:7*24*3600000,//1周
日期时间标签格式:{
第二个:“%H:%M:%S”,
分钟:“%H:%M”,
小时:“%H:%M”,
日期:'%e%b',
周:“%e%b”,
月份:'%b\'%y',
年份:'%Y'
},
//最大值:lnp[lnp.长度-1][0]+604800000,
//时间间隔:24*3600*1000*120,
//showFirstLabel:false,
分钟间隔:1*24*3600000,//1天
//最大间隔时间:1*24*3600000*365,//30天
startOnTick:错,
标签:{
风格:{
颜色:'#9696',
//fontWeight:'粗体',
字体大小:“11磅”
}
}
},
亚克斯:[{
//左轴
标题:{
文本:“”,
对齐:“高”,
轮换:0,
抵销:10,
风格:{
颜色:'#9696',
//fontWeight:'粗体',
字体大小:“11磅”
}
},
标签:{
风格:{
颜色:'#9696',
//fontWeight:'粗体',
字体大小:“11磅”
}
},
showLastLabel:false,
showFirstLabel:false,
最小范围:3,
minTickInterval:1,
分:0,,
最高:100,
反面:错,
是的,
//时间间隔:5,
allowDecimals:false
}, {
//RHS轴
标题:{
文本:“”,
对齐:“高”,
轮换:0,
抵销:20,
风格:{
颜色:'#9696',
//fontWeight:'粗体',
字体大小:“11磅”
}
},
链接到:0,
标签:{
风格:{
颜色:'#9696',
//fontWeight:'粗体',
字体大小:“11磅”
}
function chainLoadHighCharts() {
var Highcharts;
/******** Ok, /now/ dynamically load up highchart too... *********/
if (window.Highcharts === undefined) {
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src",
"http://code.highcharts.com/highcharts.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler(); //here is the call that was orginally called directly from the jquery dynamic load.
}
};
} else {
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
scriptLoadHandler();
// script_tag.setAttribute("src","http://code.highcharts.com/modules/exporting.js");
}
}