Amcharts 无法覆盖整个宽度
Amcharts unable to cover full width
我和中的OP有同样的问题,但解决方案没有解决我的问题。这是我的代码:
const dateAxis = chart.xAxes.push(new am4charts.CategoryAxis());
dateAxis.dataFields.category = 'timestamp';
dateAxis.renderer.grid.template.location = 0;
dateAxis.renderer.grid.template.disabled = true;
dateAxis.tooltip.disabled = true;
dateAxis.baseInterval = { count: 1, timeUnit: 'day' };
dateAxis.renderer.labels.template.fill = am4core.color('rgba(255, 255, 255, .4)');
dateAxis.startOnAxis = true;
dateAxis.equalSpacing = true;
this.setState(() => ({ dateAxis }));*emphasized text*
const createSeries = (field, name, stacked, fillColor, strokeColor) => {
const series = chart.series.push(new am4charts.LineSeries());
series.xAxis.width = am4core.percent(100);
...
这是图表现在的样子:
我希望线条填满图表的整个宽度。任何帮助将不胜感激
仅供参考,您链接到的示例适用于 AmCharts v3,它与 v4(您正在使用的版本)完全不同。
您需要调整startLocation
and endLocation
of the axis to control how much of the cells are visible in order to make the lines/filled area reach the ends of the axis as described in this tutorial。将两个值都设置为 .5 应该就足够了:
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
这在this stacked area chart demo中也有体现。下面复制了一个精简版:
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.data = [{
"year": "1994",
"cars": 1587,
"motorcycles": 650,
"bicycles": 121
}, {
"year": "1995",
"cars": 1567,
"motorcycles": 683,
"bicycles": 146
}, {
"year": "1996",
"cars": 1617,
"motorcycles": 691,
"bicycles": 138
}, {
"year": "1997",
"cars": 1630,
"motorcycles": 642,
"bicycles": 127
}, {
"year": "1998",
"cars": 1660,
"motorcycles": 699,
"bicycles": 105
}, {
"year": "1999",
"cars": 1683,
"motorcycles": 721,
"bicycles": 109
}, {
"year": "2000",
"cars": 1691,
"motorcycles": 737,
"bicycles": 112
}, {
"year": "2001",
"cars": 1298,
"motorcycles": 680,
"bicycles": 101
}, {
"year": "2002",
"cars": 1275,
"motorcycles": 664,
"bicycles": 97
}, {
"year": "2003",
"cars": 1246,
"motorcycles": 648,
"bicycles": 93
}, {
"year": "2004",
"cars": 1318,
"motorcycles": 697,
"bicycles": 111
}, {
"year": "2005",
"cars": 1213,
"motorcycles": 633,
"bicycles": 87
}, {
"year": "2006",
"cars": 1199,
"motorcycles": 621,
"bicycles": 79
}, {
"year": "2007",
"cars": 1110,
"motorcycles": 210,
"bicycles": 81
}, {
"year": "2008",
"cars": 1165,
"motorcycles": 232,
"bicycles": 75
}, {
"year": "2009",
"cars": 1145,
"motorcycles": 219,
"bicycles": 88
}, {
"year": "2010",
"cars": 1163,
"motorcycles": 201,
"bicycles": 82
}, {
"year": "2011",
"cars": 1180,
"motorcycles": 285,
"bicycles": 87
}, {
"year": "2012",
"cars": 1159,
"motorcycles": 277,
"bicycles": 71
}];
chart.dateFormatter.inputDateFormat = "yyyy";
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.minGridDistance = 60;
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
dateAxis.baseInterval = {
timeUnit: "year",
count: 1
}
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.dateX = "year";
series.name = "cars";
series.dataFields.valueY = "cars";
series.tooltipHTML = "<img src='https://www.amcharts.com/lib/3/images/car.png' style='vertical-align:bottom; margin-right: 10px; width:28px; height:21px;'><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 = true;
var series2 = chart.series.push(new am4charts.LineSeries());
series2.name = "motorcycles";
series2.dataFields.dateX = "year";
series2.dataFields.valueY = "motorcycles";
series2.tooltipHTML = "<img src='https://www.amcharts.com/lib/3/images/motorcycle.png' style='vertical-align:bottom; margin-right: 10px; width:28px; height:21px;'><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.6;
series2.stacked = true;
series2.strokeWidth = 2;
var series3 = chart.series.push(new am4charts.LineSeries());
series3.name = "bicycles";
series3.dataFields.dateX = "year";
series3.dataFields.valueY = "bicycles";
series3.tooltipHTML = "<img src='https://www.amcharts.com/lib/3/images/bicycle.png' style='vertical-align:bottom; margin-right: 10px; width:28px; height:21px;'><span style='font-size:14px; color:#000000;'><b>{valueY.value}</b></span>";
series3.tooltipText = "[#000]{valueY.value}[/]";
series3.tooltip.background.fill = am4core.color("#FFF");
series3.tooltip.getFillFromObject = false;
series3.tooltip.getStrokeFromObject = true;
series3.tooltip.background.strokeWidth = 3;
series3.sequencedInterpolation = true;
series3.fillOpacity = 0.6;
series3.defaultState.transitionDuration = 1000;
series3.stacked = true;
series3.strokeWidth = 2;
#chartdiv {
width: 100%;
height: 500px;
}
<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>
我和
const dateAxis = chart.xAxes.push(new am4charts.CategoryAxis());
dateAxis.dataFields.category = 'timestamp';
dateAxis.renderer.grid.template.location = 0;
dateAxis.renderer.grid.template.disabled = true;
dateAxis.tooltip.disabled = true;
dateAxis.baseInterval = { count: 1, timeUnit: 'day' };
dateAxis.renderer.labels.template.fill = am4core.color('rgba(255, 255, 255, .4)');
dateAxis.startOnAxis = true;
dateAxis.equalSpacing = true;
this.setState(() => ({ dateAxis }));*emphasized text*
const createSeries = (field, name, stacked, fillColor, strokeColor) => {
const series = chart.series.push(new am4charts.LineSeries());
series.xAxis.width = am4core.percent(100);
...
这是图表现在的样子:
我希望线条填满图表的整个宽度。任何帮助将不胜感激
仅供参考,您链接到的示例适用于 AmCharts v3,它与 v4(您正在使用的版本)完全不同。
您需要调整startLocation
and endLocation
of the axis to control how much of the cells are visible in order to make the lines/filled area reach the ends of the axis as described in this tutorial。将两个值都设置为 .5 应该就足够了:
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
这在this stacked area chart demo中也有体现。下面复制了一个精简版:
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.data = [{
"year": "1994",
"cars": 1587,
"motorcycles": 650,
"bicycles": 121
}, {
"year": "1995",
"cars": 1567,
"motorcycles": 683,
"bicycles": 146
}, {
"year": "1996",
"cars": 1617,
"motorcycles": 691,
"bicycles": 138
}, {
"year": "1997",
"cars": 1630,
"motorcycles": 642,
"bicycles": 127
}, {
"year": "1998",
"cars": 1660,
"motorcycles": 699,
"bicycles": 105
}, {
"year": "1999",
"cars": 1683,
"motorcycles": 721,
"bicycles": 109
}, {
"year": "2000",
"cars": 1691,
"motorcycles": 737,
"bicycles": 112
}, {
"year": "2001",
"cars": 1298,
"motorcycles": 680,
"bicycles": 101
}, {
"year": "2002",
"cars": 1275,
"motorcycles": 664,
"bicycles": 97
}, {
"year": "2003",
"cars": 1246,
"motorcycles": 648,
"bicycles": 93
}, {
"year": "2004",
"cars": 1318,
"motorcycles": 697,
"bicycles": 111
}, {
"year": "2005",
"cars": 1213,
"motorcycles": 633,
"bicycles": 87
}, {
"year": "2006",
"cars": 1199,
"motorcycles": 621,
"bicycles": 79
}, {
"year": "2007",
"cars": 1110,
"motorcycles": 210,
"bicycles": 81
}, {
"year": "2008",
"cars": 1165,
"motorcycles": 232,
"bicycles": 75
}, {
"year": "2009",
"cars": 1145,
"motorcycles": 219,
"bicycles": 88
}, {
"year": "2010",
"cars": 1163,
"motorcycles": 201,
"bicycles": 82
}, {
"year": "2011",
"cars": 1180,
"motorcycles": 285,
"bicycles": 87
}, {
"year": "2012",
"cars": 1159,
"motorcycles": 277,
"bicycles": 71
}];
chart.dateFormatter.inputDateFormat = "yyyy";
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.minGridDistance = 60;
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
dateAxis.baseInterval = {
timeUnit: "year",
count: 1
}
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.dateX = "year";
series.name = "cars";
series.dataFields.valueY = "cars";
series.tooltipHTML = "<img src='https://www.amcharts.com/lib/3/images/car.png' style='vertical-align:bottom; margin-right: 10px; width:28px; height:21px;'><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 = true;
var series2 = chart.series.push(new am4charts.LineSeries());
series2.name = "motorcycles";
series2.dataFields.dateX = "year";
series2.dataFields.valueY = "motorcycles";
series2.tooltipHTML = "<img src='https://www.amcharts.com/lib/3/images/motorcycle.png' style='vertical-align:bottom; margin-right: 10px; width:28px; height:21px;'><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.6;
series2.stacked = true;
series2.strokeWidth = 2;
var series3 = chart.series.push(new am4charts.LineSeries());
series3.name = "bicycles";
series3.dataFields.dateX = "year";
series3.dataFields.valueY = "bicycles";
series3.tooltipHTML = "<img src='https://www.amcharts.com/lib/3/images/bicycle.png' style='vertical-align:bottom; margin-right: 10px; width:28px; height:21px;'><span style='font-size:14px; color:#000000;'><b>{valueY.value}</b></span>";
series3.tooltipText = "[#000]{valueY.value}[/]";
series3.tooltip.background.fill = am4core.color("#FFF");
series3.tooltip.getFillFromObject = false;
series3.tooltip.getStrokeFromObject = true;
series3.tooltip.background.strokeWidth = 3;
series3.sequencedInterpolation = true;
series3.fillOpacity = 0.6;
series3.defaultState.transitionDuration = 1000;
series3.stacked = true;
series3.strokeWidth = 2;
#chartdiv {
width: 100%;
height: 500px;
}
<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>