#javascript #amcharts
#javascript #amcharts #amcharts4
Вопрос:
Как я могу удалить только линии сетки и сохранить базовые линии по осям x и y в Amcharts4. Я использую Amcharts с Vuejs. Здесь приведен код компонента диаграммы
<template>
<b-card>
<div class="d-flex align-items-center justify-content-between">
<h5>Real Time</h5>
</div>
<div class="real-time-graph mt-4" ref="chartdiv"></div>
</b-card>
</template>
<script>
import * as am4core from "@amcharts/amcharts4/core";
import * as am4charts from "@amcharts/amcharts4/charts";
import am4themes_animated from "@amcharts/amcharts4/themes/animated";
am4core.useTheme(am4themes_animated);
export default {
name: "main-details",
mounted() {
let chart = am4core.create(this.$refs.chartdiv, am4charts.XYChart);
chart.data = [{
"date": "2020-01-01",
"value1": 180,
}, {
"date": "2020-01-08",
"value1": 300,
}, {
"date": "2020-01-15",
"value1": 160,
}, {
"date": "2020-01-22",
"value1": 650,
}, {
"date": "2020-01-29",
"value1": 220,
}, {
"date": "2020-02-05",
"value1": 440,
}, {
"date": "2020-02-12",
"value1": 550,
}, {
"date": "2020-02-19",
"value1": 290,
}, {
"date": "2020-02-26",
"value1": 440,
}, {
"date": "2020-03-04",
"value1": 150,
}, {
"date": "2020-03-11",
"value1": 430,
}];
// Set input format for the dates
chart.dateFormatter.inputDateFormat = "yyyy-MM-dd";
chart.numberFormatter.numberFormat = '#,###w';
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.minGridDistance = 50;
dateAxis.renderer.grid.template.disabled = true;
dateAxis.renderer.line.disabled = false;
dateAxis.renderer.ticks.template.disabled = true;
dateAxis.renderer.labels.template.fill = am4core.color("#C3CDDD");
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.grid.template.disabled = true;
valueAxis.renderer.labels.template.fill = am4core.color("#C3CDDD");
// Create series
var series1 = chart.series.push(new am4charts.StepLineSeries());
series1.dataFields.valueY = "value1";
series1.dataFields.dateX = "date";
series1.strokeWidth = 2;
series1.stroke = "#3AB7FD";
series1.fillOpacity = 1;
var gradient1 = new am4core.LinearGradient();
gradient1.addColor(chart.colors.getIndex(0), 0.2);
gradient1.addColor(chart.colors.getIndex(0), 0);
gradient1.rotation = 90;
series1.fill = gradient1;
}
}
</script>
Обе оси и линии сетки удаляются с помощью when :
dateAxis.renderer.ticks.template.disabled = true;
valueAxis.renderer.grid.template.disabled = true;
Это выходной график приведенного выше кода. Мне нужно показать как базовые оси x, так и y, и только удаленные линии сетки.
Ответ №1:
Вы можете установить для линейного объекта осевого средства визуализации strokeOpacity ненулевое значение, чтобы отобразить линии оси:
dateAxis.renderer.line.strokeOpacity = 1;
valueAxis.renderer.line.strokeOpacity = 1;
ДЕМОНСТРАЦИЯ:
let chart = am4core.create("chartdiv", am4charts.XYChart);
chart.data = [{
"date": "2020-01-01",
"value1": 180,
}, {
"date": "2020-01-08",
"value1": 300,
}, {
"date": "2020-01-15",
"value1": 160,
}, {
"date": "2020-01-22",
"value1": 650,
}, {
"date": "2020-01-29",
"value1": 220,
}, {
"date": "2020-02-05",
"value1": 440,
}, {
"date": "2020-02-12",
"value1": 550,
}, {
"date": "2020-02-19",
"value1": 290,
}, {
"date": "2020-02-26",
"value1": 440,
}, {
"date": "2020-03-04",
"value1": 150,
}, {
"date": "2020-03-11",
"value1": 430,
}];
// Set input format for the dates
chart.dateFormatter.inputDateFormat = "yyyy-MM-dd";
chart.numberFormatter.numberFormat = '#,###w';
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.minGridDistance = 50;
dateAxis.renderer.grid.template.disabled = true;
dateAxis.renderer.ticks.template.disabled = true;
dateAxis.renderer.labels.template.fill = am4core.color("#C3CDDD");
dateAxis.renderer.line.strokeOpacity = 1;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.grid.template.disabled = true;
valueAxis.renderer.labels.template.fill = am4core.color("#C3CDDD");
valueAxis.renderer.line.strokeOpacity = 1;
// Create series
var series1 = chart.series.push(new am4charts.StepLineSeries());
series1.dataFields.valueY = "value1";
series1.dataFields.dateX = "date";
series1.strokeWidth = 2;
series1.stroke = "#3AB7FD";
series1.fillOpacity = 1;
var gradient1 = new am4core.LinearGradient();
gradient1.addColor(chart.colors.getIndex(0), 0.2);
gradient1.addColor(chart.colors.getIndex(0), 0);
gradient1.rotation = 90;
series1.fill = gradient1;
<script type="text/javascript" src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script type="text/javascript" src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv" style="width: 100%; height: 98vh"></div>
Комментарии:
1. вы знаете, как мы это делаем в am5?