How can I remove only the gridlines and keep the x-axis and y-axis base line in Amcharts4. I’m using Amcharts with Vuejs. Here the code of the chart component
JavaScript
x
90
90
1
<template>
2
<b-card>
3
<div class="d-flex align-items-center justify-content-between">
4
<h5>Real Time</h5>
5
</div>
6
<div class="real-time-graph mt-4" ref="chartdiv"></div>
7
</b-card>
8
</template>
9
10
<script>
11
import * as am4core from "@amcharts/amcharts4/core";
12
import * as am4charts from "@amcharts/amcharts4/charts";
13
import am4themes_animated from "@amcharts/amcharts4/themes/animated";
14
15
am4core.useTheme(am4themes_animated);
16
17
export default {
18
name: "main-details",
19
mounted() {
20
let chart = am4core.create(this.$refs.chartdiv, am4charts.XYChart);
21
22
chart.data = [{
23
"date": "2020-01-01",
24
"value1": 180,
25
}, {
26
"date": "2020-01-08",
27
"value1": 300,
28
}, {
29
"date": "2020-01-15",
30
"value1": 160,
31
}, {
32
"date": "2020-01-22",
33
"value1": 650,
34
}, {
35
"date": "2020-01-29",
36
"value1": 220,
37
}, {
38
"date": "2020-02-05",
39
"value1": 440,
40
}, {
41
"date": "2020-02-12",
42
"value1": 550,
43
}, {
44
"date": "2020-02-19",
45
"value1": 290,
46
}, {
47
"date": "2020-02-26",
48
"value1": 440,
49
}, {
50
"date": "2020-03-04",
51
"value1": 150,
52
}, {
53
"date": "2020-03-11",
54
"value1": 430,
55
}];
56
57
// Set input format for the dates
58
chart.dateFormatter.inputDateFormat = "yyyy-MM-dd";
59
chart.numberFormatter.numberFormat = '#,###w';
60
61
// Create axes
62
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
63
dateAxis.renderer.minGridDistance = 50;
64
dateAxis.renderer.grid.template.disabled = true;
65
dateAxis.renderer.line.disabled = false;
66
dateAxis.renderer.ticks.template.disabled = true;
67
dateAxis.renderer.labels.template.fill = am4core.color("#C3CDDD");
68
69
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
70
valueAxis.renderer.grid.template.disabled = true;
71
valueAxis.renderer.labels.template.fill = am4core.color("#C3CDDD");
72
73
// Create series
74
var series1 = chart.series.push(new am4charts.StepLineSeries());
75
series1.dataFields.valueY = "value1";
76
series1.dataFields.dateX = "date";
77
series1.strokeWidth = 2;
78
series1.stroke = "#3AB7FD";
79
80
series1.fillOpacity = 1;
81
var gradient1 = new am4core.LinearGradient();
82
gradient1.addColor(chart.colors.getIndex(0), 0.2);
83
gradient1.addColor(chart.colors.getIndex(0), 0);
84
gradient1.rotation = 90;
85
series1.fill = gradient1;
86
87
}
88
}
89
</script>
90
Both axis and gridlines are removed using when :
JavaScript
1
3
1
dateAxis.renderer.ticks.template.disabled = true;
2
valueAxis.renderer.grid.template.disabled = true;
3
This is the output graph of the above code. I need to show both x and y base axis and only the gridlines removed.
Advertisement
Answer
You can set the axis renderer’s line object strokeOpacity to a non-zero value to show the axis lines:
JavaScript
1
3
1
dateAxis.renderer.line.strokeOpacity = 1;
2
valueAxis.renderer.line.strokeOpacity = 1;
3
Demo:
JavaScript
1
66
66
1
let chart = am4core.create("chartdiv", am4charts.XYChart);
2
3
chart.data = [{
4
"date": "2020-01-01",
5
"value1": 180,
6
}, {
7
"date": "2020-01-08",
8
"value1": 300,
9
}, {
10
"date": "2020-01-15",
11
"value1": 160,
12
}, {
13
"date": "2020-01-22",
14
"value1": 650,
15
}, {
16
"date": "2020-01-29",
17
"value1": 220,
18
}, {
19
"date": "2020-02-05",
20
"value1": 440,
21
}, {
22
"date": "2020-02-12",
23
"value1": 550,
24
}, {
25
"date": "2020-02-19",
26
"value1": 290,
27
}, {
28
"date": "2020-02-26",
29
"value1": 440,
30
}, {
31
"date": "2020-03-04",
32
"value1": 150,
33
}, {
34
"date": "2020-03-11",
35
"value1": 430,
36
}];
37
38
// Set input format for the dates
39
chart.dateFormatter.inputDateFormat = "yyyy-MM-dd";
40
chart.numberFormatter.numberFormat = '#,###w';
41
42
// Create axes
43
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
44
dateAxis.renderer.minGridDistance = 50;
45
dateAxis.renderer.grid.template.disabled = true;
46
dateAxis.renderer.ticks.template.disabled = true;
47
dateAxis.renderer.labels.template.fill = am4core.color("#C3CDDD");
48
dateAxis.renderer.line.strokeOpacity = 1;
49
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
50
valueAxis.renderer.grid.template.disabled = true;
51
valueAxis.renderer.labels.template.fill = am4core.color("#C3CDDD");
52
valueAxis.renderer.line.strokeOpacity = 1;
53
54
// Create series
55
var series1 = chart.series.push(new am4charts.StepLineSeries());
56
series1.dataFields.valueY = "value1";
57
series1.dataFields.dateX = "date";
58
series1.strokeWidth = 2;
59
series1.stroke = "#3AB7FD";
60
61
series1.fillOpacity = 1;
62
var gradient1 = new am4core.LinearGradient();
63
gradient1.addColor(chart.colors.getIndex(0), 0.2);
64
gradient1.addColor(chart.colors.getIndex(0), 0);
65
gradient1.rotation = 90;
66
series1.fill = gradient1;
JavaScript
1
3
1
<script type="text/javascript" src="https://cdn.amcharts.com/lib/4/core.js"></script>
2
<script type="text/javascript" src="https://cdn.amcharts.com/lib/4/charts.js"></script>
3
<div id="chartdiv" style="width: 100%; height: 98vh"></div>