@amcharts/amcharts5
Version:
amCharts 5
168 lines (140 loc) • 4.1 kB
JavaScript
import * as am5 from "@amcharts/amcharts5";
import * as am5xy from "@amcharts/amcharts5/xy";
import * as am5radar from "@amcharts/amcharts5/radar";
import am5themes_Animated from "@amcharts/amcharts5/themes/Animated";
// Create root element
// https://www.amcharts.com/docs/v5/getting-started/#Root_element
const root = am5.Root.new("chartdiv");
// 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/radar-chart/
const chart = root.container.children.push(am5radar.RadarChart.new(root, {
panX: false,
panY: false,
startAngle: 160,
endAngle: 380
}));
// Create axis and its renderer
// https://www.amcharts.com/docs/v5/charts/radar-chart/gauge-charts/#Axes
const axisRenderer = am5radar.AxisRendererCircular.new(root, {
innerRadius: -40
});
axisRenderer.grid.template.setAll({
stroke: root.interfaceColors.get("background"),
visible: true,
strokeOpacity: 0.8,
strokeWidth: 2,
crisp: false
});
const xAxis = chart.xAxes.push(am5xy.ValueAxis.new(root, {
maxDeviation: 0,
min: -40,
max: 100,
strictMinMax: true,
renderer: axisRenderer
}));
// Add clock hand
// https://www.amcharts.com/docs/v5/charts/radar-chart/gauge-charts/#Clock_hands
const axisDataItem = xAxis.makeDataItem({});
const clockHand = am5radar.ClockHand.new(root, {
pinRadius: am5.percent(20),
radius: am5.percent(100),
bottomWidth: 40
})
const bullet = axisDataItem.set("bullet", am5xy.AxisBullet.new(root, {
sprite: clockHand
}));
xAxis.createAxisRange(axisDataItem);
const label = chart.radarContainer.children.push(am5.Label.new(root, {
fill: am5.color(0xffffff),
centerX: am5.percent(50),
textAlign: "center",
centerY: am5.percent(50),
fontSize: "3em"
}));
axisDataItem.set("value", 50);
bullet.get("sprite").on("rotation", () => {
let value = axisDataItem.get("value");
let text = Math.round(axisDataItem.get("value")).toString();
let fill = am5.color(0x000000);
xAxis.axisRanges.each((axisRange) => {
if (value >= axisRange.get("value") && value <= axisRange.get("endValue")) {
fill = axisRange.get("axisFill").get("fill");
}
})
label.set("text", Math.round(value).toString());
clockHand.pin.animate({ key: "fill", to: fill, duration: 500, easing: am5.ease.out(am5.ease.cubic) })
clockHand.hand.animate({ key: "fill", to: fill, duration: 500, easing: am5.ease.out(am5.ease.cubic) })
});
setInterval(() => {
axisDataItem.animate({
key: "value",
to: Math.round(Math.random() * 140 - 40),
duration: 500,
easing: am5.ease.out(am5.ease.cubic)
});
}, 2000)
chart.bulletsContainer.set("mask", undefined);
// Create axis ranges bands
// https://www.amcharts.com/docs/v5/charts/radar-chart/gauge-charts/#Bands
const bandsData = [{
title: "Unsustainable",
color: "#ee1f25",
lowScore: -40,
highScore: -20
}, {
title: "Volatile",
color: "#f04922",
lowScore: -20,
highScore: 0
}, {
title: "Foundational",
color: "#fdae19",
lowScore: 0,
highScore: 20
}, {
title: "Developing",
color: "#f3eb0c",
lowScore: 20,
highScore: 40
}, {
title: "Maturing",
color: "#b0d136",
lowScore: 40,
highScore: 60
}, {
title: "Sustainable",
color: "#54b947",
lowScore: 60,
highScore: 80
}, {
title: "High Performing",
color: "#0f9747",
lowScore: 80,
highScore: 100
}];
am5.array.each(bandsData, (data) => {
const axisRange = xAxis.createAxisRange(xAxis.makeDataItem({
above: true,
value: data.lowScore,
endValue: data.highScore
}));
axisRange.get("axisFill").setAll({
visible: true,
fill: am5.color(data.color),
fillOpacity: 0.8
});
axisRange.get("label").setAll({
text: data.title,
inside: true,
radius: 15,
fontSize: "0.9em",
fill: root.interfaceColors.get("background")
});
});
// Make stuff animate on load
chart.appear(1000, 100);