gpt4 book ai didi

javascript - 如果 dataLabel 的宽度大于 highCharts 中条形的高度,则禁用 dataLabel

转载 作者:行者123 更新时间:2023-12-03 04:24:27 30 4
gpt4 key购买 nike

我已经使用 HighCharts 创建了一个图表,如果数据标签的宽度大于其条形的高度(条形无法容纳数据标签),我想禁用任何数据标签。我设置了特定点的 dataLabel 属性“enabled : false”,但效果没有反射(reflect)在图表中。 Here is the image of properties where 'enabled' is false for one of the points

Here is the image of chart where points with y :- 10700 , 20300 and 20000 should have been disabled

<html>

<body>

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 100px; max-width: 400px; height: 400px; margin: 0 auto"></div>


<script language="javascript">

var pointSelected = {};
var pointHovered = {};
//Highcharts.chart('container',

var chartObject = {
chart: {
type: 'bar',

events : {

render : function(){

var ch = this;
var series = this.series;

ch.series.forEach(function(s){

s.points.forEach(function(point){

var barHeight = point.graphic.element.height.animVal.value;
var dataLabelWidth = point.dataLabel.width;
var plotBoxWidth = ch.plotBox.width;
console.log(plotBoxWidth);
if(barHeight + dataLabelWidth < plotBoxWidth) {

// console.log(barHeight + dataLabelWidth);
// console.log("point will lie inside");

}
//else{

if(dataLabelWidth > barHeight){

//USING JQUERY IT CAN BE DONE BUT I WANT TO AVOID JQUERY AS MUCH AS POSSIBLE $(point.dataLabel.element).fadeOut("fast");
point.dataLabel.alignOptions.enabled = false; //THIS IS WHERE I'M DISABLING POINT
console.log(point)
// point.update({dataLabels : {enabled : false}});
//ch.options.plotOptions.series.dataLabels.enabled = false;


}


if(barHeight + dataLabelWidth > plotBoxWidth){

// console.log(barHeight + dataLabelWidth);
var diff = barHeight + dataLabelWidth - plotBoxWidth;
// console.log(diff);
// var x = point.dataLabel.translateX;
// var y = point.dataLabel.translateY;
// console.log(x);
// console.log(point);
//// diff +=15;
var diff2 = barHeight - dataLabelWidth;
// console.log("diff2" + diff2);
point.dataLabel.translate( diff2 , point.dataLabel.alignAttr.y );
// console.log( point.dataLabel.text);
//point.dataLabel.stork("black");
// point.dataLabel.text.styles.fill = "black";
var elem = $(point.dataLabel.element).children();
$(elem).eq(0).css("fill" , "black");

// console.log(elem);
// $(textElem).attr("style" , "fill : black");
// console.log(textElem);
// $(point.dataLabel.element.innerHTML).eq(0).children().eq(0).text();
// console.log("point will lie outside");
}

// }




})

})
console.log(this);


}



}

},
title: {
text: 'Historic World Population by Region'
},
subtitle: {
text: 'Source: <a href="https://en.wikipedia.org/wiki/World_population">Wikipedia.org</a>'
},
xAxis: {
categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania'],
title: {
text: null
}
},
yAxis: {
min: 0,
title: {
text: 'Population (millions)',
align: 'high'
}
},
tooltip: {

formatter : function(){

return '<b>' +this.series.name + '<br/>' +this.x + '<br/>' + this.y+ '000000</b>'

}

},
plotOptions: {
series : {

allowPointSelect : true,
dataLabels: {
enabled : true,
color : "blue",
crop : true,
overflow: "none"


},

point:{

events : {
select :function() {

//get the selected object
pointSelected.categories = this.category;
pointSelected.y = this.y;
console.log(this);



changeOpacity(pointSelected);

},
mouseOver : function(){

//get the hovered object
pointHovered.categories = this.category;
pointHovered.y = this.y;


changeOpacityOnHover(pointHovered);


},
mouseOut : function(){

//event handler when mouse moves out
changeOpacityOnOut(pointHovered);


}

}

}







}

},
legend: {
align: 'right',
verticalAlign: 'top',
layout: 'vertical',
x: -150,
y: 100,
},
credits: {
enabled: false
},
series: [{
name: 'Year 1800',
data: [10700, 45000, 45000, 20300, 20000],
zones : [{value : 100 , color : 'orange'} , {value : 500 , color : 'black'} , { color : 'blue'}]
}]
}


