Browse Source

Fix load API with JSON - #379

pull/394/head
Masayuki Tanaka 11 years ago
parent
commit
ba9970240d
  1. 29
      c3.js
  2. 6
      c3.min.js
  3. 5
      htdocs/data/c3_test_2.json
  4. 24
      htdocs/samples/data_url.html

29
c3.js

@ -1494,6 +1494,18 @@
} }
return x; return x;
} }
function convertUrlToData(url, mimeType, keys, done) {
var type = mimeType ? mimeType : 'csv';
d3.xhr(url, function (error, data) {
var d;
if (type === 'json') {
d = convertJsonToData(JSON.parse(data.response), keys);
} else {
d = convertCsvToData(data.response);
}
done(d);
});
}
function convertCsvToData(csv) { function convertCsvToData(csv) {
var rows = d3.csv.parseRows(csv), d; var rows = d3.csv.parseRows(csv), d;
if (rows.length === 1) { if (rows.length === 1) {
@ -4457,7 +4469,7 @@
load(convertDataToTargets(args.data), args); load(convertDataToTargets(args.data), args);
} }
else if (args.url) { else if (args.url) {
d3.csv(args.url, function (error, data) { convertUrlToData(args.url, args.mimeType, args.keys, function (data) {
load(convertDataToTargets(data), args); load(convertDataToTargets(data), args);
}); });
} }
@ -5296,21 +5308,8 @@
/*-- Load data and init chart with defined functions --*/ /*-- Load data and init chart with defined functions --*/
function initWithUrl(args) {
var type = args.mimeType ? args.mimeType : 'csv';
d3.xhr(args.url, function (error, data) {
var d;
if (type === 'json') {
d = convertJsonToData(JSON.parse(data.response), args.keys);
} else {
d = convertCsvToData(data.response);
}
init(d);
});
}
if (config.data.url) { if (config.data.url) {
initWithUrl(config.data); convertUrlToData(config.data.url, config.data.mimeType, config.data.keys, init);
} }
else if (config.data.json) { else if (config.data.json) {
init(convertJsonToData(config.data.json, config.data.keys)); init(convertJsonToData(config.data.json, config.data.keys));

6
c3.min.js vendored

File diff suppressed because one or more lines are too long

5
htdocs/data/c3_test_2.json

@ -0,0 +1,5 @@
{
"data1": [20, 40, 70, 50, 80, 30],
"data2": [180, 150, 200, 170, 220, 400],
"data3": [1200, 1210, 1250, 1300, 1280, 1000]
}

24
htdocs/samples/data_url.html

@ -3,27 +3,39 @@
<link href="/css/c3.css" rel="stylesheet" type="text/css"> <link href="/css/c3.css" rel="stylesheet" type="text/css">
</head> </head>
<body> <body>
<div id="chart1"></div> <div id="chart"></div>
<div id="chart2"></div>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="/js/c3.js"></script> <script src="/js/c3.js"></script>
<script> <script>
var chart1 = c3.generate({ var chart = c3.generate({
bindto: '#chart1',
data: { data: {
url: '/data/c3_test.csv' url: '/data/c3_test.csv'
} }
}); });
var chart2 = c3.generate({ setTimeout(function () {
bindto: '#chart2', chart.load({
url: '/data/c3_test2.csv',
});
}, 1000);
setTimeout(function () {
chart = c3.generate({
data: { data: {
url: '/data/c3_test.json', url: '/data/c3_test.json',
mimeType: 'json' mimeType: 'json'
} }
}); });
}, 2000);
setTimeout(function () {
chart.load({
url: '/data/c3_test_2.json',
mimeType: 'json'
});
}, 3000);
</script> </script>
</body> </body>

Loading…
Cancel
Save