简体   繁体   English

eCharts 如何获取 xAxis 数据

[英]eCharts how to get xAxis data

I have some code with event on click in echart.js like this我在 echart.js 中有一些带有点击事件的代码,就像这样

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

I read documentation echart on https://www.echartsjs.com/en/tutorial.html#Events%20and%20Actions%20in%20ECharts我在https://www.echartsjs.com/en/tutorial.html#Events%20and%20Actions%20in%20ECharts上阅读了文档 echart

but i dont find param to get xAxis data, xAxis data on my chart is date.但我没有找到获取 xAxis 数据的参数,我图表上的 xAxis 数据是日期。

在此处输入图片说明 Look my picture i want to get this value (red circle)看我的图片我想得到这个值(红色圆圈)

My full code我的完整代码

// ============================================================== 
    // 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');
    });

You can use the params object as specified by you.您可以使用您指定的 params object you need to access the name property which holds the value on x axis.您需要访问保存 x 轴上的值的name属性。

Please find the working code below:请在下面找到工作代码:

 // 基于准备好的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