dongxue7306 2016-02-06 04:29
浏览 406
已采纳

格式化HighCharts中的日期

I have the following code:

  <script>
$.getJSON('https://www.quandl.com/api/v3/datasets/OPEC/ORB.json?order=asc', function(json) {
    var hiJson = json.dataset.data.map(function(d) {
      return [new Date(d[0]), d[1]]
    });

    // Create the chart
    $('#container').highcharts('chart', {
      rangeSelector: {
        selected: 1
      },
      title: {
        text: 'OPEC Crude Oil Price',
      },
      series: [{
        type: 'line',
        name: 'OPEC',
        data: hiJson,
      }]
    });
});

Which prints a beautiful chart as follows:

OPEC Crude Oil Price

But as you can see, the dates are not in the correct format. I am struggling to work out what is wrong?

All help much appreciated as always!

UPDATE:

So thanks to Holvar's comment I solved one problem, but now I have another on the same theme.

My code is as follows:

<script>
$.getJSON('https://www.quandl.com/api/v3/datasets/BOE/IUAAAMIH.json?auth_token=pg6FBmvfQazVFdUgpqHz&start_date=2003-01-02&order=asc', function(json) {
    var hiJson = json.dataset.data.map(function(d) {
      return [new Date(d[0]), d[1]]
    });

    // Create the chart
    $('#interest').highcharts('chart', {
      xAxis: {
        type: 'datetime',
        dateTimeLabelFormats: {
                          day: '%Y'
                      }  
      },            
      rangeSelector: {
      selected: 1
      },
      title: {
        text: 'UK Interest Rates',
      },
      series: [{
        type: 'line',
        name: 'Interest Rate',
        data: hiJson,
      }]
    });
});

But this produces a chart without dates on the bottom. I'd like years from 2003-01-02. The chart looks like this

UK Interest Rate

I don't understand why it's not showing an annual date as in the solution to the initially posed question?!

You help is much appreciated!

展开全部

  • 写回答

1条回答 默认 最新

  • dsbc80836 2016-02-06 07:09
    关注

    I believe the issue is with the way the data map is happening. The ending array contains multiple arrays, instead of an object with "x" and "y" properties. Try changing the initialization of the hiJson variable to something like:

    var hiJson = json.dataset.data.map(function(d) { return { x: new Date(d[0]), y: d[1] }; });

    That seems to be working on my local environment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
  • ¥15 IEC61850 客户端和服务端的通讯机制
  • ¥15 MAX98357a(关键词-播放音频)
  • ¥15 Linux误删文件,请求帮助
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部