简体   繁体   English

如何在Highcharts Javascript库中的散点图上添加额外标签?

[英]How to add an extra label on a scatter plot point in Highcharts Javascript library?

I am using the Highcharts JavaScript library to visualize some float values, which i feed into the js code via php. 我正在使用Highcharts JavaScript库来显示一些浮点值,我通过php提供给js代码。 As you can see in the following picture, on each point's mouseover are now displayed the two values that correspond to the axes values and the text "text: undefined". 正如您在下图中所看到的,现在每个点的鼠标悬停都会显示与轴值和文本“text:undefined”对应的两个值。 当前可视化

My question is : Is there a way to display a different text for each point of the scatter plot? 我的问题是 :有没有办法为散点图的每个点显示不同的文字? I have a text that corresponds to each point, but I haven't found a way to display it. 我有一个与每个点对应的文本,但我还没有找到显示它的方法。

My JavaScript/php code is: 我的JavaScript / php代码是:

<script type="text/javascript">
    $(function () {
var chart;
$(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'scatter',
            zoomType: 'xy'},
        title: {
            text: 'Average 24hrs Sentiment for <?php echo $channel?> by Gender '
        },
        subtitle: {
            text: 'Source: '
        },
        xAxis: {
            title: {
                enabled: true,
                text: 'Hours ([0-24h].[0-60mins])'
            },
            startOnTick: true,
            endOnTick: true,
            showLastLabel: true
        },
        yAxis: {
            title: {
                text: 'Sentiment (N. Bayes) %'
            }
        },
        tooltip: {
            formatter: function() {
                    return ''+
                    this.x +' hrs, '+ this.y +' sentiment, text: ';
            }
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            verticalAlign: 'top',
            x: 24,
            y: 1,
            floating: true,
            backgroundColor: '#FFFFFF',
            borderWidth: 1
        },
        plotOptions: {
            scatter: {
                marker: {
                    radius: 5,
                    states: {
                        hover: {
                            enabled: true,
                            lineColor: 'rgb(100,100,100)'
                        }
                    }
                },
                states: {
                    hover: {
                        marker: {
                            enabled: false
                        }
                    }
                }
            }
        },
        series: [{
            name: 'Female',
            color: 'rgba(223, 83, 83, .5)',
            data: [
            <?php 

            for($j=0;$j<$i1;$j++)
            {
                if($females[$j]['Hour'][0] == "0")
                {
                    echo '['.$females[$j]['Hour'][1].'.'.$females[$j]['Min'].','.$females[$j]['Sent'].'"]';
                }
                else
                    echo '['.$females[$j]['Hour'].'.'.$females[$j]['Min'].','.$females[$j]['Sent'].'"]';

                if(($j+1)!=$i1)
                {
                    echo ",";
                }
            }
        ?>
        ]}, 
            {
            name: 'Male',
            color: 'rgba(119, 152, 191, .5)',
            data: [
            <?php 

            for($j=0;$j<$i2;$j++)
            {
                if($males[$j]['Hour'][0] == "0")
                {
                    echo '['.$males[$j]['Hour'][1].'.'.$males[$j]['Min'].','.$males[$j]['Sent'].',"'.$males[$j]['Text'].'"]';
                }
                else
                    echo '['.$males[$j]['Hour'].'.'.$males[$j]['Min'].','.$males[$j]['Sent'].',"'.$males[$j]['Text'].'"]';

                if(($j+1)!=$i2)
                {
                    echo ",";
                }
            }
            ?>
            ]}]
    });
});    

}); });

Thank you. 谢谢。

If the text is unique to each point, you can pass more than the x and y as values. 如果文本对于每个点都是唯一的,则可以传递超过x和y的值作为值。 In the following example I pass three other values: locked, unlocked, and potential. 在下面的示例中,我传递了三个其他值:locked,unlocked和potential。 Then to access them in the tooltip formatter, do so by using the this.point.locked 然后要在工具提示格式化程序中访问它们,请使用this.point.locked执行此this.point.locked

 this.x +' hrs, '+ this.y +' sentiment, text: ';

Try to set the text in this code line, just in order to check whether my suggestion will solve your problem, try : 尝试在此代码行中设置文本,只是为了检查我的建议是否会解决您的问题,请尝试:

 this.x +' hrs, '+ this.y +' sentiment, text: '+this.x;

And then check whether (this.x) value appears insted of "undefined" . 然后检查(this.x)值是否显示为“未定义”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM