#javascript #amcharts4 #candlestick-chart
Вопрос:
У меня возникли некоторые проблемы с тем, чтобы мои еженедельные данные отображались правильно в форме свечей на amCharts.
Когда у меня есть настройка: DateAxis.skipEmptyPeriods = false, когда я наводю курсор мыши на свечу, дата во всплывающей подсказке не соответствует дате начала свечи. Например, в приведенном ниже примере дата на второй свече 2020-01-06, но когда я наведу курсор мыши, она отобразит 2020-01-04.
Когда у меня есть настройка: DateAxis.skipEmptyPeriods = true, многие свечи исчезают. Например, первая свеча-2019-12-30, следующая свеча-2020-02-02 (в январе свечи нет), а следующая свеча-2021-03-09.
У кого-нибудь есть рабочий пример данных недельных свечей на amCharts или есть представление о том, почему происходит вышесказанное?
Я надеюсь, что смогу правильно объяснить свою проблему. Это легко увидеть, глядя на график.
Вот базовый сценарий с некоторыми еженедельными данными, которые показывают проблемы:
am4core.ready(function() {
// Themes begin
//am4core.useTheme(am4themes_animated);
// Themes end
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.paddingRight = 20;
chart.dateFormatter.inputDateFormat = "yyyy-MM-dd";
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.skipEmptyPeriods = true;
//dateAxis.renderer.grid.template.location = 0;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
var series = chart.series.push(new am4charts.CandlestickSeries());
series.dataFields.dateX = "date";
series.dataFields.valueY = "close";
series.dataFields.openValueY = "open";
series.dataFields.lowValueY = "low";
series.dataFields.highValueY = "high";
series.simplifiedProcessing = true;
series.tooltipText = "Open:${openValueY.value}nLow:${lowValueY.value}nHigh:${highValueY.value}nClose:${valueY.value}";
chart.cursor = new am4charts.XYCursor();
// a separate series for scrollbar
var lineSeries = chart.series.push(new am4charts.LineSeries());
lineSeries.dataFields.dateX = "date";
lineSeries.dataFields.valueY = "close";
// need to set on default state, as initially series is "show"
lineSeries.defaultState.properties.visible = false;
// hide from legend too (in case there is one)
lineSeries.hiddenInLegend = true;
lineSeries.fillOpacity = 0.5;
lineSeries.strokeOpacity = 0.5;
var scrollbarX = new am4charts.XYChartScrollbar();
scrollbarX.series.push(lineSeries);
chart.scrollbarX = scrollbarX;
chart.data = [ {
"date": "2019-12-30",
"open": "136.65",
"high": "136.96",
"low": "134.15",
"close": "136.49"
}, {
"date": "2020-01-06",
"open": "135.26",
"high": "135.95",
"low": "131.50",
"close": "131.85"
}, {
"date": "2020-01-13",
"open": "132.90",
"high": "135.27",
"low": "128.30",
"close": "135.25"
}, {
"date": "2020-01-20",
"open": "134.94",
"high": "137.24",
"low": "132.63",
"close": "135.03"
}, {
"date": "2020-01-27",
"open": "136.76",
"high": "136.86",
"low": "132.00",
"close": "134.01"
}, {
"date": "2020-02-03",
"open": "131.11",
"high": "133.00",
"low": "125.09",
"close": "126.39"
}, {
"date": "2020-02-10",
"open": "123.12",
"high": "127.75",
"low": "120.30",
"close": "125.00"
}, {
"date": "2020-02-17",
"open": "128.32",
"high": "129.35",
"low": "126.50",
"close": "127.79"
}, {
"date": "2020-02-24",
"open": "128.29",
"high": "128.30",
"low": "123.71",
"close": "124.03"
}, {
"date": "2020-03-02",
"open": "122.74",
"high": "124.86",
"low": "119.65",
"close": "119.90"
}, {
"date": "2020-03-09",
"open": "117.01",
"high": "118.50",
"low": "111.62",
"close": "117.05"
}, {
"date": "2020-03-16",
"open": "122.01",
"high": "123.50",
"low": "119.82",
"close": "122.06"
}, {
"date": "2020-03-23",
"open": "123.96",
"high": "124.50",
"low": "120.50",
"close": "122.22"
}, {
"date": "2020-03-30",
"open": "122.21",
"high": "128.96",
"low": "121.00",
"close": "127.57"
}, {
"date": "2020-04-06",
"open": "131.22",
"high": "132.75",
"low": "130.33",
"close": "132.51"
}, {
"date": "2020-04-14",
"open": "133.09",
"high": "133.34",
"low": "129.76",
"close": "131.07"
}, {
"date": "2020-04-20",
"open": "130.53",
"high": "135.37",
"low": "129.81",
"close": "135.30"
}, {
"date": "2020-04-28",
"open": "133.39",
"high": "134.66",
"low": "132.10",
"close": "132.25"
}, {
"date": "2020-05-04",
"open": "130.99",
"high": "132.41",
"low": "126.63",
"close": "126.82"
}, {
"date": "2020-05-11",
"open": "129.88",
"high": "134.18",
"low": "129.54",
"close": "134.08"
}, {
"date": "2020-05-18",
"open": "132.67",
"high": "138.25",
"low": "132.30",
"close": "136.25"
}, {
"date": "2020-05-25",
"open": "139.49",
"high": "139.65",
"low": "137.41",
"close": "138.48"
}, {
"date": "2020-06-01",
"open": "139.94",
"high": "145.73",
"low": "139.84",
"close": "144.16"
}, {
"date": "2020-06-08",
"open": "144.97",
"high": "145.84",
"low": "136.10",
"close": "136.76"
}, {
"date": "2020-06-15",
"open": "135.56",
"high": "137.57",
"low": "132.71",
"close": "135.01"
}, {
"date": "2020-06-22",
"open": "132.01",
"high": "132.30",
"low": "130.00",
"close": "131.77"
}, {
"date": "2020-06-29",
"open": "136.99",
"high": "138.04",
"low": "133.95",
"close": "136.71"
}, {
"date": "2020-07-06",
"open": "137.90",
"high": "138.30",
"low": "133.75",
"close": "135.49"
}, {
"date": "2020-07-13",
"open": "135.99",
"high": "139.40",
"low": "135.75",
"close": "136.85"
}];
}); // end am4core.ready()
<html>
<head>
<script src="http://www.amcharts.com/lib/4/core.js"></script>
<script src="http://www.amcharts.com/lib/4/charts.js"></script>
</head>
<body>
<div id="chartdiv" style="width: 100%; height: 500px;"></div>
</body>
</html>
Ответ №1:
Иногда вам нужно вручную указать базовый интервал на вашей оси, если автоматически обнаруженная степень детализации не соответствует вашим данным (ссылка). В этом случае установка базового интервала в 1 день исправит ваш график:
dateAxis.baseInterval = {
timeUnit: 'day',
count: 1
};
Обновленный код ниже:
am4core.ready(function() {
// Themes begin
//am4core.useTheme(am4themes_animated);
// Themes end
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.paddingRight = 20;
chart.dateFormatter.inputDateFormat = "yyyy-MM-dd";
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.skipEmptyPeriods = true;
//dateAxis.renderer.grid.template.location = 0;
dateAxis.baseInterval = {
timeUnit: 'day',
count: 1
};
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
var series = chart.series.push(new am4charts.CandlestickSeries());
series.dataFields.dateX = "date";
series.dataFields.valueY = "close";
series.dataFields.openValueY = "open";
series.dataFields.lowValueY = "low";
series.dataFields.highValueY = "high";
series.simplifiedProcessing = true;
series.tooltipText = "Open:${openValueY.value}nLow:${lowValueY.value}nHigh:${highValueY.value}nClose:${valueY.value}";
chart.cursor = new am4charts.XYCursor();
// a separate series for scrollbar
var lineSeries = chart.series.push(new am4charts.LineSeries());
lineSeries.dataFields.dateX = "date";
lineSeries.dataFields.valueY = "close";
// need to set on default state, as initially series is "show"
lineSeries.defaultState.properties.visible = false;
// hide from legend too (in case there is one)
lineSeries.hiddenInLegend = true;
lineSeries.fillOpacity = 0.5;
lineSeries.strokeOpacity = 0.5;
var scrollbarX = new am4charts.XYChartScrollbar();
scrollbarX.series.push(lineSeries);
chart.scrollbarX = scrollbarX;
chart.data = [ {
"date": "2019-12-30",
"open": "136.65",
"high": "136.96",
"low": "134.15",
"close": "136.49"
}, {
"date": "2020-01-06",
"open": "135.26",
"high": "135.95",
"low": "131.50",
"close": "131.85"
}, {
"date": "2020-01-13",
"open": "132.90",
"high": "135.27",
"low": "128.30",
"close": "135.25"
}, {
"date": "2020-01-20",
"open": "134.94",
"high": "137.24",
"low": "132.63",
"close": "135.03"
}, {
"date": "2020-01-27",
"open": "136.76",
"high": "136.86",
"low": "132.00",
"close": "134.01"
}, {
"date": "2020-02-03",
"open": "131.11",
"high": "133.00",
"low": "125.09",
"close": "126.39"
}, {
"date": "2020-02-10",
"open": "123.12",
"high": "127.75",
"low": "120.30",
"close": "125.00"
}, {
"date": "2020-02-17",
"open": "128.32",
"high": "129.35",
"low": "126.50",
"close": "127.79"
}, {
"date": "2020-02-24",
"open": "128.29",
"high": "128.30",
"low": "123.71",
"close": "124.03"
}, {
"date": "2020-03-02",
"open": "122.74",
"high": "124.86",
"low": "119.65",
"close": "119.90"
}, {
"date": "2020-03-09",
"open": "117.01",
"high": "118.50",
"low": "111.62",
"close": "117.05"
}, {
"date": "2020-03-16",
"open": "122.01",
"high": "123.50",
"low": "119.82",
"close": "122.06"
}, {
"date": "2020-03-23",
"open": "123.96",
"high": "124.50",
"low": "120.50",
"close": "122.22"
}, {
"date": "2020-03-30",
"open": "122.21",
"high": "128.96",
"low": "121.00",
"close": "127.57"
}, {
"date": "2020-04-06",
"open": "131.22",
"high": "132.75",
"low": "130.33",
"close": "132.51"
}, {
"date": "2020-04-14",
"open": "133.09",
"high": "133.34",
"low": "129.76",
"close": "131.07"
}, {
"date": "2020-04-20",
"open": "130.53",
"high": "135.37",
"low": "129.81",
"close": "135.30"
}, {
"date": "2020-04-28",
"open": "133.39",
"high": "134.66",
"low": "132.10",
"close": "132.25"
}, {
"date": "2020-05-04",
"open": "130.99",
"high": "132.41",
"low": "126.63",
"close": "126.82"
}, {
"date": "2020-05-11",
"open": "129.88",
"high": "134.18",
"low": "129.54",
"close": "134.08"
}, {
"date": "2020-05-18",
"open": "132.67",
"high": "138.25",
"low": "132.30",
"close": "136.25"
}, {
"date": "2020-05-25",
"open": "139.49",
"high": "139.65",
"low": "137.41",
"close": "138.48"
}, {
"date": "2020-06-01",
"open": "139.94",
"high": "145.73",
"low": "139.84",
"close": "144.16"
}, {
"date": "2020-06-08",
"open": "144.97",
"high": "145.84",
"low": "136.10",
"close": "136.76"
}, {
"date": "2020-06-15",
"open": "135.56",
"high": "137.57",
"low": "132.71",
"close": "135.01"
}, {
"date": "2020-06-22",
"open": "132.01",
"high": "132.30",
"low": "130.00",
"close": "131.77"
}, {
"date": "2020-06-29",
"open": "136.99",
"high": "138.04",
"low": "133.95",
"close": "136.71"
}, {
"date": "2020-07-06",
"open": "137.90",
"high": "138.30",
"low": "133.75",
"close": "135.49"
}, {
"date": "2020-07-13",
"open": "135.99",
"high": "139.40",
"low": "135.75",
"close": "136.85"
}];
}); // end am4core.ready()
<html>
<head>
<script src="http://www.amcharts.com/lib/4/core.js"></script>
<script src="http://www.amcharts.com/lib/4/charts.js"></script>
</head>
<body>
<div id="chartdiv" style="width: 100%; height: 500px;"></div>
</body>
</html>
Комментарии:
1. Что ж, спасибо вам, добрый сэр. Это сработало как абсолютное удовольствие! Иногда мне кажется, что я сделал настоящий хэш из этих графиков. Существует так много настроек, которые влияют на функциональность и скорость. Я искренне благодарен. Вы оказали положительное влияние на мое заявление.
2. Пожалуйста. Убедитесь, что вы также принимаете ответ.
3. Сделано и принято. На самом деле я не занимаюсь разработкой полный рабочий день, поэтому я не знаком с протоколами здесь. Спасибо вам за помощь. Возможно, я вернусь, так как мне еще многое предстоит разработать с помощью моей диаграммы 🙂