nvd3.js和daterangepicker.js结合起来不起作用

时间:2013-08-16 06:01:03

标签: charts line nvd3.js

我使用nvd3制作折线图。我还包括date-picker/daterangepicker.js以选择日期范围。但我的图表不起作用。如果我排除daterangepicker.js,那么图表正常工作。 我的代码就像:

var testdata = [
                {
                  "key" : "Price" ,
                  "values" : [ [ 1376611200000 , 71.89] , [ 1379289600000 , 75.51] , [ 1141102800000 , 68.49] , [ 1143781200000 , 62.72] , [ 1146369600000 , 70.39] , [ 1149048000000 , 59.77] , [ 1151640000000 , 57.27] , [ 1154318400000 , 67.96] , [ 1156996800000 , 67.85] , [ 1159588800000 , 76.98] , [ 1162270800000 , 81.08] , [ 1164862800000 , 91.66] , [ 1167541200000 , 84.84] , [ 1170219600000 , 85.73] , [ 1172638800000 , 84.61] , [ 1175313600000 , 92.91] , [ 1177905600000 , 99.8] , [ 1180584000000 , 121.191] , [ 1183176000000 , 122.04] , [ 1185854400000 , 131.76] , [ 1188532800000 , 138.48] , [ 1191124800000 , 153.47] , [ 1193803200000 , 189.95] , [ 1196398800000 , 182.22] , [ 1199077200000 , 198.08] , [ 1201755600000 , 135.36] , [ 1204261200000 , 125.02] , [ 1206936000000 , 143.5] , [ 1209528000000 , 173.95] , [ 1212206400000 , 188.75] , [ 1214798400000 , 167.44] , [ 1217476800000 , 158.95] , [ 1220155200000 , 169.53] , [ 1222747200000 , 113.66] , [ 1225425600000 , 107.59] , [ 1228021200000 , 92.67] , [ 1230699600000 , 85.35] , [ 1233378000000 , 90.13] , [ 1235797200000 , 89.31] , [ 1238472000000 , 105.12] , [ 1241064000000 , 125.83] , [ 1243742400000 , 135.81] , [ 1246334400000 , 142.43] , [ 1249012800000 , 163.39] , [ 1251691200000 , 168.21] , [ 1254283200000 , 185.35] , [ 1256961600000 , 188.5] , [ 1259557200000 , 199.91] , [ 1262235600000 , 210.732] , [ 1264914000000 , 192.063] , [ 1267333200000 , 204.62] , [ 1270008000000 , 235.0] , [ 1272600000000 , 261.09] , [ 1275278400000 , 256.88] , [ 1277870400000 , 251.53] , [ 1280548800000 , 257.25] , [ 1283227200000 , 243.1] , [ 1285819200000 , 283.75] , [ 1288497600000 , 300.98] , [ 1291093200000 , 311.15] , [ 1293771600000 , 322.56] , [ 1296450000000 , 339.32] , [ 1298869200000 , 353.21] , [ 1301544000000 , 348.5075] , [ 1304136000000 , 350.13] , [ 1306814400000 , 347.83] , [ 1309406400000 , 335.67] , [ 1312084800000 , 390.48] , [ 1314763200000 , 384.83] , [ 1317355200000 , 381.32] , [ 1320033600000 , 404.78] , [ 1322629200000 , 382.2] , [ 1325307600000 , 405.0] , [ 1327986000000 , 456.48] , [ 1330491600000 , 542.44] , [ 1333166400000 , 599.55] , [ 1335758400000 , 583.98] ]
                }
              ].map(function(series) {
                series.values = series.values.map(function(d) { return {x: d[0], y: d[1] } });
                return series;
              });

              var chart;
              nv.addGraph(function() {
                    chart = nv.models.linePlusBarChart()
                        .margin({top: 30, right: 60, bottom: 50, left: 70})
                        .x(function(d,i) { return i })
                        .color(d3.scale.category10().range());

                    chart.xAxis.tickFormat(function(d) {
                      var dx = testdata[0].values[d] && testdata[0].values[d].x || 0;
                      return dx ? d3.time.format('%x')(new Date(dx)) : '';
                      })
                      .showMaxMin(false);

                    chart.y1Axis
                        .tickFormat(d3.format(',f'));

                    chart.y2Axis
                        .tickFormat(function(d) { return '$' + d3.format(',.2f')(d) });

                    chart.bars.forceY([0]).padData(false);
                    //chart.lines.forceY([0]);

                    d3.select('#lineChart svg')
                        .datum(testdata)
                      .transition().duration(500).call(chart);

                    nv.utils.windowResize(chart.update);

                    //chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e)); });

                    return chart;
                });

有没有办法解决这个问题?

请指导我。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

很抱歉,我无法发表评论。

我看不到你加载daterangepicker的任何html脚本。请提供更多信息。

并且daterangepicker.js不能单独使用。它依赖于Bootstrap,jQuery和Moment.js。这是可以帮助您的链接。 Date Range Picker

答案 1 :(得分:0)

Daterangepicker定义了d3也使用的函数Date.now(),但是此函数为daterangepicker返回一个字符串,如Mon Apr 27 2015 19:01:54 GMT+0800,而此函数应为d3返回类似1430129920761的整数,所以d3会表现不对。您可以更改d3.js脚本并替换函数defination

    if (!Date.now) Date.now = function() {
        return +new Date();
    };

使用其他名称(如Date.intNow())然后替换Date.intNow()使用Date.now()的所有位置,然后问题将得到解决