2014-10-29 3 views
0

JSON 개체를 읽고 그래프를 그려주는 자바 스크립트가 있습니다. JSON에는 일련의 주문 날짜가 있지만, 작성된 날짜 중 일부 요소가 누락 된 일부 누락 된 부분이 있습니다. 뭔가 같이 :자바 스크립트 시리즈에없는 날짜 추가

[ 
    { 
     "date": "2014-09-22", 
     "similar": 1, 
     "trend": 0 
    }, 
    { 
     "date": "2014-09-28", 
     "similar": 1, 
     "trend": 0 
    }, 
    { 
     "date": "2014-09-29", 
     "similar": 16, 
     "trend": 1 
    }, 
    { 
     "date": "2014-10-11", 
     "similar": 3, 
     "trend": 0 
    }, 
    { 
     "date": "2014-10-10", 
     "similar": 2, 
     "trend": 0 
    }, 
    { 
     "date": "2014-10-16", 
     "similar": 2, 
     "trend": 0 
    }, 
    { 
     "date": "2014-10-15", 
     "similar": 1, 
     "trend": 0 
    }, 
    { 
     "date": "2014-10-19", 
     "similar": 3, 
     "trend": 0 
    }, 
    { 
     "date": "2014-10-18", 
     "similar": 1, 
     "trend": 0 
    }, 
    { 
     "date": "2014-10-22", 
     "similar": 1, 
     "trend": 0 
    } 
] 

그래서, 내 생각은 존재하지 않는 날짜를 추가, 그래프에서이 정보를 음모하는 것입니다. 그래프에 플롯하기 위해, Chart.js와 함께 사용하기 위해 날짜가있는 배열, 비슷하거나 비슷한 트렌드가있는 3 개의 배열을 만듭니다. .

/*plot timeline on chart*/ 
var d = new Array; 
var t = new Array; 
var s = new Array; 

$.each (timeline, function(i,item){ 
    idt = new Date(item.date); 
    idt.setDate(idt.getDate()+1); 
    month = idt.getMonth()+1; 
    day = idt.getDate(); 
    if (month<10){month = "0" + month;} 
    if (day<10){day = "0" + day;} 
    nextdt = new Date(); 

    d.push(item.date); 
    t.push(item.trend); 
    s.push(item.similar); 

    if (i+1 < timeline.length && item.date != timeline[i+1].date && idt!=nextdt){ 
     nextdt = new Date (timeline[i+1].date); 
     nextdt.setDate(nextdt.getDate()+1); 
     month = nextdt.getMonth()+1; 
     day = nextdt.getDate(); 
     if (month<10){month = "0" + month;} 
     if (day<10){day = "0" + day;} 

     idt.setDate(idt.getDate()+1); 
     while(nextdt > idt){ 
      month = (idt.getMonth())+1; 
      day = idt.getDate(); 
      if (month<10){month = "0" + month;} 
      if (day<10){day = "0" + day;}    
      d.push(idt.getFullYear()+'-'+ month +'-'+ day); 
      t.push(0); 
      s.push(0); 
      idt.setDate(idt.getDate()+1); 

     } 
    } 
}); 

문제는 좀 중복 날짜를 데 어떤 점에서 나는에서 오전 전날 다음날을 쓰고 있다는 것입니다 내가 노력하고있어 : 그래서 3 개 언급 배열을 만들려면 다음 스크립트를 생성 며칠 동안이 문제를 해결하고 여전히 성공을 거두지는 않았지만 많은 일들과 많은 옵션을 시도했지만 그 중 아무도 성공하지 못했습니다. . :( 날짜 당신이 문제를 볼 수 있습니다 2014년 10월 10일를 보는 경우에, 형식 DD-MM-YYYY를 사용하는

위의 JSON에 대한 3 개 개의 출력은 다음과 같습니다

d   s t 
22/09/2014 1 0 
23/09/2014 0 0 
24/09/2014 0 0 
25/09/2014 0 0 
26/09/2014 0 0 
27/09/2014 0 0 
28/09/2014 1 0 
29/09/2014 16 1 
30/09/2014 0 0 
01/10/2014 0 0 
02/10/2014 0 0 
03/10/2014 0 0 
04/10/2014 0 0 
05/10/2014 0 0 
06/10/2014 0 0 
07/10/2014 0 0 
08/10/2014 0 0 
09/10/2014 0 0 
10/10/2014 0 0 
11/10/2014 3 0 
10/10/2014 2 0 
11/10/2014 0 0 
12/10/2014 0 0 
13/10/2014 0 0 
14/10/2014 0 0 
15/10/2014 0 0 
16/10/2014 2 0 
15/10/2014 1 0 
16/10/2014 0 0 
17/10/2014 0 0 
18/10/2014 0 0 
19/10/2014 3 0 
18/10/2014 1 0 
19/10/2014 0 0 
20/10/2014 0 0 
21/10/2014 0 0 
22/10/2014 0 0 
22/10/2014 1 0 

고마워요! 나는 당신의 타임 라인이 승순이 아닌, 당신은 그것이 당신의 코드에 가정 것을 볼

답변

1

.

당신은

{ 
    "date": "2014-10-11", 
    "similar": 3, 
    "trend": 0 
} 
이 6,

timeline 반복하고 없는 날짜를 생성하는 당신의 생각에 충실하려고 노력하는

{ 
    "date": "2014-10-10", 
    "similar": 2, 
    "trend": 0 
}, 

전에, 나는 이런 식으로 뭔가에 조금에게 코드를 리팩토링했다. 물론 timeline이 날짜별로 오름차순 일 경우에만 작동합니다.

var d = []; 
var t = []; 
var s = []; 

if (timeline.length > 0) { 
    // Assuming dates are 'yyyy-mm-dd'. Sort by date in ascending order. 
    timeline.sort(function (s1, s2) { 
     return s2.date < s1.date; 
    }); 
    // Adds a new point in the three arrays. 
    var addSerie = function (date, trend, similar) { 
     // NOTE: Maybe the way I'm using to get the dd/mm/yyyy is a little bit obscure, 
     // you can use your way here if you see it more clear. 
     d.push(date.toISOString().slice(0, 10).split('-').reverse().join('/')); 
     t.push(trend); 
     s.push(similar); 
    } 
    // Insert the first serie 
    addSerie(new Date(timeline[0].date), timeline[0].trend, timeline[0].similar); 
    // Then the rest 
    for (var i = 1; i < timeline.length; i++) { 
     var d1 = new Date(timeline[i - 1].date); 
     d1.setDate(d1.getDate() + 1); 
     var d2 = new Date(timeline[i].date); 
     // Generate the empty gap dates starting on the next date 
     while (d1 < d2) { 
      addSerie(d1, 0, 0); 
      d1 = new Date(d1); 
      d1.setDate(d1.getDate() + 1); 
     } 
     addSerie(d2, timeline[i].trend, timeline[i].similar); 
    } 
} 
+0

날짜를 주문하는 경우에도 내 스크립트로 마지막 날짜의 두 번 인쇄합니다. 네가 확실히 더 깨끗하고 좋다. 감사. – diegodacal