简体   繁体   English

plotly.js如何在悬停时更改z数据以显示%以及色标%

[英]plotly.js how to change z data on hover to display % as well as % on colorscale

I would like to add a % tag to the z value that is show on hover. 我想将一个%标签添加到悬停显示的z值上。 Also, right now, my colorscale is only displaying -10 to 10, but I would like it to show % so -10% to 10%. 另外,现在,我的色阶仅显示-10到10,但是我希望它显示%,所以-10%到10%。 Here is the code I have. 这是我的代码。 I have tried to add the ticksuffix : "%", but I'm not sure where I should put it since it seems like the colorscale is not an axis like the x and y axis. 我已经尝试添加ticksuffix:“%”,但是我不确定应该在哪里放置它,因为看来色标不是像x和y轴那样的轴。

var xValues = ['A', 'B', 'C',];

var yValues = ['W', 'X', 'Y', 'Z'];

var zValues = [[-2.45,-0.4,1.3],
          [2.9,3.9,-5.66],
          [0.5,-2.6,-3.2],
          [-8.3,-0.5,-0.1]];


var a_text = [["Comodities + producers","Consumer Discretionary","Utilities Equities"],
        ["Health and Biotech","Global Real Estate","Financial Equities"],
        ["Emerging Market Bonds","Technology Equities","Industrials Equities"],
        ["Oil and Gas","China Equities","Diversified Portfolio"]];

var data = [{

  x: xValues,
  y: yValues,
  z: zValues,
  hoverinfo: "z",
  reversescale: true,
  type: 'heatmap',
  // zsmooth: "best",
  colorscale: "Spectral",
  zauto: false,
  zmin:-10,
  zmax:10,
  showscale: true,
}];

var layout = {
  title: 'Annotated Heatmap',
  annotations: [],
  xaxis:  {
    ticks: '',
    side: 'top',
    tickfont: {color: 'rgb(255,255,255)'}
  },
  yaxis: {
    ticks: '',
    ticksuffix: ' ',
    width: 700,
    height: 700,
    autosize: true,
    tickfont: {color: 'rgb(255,255,255)'},
   }
};

for ( var i = 0; i < yValues.length; i++ ) {
  for ( var j = 0; j < xValues.length; j++ ) {
    var currentValue = (zValues[i][j]);
    if (currentValue < -0.05) {
      var textColor = 'white';
    }else{
      var textColor = 'black';
    }
    var result = {
  xref: 'x1',
  yref: 'y1',
  x: xValues[j],
  y: yValues[i],
  text: a_text[i][j],
  font: {
    family: 'Arial',
    size: 12,
    color: 'rgb(50, 171, 96)'
  },
  showarrow: false,
  font: {
    color: textColor
  }
};
layout.annotations.push(result);
  }
}

Plotly.newPlot('myDiv', data, layout);

You need to change two things, the color bar and the hover text. 您需要更改两项,颜色栏和悬停文本。

Color bar 彩条

Add this info to your variable data . 将此信息添加到您的变量data

colorbar: {
    title: 'Relative change',
    ticksuffix: '%',
}

Hover text 悬停文字

First convert the z-values into strings and append % . 首先将z值转换为字符串,然后附加%

var zText = [];
var prefix = "+";
var i = 0;
var j = 0;
for (i = 0; i < zValues.length; i += 1) {
    zText.push([]);
    for (j = 0; j < zValues[i].length; j += 1) {
        if (zValues[i][j] > 0) {
            prefix = "+";
        } else {
            prefix = "";
        }
        zText[i].push(prefix + zValues[i][j] + "%");
    }
}

Then assign the new text to the plot. 然后将新文本分配给绘图。 Add

text: zText,
hoverinfo: "text",

to your variable data . 对您的可变data

Here is a fiddle with the complete data and code: https://jsfiddle.net/Ashafix/e6936boq/2/ 这是一个带有完整数据和代码的小提琴: https : //jsfiddle.net/Ashafix/e6936boq/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM