簡體   English   中英

eCharts 如何獲取 xAxis 數據

[英]eCharts how to get xAxis data

我在 echart.js 中有一些帶有點擊事件的代碼,就像這樣

mytempChart2.on('click', function (params) 
        //params.seriesName return aktif/non aktif
        {window.open('' + encodeURIComponent(params.dataType) + '.html', '_self');
    });

我在https://www.echartsjs.com/en/tutorial.html#Events%20and%20Actions%20in%20ECharts上閱讀了文檔 echart

但我沒有找到獲取 xAxis 數據的參數,我圖表上的 xAxis 數據是日期。

在此處輸入圖片說明 看我的圖片我想得到這個值(紅色圓圈)

我的完整代碼

// ============================================================== 
    // Line chart
    // ============================================================== 
    var dom2 = document.getElementById("grafik2");
    var mytempChart2 = echarts.init(dom2);
    var app = {};
    option2 = null;
    option2 = {
        tooltip : {
            trigger: 'axis'
        },
        legend: {
            data:['Non-Aktif', 'Aktif Kembali']
        },
        toolbox: {
            show : true,
            feature : {
                magicType : {show: true, type: ['line', 'bar']},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        color: ["#EF5350", "#2FBF2F"],
        calculable : true,
        xAxis : [
            {
                type : 'category',
                boundaryGap : false,
                data : [01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,],
            }
        ],
        yAxis : [
            {
                type : 'value',
                axisLabel : {
                    formatter: '{value}'
                }
            }
        ],
        series : [
            {
                name:'Non-Aktif',
                type:'line',
                data:[0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
                markPoint : {
                    data : [
                        {name: 'tanggal', value: 100}, 
                        {type : 'max', name: 'Tertinggi'},
                        {type : 'min', name: 'Terendah'}
                    ]
                },
                itemStyle: {
                    normal: {
                        lineStyle: {
                            shadowColor : 'rgba(0,0,0,0.3)',
                            shadowBlur: 10,
                            shadowOffsetX: 8,
                            shadowOffsetY: 8 
                        }
                    }
                }, 
                markLine : {
                    data : [
                        {type : 'average', name : 'Rata-rata'}
                    ]
                }
            },
            {
                name:'Aktif Kembali',
                type:'line',
                data:[0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
                markPoint : {
                    data : [
                        {type : 'max', name: 'Tertinggi'},
                        {type : 'min', name: 'Terendah'}
                    ]
                },
                itemStyle: {
                    normal: {
                        lineStyle: {
                            shadowColor : 'rgba(0,0,0,0.3)',
                            shadowBlur: 10,
                            shadowOffsetX: 8,
                            shadowOffsetY: 8 
                        }
                    }
                }, 
                markLine : {
                    data : [
                        {type : 'average', name : 'Rata-rata'}
                    ]
                }
            }
        ]
    };

    if (option2 && typeof option2 === "object") {
        mytempChart2.setOption(option2, true), $(function() {
            function resize() {
                setTimeout(function() {
                    mytempChart2.resize()
                }, 100)
            }
            $(window).on("resize", resize), $(".sidebartoggler").on("click", resize)
        });
    }

    mytempChart2.setOption(option2);
    mytempChart2.on('click', function (params) 
        //params.seriesName return aktif/non aktif
        {window.open('' + encodeURIComponent(params.name) + '.html', '_self');
    });

您可以使用您指定的 params object 您需要訪問保存 x 軸上的值的name屬性。

請在下面找到工作代碼:

 // 基於准備好的dom,初始化echarts實例var myChart = echarts.init(document.getElementById('main')); option = { xAxis: [{ type: 'category', boundaryGap: false, data: [01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, ], }], yAxis: [{ type: 'value', axisLabel: { formatter: '{value}' } }], series: [{ name: 'Non-Aktif', type: 'line', data: [0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ], markPoint: { data: [{ name: 'tanggal', value: 100 }, { type: 'max', name: 'Tertinggi' }, { type: 'min', name: 'Terendah' } ] }, itemStyle: { normal: { lineStyle: { shadowColor: 'rgba(0,0,0,0.3)', shadowBlur: 10, shadowOffsetX: 8, shadowOffsetY: 8 } } }, markLine: { data: [{ type: 'average', name: 'Rata-rata' }] } }, { name: 'Aktif Kembali', type: 'line', data: [0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ], markPoint: { data: [{ type: 'max', name: 'Tertinggi' }, { type: 'min', name: 'Terendah' } ] }, itemStyle: { normal: { lineStyle: { shadowColor: 'rgba(0,0,0,0.3)', shadowBlur: 10, shadowOffsetX: 8, shadowOffsetY: 8 } } }, markLine: { data: [{ type: 'average', name: 'Rata-rata' }] } } ] } // 使用剛指定的配置項和數據顯示圖表。 myChart.setOption(option); myChart.on('click', function(params) { console.log(params.name); });
 <div id="main" style="width: 600px;height:400px;"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.6.0/echarts.min.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM