gpt4 book ai didi

javascript - AmCharts V4 : Clickable category axes labels (Stacked columns chart)

转载 作者:行者123 更新时间:2023-11-29 18:42:11 24 4
gpt4 key购买 nike

我使用 amchart 4 创建了一个 XYChart ,更准确地说是堆积柱形图,我想让列标签可以点击。这是我所做的(用一点 Twig ):

var chart = am4core.create("chartdiv", am4charts.XYChart);

chart.data = [
{
'CategoryAxis' : 'Column label 1',
{% for valueAxis in listValueAxes %}
'{{ valueAxis.name }}': {{ valueAxis.numericalValue }},
{% endfor %}
},{
'CategoryAxis' : 'Column label 2',
{% for valueAxis in listValueAxes %}
'{{ valueAxis.name }}': {{ valueAxis.numericalValue }},
{% endfor %}
}
];

var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "CategoryAxis";

var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.labels.template.disabled = true;

var label = categoryAxis.renderer.labels.template;

function createSeries(field) {
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = field;
series.dataFields.categoryX = "CategoryAxis";
series.stacked = true;

return series;
}

{% for valueAxis in listValueAxes %}
series = createSeries('{{ valueAxis.name }}');
// Clickable cells
series.columns.template.url = "{{path('viewValueAxis', {'id':valueAxis.id})}}";
{% endfor %}

具有 url 属性的最后一行允许单击列的“单元格”并重定向到专用于表示的值的页面。我也想让列的标签可以点击。我试着做这样的事情:

{% for categoryAxis in listCategoryAxes %}
label.events.on("hit", function(){
window.location.href = "{{path('viewCategoryAxis', {'id':categoryAxis.id})}}";
}, this);
{% endfor %}

问题是标签没有被独立处理,它们重定向到同一个页面。我想使用 url 属性,但点击的目标是在标签上而不是列本身。我会使用像 series.columns.template.labels.url 或者 urlTarget property 这样的东西但这些似乎都不起作用。

最佳答案

如果您的数据中有 url,那么最简单的方法就是设置:

categoryAxis.renderer.labels.template.propertyFields.url = "url"

与命中事件相同:

categoryAxis.renderer.labels.template.events.on("hit", function(event){
window.location.href = event.target.dataItem.dataContext.url;
})

关于javascript - AmCharts V4 : Clickable category axes labels (Stacked columns chart),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56235789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com