gpt4 book ai didi

csv - 如何从我的 CSV 中选择哪些列来使用 HighChart 绘制图表?

转载 作者:行者123 更新时间:2023-12-04 19:44:56 26 4
gpt4 key购买 nike

我正在尝试从 CSV 文件创建一个图表,该文件包含每个时间值的多个数据值。我想绘制其中两个数据点的图表,但无法弄清楚如何将 CSV 文件导入数组。

这是我的 CSV 样本

Year,Month,Day,Hour,Time,kWh,Savings,Total kWh
2013,02,06,11,11:00,0,0,308135
2013,02,06,11,11:59,15,1.875,308150
2013,02,06,12,12:59,27,3.375,308177
2013,02,06,13,13:59,34,4.25,308211
2013,02,06,14,14:59,32,4,308243

我想在 y 轴上绘制 kWhSavings 并在 x 轴上绘制时间。任何帮助,将不胜感激。我正在使用标准代码为 Highcharts 导入 CSV 文件,但我确信我需要以某种方式更改它。谢谢!

    var options = {
chart: {
renderTo: 'container',
defaultSeriesType: 'column'
},
title: {
text: 'Wind Turbine Hourly Production'
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'kWh'
}
},
series: []
};

/*
Load the data from the CSV file. This is the contents of the file:

Apples,Pears,Oranges,Bananas,Plums
John,8,4,6,5
Jane,3,4,2,3
Joe,86,76,79,77
Janet,3,16,13,15

*/
$.get('medford-hour.csv', function(data) {
// Split the lines
var lines = data.split('\n');
$.each(lines, function(lineNo, line) {
var items = line.split(',');

// header line containes categories
if (lineNo == 0) {
$.each(items, function(itemNo, item) {
if (itemNo > 0) options.xAxis.categories.push(item);
});
}

// the rest of the lines contain data with their name in the first position
else {
var series = {
data: []
};
$.each(items, function(itemNo, item) {
if (itemNo == 0) {
series.name = item;
} else {
series.data.push(parseFloat(item));
}
});

options.series.push(series);

}

});

最佳答案

您需要编写自己的解析器。这是一个简单的例子,这看起来像什么:

var lines = data.split('\n');
$.each(lines, function (lineNo, line) {
var items = line.split(',');
if(lineNo !== 0) {
var x = + new Date(items[1]+'/'+items[2]+'/'+items[0]+' '+items[4]),
kwh = parseFloat(items[5]),
savings = parseFloat(items[6]);
if(!isNaN(kwh) && !isNaN(savings)){
options.series[0].data.push([x,kwh]);
options.series[1].data.push([x,savings])
}
}
}

和 jsfiddle:https://jsfiddle.net/BlackLabel/9a7wfys8/

关于csv - 如何从我的 CSV 中选择哪些列来使用 HighChart 绘制图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14816749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com