2013-04-20 54 views
1

我是JavaScript编程的新手,并且遇到了一些实现HighCharts图表的麻烦。我使用的API是麦盖提(https://github.com/markitondemand/DataApis/blob/master/MarkitTimeseriesServiceSample.js)产生图表作为中间的图显示了这个页面http://dev.markitondemand.com/HighCharts - 让图表显示 - 简单的例子(markit API)

现在,我想我不得不补充到位在这个小提琴(http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/line-basic/)所示的JS的那个JS脚本,但它不似乎没有工作。

我认为这可能是因为我没有分配我实际想要放置的位置,但我不是100%确定的。

请参阅下面我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 

<script src="http://www.google.com/jsapi"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script> 

<script src="http://code.highcharts.com/highcharts.js"></script> 
<script src="http://code.highcharts.com/modules/exporting.js"></script> 



<script> 
    /** 
    * Version 1.1, Jan 2012 
    */ 
    var Markit = {}; 
    /** 
    * Define the TimeseriesService. 
    * First argument is symbol (string) for the quote. Examples: AAPL, MSFT, JNJ, GOOG. 
    * Second argument is duration (int) for how many days of history to retrieve. 
    */ 
    Markit.TimeseriesService = function(symbol,duration){ 
     this.symbol = symbol; 
     this.duration = duration; 
     this.PlotChart(); 
    }; 

    Markit.TimeseriesService.prototype.PlotChart = function(){ 

     //Make JSON request for timeseries data 
     $.ajax({ 
      beforeSend:function(){ 
       $("#chartDemoContainer").text("Loading chart..."); 
      }, 
      data: { 
       symbol: this.symbol, 
       duration: this.duration 
      }, 
      url: "http://dev.markitondemand.com/Api/Timeseries/jsonp", 
      dataType: "jsonp", 
      context: this, 
      success: function(json){ 
      //Catch errors 
    if (!json.Data || json.Message){ 
    console.error("Error: ", json.Message); 
    return; 
    } 
       this.BuildDataAndChart(json); 
      }, 
      error: function(){ 
       alert("Couldn't generate chart."); 
      } 
     }); 
    }; 

    Markit.TimeseriesService.prototype.BuildDataAndChart = function(json){ 
     var dateDS = json.Data.SeriesDates, 
      closeDS = json.Data.Series.close.values, 
      openDS = json.Data.Series.open.values, 
      closeDSLen = closeDS.length, 
      irregularIntervalDS = []; 

     /** 
    * Build array of arrays of date & price values 
    * Market data is inherently irregular and HighCharts doesn't 
    * really like irregularity (for axis intervals, anyway) 
    */ 
     for (var i=0; i<closeDSLen;i++){ 
      var dat = new Date(dateDS[i]); 
      var dateIn = Date.UTC(dat.getFullYear(), dat.getMonth(), dat.getDate()); 
      var val = closeDS[i]; 
      irregularIntervalDS.push([dateIn,val]); 
     } 

     //set dataset and chart label 
     this.oChartOptions.series[0].data = irregularIntervalDS; 
     this.oChartOptions.title.text = "Price History of " + json.Data.Name + " (1 year)"; 

     //init chart 
     new Highcharts.Chart(this.oChartOptions); 
    }; 

    //Define the HighCharts options 
    Markit.TimeseriesService.prototype.oChartOptions = { 
    chart: { 
    renderTo: 'chartDemoContainer' 
    }, 
    title:{}, 
    subtitle: { 
    text: 'Source: Thomson Reuters DataScope/Markit On Demand' 
    }, 
    xAxis: { 
    type: 'datetime' 
    }, 
    yAxis: [{ // left y axis 
    title: { 
    text: null 
    }, 
    labels: { 
    align: 'left', 
    x: 3, 
    y: 16, 
    formatter: function() { 
    return Highcharts.numberFormat(this.value, 0); 
    } 
    }, 
    showFirstLabel: false 
    }, { // right y axis 
    linkedTo: 0, 
    gridLineWidth: 0, 
    opposite: true, 
    title: { 
    text: null 
    }, 
    labels: { 
    align: 'right', 
    x: -3, 
    y: 16, 
    formatter: function() { 
    return Highcharts.numberFormat(this.value, 0); 
    } 
    }, 
    showFirstLabel: false 
    }], 
    tooltip: { 
    shared: true, 
    crosshairs: true 
    }, 
    plotOptions: { 
    series: { 
    marker: { 
    lineWidth: 1 
    } 
    } 
    }, 
    series: [{ 
    name: "Close price", 
    lineWidth: 2, 
    marker: { 
    radius: 0 
    } 
    }] 
    //,credits:{ enabled:false }, 
    }; 

    new Markit.TimeseriesService("GOOG", 365); 

    /** 
    * Need help? Visit the API documentation at: 
    * http://dev.markitondemand.com 
    */ 
</script> 
</head> 

<body> 

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> 

</body> 
</html> 

非常感谢您的帮助。

+1

你收到任何错误? – 2013-04-22 09:03:59

+1

@New_programmer你找到了解决方案吗?让我知道,好奇你的结果.... – Justin 2013-11-05 00:57:47

+0

@Justin - 请参阅下面的答案。它似乎工作正常! – 2016-04-04 01:37:14

回答

0

在你<div>更改ID,#chartDemoContainer

与这些更换你的jQuery库,

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 

而且这里是你的榜样的工作版本 - http://jsbin.com/behicetivi/edit?html,output

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
<script src="https://code.highcharts.com/stock/highstock.js"></script> 
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script> 



<script> 
    /** 
    * Version 1.1, Jan 2012 
    */ 
    var Markit = {}; 
    /** 
    * Define the TimeseriesService. 
    * First argument is symbol (string) for the quote. Examples: AAPL, MSFT, JNJ, GOOG. 
    * Second argument is duration (int) for how many days of history to retrieve. 
    */ 
    Markit.TimeseriesService = function(symbol,duration){ 
     this.symbol = symbol; 
     this.duration = duration; 
     this.PlotChart(); 
    }; 

    Markit.TimeseriesService.prototype.PlotChart = function(){ 

     //Make JSON request for timeseries data 
     $.ajax({ 
      beforeSend:function(){ 
       $("#chartDemoContainer").text("Loading chart..."); 
      }, 
      data: { 
       symbol: this.symbol, 
       duration: this.duration 
      }, 
      url: "http://dev.markitondemand.com/Api/Timeseries/jsonp", 
      dataType: "jsonp", 
      context: this, 
      success: function(json){ 
      //Catch errors 
    if (!json.Data || json.Message){ 
    console.error("Error: ", json.Message); 
    return; 
    } 
       this.BuildDataAndChart(json); 
      }, 
      error: function(){ 
       alert("Couldn't generate chart."); 
      } 
     }); 
    }; 

    Markit.TimeseriesService.prototype.BuildDataAndChart = function(json){ 
     var dateDS = json.Data.SeriesDates, 
      closeDS = json.Data.Series.close.values, 
      openDS = json.Data.Series.open.values, 
      closeDSLen = closeDS.length, 
      irregularIntervalDS = []; 

     /** 
    * Build array of arrays of date & price values 
    * Market data is inherently irregular and HighCharts doesn't 
    * really like irregularity (for axis intervals, anyway) 
    */ 
     for (var i=0; i<closeDSLen;i++){ 
      var dat = new Date(dateDS[i]); 
      var dateIn = Date.UTC(dat.getFullYear(), dat.getMonth(), dat.getDate()); 
      var val = closeDS[i]; 
      irregularIntervalDS.push([dateIn,val]); 
     } 

     //set dataset and chart label 
     this.oChartOptions.series[0].data = irregularIntervalDS; 
     this.oChartOptions.title.text = "Price History of " + json.Data.Name + " (1 year)"; 

     //init chart 
     new Highcharts.Chart(this.oChartOptions); 
    }; 

    //Define the HighCharts options 
    Markit.TimeseriesService.prototype.oChartOptions = { 
    chart: { 
    renderTo: 'chartDemoContainer' 
    }, 
    title:{}, 
    subtitle: { 
    text: 'Source: Thomson Reuters DataScope/Markit On Demand' 
    }, 
    xAxis: { 
    type: 'datetime' 
    }, 
    yAxis: [{ // left y axis 
    title: { 
    text: null 
    }, 
    labels: { 
    align: 'left', 
    x: 3, 
    y: 16, 
    formatter: function() { 
    return Highcharts.numberFormat(this.value, 0); 
    } 
    }, 
    showFirstLabel: false 
    }, { // right y axis 
    linkedTo: 0, 
    gridLineWidth: 0, 
    opposite: true, 
    title: { 
    text: null 
    }, 
    labels: { 
    align: 'right', 
    x: -3, 
    y: 16, 
    formatter: function() { 
    return Highcharts.numberFormat(this.value, 0); 
    } 
    }, 
    showFirstLabel: false 
    }], 
    tooltip: { 
    shared: true, 
    crosshairs: true 
    }, 
    plotOptions: { 
    series: { 
    marker: { 
    lineWidth: 1 
    } 
    } 
    }, 
    series: [{ 
    name: "Close price", 
    lineWidth: 2, 
    marker: { 
    radius: 0 
    } 
    }] 
    //,credits:{ enabled:false }, 
    }; 

    new Markit.TimeseriesService("GOOG", 365); 

    /** 
    * Need help? Visit the API documentation at: 
    * http://dev.markitondemand.com 
    */ 
</script> 
</head> 

<body> 

<div id="chartDemoContainer" style="min-width: 400px; height: 400px; margin: 0 auto"></div> 

</body> 
</html>