gpt4 book ai didi

javascript - 避免在 Google Visualization Chart API 中相同的点被绘制两次

转载 作者:行者123 更新时间:2023-12-02 18:25:32 24 4
gpt4 key购买 nike

我正在使用 Google 的 Visualization Chart API 绘制折线图,它只是每 30 分钟改变一次潜在客户数量(整数)。这是我到目前为止所做的:

  google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = 'json string goes here';
var report = $.parseJSON(jsonData); //make it a json object

var data = new google.visualization.DataTable();
data.addColumn('timeofday', 'Time');
data.addColumn('number', 'Leads');
var interval = 1000 * 60 * 30; //interval of 30mins
var graphData = report['rush_hour_reports'];
var length = graphData.length;
var normalized_data = {}; //placeholder object
for(var i=0; i<length; i++){
var dt = new Date(graphData[i]['my_hour']); //date obj from timestamp
//next we round of time in chunks of 30mins(interval)
var dt_rounded = new Date(Math.round(dt.getTime() / interval) * interval);
//check if that time exits, if yes & sum the new lead count with old one as time is same
// Else, just create a new key with timestamp
if(typeof normalized_data[dt_rounded] == 'undefined'){
normalized_data[dt_rounded] = graphData[i]['lead_count'];
}else{
normalized_data[dt_rounded] += graphData[i]['lead_count'];
}
for(key in normalized_data){
if(normalized_data.hasOwnProperty(key)){
var dt = new Date(key);
var hrs = parseInt(dt.getHours(), 10);
var mins = parseInt(dt.getMinutes(), 10);
//add the data into Google Chart using addRow
data.addRow([ [hrs, mins,0], parseInt(normalized_data[key], 10) ]);
}
}
}
var format = new google.visualization.DateFormat({pattern: 'h:mm a'});
console.log(normalized_data);
data.sort(0); //sort it, just in case its not already sorted
format.format(data, 0);

var options = {
title: 'Company Performance',
fontSize: '12px',
curveType: 'function',
animation:{
duration: 1000,
easing: 'out',
},
pointSize: 5,
hAxis: {title: report.time_format,
titleTextStyle: {color: '#FF0000'}
},
vAxis: {title: 'Leads',
titleTextStyle: {color: '#FF0000'}}
};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}

现在,该图的呈现方式如下: http://ubuntuone.com/3NMEtWYkhQSCHx4RERVcgq

如果您仔细注意到它仍然同时绘制了两个潜在客户计数,这是错误的(例如在 6:30 或 7:30),相反,如果它们同时绘制,则应该计算潜在客户的总数/总和.

我在这里做错了什么?

最佳答案

您的问题是,您正在将日期对象转换为“timeofday”数据类型,但您没有在 timeofday 级别聚合数据,因此当您有多天的数据处于同一时间段(12:00)你的例子),你当时得到多个数据点。试试这个:

for(var i=0; i<length; i++){
var dt = new Date(graphData[i]['my_hour']);
var dt_rounded = new Date(Math.round(dt.getTime() / interval) * interval);
var minutes = (parseInt(dt_rounded.getHours(), 10) * 60) + parseInt(dt_rounded.getMinutes(), 10);
if(typeof normalized_data[minutes] == 'undefined'){
normalized_data[minutes] = graphData[i]['lead_count'];
}else{
normalized_data[minutes] += graphData[i]['lead_count'];
}
}
for(var key in normalized_data){
if(normalized_data.hasOwnProperty(key)){
var hrs = Math.floor(key / 60);
var mins = key % 60;
data.addRow([ [hrs, mins,0], parseInt(normalized_data[key], 10) ]);
}
}

在这里查看它的工作情况:http://jsfiddle.net/asgallant/MYUHw/

关于javascript - 避免在 Google Visualization Chart API 中相同的点被绘制两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18411846/

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