Las etiquetas giradas del eje X superponen el gráfico en sí
-
26-10-2019 - |
Pregunta
Estoy luchando con etiquetas giradas del eje X. Si tienen más de 5-6 caracteres, se superponen al gráfico como puede ver aquí: http://jsfiddle.net/kmft9/215/Si esto no aparece, puede reproducir el error de pastoreo a continuación en una ventana JSFIDDLE.
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
marginLeft: 120
},
xAxis: {
categories: ['Jan', '02/03/2011', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], labels : { y : 20, rotation: -45 }
},
yAxis: {
lineWidth: 1,
offset: 0,
labels : { x: -20 },
title: {
text: 'Primary Axis'
},
tickWidth: 1
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
A pesar de que establece un valor Y en la propiedad de las etiquetas, esto solo se respeta por etiquetas más pequeñas.
¿Alguien sabe la solución o lo que estoy haciendo mal?
Solución
Puede intentar agregar Alinear: 'correcto' al objeto de etiquetas del eje X.
p.ej
xAxis: { categories: ['Jan', '02/03/2011', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], labels : { y : 20, rotation: -45, align: 'right' } },
Otros consejos
A veces debe hacer que los clientes quieran, y sé que no es la mejor manera, pero puede que ayude a alguien). Entonces, como sé, HighCharts usa dos vías para visualizar los gráficos. Es SVG (por ejemplo, compatible con Chrome, es decir> 8 navegadores) y VML (Apoyado por IE <= 8). Entonces, cada forma contiene puntos en los que este problema se puede resolver que necesita con una ruptura suave.
Para resolverlo en SVG, debe encontrar la función BuildText y modificar en este punto:
// check width and apply soft breaks
if (width) {
...
}
Por ejemplo, para agregar un nuevo símbolo separado:
...
var words = span.replace(/\\/g, '\\ ').replace(/-/g, '- ').split(' '),
...
tspan.appendChild(doc.createTextNode(words.join(' ').replace(/\\ /g, '\\').replace(/- /g, '-')));
...
Si quieres hacer que funcione la posibilidad en VML. Puede escribir su propia función que haga lo mismo que el código en la función buildText:
function softBreaks()
{
//if ie and vml
hasSVG = !!document.createElementNS && !!document.createElementNS("http://www.w3.org/2000/svg", "svg").createSVGRect;
if(!hasSVG)
{
//for each
$.each($('.highcharts-axis > span > div'), function(index, value) {
var width = value.parentNode.style.posWidth;
var div = value;
if (width) {
var words = value.innerText.replace(/\//g, '/ ').replace(/\\/g, '\\ ').replace(/\./g, '. ').replace(/-/g, '- ').split(' '),
tooLong,
actualWidth,
rest = [];
while (words.length || rest.length) {
//actualWidth = value.parentNode.offsetWidth;
actualWidth = value.parentNode.scrollWidth;
tooLong = actualWidth > width;
if (!tooLong || words.length === 1) { // new line needed
words = rest;
rest = [];
if (words.length) {
div = document.createElement("div");
value.parentNode.appendChild(div);
if (actualWidth > width) { // a single word is pressing it out
width = actualWidth;
}
}
} else {
div.removeChild(div.firstChild);
rest.unshift(words.pop());
}
if (words.length) {
div.appendChild(document.createTextNode(words.join(' ').replace(/\/ /g, '/').replace(/\\ /g, '\\').replace(/\. /g, '.').replace(/- /g, '-')));
}
}
}
});
}
}
Y después de esto, debe llamar a esta función cuando se cargue con gráfico www.highcharts.com/ref/#chart-events-- Load (lo siento, soy un nuevo usuario). Si tiene varios gráficos en la página, puede pasar como ID del gráfico de parámetros a la función SoftBreak ().