Data from json file for use with CHAPS timeline

1.3k Views Asked by At

I'm trying to use the CHAP links library timeline (http://almende.github.io/chap-links-library/timeline.html).

Example17 is using JSON, but it's in the html file itself. I'd like to use an external JSON file sitting on the web server instead.

Here's my example.json:

{"timeline":[
    {
        "start":"2013,7,26",
        "end":"2013,7,26",
        "content": "Bleah1"
    },
    {
        "start":"2013,7,26",
        "end":"2013,8,2",
        "content": "Bleah2"
    },
    {
        "start":"2013,7,26",
        "end":"2013,8,2",
        "content": "Bleah3"
    },
    {
        "start":"2013,7,26",
        "end":"2013,8,2",
        "content": "Bleah4"
    }
]}

I added this:

<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

And here's the modified function:

        // Called when the Visualization API is loaded.
    function drawVisualization() {
        // Create a JSON data table

          $.getJSON('example.json', function(jsondata) {
                data = jsondata.timeline;
            });


        // specify options
        var options = {
            'width':  '100%',
            'height': '300px',
            'editable': true,   // enable dragging and editing events
            'style': 'box'
        };

        // Instantiate our timeline object.
        timeline = new links.Timeline(document.getElementById('mytimeline'));

        function onRangeChanged(properties) {
            document.getElementById('info').innerHTML += 'rangechanged ' +
                    properties.start + ' - ' + properties.end + '<br>';
        }

        // attach an event listener using the links events handler
        links.events.addListener(timeline, 'rangechanged', onRangeChanged);

        // Draw our timeline with the created data and options
        timeline.draw(data, options);
    }

Anyone who can tell me what I'm doing wrong gets a cookie! :-)

Update: I should specify that it's rendering the timeline div correctly, I'm just getting no data showing up.

2

There are 2 best solutions below

1
On

Your start and end dates need to be parsed as Date objects for use in the timeline

0
On

I stumbled on this post as I was implementing similar functionality.

In version 2.6.1 of timeline.js, around line 3439 where the function links.Timeline.Item is declared, you'll notice a comment relating to implementing parseJSONDate.

/* TODO: use parseJSONDate as soon as it is tested and working (in two directions)
         this.start = links.Timeline.parseJSONDate(data.start);
         this.end = links.Timeline.parseJSONDate(data.end);
         */

I enabled the suggested code and it all works!* (go to the parseJSONDate function to see which formats are accepted)

*(works insofar as dates appear on the timeline.. I'm not using therefore not testing any selection/removal features, images, or anything like that..)