小智 5
有一个 Chart.js 插件chartjs-plugin-datasource可以轻松地从 CSV 文件加载数据。
假设您有一个如下所示的 CSV 文件,它sample-dataset.csv与您的 HTML 文件保存在同一目录中:
,January,February,March,April,May,June,July
Temperature,7,7,10,15,20,23,26
Precipitation,8.1,14.9,41.0,31.4,42.6,57.5,36.0
Run Code Online (Sandbox Code Playgroud)
在页面中包含 Chart.js 和 chartjs-plugin-datasource:
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datasource@0.1.0"></script>
<canvas id="myChart"></canvas>
Run Code Online (Sandbox Code Playgroud)
然后,sample-dataset.csv在您的脚本中指定。默认情况下,CSV 文件中的每一行都将映射到一个数据集,第一列和第一行将分别被视为数据集标签和索引标签。您还可以使用选项自定义解析 CSV 数据的方式。
var ctx = document.getElementsById("myChart");
var chart = new Chart(ctx, {
type: 'bar',
data: {
datasets: [{
type: 'line',
yAxisID: 'temperature',
backgroundColor: 'transparent',
borderColor: 'rgb(255, 99, 132)',
pointBackgroundColor: 'rgb(255, 99, 132)',
tension: 0,
fill: false
}, {
yAxisID: 'precipitation',
backgroundColor: 'rgba(54, 162, 235, 0.5)',
borderColor: 'transparent'
}]
},
plugins: [ChartDataSource],
options: {
scales: {
yAxes: [{
id: 'temperature',
gridLines: {
drawOnChartArea: false
}
}, {
id: 'precipitation',
position: 'right',
gridLines: {
drawOnChartArea: false
}
}]
},
plugins: {
datasource: {
url: 'sample-dataset.csv'
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
我也时不时地需要做这样的事情。以下是有关如何使用 Chart.js 从 CSV 文件创建图表的链接,该链接准确说明了如何直接从 CSV 文件使用 Chart.js 创建图表。
该用例解释了如何使用 Flex.io Web 服务将 CSV 文件转换为 JSON(全面披露:我是 Flex.io 的高级前端开发人员)。
如果您想查看实际用例,可以使用JsFiddle :
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
$.ajax({
type: 'post',
url: 'https://www.flex.io/api/v1/pipes/flexio-chart-js-csv-to-json/run?stream=0',
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'Bearer nmgzsqppgwqbvkfhjdjd');
},
data: $('form').serialize(),
dataType: "json",
success: function(content) {
// render the JSON result from from the Flex.io pipe
$("#flexio-result-data").text(JSON.stringify(content, null, 2))
var first_item = _.get(content, '[0]', {})
var column_labels = _.map(_.omit(first_item, ['os']), function(val, key) {
if (key != 'os')
return key
})
// use Lodash to reformat the JSON for use with Chart.js
var datasets = _.map(content, function(item) {
// use the 'os' column as our label
var item_label = _.get(item, 'os', 'Not Found')
// create an array of number values from each item's JSON object
var item_values = _.map(_.omit(item, ['os']), function(val) {
return parseFloat(val)
})
return {
label: item_label,
data: item_values,
backgroundColor: getRandomColor()
}
})
var chart_data = {
labels: column_labels,
datasets: datasets
}
// render the JSON result after mapping the data with Lodash
$("#chart-data").text(JSON.stringify(chart_data, null, 2))
// render the chart using Chart.js
var ctx = document.getElementById("canvas").getContext("2d");
window.my_chart = new Chart(ctx, {
type: 'bar',
data: chart_data,
options: {
responsive: true,
legend: {
position: 'top'
},
title: {
display: true,
text: 'Use Flex.io to Create a Chart With Chart.js Directly From a CSV File'
}
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
请随意逐步完成用例,如果有任何问题请告诉我。
| 归档时间: |
|
| 查看次数: |
13916 次 |
| 最近记录: |