自定义 amCharts 股票图表图例

Customize the amCharts Stock Chart legend

我使用具有比较功能的 amCharts 股票图表。我使用 StockLegend 对象作为图例,我想自定义 valueTextComparing 参数。实际上,我有这个:

var stockLegend = new AmCharts.StockLegend();
stockLegend.markerType = 'bubble';
stockLegend.markerSize = 8;
stockLegend.horizontalGap = 1;
stockLegend.spacing = 100;
stockLegend.periodValueText = '[[value.close]]';
stockLegend.valueTextComparing = '[[value]] | [[percents.value]]%';

我想要的是 [[percents.value]] 有两种不同的颜色切换值是正值还是负值(并在所有 valueTextComparing 上添加粗体效果)。

我在文档中看到一个 valueFunction 参数,但不是比较的等效参数。

你能帮帮我吗?

好的,我找到了一种方法来做我想做的事。这有点作弊,但它的工作。 首先,我使用一个特定的字符来分隔值和百分比(这里是“|”):

stockLegend.periodValueText = '[[value.close]]|[[percents.value.close]]%';
stockLegend.valueTextComparing = '[[value]]|[[percents.value]]%';

之后,我在 HTML 中创建了另一个没有 amCharts 的 Legend :

<div id="graph_second_legend">
    <div id="gsl_0_circle"></div>
    <div id="gsl_0"></div>
    <div id="gsl_1"></div>
    <div id="gsl_2_circle"></div>
    <div id="gsl_2"></div>
    <div id="gsl_3"></div>
</div>

然后,我创建了一个函数来更改此图例:

function parseLegend($){
    $('.amChartsLegend text').each(function(index){
        switch(index){
            case 0:
                var text = $(this).text();
                var content = '<span class="graph_fund_label">' + text + '</span>';
                $('#gsl_'+index).html(content);
                break;
            case 2:
                var text = $(this).text();
                var content = '<span class="graph_index_label">' + text + '</span>';
                $('#gsl_'+index).html(content);
                break;
            default:
                var text = $(this).text().split('|');
                var negative = text[1].split('-');
                var negaClass = '';
                if(negative.length > 1){
                    negaClass = ' negative';
                }
                var content = '<span class="graph_vl_amount">' + text[0] + '</span>&nbsp;&nbsp;&nbsp;&nbsp;';
                content = content + '<span class="graph_vl_percent' + negaClass + '">' + text[1] + '</span>';
                $('#gsl_'+index).html(content);
                break;
        }
    });
}

最后,我在图形选择更改时调用此函数:

chart.addListener("zoomed", function(event){
    parseLegend($);
});

当鼠标悬停在图表上时:

$('.amChartsPanel').mouseover(function(){
    setTimeout(function(){parseLegend($);}, 10);
});
$('.amChartsPanel').mouseout(function(){
    setTimeout(function(){parseLegend($);}, 10);
});
$('.amChartsPanel').mousemove(function(){
    setTimeout(function(){parseLegend($);}, 10);
});
$('.amChartsPanel').mouseleave(function(){
    setTimeout(function(){parseLegend($);}, 10);
});

(我使用了超时,因为 amCharts 需要一点时间来更改图例,而 javascript 事件对他来说太快了)。