【问题标题】:How to add JSON data to nvd3 charts如何将 JSON 数据添加到 nvd3 图表
【发布时间】:2016-02-03 16:58:53
【问题描述】:

我是 Javascript 新手,在我的代码中找不到错误。 我在这里使用 NVD3 图表。它是基于时间序列的图表,包含特定股票的日期和收盘价。数据范围从 2005 年至今。

这里是代码

var data= JSON.parse("Data.JSON")

nv.addGraph(function() {
 var chart = nv.models.lineChart()
              .margin({top: 70, right: 70, bottom: 70, left: 70})  
              .useInteractiveGuideline(true)  
              .transitionDuration(100)  
              .showYAxis(true)
              .showXAxis(true)
;
 //Chart x-axis settings
chart.xAxis  
    .axisLabel('date')
    .tickFormat(function(d) {return new Date((data.Date - (25567 + 1))*86400*1000);

chart.yAxis     //Chart y-axis settings
    .axisLabel('close')
    .tickFormat(d3.scale.linear(data.Close));




d3.select('#Charts svg')    //Selecting the <svg> element where i want to render the chart in.   
    .datum(data)         //Populating the <svg> element with chart data...
    .call(chart);          //Finally, rendering the chart!

//Update the chart when window resizes.

  })

;

//数据 { “日期”:[13089、13094、13095、13096、13097、13098、13101、13103、13104、13105、13108、13109、13110] “关闭”:[2419.1、2461.6、2492.7、2489.1、2500.7、2548.7、2558.7、2582.8、2603.9、2620.1、2602.5、2572.8] }

【问题讨论】:

  • 您遇到的问题是什么?
  • 代码没有运行。什么都没有形成。

标签: javascript html json d3.js nvd3.js


【解决方案1】:

“关闭”中的数组元素数量比“日期”要少。

这是您可能正在寻找的可能解决方案:

    nv.addGraph(function () {
    var chart = nv.models.lineChart();

    chart.xAxis.axisLabel('date')
        .tickFormat(d3.format(''));

    chart.yAxis.axisLabel('close')
        .tickFormat(d3.format(''));

    d3.select('#dateChart')
        .datum(chartData())
        .transition().duration(500)
        .call(chart);

    nv.utils.windowResize(function () {
        d3.select('#dateChart').call(chart)
    });

    return chart;
});


function chartData() {
    var myData = {
        "Date": [13089, 13094, 13095, 13096, 13097, 13098, 13101, 13103, 13104, 13105, 13108, 13109, 13110],
        "Close": [2419.1, 2461.6, 2492.7, 2489.1, 2500.7, 2548.7, 2558.7, 2582.8, 2603.9, 2620.1, 2602.5, 2572.8, 2588.8]
        //The number of array elements in Close were less compared to Date. Hence added 2588.8 as the last element
    };

    var result = [];
    for (var i = 0; i < myData.Date.length; i++) {
        result.push({
            x: myData.Date[i],
            y: myData.Close[i]
        });
    }

    return [{
        values: result,
        key: 'Date Chart',
        color: '#ff7f0e'
    }];
}

JS 小提琴:https://jsfiddle.net/m7oaxjue/3/

【讨论】:

  • 感谢您的回答。您能告诉我如何将 JSON 文件中的数据添加到图表中吗?我还需要将这些日期更改为 %d-%m-%Y 格式。
  • @Vishukapoor:您可以将 json 数据保存在文件中,然后在代码中访问它,或者您可以创建一个公开 json 数据的后端 Web 服务端点。然后,您可以在您的应用程序中调用此 api。以下是从文件中访问 json 数据的示例:plnkr.co/edit/PTiH0GqfknsZG6gtNhnU?p=preview
  • @gaura 我不能从 plnkr 复制并粘贴代码并使用它吗?代码适用于 plnkr 上的不同数据集,但当我在我的系统上使用它时却不行。
  • 您需要在本地网络服务器上运行您的应用程序或使用在临时服务器上运行您的应用程序的 IDE(例如 Visual Studio、Webstorm、IntelliJ 等)。这可能是一个有用的资源stackoverflow.com/questions/19706046/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 1970-01-01
相关资源
最近更新 更多