anjel
anjel

Reputation: 1382

dates cannot be shown on highcharts

I am having difficulty showing a string separated with dates on a basic high chart

I am getting and saving the tweets from twitter into arrays and then converting the array into a string separated by commas and quotes.
Unfortunately I cant display them on the graph, I have no idea what I'm doing wrong.

    function search(){
    var value = $('#box').val();
    var array=[];
    var dateArray = [];
    var dateString;
    if (value!==""){$.getJSON("",
      function(data){

   $.each(data.results, function(i, item){
      var user=item.from_user;
      var created_at=new Date(item.created_at);
      var month = created_at.getMonth();
      var day = created_at.getDate();
      var year = created_at.getFullYear();
      var created= day +'/'+ month+ '/'+ year;
      array.push({date:created,username:user});
     });
    //    console.log(array);
    for (var i in array) {
        dateArray.push(array[i].date);
            }  
    dateString="'" + dateArray.join("','") + "'";
    console.log(dateString);
    });
      }
    var chart;
    chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container',
        type: 'line',
        marginRight: 130,
        marginBottom: 25
    },
    title: {
        text: 'Monthly Average Temperature',
        x: -20 //center
    },
    subtitle: {
        text: 'Source: WorldClimate.com',
        x: -20
    },
    xAxis: {
        categories: [dateString]
    },

Upvotes: 1

Views: 645

Answers (2)

EricG
EricG

Reputation: 3849

Fixed JsFiddle

Your x-axis parameter should be as follows:

xAxis: {
        categories: dateString
    },

Als, just change this :-)

for (var i in array) {
    dateString.push(array[i].date);
}  

highchart(dateString);

Oh, and you should change to this, definately.

<input type="text" id="box" value="a"/>


Extra investigation info: It appeared that (in the old situation)

console.log(stringArray);
highchart(stringArray);

was empty. Would you pass

highchart(["ab", "b", "c"]);

then you'd be fine. This is because you are passing an empty array, you are creating the chart before you have the JSON data. I therefore moved the creation to the JSON function.

Upvotes: 2

wergeld
wergeld

Reputation: 14442

Why not use xAxis as datetime:

xAxis: {
    type: 'datetime'
},

Parse the dates into JS time from the twitter data and have something like this as your series data array:

[[<jstime1>, "big cool tweet1"], [<jstime2>, "big cool tweet2"]] 

Upvotes: 0

Related Questions