Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-chatjs甜甜圈悬停运行脚本_Javascript_Jquery_Html_Css_Charts - Fatal编程技术网

Javascript-chatjs甜甜圈悬停运行脚本

Javascript-chatjs甜甜圈悬停运行脚本,javascript,jquery,html,css,charts,Javascript,Jquery,Html,Css,Charts,这个问题似乎很容易,但我不知道如何解决它 我有这个。我想要的是使用实际标题运行脚本。例如,如果带有“S:7”的部分悬停,我想console.log()S:7。你能帮忙吗?我不熟悉chart.js,所以这有点像黑客,但在大多数情况下,它似乎是有效的。它使用@drinor的答案中的代码 一个潜在的问题是,如果您将鼠标悬停在某个特定的切片上,然后将鼠标从饼图上移开,然后再次将鼠标悬停在同一个切片上,则不会向控制台写入任何内容。如果在这种情况下确实需要控制台输出,那么可以通过添加一个mouseout事件

这个问题似乎很容易,但我不知道如何解决它


我有这个。我想要的是使用实际标题运行脚本。例如,如果带有“S:7”的部分悬停,我想
console.log()
S:7。你能帮忙吗?

我不熟悉chart.js,所以这有点像黑客,但在大多数情况下,它似乎是有效的。它使用@drinor的答案中的代码

一个潜在的问题是,如果您将鼠标悬停在某个特定的切片上,然后将鼠标从饼图上移开,然后再次将鼠标悬停在同一个切片上,则不会向控制台写入任何内容。如果在这种情况下确实需要控制台输出,那么可以通过添加一个
mouseout
事件来设置
currentIndex=-1

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.min.js"></script>
        <style type="text/css">
            .container {
              width: 80%;
              margin: 15px auto;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h2>Chart.js — Pie Chart Demo (apples)</h2>
            <div>
                <canvas id="myChart"></canvas>
            </div>
        </div>

        <script type="text/javascript">

            var currentIndex = -1;

            Chart.defaults.global.hover.onHover = function(x) {
                if(x[0]) {
                    var index = x[0]._index;

                    if (index !== currentIndex) {
                        currentIndex = index;
                        console.log(x[0]._model.label + ': ' + x[0]._chart.config.data.datasets[0].data[index]);
                    }
                }
            };

            var ctx = document.getElementById("myChart").getContext('2d');
            var myChart = new Chart(ctx, {
              type: 'pie',
              data: {
                labels: ["M", "T", "W", "T", "F", "S", "S"],
                datasets: [{
                  backgroundColor: [
                    "#2ecc71",
                    "#3498db",
                    "#95a5a6",
                    "#9b59b6",
                    "#f1c40f",
                    "#e74c3c",
                    "#34495e"
                  ],
                  data: [12, 19, 3, 17, 28, 24, 7]
                }]
              }
            });
        </script>
    </body>
</html>

.集装箱{
宽度:80%;
利润率:15px自动;
}
Chart.js-饼图演示(苹果)
var currentIndex=-1;
Chart.defaults.global.hover.onHover=函数(x){
if(x[0]){
var指数=x[0]。\u指数;
如果(索引!==当前索引){
currentIndex=索引;
console.log(x[0]。_model.label+':'+x[0]。_chart.config.data.datasets[0]。data[index]);
}
}
};
var ctx=document.getElementById(“myChart”).getContext(“2d”);
var myChart=新图表(ctx{
键入“pie”,
数据:{
标签:[“M”、“T”、“W”、“T”、“F”、“S”、“S”],
数据集:[{
背景颜色:[
“#2ecc71”,
“#3498db”,
“#95a5a6”,
“#9b59b6”,
“#f1c40f”,
“#e74c3c”,
“#34495e”
],
数据:[12,19,3,17,28,24,7]
}]
}
});