I have this AmCharts5 XY-chart:
I want to change it so that each column gets its own color:
I have added fillColor1
and strokeColor1
to each dictionary in the data
list. I need to use them when I generate the bars.
HTML code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Graph</title>
</head>
<body>
<h1>Graph</h1>
<div id="chartdiv_total_count_risk" style="width: 100%;height: 80vh;"></div>
<script src="https://cdn.amcharts.com/lib/5/index.js"></script>
<script src="https://cdn.amcharts.com/lib/5/xy.js"></script>
<script src="https://cdn.amcharts.com/lib/5/themes/Animated.js"></script>
<script src="graph.js"></script>
</body>
</html>
graph.js
// Create root element
// https://www.amcharts.com/docs/v5/getting-started/#Root_element
var root = am5.Root.new("chartdiv_total_count_risk");
// Set themes
// https://www.amcharts.com/docs/v5/concepts/themes/
root.setThemes([
am5themes_Animated.new(root)
]);
// Create chart
// https://www.amcharts.com/docs/v5/charts/xy-chart/
var chart = root.container.children.push(am5xy.XYChart.new(root, {
panX: false,
panY: false,
layout: root.verticalLayout
}));
// Set data
var data = [
{
xlabel: "None",
value1: 5,
fillColor1: "#cccccc",
strokeColor1: "#7a7a7a"
},
{
xlabel: "Low",
value1: 11,
fillColor1: "#0acb8e",
strokeColor1: "#0c664a"
},
{
xlabel: "Medium",
value1: 3,
fillColor1: "#fec400",
strokeColor1: "#806300"
},
{
xlabel: "High",
value1: 5,
fillColor1: "#fe5461",
strokeColor1: "#7a030c"
},
{
xlabel: "Critical",
value1: 5,
fillColor1: "#000000",
strokeColor1: "#000000"
}
]
// Create axes
// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
var xAxis = chart.xAxes.push(am5xy.CategoryAxis.new(root, {
categoryField: "xlabel",
renderer: am5xy.AxisRendererX.new(root, {
minGridDistance: 40,
minorGridEnabled: false
}),
tooltip: am5.Tooltip.new(root, {})
}));
xAxis.data.setAll(data);
var yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
renderer: am5xy.AxisRendererY.new(root, {})
}));
// Add series
// https://www.amcharts.com/docs/v5/charts/xy-chart/series/
function makeSeries(name, fieldName) {
var series = chart.series.push(am5xy.ColumnSeries.new(root, {
name: name,
xAxis: xAxis,
yAxis: yAxis,
valueYField: fieldName,
categoryXField: "xlabel"
}));
series.columns.template.setAll({
tooltipText: "{categoryX} {name}: {valueY}",
width: am5.percent(90),
tooltipY: 0
});
series.data.setAll(data);
}
makeSeries("Criticality", "value1");
// Make stuff animate on load
// https://www.amcharts.com/docs/v5/concepts/animations/#Forcing_appearance_animation
chart.appear(1000, 100);