//debugger
var barChart = new Highcharts.chart('container', chartObject);


//function on mouseOver
function changeOpacityOnHover(pointHovered){

//get the current chart object
var chart = $("#container").highcharts();
//get the points and check each point whether it is the hovered one
chart.series.forEach(function(obj){

obj.data.forEach(function(datum){

//if hovered one then get its graphic element(rect) and change its opacity to 1
if(datum.category == pointHovered.categories && datum.y == pointHovered.y ){

// console.log(datum) ;

// console.log(datum.graphic.element);

var tag = datum.graphic.element;
var x = $(tag).attr("x");
// console.log(x);
$(tag).css("opacity" , "1");

}
});

})



}

function changeOpacityOnOut(pointHovered){

//get the current chart object
var chart = $("#container").highcharts();

//get the points and check each point whether it is the hovered one from which mouse is over
chart.series.forEach(function(obj){
obj.data.forEach(function(datum){
//get its graphic element(rect)
if(datum.category == pointHovered.categories && datum.y == pointHovered.y ){

// console.log(datum) ;

// console.log(datum.graphic.element);

var tag = datum.graphic.element;
var x = $(tag).attr("x");
// console.log(x);

//if the current point(hovered) is selected one OR no point is yet selected , opacity will be 1
if((pointHovered.categories == pointSelected.categories && pointHovered.y== pointSelected.y) || Object.keys(pointSelected).length == 0 )
{

$(tag).css("opacity" , "1");

}
//else change opacity to 0.1
else{

$(tag).css("opacity" , "0.1");

}


}

});

})






}


// if point is selected
function changeOpacity(pointSelected){

//get the current chart object
var chart = $("#container").highcharts();

//get the selected point by comparing each point to pointSelected
chart.series.forEach(function(obj){

obj.data.forEach(function(datum){

// if current point is selected point then change opacity to 1 and its color to the color of its rect tag fill attribute
if(datum.category == pointSelected.categories && datum.y == pointSelected.y){

console.log(datum) ;

// console.log(datum.graphic.element);

var tag = datum.graphic.element;
//var xVal = datum.graphic.element.x.animVal.value;
//var yVal = datum.graphic.element.y.animVal.value
//console.log(xVal);
//console.log(yVal);


var x = $(tag).attr("x");
//console.log(x);
// var x2 = xVal -1;
// console.log(datum.dataLabel.translate(xVal , yVal - 1));
//console.log("after");
// console.log(x2);
//console.log(yVal-1);


$(tag).css("opacity" , "1");
var color = $(tag).attr("fill");
$(tag).css("fill" , color);
// console.log(color + "when clicked");


}
//else let its opacity be 0.1
else{

var tag = datum.graphic.element;
$(tag).css("opacity" , "0.1");



}


});

})


}
</script>






</body>


</html>

我已经使用 jQuery 解决了这个问题,但如果它可以简单地通过设置属性来完成,那就太好了。另外,为什么设置了属性却没有体现出效果?

谢谢。

最佳答案

它应该与您的配置配合使用,这就是 dataLabels 的配置方式:

plotOptions: {
bar: {
dataLabels: {
enabled: true,
crop: true,
overflow: 'none',
// inside: true // by default it's outside the bar
}
}
},

演示:http://jsfiddle.net/pbjpr47t/

关于javascript - 如果 dataLabel 的宽度大于 highCharts 中条形的高度,则禁用 dataLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43798268/

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