Giter Site home page Giter Site logo

Comments (2)

etpinard avatar etpinard commented on May 4, 2024

Bug confirmed. Thanks

from plotly.js.

filipinascimento avatar filipinascimento commented on May 4, 2024

Hello, I made a quick-and-dirty fix for this. This may be useful for someone while we wait for the real fix.

I changed plotly.js/src/traces/surface/convert.js by adding the following code to the update function of SurfaceTrace:

// Add the following code just before:
//    //Save data
//    this.data = data;

    //Workaround for issue: https://github.com/plotly/plotly.js/issues/86
    //Determining the original bounds.
    var rzmin = 0.0;
    var rzmax = 1.0;

    if(z.length>0 && z[0].length>0){
      rzmin = z[0][0];
      rzmax = z[0][0];
    }

    for (var i = 0; i < z.length; i++) {
      for (var j = 0; j < z[i].length; j++) {
        rzmin = Math.min(rzmin,z[i][j]);
        rzmax = Math.max(rzmax,z[i][j]);
      }
    }

    //Rescale colormap indices to account for zmax and zmin.
    colormap.map(function(d){
      d.index = ((d.index)*(data.zmax-data.zmin)+data.zmin-rzmin)/(rzmax-rzmin);
      return d;
    });

    //Simple color linear interpolation function.
    function interpolateColors(torgba,fromrgba,fromIndex,toIndex,desired){
      var m = (desired-fromIndex)/(toIndex-fromIndex);
      var r = m*(torgba[0]-fromrgba[0])+fromrgba[0];
      var g = m*(torgba[1]-fromrgba[1])+fromrgba[1];
      var b = m*(torgba[2]-fromrgba[2])+fromrgba[2];
      var a = m*(torgba[3]-fromrgba[3])+fromrgba[3];
      return [r,g,b,a];
    }

    var lastIndexLEZero = false; 
    var newColormap = [];
    var hasZero = false;
    var hasOne = false;
    for (var i = 0; i < colormap.length; i++) {
      if(colormap[i].index<=0){
        lastIndexLEZero = true;
      }else{
        //Interpolates for index=0.0.
        if(lastIndexLEZero){
          var torgba = colormap[i].rgb;
          var fromrgba = colormap[i-1].rgb;
          var toIndex = colormap[i].index;
          var fromIndex = colormap[i-1].index;
          var intrgba = interpolateColors(torgba,fromrgba,fromIndex,toIndex,0.0);
          newColormap.push({index:0.0, rgb : intrgba});
          lastIndexLEZero=false;
          hasZero=true;
        }
        //Interpolates for index=1.0.
        if(colormap[i].index>=1.0){
          var torgba = colormap[i].rgb;
          var fromrgba = colormap[i-1].rgb;
          var toIndex = colormap[i].index;
          var fromIndex = colormap[i-1].index;
          var intrgba = interpolateColors(torgba,fromrgba,fromIndex,toIndex,1.0);
          newColormap.push({index:1.0, rgb : intrgba});
          hasOne=true;
          break;
        }else{
          newColormap.push(colormap[i]);
        }
      }
    }
    //If endpoints are missing, add them.
    if(!hasZero){
      newColormap.unshift({index:0.0, rgb : newColormap[0].rgb});
    }
    if(!hasOne){
      newColormap.push({index:1.0, rgb : newColormap[newColormap.length-1].rgb});
    }

    colormap=newColormap;

// Continues with:
//    //Save data
//    this.data = data;
//    ...

What I did was rescale the colormap to [zmin,zmax] range, while also clamping the indices between 0 and 1.0. I tried, without success, to change the exported colormap function, but it became troublesome to carry zmin, zmax and the original bounds along all the way down to the texture generation. This may introduce more bugs, but seems to work for various surfaces and colormaps.

from plotly.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.