Home Forums Feature Requests & Feedback logarithmic function Reply To: logarithmic function

#35738

@elanchezhiyan,

Previously shared code seems to work fine with positive values. The logic can be improved to make it work with negative values as shown in the below code-snippet

function getSubscriptString(value) {
    var subscriptValue = Math.ceil(Math.log(value)/Math.log(10));

    //Handling double digit numbers
    if(subscriptValue > 9 || subscriptValue < -9) {
      return (subscriptValue < -9 ? "\u207B" : "") + (Math.abs(subscriptValue)+"").split('').reduce(
        function(prevVal,val) {
          return prevVal + unicodeStringForSubscript[val];
        }, "")
    }
    
    return (subscriptValue < 0 ? "\u207B": "") + unicodeStringForSubscript[Math.abs(subscriptValue)];
  }
..
axisX:{
      title: "Logarithmic Axis",
      logarithmic: true,
      logarithmBase: 10,
      labelFormatter: function(e) {
        return 10 + getSubscriptString(e.value);
      }
    }
..

Also, check out this JSFiddle for complete working sample.

Display negative power values in x-Axis labels

—-
Manoj Mohan
Team CanvasJS