gpt4 book ai didi

javascript - 在谷歌图表上只显示 7 天的数据

转载 作者:行者123 更新时间:2023-11-29 21:03:58 26 4
gpt4 key购买 nike

我有一个谷歌图表,我只想在图表上向用户显示 7 天的数据,他们应该能够看到前 7 天的数据。

例如,如果我有 1/7/2017 到 15/7/2017 的数据

最初 grpah 应该只显示 8/7/2017 到 15/7/2017。但如果用户愿意,他们可以返回到之前的 1/7/2017 至 8/7/2017 结果。

现在我正在显示所有 14 天的数据,这看起来不太好

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Google Visualization API Sample
</title>
</script> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load google charts
google.charts.load('current', {'packages':['corechart','line']});

google.charts.setOnLoadCallback(seven_day_compare);

// Draw the chart and set the chart values

function seven_day_compare() {
var data = google.visualization.arrayToDataTable(
[["Date","likes","share"],["2017-07-01",30,20],["2017-07-02",20,20],["2017-07-03",16,10],["2017-07-4",10,15],["2017-07-5",31,66],["2017-07-6",20,15],["2017-07-7",2,5],["2017-07-8",8,6],["2017-07-09",30,20],["2017-07-10",20,20],["2017-07-11",16,10],["2017-07-12",10,15],["2017-07-13",31,66],["2017-07-14",20,15],["2017-07-15",2,5],["2017-07-16",8,6]]
);

// Optional; add a title and set the width and height of the chart
var options = {'title':'Compare like and share', 'width':550, 'height':400,'pointSize': 10,'hAxis':{
'slantedText': true,
'slantedTextAngle':30},'vAxis':{}};

// Display the chart inside the <div> element with id="differnt"

var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);
}
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id = "chart"></div>
</body>
</html>

所以基本上我想将图表分成 7 天的时间段谷歌图表有可能吗?或任何其他建议

最佳答案

这可以使用 DataView

首先,需要将第一列转换为实际日期,
这可以通过计算列来完成...

// convert first column to date
view.setColumns([{
calc: function (dt, row) {
return new Date(dt.getValue(row, 0));
},
label: data.getColumnLabel(0),
type: 'date'
}, 1, 2]);

然后使用getFilteredRows 方法来限制图表上显示的日期范围...

// filter date range
view.setRows(view.getFilteredRows([{
column: 0,
minValue: new Date(2017, 6, 8),
maxValue: new Date(2017, 6, 15)
}]));

请参阅以下工作片段...

google.charts.load('current', {
callback: function () {
var data = google.visualization.arrayToDataTable([["Date","likes","share"],["2017-07-01",30,20],["2017-07-02",20,20],["2017-07-03",16,10],["2017-07-4",10,15],["2017-07-5",31,66],["2017-07-6",20,15],["2017-07-7",2,5],["2017-07-8",8,6],["2017-07-09",30,20],["2017-07-10",20,20],["2017-07-11",16,10],["2017-07-12",10,15],["2017-07-13",31,66],["2017-07-14",20,15],["2017-07-15",2,5],["2017-07-16",8,6]]);

var view = new google.visualization.DataView(data);

// convert first column to date
view.setColumns([{
calc: function (dt, row) {
return new Date(dt.getValue(row, 0));
},
label: data.getColumnLabel(0),
type: 'date'
}, 1, 2]);

// filter date range
view.setRows(view.getFilteredRows([{
column: 0,
minValue: new Date(2017, 6, 8),
maxValue: new Date(2017, 6, 15)
}]));

var chart = new google.visualization.LineChart($('#chart').get(0));
chart.draw(view);
},
packages:['corechart']
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>


编辑

使用选项 --> hAxis.ticks -- 确保哪些日期出现在 x 轴上

动态构建ticks,使用数据表/ View 方法 --> getColumnRange,
它返回具有 minmax

属性的对象

请参阅以下工作片段...

google.charts.load('current', {
callback: function () {
var data = google.visualization.arrayToDataTable([["Date","likes","share"],["2017-07-01",30,20],["2017-07-02",20,20],["2017-07-03",16,10],["2017-07-4",10,15],["2017-07-5",31,66],["2017-07-6",20,15],["2017-07-7",2,5],["2017-07-8",8,6],["2017-07-09",30,20],["2017-07-10",20,20],["2017-07-11",16,10],["2017-07-12",10,15],["2017-07-13",31,66],["2017-07-14",20,15],["2017-07-15",2,5],["2017-07-16",8,6]]);

var view = new google.visualization.DataView(data);

// convert first column to date
view.setColumns([{
calc: function (dt, row) {
return new Date(dt.getValue(row, 0));
},
label: data.getColumnLabel(0),
type: 'date'
}, 1, 2]);

// filter date range
view.setRows(view.getFilteredRows([{
column: 0,
minValue: new Date(2017, 6, 8),
maxValue: new Date(2017, 6, 15)
}]));

// build tick for each day
var oneDay = (1000 * 60 * 60 * 24);
var dateRange = view.getColumnRange(0);
var ticksAxisH = [];
for (var i = dateRange.min.getTime(); i <= dateRange.max.getTime(); i = i + oneDay) {
ticksAxisH.push(new Date(i));
}
// optional, add final tick at far right
if (ticksAxisH.length > 0) {
ticksAxisH.push(new Date(ticksAxisH[ticksAxisH.length - 1].getTime() + oneDay));
}

var chart = new google.visualization.LineChart($('#chart').get(0));
chart.draw(view, {
hAxis: {
ticks: ticksAxisH
}
});
},
packages:['corechart']
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

关于javascript - 在谷歌图表上只显示 7 天的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45037457/

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