缩小删除日期但保留时间
Zooming out remove date but keep time
当我放大我的图表时,我可以看到像 26 Apr, 9:05
这样的日期,但是当我缩小我的图表时,我只能看到像 9:00
这样的时间。我怎样才能一直显示日期?这是我的图表 (https://codepen.io/anon/pen/GLPPGq) :
am4core.useTheme(am4themes_animated);
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.dataSource.url = "https://api.myjson.com/bins/i3np8";
chart.dataSource.events.on("parseended", function(ev) {
var data = ev.target.data;
data.sort(function(lhs, rhs) {
return lhs.time - rhs.time;
});
data.forEach(function(item) {
item.time *= 1000;
});
});
chart.numberFormatter.numberFormat = "#.0b";
chart.dateFormatter.inputDateFormat = "x";
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.dateFormats.setKey("minute", "d MMM, \nHH:mm");
dateAxis.periodChangeDateFormats.setKey("minute", "d MMM, \nHH:mm");
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.dateX = "time";
series.name = "Traffic out";
series.dataFields.valueY = "outDiffFormatted";
series.tooltipHTML = "<span style='font-size:14px; color:#000000;'><b>{valueY.value}</b></span>";
series.tooltipText = "[#000]{valueY.value}[/]";
series.tooltip.background.fill = am4core.color("#FFF");
series.tooltip.getStrokeFromObject = true;
series.tooltip.background.strokeWidth = 3;
series.tooltip.getFillFromObject = false;
series.fillOpacity = 0.6;
series.strokeWidth = 2;
series.stacked = false;
var series2 = chart.series.push(new am4charts.LineSeries());
series2.dataFields.dateX = "time";
series2.name = "Traffic in";
series2.dataFields.valueY = "inDiffFormatted";
series2.tooltipHTML = "<span style='font-size:14px; color:#000000;'><b>{valueY.value}</b></span>";
series2.tooltipText = "[#000]{valueY.value}[/]";
series2.tooltip.background.fill = am4core.color("#FFF");
series2.tooltip.getFillFromObject = false;
series2.tooltip.getStrokeFromObject = true;
series2.tooltip.background.strokeWidth = 3;
series2.sequencedInterpolation = true;
series2.fillOpacity = 0.5;
series2.stacked = false;
series2.strokeWidth = 2;
chart.cursor = new am4charts.XYCursor();
chart.cursor.lineY.opacity = 0;
chart.cursor.xAxis = dateAxis;
chart.scrollbarX = new am4charts.XYChartScrollbar();
chart.scrollbarX.series.push(series);
chart.legend = new am4charts.Legend();
chart.legend.position = "top";
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv {
width: 100%;
height: 500px;
}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>
</body>
</html>
缩放时的时间变化是由于 AmCharts 处理周期的方式——当可见数据的时间跨度达到阈值时,图表会将格式升级或降级为不同周期的日期格式,以确保点更加可见.您可以通过修改 periodChangeDateFormats
和 dateFormats
中的格式来更改此设置,如 dateAxis 的 formatting date and time documentation 中所述:
dateAxis.dateFormats.setKey("hour", "d MMM,\nHH:mm");
dateAxis.periodChangeDateFormats.setKey("hour", "d MMM,\nHH:mm");
根据您的数据集大小,您需要在 baseInterval 下方 and/or 以上的多个时间段上设置此项,以确保各种缩放级别的所需输出。在您的情况下,除了 "minute"
.
之外,还向 "hour"
键添加格式
am4core.useTheme(am4themes_animated);
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.dataSource.url = "https://api.myjson.com/bins/i3np8";
chart.dataSource.events.on("parseended", function(ev) {
var data = ev.target.data;
data.sort(function(lhs, rhs) {
return lhs.time - rhs.time;
});
data.forEach(function(item) {
item.time *= 1000;
});
});
chart.numberFormatter.numberFormat = "#.0b";
chart.dateFormatter.inputDateFormat = "x";
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.dateFormats.setKey("minute", "d MMM, \nHH:mm");
dateAxis.periodChangeDateFormats.setKey("minute", "d MMM, \nHH:mm");
dateAxis.dateFormats.setKey("hour", "d MMM, \nHH:mm");
dateAxis.periodChangeDateFormats.setKey("hour", "d MMM, \nHH:mm");
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.dateX = "time";
series.name = "Traffic out";
series.dataFields.valueY = "outDiffFormatted";
series.tooltipHTML = "<span style='font-size:14px; color:#000000;'><b>{valueY.value}</b></span>";
series.tooltipText = "[#000]{valueY.value}[/]";
series.tooltip.background.fill = am4core.color("#FFF");
series.tooltip.getStrokeFromObject = true;
series.tooltip.background.strokeWidth = 3;
series.tooltip.getFillFromObject = false;
series.fillOpacity = 0.6;
series.strokeWidth = 2;
series.stacked = false;
var series2 = chart.series.push(new am4charts.LineSeries());
series2.dataFields.dateX = "time";
series2.name = "Traffic in";
series2.dataFields.valueY = "inDiffFormatted";
series2.tooltipHTML = "<span style='font-size:14px; color:#000000;'><b>{valueY.value}</b></span>";
series2.tooltipText = "[#000]{valueY.value}[/]";
series2.tooltip.background.fill = am4core.color("#FFF");
series2.tooltip.getFillFromObject = false;
series2.tooltip.getStrokeFromObject = true;
series2.tooltip.background.strokeWidth = 3;
series2.sequencedInterpolation = true;
series2.fillOpacity = 0.5;
series2.stacked = false;
series2.strokeWidth = 2;
chart.cursor = new am4charts.XYCursor();
chart.cursor.lineY.opacity = 0;
chart.cursor.xAxis = dateAxis;
chart.scrollbarX = new am4charts.XYChartScrollbar();
chart.scrollbarX.series.push(series);
chart.legend = new am4charts.Legend();
chart.legend.position = "top";
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv {
width: 100%;
height: 500px;
}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>
</body>
</html>
当我放大我的图表时,我可以看到像 26 Apr, 9:05
这样的日期,但是当我缩小我的图表时,我只能看到像 9:00
这样的时间。我怎样才能一直显示日期?这是我的图表 (https://codepen.io/anon/pen/GLPPGq) :
am4core.useTheme(am4themes_animated);
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.dataSource.url = "https://api.myjson.com/bins/i3np8";
chart.dataSource.events.on("parseended", function(ev) {
var data = ev.target.data;
data.sort(function(lhs, rhs) {
return lhs.time - rhs.time;
});
data.forEach(function(item) {
item.time *= 1000;
});
});
chart.numberFormatter.numberFormat = "#.0b";
chart.dateFormatter.inputDateFormat = "x";
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.dateFormats.setKey("minute", "d MMM, \nHH:mm");
dateAxis.periodChangeDateFormats.setKey("minute", "d MMM, \nHH:mm");
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.dateX = "time";
series.name = "Traffic out";
series.dataFields.valueY = "outDiffFormatted";
series.tooltipHTML = "<span style='font-size:14px; color:#000000;'><b>{valueY.value}</b></span>";
series.tooltipText = "[#000]{valueY.value}[/]";
series.tooltip.background.fill = am4core.color("#FFF");
series.tooltip.getStrokeFromObject = true;
series.tooltip.background.strokeWidth = 3;
series.tooltip.getFillFromObject = false;
series.fillOpacity = 0.6;
series.strokeWidth = 2;
series.stacked = false;
var series2 = chart.series.push(new am4charts.LineSeries());
series2.dataFields.dateX = "time";
series2.name = "Traffic in";
series2.dataFields.valueY = "inDiffFormatted";
series2.tooltipHTML = "<span style='font-size:14px; color:#000000;'><b>{valueY.value}</b></span>";
series2.tooltipText = "[#000]{valueY.value}[/]";
series2.tooltip.background.fill = am4core.color("#FFF");
series2.tooltip.getFillFromObject = false;
series2.tooltip.getStrokeFromObject = true;
series2.tooltip.background.strokeWidth = 3;
series2.sequencedInterpolation = true;
series2.fillOpacity = 0.5;
series2.stacked = false;
series2.strokeWidth = 2;
chart.cursor = new am4charts.XYCursor();
chart.cursor.lineY.opacity = 0;
chart.cursor.xAxis = dateAxis;
chart.scrollbarX = new am4charts.XYChartScrollbar();
chart.scrollbarX.series.push(series);
chart.legend = new am4charts.Legend();
chart.legend.position = "top";
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv {
width: 100%;
height: 500px;
}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>
</body>
</html>
缩放时的时间变化是由于 AmCharts 处理周期的方式——当可见数据的时间跨度达到阈值时,图表会将格式升级或降级为不同周期的日期格式,以确保点更加可见.您可以通过修改 periodChangeDateFormats
和 dateFormats
中的格式来更改此设置,如 dateAxis 的 formatting date and time documentation 中所述:
dateAxis.dateFormats.setKey("hour", "d MMM,\nHH:mm");
dateAxis.periodChangeDateFormats.setKey("hour", "d MMM,\nHH:mm");
根据您的数据集大小,您需要在 baseInterval 下方 and/or 以上的多个时间段上设置此项,以确保各种缩放级别的所需输出。在您的情况下,除了 "minute"
.
"hour"
键添加格式
am4core.useTheme(am4themes_animated);
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.dataSource.url = "https://api.myjson.com/bins/i3np8";
chart.dataSource.events.on("parseended", function(ev) {
var data = ev.target.data;
data.sort(function(lhs, rhs) {
return lhs.time - rhs.time;
});
data.forEach(function(item) {
item.time *= 1000;
});
});
chart.numberFormatter.numberFormat = "#.0b";
chart.dateFormatter.inputDateFormat = "x";
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.dateFormats.setKey("minute", "d MMM, \nHH:mm");
dateAxis.periodChangeDateFormats.setKey("minute", "d MMM, \nHH:mm");
dateAxis.dateFormats.setKey("hour", "d MMM, \nHH:mm");
dateAxis.periodChangeDateFormats.setKey("hour", "d MMM, \nHH:mm");
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.dateX = "time";
series.name = "Traffic out";
series.dataFields.valueY = "outDiffFormatted";
series.tooltipHTML = "<span style='font-size:14px; color:#000000;'><b>{valueY.value}</b></span>";
series.tooltipText = "[#000]{valueY.value}[/]";
series.tooltip.background.fill = am4core.color("#FFF");
series.tooltip.getStrokeFromObject = true;
series.tooltip.background.strokeWidth = 3;
series.tooltip.getFillFromObject = false;
series.fillOpacity = 0.6;
series.strokeWidth = 2;
series.stacked = false;
var series2 = chart.series.push(new am4charts.LineSeries());
series2.dataFields.dateX = "time";
series2.name = "Traffic in";
series2.dataFields.valueY = "inDiffFormatted";
series2.tooltipHTML = "<span style='font-size:14px; color:#000000;'><b>{valueY.value}</b></span>";
series2.tooltipText = "[#000]{valueY.value}[/]";
series2.tooltip.background.fill = am4core.color("#FFF");
series2.tooltip.getFillFromObject = false;
series2.tooltip.getStrokeFromObject = true;
series2.tooltip.background.strokeWidth = 3;
series2.sequencedInterpolation = true;
series2.fillOpacity = 0.5;
series2.stacked = false;
series2.strokeWidth = 2;
chart.cursor = new am4charts.XYCursor();
chart.cursor.lineY.opacity = 0;
chart.cursor.xAxis = dateAxis;
chart.scrollbarX = new am4charts.XYChartScrollbar();
chart.scrollbarX.series.push(series);
chart.legend = new am4charts.Legend();
chart.legend.position = "top";
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv {
width: 100%;
height: 500px;
}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>
</body>
</html>