Commit 296b4d40 authored by Evgeny's avatar Evgeny

Add setValue getValue functions

parent d118b46b
......@@ -194,4 +194,64 @@ describe('c3 api load', function () {
});
describe("getValue", function(){
it("should get value", function(done){
chart.loadColumns([['data1', 100, 200], ['data2', 200, 300]]);
expect(chart.getValue('data1', 0)).toBe(100);
expect(chart.getValue('data1', 1)).toBe(200);
expect(chart.getValue('data2', 0)).toBe(200);
expect(chart.getValue('data2', 1)).toBe(300);
done();
});
it("should return undefined when not found", function(done){
chart.loadColumns([['data1', 100], ['data2', 200, 300]]);
expect(chart.getValue('data1', 1)).toBe(undefined);
expect(chart.getValue('data2', 2)).toBe(undefined);
done();
});
});
describe("setValue", function(){
it("should set value", function(done){
chart.loadColumns([['data1', 0, 100, 200], ['data2', 0, -100]]);
chart.setValue('data1', 0, 200);
chart.setValue('data2', 1, 100);
expect(chart.getValue('data1', 0)).toBe(200);
expect(chart.getValue('data2', 1)).toBe(100);
done();
});
it("should throw when setting to non-existing sequence", function(done){
expect(function(){
chart.setValue('not presented', 0, 1);
}).toThrow();
done();
});
it("should append value if set to higher point", function(done){
chart.loadColumns([['data1', 0]]);
chart.setValue('data1', 10, 100);
chart.setValue('data1', 2, 200);
var data = chart.getDataById('data1');
expect(data.length).toBe(3);
for(var i = 0; i < 3; i++){
expect(data[i].value).toBe(i*100);
}
done();
});
});
});
......@@ -91,3 +91,35 @@ c3_chart_fn.loadColumns = function(cols){
});
};
c3_chart_fn.setValue = function(id, i, value){
var $$ = this.internal;
var column = $$.api.getDataById(id);
if(isUndefined(column)){
throw new Error("Setting value to non-existing sequence " + id);
}
if(isUndefined(column[i])){
$$.api.appendToColumn([id, value]);
} else {
column[i] = value;
$$.api.loadColumns([[id].concat(column)]);
}
};
c3_chart_fn.getValue = function(id, i){
var $$ = this.internal;
var column = $$.api.getDataById(id);
if(isUndefined(column)){
return undefined;
}
var v = column[i];
if(isUndefined(v)){
return undefined;
}
return v.value;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment