pablojim / highcharts-ng

AngularJS directive for Highcharts
MIT License
1.73k stars 463 forks source link

error typeerror: cannot read property 'y' of undefined #661

Closed haowanya closed 3 years ago

haowanya commented 3 years ago

label has a value, but label.xy may not have a value, because only the value of label.xy is taken below, so I think it can be modified here

Tick.prototype.moveLabel = function (str, labelOptions) { var tick = this, label = tick.label, moved = false, xAxis = tick.axis, chart = xAxis.chart, labelPos, reversed = xAxis.reversed, inverted = chart.inverted, xPos, yPos; if (label && label.textStr === str) { tick.movedLabel = label; moved = true; delete tick.label; } else { // Find a label with the same string objectEach(xAxis.ticks, function (currentTick) { if (!moved && !currentTick.isNew && currentTick !== tick && currentTick.label && currentTick.label.textStr === str) { tick.movedLabel = currentTick.label; moved = true; currentTick.labelPos = tick.movedLabel.xy; delete currentTick.label; } }); } //I think this kind of writing can be changed to the following inner writing, it can use loadsh writing, or it can be directly replaced with label.xy, which can effectively prevent errors. // if (!moved && (tick.labelPos || label)) { if (!moved && (tick.labelPos || _.get(label, 'xy', false))) { labelPos = tick.labelPos || label.xy; xPos = inverted ? labelPos.x : (reversed ? 0 : xAxis.width + xAxis.left); yPos = inverted ? (reversed ? (xAxis.width + xAxis.left) : 0) : labelPos.y; tick.movedLabel = tick.createLabel({ x: xPos, y: yPos }, str, labelOptions); if (tick.movedLabel) { tick.movedLabel.attr({ opacity: 0 }); } } };

haowanya commented 3 years ago
            Tick.prototype.moveLabel = function (str, labelOptions) {
                var tick = this, label = tick.label, moved = false, xAxis = tick.axis, chart = xAxis.chart, labelPos, reversed = xAxis.reversed, inverted = chart.inverted, xPos, yPos;
                if (label && label.textStr === str) {
                    tick.movedLabel = label;
                    moved = true;
                    delete tick.label;
                }
                else { // Find a label with the same string
                    objectEach(xAxis.ticks, function (currentTick) {
                        if (!moved &&
                            !currentTick.isNew &&
                            currentTick !== tick &&
                            currentTick.label &&
                            currentTick.label.textStr === str) {
                            tick.movedLabel = currentTick.label;
                            moved = true;
                            currentTick.labelPos = tick.movedLabel.xy;
                            delete currentTick.label;
                        }
                    });
                }
                // Create new label if the actual one is moved
                // if (!moved && (tick.labelPos || label)) {
                if (!moved && (tick.labelPos || _.get(label,'xy',false))) {
                        labelPos = tick.labelPos || label.xy;
                    xPos = inverted ?
                        labelPos.x : (reversed ? 0 : xAxis.width + xAxis.left);
                    yPos = inverted ?
                        (reversed ? (xAxis.width + xAxis.left) : 0) : labelPos.y;
                    tick.movedLabel = tick.createLabel({ x: xPos, y: yPos }, str, labelOptions);
                    if (tick.movedLabel) {
                        tick.movedLabel.attr({ opacity: 0 });
                    }
                }
            };
haowanya commented 3 years ago

1