Browse Source

Allow for optional xhr headers object when retrieving data from url

pull/1281/head
Adam Shukur 9 years ago
parent
commit
747740de70
  1. 1
      src/config.js
  2. 2
      src/core.js
  3. 10
      src/data.convert.js
  4. 2
      src/data.load.js

1
src/config.js

@ -55,6 +55,7 @@ c3_chart_internal_fn.getDefaultConfig = function () {
data_onselected: function () {},
data_onunselected: function () {},
data_url: undefined,
data_headers: undefined,
data_json: undefined,
data_rows: undefined,
data_columns: undefined,

2
src/core.js

@ -72,7 +72,7 @@ c3_chart_internal_fn.init = function () {
$$.initParams();
if (config.data_url) {
$$.convertUrlToData(config.data_url, config.data_mimeType, config.data_keys, $$.initWithData);
$$.convertUrlToData(config.data_url, config.data_mimeType, config.data_headers, config.data_keys, $$.initWithData);
}
else if (config.data_json) {
$$.initWithData($$.convertJsonToData(config.data_json, config.data_keys));

10
src/data.convert.js

@ -1,6 +1,12 @@
c3_chart_internal_fn.convertUrlToData = function (url, mimeType, keys, done) {
c3_chart_internal_fn.convertUrlToData = function (url, mimeType, headers, keys, done) {
var $$ = this, type = mimeType ? mimeType : 'csv';
$$.d3.xhr(url, function (error, data) {
var req = $$.d3.xhr(url);
if (headers) {
Object.keys(headers).forEach(function (header) {
req.header(header, headers[header]);
});
}
req.get(function (error, data) {
var d;
if (!data) {
throw new Error(error.responseURL + ' ' + error.status + ' (' + error.statusText + ')');

2
src/data.load.js

@ -39,7 +39,7 @@ c3_chart_internal_fn.loadFromArgs = function (args) {
$$.load($$.convertDataToTargets(args.data), args);
}
else if (args.url) {
$$.convertUrlToData(args.url, args.mimeType, args.keys, function (data) {
$$.convertUrlToData(args.url, args.mimeType, args.headers, args.keys, function (data) {
$$.load($$.convertDataToTargets(data), args);
});
}

Loading…
Cancel
Save