Browse Source

Support TSV - #532

pull/590/head
Masayuki Tanaka 10 years ago
parent
commit
0bf66eef2b
  1. 14
      c3.js
  2. 2
      c3.min.js
  3. 6
      htdocs/data/c3_test.tsv
  4. 13
      htdocs/samples/data_url.html
  5. 14
      src/data.convert.js

14
c3.js

@ -1699,24 +1699,32 @@
var d; var d;
if (type === 'json') { if (type === 'json') {
d = $$.convertJsonToData(JSON.parse(data.response), keys); d = $$.convertJsonToData(JSON.parse(data.response), keys);
} else if (type === 'tsv') {
d = $$.convertTsvToData(data.response);
} else { } else {
d = $$.convertCsvToData(data.response); d = $$.convertCsvToData(data.response);
} }
done.call($$, d); done.call($$, d);
}); });
}; };
c3_chart_internal_fn.convertCsvToData = function (csv) { c3_chart_internal_fn.convertXsvToData = function (xsv, parser) {
var d3 = this.d3, rows = d3.csv.parseRows(csv), d; var rows = parser.parseRows(xsv), d;
if (rows.length === 1) { if (rows.length === 1) {
d = [{}]; d = [{}];
rows[0].forEach(function (id) { rows[0].forEach(function (id) {
d[0][id] = null; d[0][id] = null;
}); });
} else { } else {
d = d3.csv.parse(csv); d = parser.parse(xsv);
} }
return d; return d;
}; };
c3_chart_internal_fn.convertCsvToData = function (csv) {
return this.convertXsvToData(csv, this.d3.csv);
};
c3_chart_internal_fn.convertTsvToData = function (tsv) {
return this.convertXsvToData(tsv, this.d3.tsv);
};
c3_chart_internal_fn.convertJsonToData = function (json, keys) { c3_chart_internal_fn.convertJsonToData = function (json, keys) {
var $$ = this, var $$ = this,
new_rows = [], targetKeys, data; new_rows = [], targetKeys, data;

2
c3.min.js vendored

File diff suppressed because one or more lines are too long

6
htdocs/data/c3_test.tsv

@ -0,0 +1,6 @@
data1 data2 data3
520 380 100
540 350 110
570 400 150
550 370 200
580 420 180
1 data1 data2 data3
2 520 380 100
3 540 350 110
4 570 400 150
5 550 370 200
6 580 420 180

13
htdocs/samples/data_url.html

@ -21,6 +21,13 @@
}); });
}, 1000); }, 1000);
setTimeout(function () {
chart.load({
url: '/data/c3_test.tsv',
mimeType: 'tsv'
});
}, 2000);
setTimeout(function () { setTimeout(function () {
chart = c3.generate({ chart = c3.generate({
data: { data: {
@ -28,14 +35,14 @@
mimeType: 'json' mimeType: 'json'
} }
}); });
}, 2000); }, 3000);
setTimeout(function () { setTimeout(function () {
chart.load({ chart.load({
url: '/data/c3_test_2.json', url: '/data/c3_test_2.json',
mimeType: 'json' mimeType: 'json'
}); });
}, 3000); }, 4000);
setTimeout(function () { setTimeout(function () {
chart.load({ chart.load({
@ -45,7 +52,7 @@
value: ['data1', 'data2'] value: ['data1', 'data2']
} }
}); });
}, 4000); }, 5000);
</script> </script>
</body> </body>

14
src/data.convert.js

@ -4,24 +4,32 @@ c3_chart_internal_fn.convertUrlToData = function (url, mimeType, keys, done) {
var d; var d;
if (type === 'json') { if (type === 'json') {
d = $$.convertJsonToData(JSON.parse(data.response), keys); d = $$.convertJsonToData(JSON.parse(data.response), keys);
} else if (type === 'tsv') {
d = $$.convertTsvToData(data.response);
} else { } else {
d = $$.convertCsvToData(data.response); d = $$.convertCsvToData(data.response);
} }
done.call($$, d); done.call($$, d);
}); });
}; };
c3_chart_internal_fn.convertCsvToData = function (csv) { c3_chart_internal_fn.convertXsvToData = function (xsv, parser) {
var d3 = this.d3, rows = d3.csv.parseRows(csv), d; var rows = parser.parseRows(xsv), d;
if (rows.length === 1) { if (rows.length === 1) {
d = [{}]; d = [{}];
rows[0].forEach(function (id) { rows[0].forEach(function (id) {
d[0][id] = null; d[0][id] = null;
}); });
} else { } else {
d = d3.csv.parse(csv); d = parser.parse(xsv);
} }
return d; return d;
}; };
c3_chart_internal_fn.convertCsvToData = function (csv) {
return this.convertXsvToData(csv, this.d3.csv);
};
c3_chart_internal_fn.convertTsvToData = function (tsv) {
return this.convertXsvToData(tsv, this.d3.tsv);
};
c3_chart_internal_fn.convertJsonToData = function (json, keys) { c3_chart_internal_fn.convertJsonToData = function (json, keys) {
var $$ = this, var $$ = this,
new_rows = [], targetKeys, data; new_rows = [], targetKeys, data;

Loading…
Cancel
Save