canvasjs,在图表中显示一个值,在X轴上显示另一个值

时间:2017-06-13 21:15:07

标签: javascript jquery canvasjs

我使用CanvasJs来显示一些图表。 当我将鼠标移动到该点上时,它显示错误的日期 在图片我超过12-jun但它说11-jun ..在每一点都发生了相同的事情。

值“32”没问题,日期不正确..

但是如果我在分开的图表中显示系列,那么数据就可以了。

Chart

脚本:

$.getJSON("dataNro7.php", function (result) {


            var dps1 = [], dps2 = [];
        for(var i = 0; i < result.length; i++) {



            switch(result[i].palabra) {

            case 'pa': dps1.push({ x:  new Date (result[i].label), y: result[i].y });
                break;
            case 'ma': dps2.push({ x:  new Date(result[i].label), y: result[i].y });
                break;
            }
        }


                var revenueBarChart = new CanvasJS.Chart("Historiales2y3", {
                    animationEnabled: false,
                    backgroundColor: "transparent",
                    theme: "theme2",
                    axisX:{
                            interval: 2,
                            valueFormatString: "DD-MMM" ,
                            gridThickness: 0,
                            labelFontSize: 12,
                            labelMaxWidth: 100,
                            labelAngle: 0,
                            culture: "es",
                            labelFontStyle: "normal",
                            labelFontWeight: "normal",
                            labelFontFamily: "Lucida Sans Unicode"

                        },
                    axisY2:{

                            labelFontSize: 12,
                            interlacedColor: "rgba(1,77,101,.05)",
                            gridColor: "rgba(1,77,101,.1)"

                        },


                    data: [   { type: "splineArea",
                                showInLegend: true,
                                name: "PA",
                                color: "rgba(12,143,221,.8)",
                                axisYType: "secondary",
                                 dataPoints: dps1 },

                            { type: "splineArea",
                                showInLegend: true,
                                name: "MA",
                                color: "rgba(192,143,121,.8)",
                                axisYType: "secondary",
                                 dataPoints: dps2 } ]

                });


                revenueBarChart.render();
            }); 

JSON:

[{"palabra":"pa","y":22,"label":"2017-05-28"},
 {"palabra":"ma","y":10,"label":"2017-05-28"},
 {"palabra":"pa","y":8,"label":"2017-05-29"},
 {"palabra":"ma","y":30,"label":"2017-05-29"},
 {"palabra":"pa","y":0,"label":"2017-05-30"},
 {"palabra":"ma","y":0,"label":"2017-05-30"},
 {"palabra":"pa","y":12,"label":"2017-05-31"},
 {"palabra":"ma","y":16,"label":"2017-05-31"},                    
 {"palabra":"pa","y":7,"label":"2017-06-01"},
 {"palabra":"ma","y":36,"label":"2017-06-01"},
 {"palabra":"pa","y":12,"label":"2017-06-02"},
 {"palabra":"ma","y":33,"label":"2017-06-02"},
 {"palabra":"pa","y":0,"label":"2017-06-03"},
 {"palabra":"ma","y":14,"label":"2017-06-03"},{"palabra":"pa","y":5,"label":"2017-06-04"},{"palabra":"ma","y":30,"label":"2017-06-04"},{"palabra":"pa","y":19,"label":"2017-06-05"},{"palabra":"ma","y":25,"label":"2017-06-05"},{"palabra":"pa","y":11,"label":"2017-06-06"},{"palabra":"ma","y":35,"label":"2017-06-06"},{"palabra":"pa","y":24,"label":"2017-06-07"},{"palabra":"ma","y":34,"label":"2017-06-07"},{"palabra":"pa","y":24,"label":"2017-06-08"},{"palabra":"ma","y":59,"label":"2017-06-08"},{"palabra":"pa","y":0,"label":"2017-06-09"},{"palabra":"ma","y":39,"label":"2017-06-09"},{"palabra":"pa","y":14,"label":"2017-06-10"},{"palabra":"ma","y":22,"label":"2017-06-10"},{"palabra":"pa","y":13,"label":"2017-06-11"},{"palabra":"ma","y":6,"label":"2017-06-11"},{"palabra":"pa","y":18,"label":"2017-06-12"},{"palabra":"ma","y":32,"label":"2017-06-12"},{"palabra":"pa","y":17,"label":"2017-06-13"},{"palabra":"ma","y":49,"label":"2017-06-13"},{"palabra":"pa","y":15,"label":"2017-06-14"},{"palabra":"ma","y":12,"label":"2017-06-14"}]

2 个答案:

答案 0 :(得分:3)

MDN中所述,

  

给定日期字符串“2014年3月7日”,parse()假定当地时间   区域,但给定ISO格式,如“2014-03-07”,它将假设一个   UTC时区(ES5和ECMAScript 2015)。因此Date对象   使用这些字符串生成可能代表不同的时刻   取决于支持的ECMAScript的版本,除非系统是   使用UTC的本地时区进行设置。这意味着两个日期字符串   看似等效的可能会导致两个不同的值取决于   正在转换的字符串的格式。

请查看this thread了解详情。

答案 1 :(得分:0)

我在推入DSP1和DSP2阵列之前将其添加工作:

var current = new Date(result[i].label); 
                var followingDay = new Date(current.getTime() + 12400000); 
                followingDay.toLocaleDateString();

我确定这不是非常准确..但现在有用..