Browse Source

Add data.mineType option for JSON - #236

pull/282/head
Masayuki Tanaka 11 years ago
parent
commit
81bec125a4
  1. 35
      c3.js
  2. 6
      c3.min.js
  3. 18
      htdocs/samples/data_url.html

35
c3.js

@ -1453,6 +1453,18 @@
} }
return x; return x;
} }
function convertCsvToData(csv) {
var rows = d3.csv.parseRows(csv), d;
if (rows.length === 1) {
d = [{}];
rows[0].forEach(function (id) {
d[0][id] = null;
});
} else {
d = d3.csv.parse(csv);
}
return d;
}
function convertJsonToData(json) { function convertJsonToData(json) {
var keys = Object.keys(json), new_rows = []; var keys = Object.keys(json), new_rows = [];
keys.forEach(function (key) { keys.forEach(function (key) {
@ -2118,7 +2130,7 @@
} }
function isLineType(d) { function isLineType(d) {
var id = (typeof d === 'string') ? d : d.id; var id = (typeof d === 'string') ? d : d.id;
return !(id in __data_types) || ['line', 'spline', 'area', 'area-spline', 'step', 'area-step'].indexOf(__data_types[id]) >= 0; return !__data_types[id] || ['line', 'spline', 'area', 'area-spline', 'step', 'area-step'].indexOf(__data_types[id]) >= 0;
} }
function isStepType(d) { function isStepType(d) {
var id = (typeof d === 'string') ? d : d.id; var id = (typeof d === 'string') ? d : d.id;
@ -5061,21 +5073,22 @@
/*-- Load data and init chart with defined functions --*/ /*-- Load data and init chart with defined functions --*/
if (config.data.url) { function initWithUrl(args) {
var type = args.mineType ? args.mineType : 'csv';
d3.xhr(config.data.url, function (error, data) { d3.xhr(config.data.url, function (error, data) {
// TODO: other mine/type var d;
var rows = d3.csv.parseRows(data.response), d; if (type === 'json') {
if (rows.length === 1) { d = convertJsonToData(JSON.parse(data.response));
d = [{}];
rows[0].forEach(function (id) {
d[0][id] = null;
});
} else { } else {
d = d3.csv.parse(data.response); d = convertCsvToData(data.response);
} }
init(d); init(d);
}); });
} }
if (config.data.url) {
initWithUrl(config.data);
}
else if (config.data.json) { else if (config.data.json) {
init(convertJsonToData(config.data.json)); init(convertJsonToData(config.data.json));
} }
@ -5086,7 +5099,7 @@
init(convertColumnsToData(config.data.columns)); init(convertColumnsToData(config.data.columns));
} }
else { else {
throw Error('url or rows or columns is required.'); throw Error('url or json or rows or columns is required.');
} }
return c3; return c3;

6
c3.min.js vendored

File diff suppressed because one or more lines are too long

18
htdocs/samples/data_url.html

@ -3,16 +3,28 @@
<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="chart"></div> <div id="chart1"></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.min.js"></script> <script src="/js/c3.js"></script>
<script> <script>
var chart = c3.generate({
var chart1 = c3.generate({
bindto: '#chart1',
data: { data: {
url: '/data/c3_test.csv' url: '/data/c3_test.csv'
} }
}); });
var chart2 = c3.generate({
bindto: '#chart2',
data: {
url: '/data/c3_test.json',
mineType: 'json'
}
});
</script> </script>
</body> </body>
</html> </html>

Loading…
Cancel
Save