
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
<template>
<div class="chart-container" ref="chartdiv"></div>
</template>
<script>
import * as am5 from '@amcharts/amcharts5';
import * as am5xy from '@amcharts/amcharts5/xy';
import am5themes_Animated from '@amcharts/amcharts5/themes/Animated';
export default {
props: {
chartData: {
type: Array,
required: true,
},
xField: {
type: String,
required: true,
},
yField: {
type: String,
required: true,
},
color: {
type: Array,
default: ['#1c7ed6'],
},
},
data() {
return {
chart: null,
};
},
methods: {
createChart(data, xField, yField, color) {
console.log(color)
let chartWarp = this.$refs["chartdiv"]; // 차트 상위 div ref 매칭
chartWarp.innerHTML = ""; // 차트 상위 div 내용 초기화 (기존 차트 삭제)
let div = document.createElement("div"); // 차트를 담을 빈 div 생성 (차트 하위 div)
div.style.width = "100%"; // 차트를 담을 div의 넓이
div.style.height = "100%"; // 차트를 담을 div의 높이
chartWarp.appendChild(div); // 차트 상위 div 안에 차트 하위 div를 추가
let root = am5.Root.new(div); // 차트 하위 div에 차트(root) 담기
this.charts = root; // 차트 정보 전역에 담기
root._logo.dispose();
// Set themes
// https://www.amcharts.com/docs/v5/concepts/themes/
root.setThemes([
am5themes_Animated.new(root)
]);
// Create chart
let chart = root.container.children.push(am5xy.XYChart.new(root, {
panX: true,
panY: true,
wheelX: "panX",
wheelY: "zoomX",
pinchZoomX: true
}));
// Add cursor
let cursor = chart.set("cursor", am5xy.XYCursor.new(root, {}));
cursor.lineY.set("visible", false);
// Create axes
let xRenderer = am5xy.AxisRendererX.new(root, { minGridDistance: 30 });
xRenderer.labels.template.setAll({
centerY: am5.p50,
centerX: am5.p50,
});
xRenderer.grid.template.setAll({
location: 1
});
let xAxis = chart.xAxes.push(am5xy.CategoryAxis.new(root, {
maxDeviation: 0.3,
categoryField: xField,
renderer: xRenderer,
tooltip: am5.Tooltip.new(root, {})
}));
let yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
maxDeviation: 0.3,
min: 0,
renderer: am5xy.AxisRendererY.new(root, {
strokeOpacity: 0.1,
inversed: true
})
}));
// Create series
let series = chart.series.push(am5xy.ColumnSeries.new(root, {
name: "Series 1",
xAxis: xAxis,
yAxis: yAxis,
valueYField: yField,
sequencedInterpolation: true,
categoryXField: xField,
tooltip: am5.Tooltip.new(root, {
labelText: `{${yField}}`
})
}));
series.columns.template.setAll({
cornerRadiusTL: 5,
cornerRadiusTR: 5,
strokeOpacity: 0,
});
series.columns.template.adapters.add("fill", function (fill, target) {
const dataIndex = series.dataItems.indexOf(target.dataItem);
// Check if the data index is valid
if (dataIndex !== -1) {
// Calculate the reverse index to apply colors from the end of the array
const reverseIndex = series.dataItems.length - 1 - dataIndex;
// Use the provided colors array
return color[reverseIndex % color.length];
} else {
// If data index is invalid, use the default chart colors
return chart.get("colors").getIndex(series.dataItems.indexOf(target.dataItem));
}
});
// Set data
xAxis.data.setAll(data);
series.data.setAll(data);
// Make stuff animate on load
series.appear(1000);
chart.appear(1000, 100);
return chart;
}
},
watch: {
'chartData': function (newData) {
this.createChart(newData, this.xField, this.yField, this.color);
}
},
mounted() {
// this.chart = this.createChart(this.data, this.xField, this.yField, this.color);
},
};
</script>
<style scoped>
.chart-container {
width: 100%;
height: 300px;
}
</style>