<template>
    <div class="chart-wrap">
        <div class="content"></div>
    </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,
            default: [],
            // required: true,
        },
        xField: {
            type: String,
            default: null
            // required: true,
        },
        yField: {
            type: String,
            default: null
            // required: true,
        },
    },
    data() {
        return {
            chart: null,
        };
    },
    methods: {
        createChart(data, xField, yField) {
            let chartWarp = this.$parent.$refs["singleBarchartDiv"]; // 차트 상위 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; // 차트 정보 전역에 담기

            // let root = am5.Root.new(this.$refs.chartdiv);
            // 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,
                paddingBottom:30
            }));

            // 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({
                rotation: 0,  
                centerY: am5.p50,
                centerX: am5.p50,

            });

            xRenderer.grid.template.setAll({
                location: 1
            });

            xRenderer.labels.template.setAll({
                tooltipText: "{category}",
                oversizedBehavior: "truncate",
                maxWidth: 100
            });

            xRenderer.labels.template.setup = function (target) {
                target.set("background", am5.Rectangle.new(root, {
                    fill: am5.color(0x000000),
                    fillOpacity: 0
                }));
            };


            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
                })
            }));

            // 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}}`
                //     labelText: "{categoryX}:{valueY}"
                    
                // })
            }));

            series.columns.template.setAll({ 
                tooltipText: "{categoryX}:{valueY}",
                fillOpacity: 0.5,
                strokeWidth: 2,
                cornerRadiusTL: 5,
                cornerRadiusTR: 5 
            });
            
            series.columns.template.adapters.add("fill", function (fill, target) {
                return chart.get("colors").getIndex(series.columns.indexOf(target));
            });

            // Set data
            data.sort((a, b) => a[yField] - b[yField]);  // 추가된 정렬 코드
            xAxis.data.setAll(data);
            series.data.setAll(data);


            // Make stuff animate on load
            series.appear(1000);
            chart.appear(1000, 100);
            this.chart = chart;
        },


    },
    watch: {
        'chartData': function (newData, oldData) {
            this.createChart(newData, this.xField, this.yField);
        },
    },
    mounted() {
    },
};
</script>

<style scoped>
#chartdiv{
    height: 300px;
}
</style>
  
  