Raichu Raichu - 5 months ago 29
Javascript Question

How to change the onClick function of C3 load function

I am trying to update a C3 graph with different onclick funtion by using the load function of C3.

The code snippet is here:



var json1 = [{
date: '2014-01-01',
upload: 200,
download: 200,
total: 400
}, {
date: '2014-01-02',
upload: 100,
download: 300,
total: 400
}, {
date: '2014-02-01',
upload: 300,
download: 200,
total: 500
}, {
date: '2014-02-02',
upload: 400,
download: 100,
total: 500
}];

var json2 = [{
date: '2014-01-01',
upload: 200,
download: 500,
total: 700
}, {
date: '2014-01-02',
upload: 500,
download: 450,
total: 950
}, {
date: '2014-02-01',
upload: 200,
download: 800,
total: 1000
}, {
date: '2014-02-02',
upload: 300,
download: 500,
total: 800
}];

var chart = c3.generate({
bindto: '#div1',
data: {
json: json1,
onclick: function (event) {
console.log("11111111111") ;
},
keys: {
x: 'date',
value: ['upload', 'download']
}
},
axis: {
x: {
type: 'timeseries',
tick: {
format: function (x) {
if (x.getDate() === 1) {
return x.toLocaleDateString();
}
}
}
}
}
});

setTimeout(function () {
chart.unload();
}, 1000);

setTimeout(function () {
chart.load({
json: json2,
keys: {
x: 'date',
value: ['upload', 'download']
},
onclick: function (event) {
console.log("22222222") ;
},
});
}, 1000);

<link href="https://rawgit.com/masayuki0812/c3/master/c3.css" rel="stylesheet"/>
<script src="https://rawgit.com/masayuki0812/c3/master/c3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.10/d3.min.js"></script>
<div id="div1"></div>





Please tell me a way so to update the onClick funtion on the load function itself.

Answer

From what I can find, c3.load() does not offer all the elements of c3.generate(), therefore you have 2 ways of doing this.

Reuse c3.generate(), or hand off the click event to a variable that you can change, such as:

// INITIAL CLICK HANDLER
var clickhandler = function(event) {
        	console.log("11111111111");
}

var json1 = [{
        date: '2014-01-01',
        upload: 200,
        download: 200,
        total: 400
    }, {
        date: '2014-01-02',
        upload: 100,
        download: 300,
        total: 400
    }, {
        date: '2014-02-01',
        upload: 300,
        download: 200,
        total: 500
    }, {
        date: '2014-02-02',
        upload: 400,
        download: 100,
        total: 500
    }];
    
    var json2 = [{
        date: '2014-01-01',
        upload: 200,
        download: 500,
        total: 700
    }, {
        date: '2014-01-02',
        upload: 500,
        download: 450,
        total: 950
    }, {
        date: '2014-02-01',
        upload: 200,
        download: 800,
        total: 1000
    }, {
        date: '2014-02-02',
        upload: 300,
        download: 500,
        total: 800
    }];

// GENERATE CHART FROM JSON1            
var chart = c3.generate({
  bindto: '#div1',  
  data: {
        json: json1,
        onclick: function(event) { clickhandler(event) },
        keys: {
            x: 'date',
            value: ['upload', 'download']
        }
    },
    axis: {
        x: {
            type: 'timeseries',
            tick: {
                format: function (x) {
                    if (x.getDate() === 1) {
                        return x.toLocaleDateString();
                    }
                }
            }
        }
    }
});

// MAKE CHANGES TO THE LOADED CHART
setTimeout(function () {
    // CHANGE THE CLICK EVENT HANDLER
    clickhandler = function(event) {
        console.log('22222222222');
    };
    // UNLOAD EXISTING DATA AND LOAD NEW DATA FROM JSON2
    chart.load({
                json: json2,
                keys: {
                    x: 'date',
                    value: ['upload', 'download']
                },
                unload: chart.columns
   });
}, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.11/c3.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.11/c3.js"></script>
<div id="div1"></div>