Forest Plot
A demo of amCharts 4, the previous version. See it in amCharts 5.
This demo shows a basic Forest Plot chart.
This is the amCharts 4 version. See this demo in amCharts 5
Open in:
Code
JavaScript
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"category": "Smith et al. 1991",
"measure": 1.3,
"bulletSize": 25,
"high": 3.4,
"low": 1.0
}, {
"category": "Jones et al. 1993",
"measure": 2.1,
"bulletSize": 15,
"high": 2.6,
"low": 0.5
}, {
"category": "Smith et al. 1999",
"measure": 1.8,
"bulletSize": 10,
"high": 3.2,
"low": 0.9
}, {
"category": "Ng et al. 2004",
"measure": 2.3,
"bulletSize": 30,
"high": 2.7,
"low": 1.9
}, {
"category": "Chu et al. 2009",
"measure": 2.1,
"bulletSize": 35,
"high": 2.5,
"low": 1.8
}, {
"category": "Summary measure",
"measure": 2.2,
"bulletSize": 55,
"high": 2.4,
"low": 1.9,
"rotation": 45,
"fill": am4core.color("#fff"),
"label": "{valueX}"
}];
// Create axes
var yAxis = chart.yAxes.push(new am4charts.CategoryAxis());
yAxis.dataFields.category = "category";
yAxis.renderer.grid.template.location = 0;
//yAxis.renderer.minGridDistance = 30;
yAxis.renderer.inversed = true;
var yAxis2 = chart.yAxes.push(new am4charts.CategoryAxis());
yAxis2.dataFields.category = "category";
yAxis2.renderer.grid.template.location = 0;
//yAxis.renderer.minGridDistance = 30;
yAxis2.renderer.inversed = true;
yAxis2.renderer.labels.template.adapter.add("text", function(text, target) {
return "[bold]{measure}[/] ({low}-{high})";
});
yAxis2.renderer.opposite = true;
var xAxis = chart.xAxes.push(new am4charts.ValueAxis());
// Create series for lines
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.openValueX = "low";
series.dataFields.valueX = "high";
series.dataFields.categoryY = "category";
series.columns.template.height = 2;
series.columns.template.strokeWidth = 0;
series.columns.template.fill = chart.colors.getIndex(0);
// Create series for markers
var series2 = chart.series.push(new am4charts.LineSeries());
series2.dataFields.customValue = "bulletSize";
series2.dataFields.valueX = "measure";
series2.dataFields.categoryY = "category";
series2.strokeWidth = 0;
var marker = series2.bullets.push(new am4core.Rectangle());
marker.width = 10;
marker.height = 10;
marker.strokeWidth = 2;
marker.fill = chart.colors.getIndex(0);
marker.stroke = chart.colors.getIndex(0);
marker.propertyFields.rotation = "rotation";
marker.propertyFields.fill = "fill";
marker.nonScalingStroke = true;
marker.verticalCenter = "middle";
marker.horizontalCenter = "middle";
marker.tooltipText = "[bold]{valueX}[/] ({low}-{high})";
var label = series2.bullets.push(new am4core.Label());
label.propertyFields.text = "label";
label.strokeWidth = 0;
label.verticalCenter = "middle";
label.horizontalCenter = "middle";
label.interactionsEnabled = false;
series2.heatRules.push({
"target": marker,
"property": "scale",
"min": 1,
"max": 4,
"dataField": "customValue"
});
// Add summary line
var range = xAxis.axisRanges.create();
range.value = 2.2;
range.grid.strokeWidth = 2;
range.grid.strokeDasharray = "3,3";
HTML
<div id="chartdiv"></div>
CSS
#chartdiv {
width: 100%;
height: 500px;
}
Resources
https://cdn.amcharts.com/lib/4/core.js
https://cdn.amcharts.com/lib/4/charts.js
https://cdn.amcharts.com/lib/4/themes/animated.js
All in one page
<!-- Resources -->
<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>
<!-- Chart code -->
<script>
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"category": "Smith et al. 1991",
"measure": 1.3,
"bulletSize": 25,
"high": 3.4,
"low": 1.0
}, {
"category": "Jones et al. 1993",
"measure": 2.1,
"bulletSize": 15,
"high": 2.6,
"low": 0.5
}, {
"category": "Smith et al. 1999",
"measure": 1.8,
"bulletSize": 10,
"high": 3.2,
"low": 0.9
}, {
"category": "Ng et al. 2004",
"measure": 2.3,
"bulletSize": 30,
"high": 2.7,
"low": 1.9
}, {
"category": "Chu et al. 2009",
"measure": 2.1,
"bulletSize": 35,
"high": 2.5,
"low": 1.8
}, {
"category": "Summary measure",
"measure": 2.2,
"bulletSize": 55,
"high": 2.4,
"low": 1.9,
"rotation": 45,
"fill": am4core.color("#fff"),
"label": "{valueX}"
}];
// Create axes
var yAxis = chart.yAxes.push(new am4charts.CategoryAxis());
yAxis.dataFields.category = "category";
yAxis.renderer.grid.template.location = 0;
//yAxis.renderer.minGridDistance = 30;
yAxis.renderer.inversed = true;
var yAxis2 = chart.yAxes.push(new am4charts.CategoryAxis());
yAxis2.dataFields.category = "category";
yAxis2.renderer.grid.template.location = 0;
//yAxis.renderer.minGridDistance = 30;
yAxis2.renderer.inversed = true;
yAxis2.renderer.labels.template.adapter.add("text", function(text, target) {
return "[bold]{measure}[/] ({low}-{high})";
});
yAxis2.renderer.opposite = true;
var xAxis = chart.xAxes.push(new am4charts.ValueAxis());
// Create series for lines
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.openValueX = "low";
series.dataFields.valueX = "high";
series.dataFields.categoryY = "category";
series.columns.template.height = 2;
series.columns.template.strokeWidth = 0;
series.columns.template.fill = chart.colors.getIndex(0);
// Create series for markers
var series2 = chart.series.push(new am4charts.LineSeries());
series2.dataFields.customValue = "bulletSize";
series2.dataFields.valueX = "measure";
series2.dataFields.categoryY = "category";
series2.strokeWidth = 0;
var marker = series2.bullets.push(new am4core.Rectangle());
marker.width = 10;
marker.height = 10;
marker.strokeWidth = 2;
marker.fill = chart.colors.getIndex(0);
marker.stroke = chart.colors.getIndex(0);
marker.propertyFields.rotation = "rotation";
marker.propertyFields.fill = "fill";
marker.nonScalingStroke = true;
marker.verticalCenter = "middle";
marker.horizontalCenter = "middle";
marker.tooltipText = "[bold]{valueX}[/] ({low}-{high})";
var label = series2.bullets.push(new am4core.Label());
label.propertyFields.text = "label";
label.strokeWidth = 0;
label.verticalCenter = "middle";
label.horizontalCenter = "middle";
label.interactionsEnabled = false;
series2.heatRules.push({
"target": marker,
"property": "scale",
"min": 1,
"max": 4,
"dataField": "customValue"
});
// Add summary line
var range = xAxis.axisRanges.create();
range.value = 2.2;
range.grid.strokeWidth = 2;
range.grid.strokeDasharray = "3,3";
</script>
<!-- HTML -->
<div id="chartdiv"></div>