javascript - Plotly JavaScript - donut chart - Hoverinfo,尝试使用数组添加自定义定义 - 我的代码的小问题

标签 javascript html plotly plotly.js donut-chart

我有一个 Plotly.JS 圆环图,我希望用户能够将鼠标悬停在数据上并查看除了标签和数据之外的每种医疗保健类型的定义。例如,当他们将鼠标悬停在 Specialty Care 上时,我希望它看起来像这样:
专科护理
42.9%
专科护理定义

我想我可以添加一组定义(参见“desc:”)并添加它hoverinfo,但要么你不能,要么我做错了。我的问题是我如何让它工作,这样我才能让它看起来像上面的粗体?
这是我的代码笔:https://codepen.io/tenebris_silentio/pen/OJNJxGX

   <!DOCTYPE html>
   <html lang="en">
   <div id='myDiv'>
<script>

  var ultimateColors = [
    ['rgb(0, 25, 51)', 'rgb(0, 76, 153)', 'rgb(51, 153, 255)', 'rgb(153, 204, 255)', 'rgb(51, 51, 255)', 'rgb(204, 255, 255)']
  ];

  var data = [{
    values: [4, 21, 8, 1, 1, 14],
    labels: ['Primary Care', 'Specialty Care', 'Mental Health', 'Inpatient/Acute Care', 'Long-term Care', 'General Care/Not Specified'],
    desc: ['Primary Care Definition', 'Specialty Care Definition', 'Mental Health Definition', 'Inpatient/Acute Care Definition', 'Long-term Care Definition', 'General Care/Not Specified Definition'],
    domain: {column: 0},
    name: 'Sources',
    marker: {
      colors: ultimateColors[0]
    },
    hoverinfo: 'label+percent+desc',

    hole: .4,
    type: 'pie'
  }];

  var layout = {
    title: 'Access Projects by Care Type' + '<br>' + 'N = 49',
    annotations: [
      {
        font: {
          size: 17
        },
        showarrow: false,
        text: '',
        x: 0.5,
        y: 0.5
      },


    ],
    height: 650,
    width: 800,
    showlegend: true,
    grid: {rows: 1, columns: 1}
  };

  Plotly.newPlot('myDiv', data, layout);
  </script>

最佳答案

天啊,当答案如此接近以至于可能会打到你的脸时,你不讨厌吗?我需要做的就是添加一个文本数组并引用它。我只需要修复 var 数据中的部分。特别是,我添加了“text:”部分和“textinfo”(从出现在 donut 中取出新添加的定义)。然后我只是添加了对hoverinfo的文本引用。就是这样!

var data = [{
    values: [4, 21, 8, 1, 1, 14],
    labels: ['Primary Care', 'Specialty Care', 'Mental Health', 'Inpatient/Acute Care', 
    'Long-term Care', 'General Care/Not Specified'],
    desc: ['Primary Care Definition', 'Specialty Care Definition', 'Mental Health 
    Definition', 'Inpatient/Acute Care Definition', 'Long-term Care Definition', 'General 
    Care/Not Specified Definition'],
    text: ['The project is focused on primary care.', 'The project is focused on specialty 
    care.', 'The project is focused on mental health', 'The project is focused on 
    inpatient/acute care', 'The project is focused on long-term care', 'The type of care 
    is not indicated or is not specific to one type'],
    textinfo: 'none',
    domain: {column: 0},
    name: 'Sources',
    marker: {
      colors: ultimateColors[0]
    },
    hoverinfo: 'label+percent+desc+text',

    hole: .4,
    type: 'pie'
    }];

关于javascript - Plotly JavaScript - donut chart - Hoverinfo,尝试使用数组添加自定义定义 - 我的代码的小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63265445/

相关文章:

r - Plotly 和 Shiny Dashboard - 使用 daterangeinput 更改绘图中日期轴上的范围

r - 如何防止 Plotly 中的散点淡出?

javascript - JS λ 函数和 "upper context"变量

javascript - 跨浏览器 session 持久化 javascript 对象可能吗?

javascript - 尝试使用 Javascript 更改元素的 id

python - Plotly:如何自定义轴变换?

javascript - 如何在发布到服务器之前正确地将文件附加到 formData?

javascript - 在没有回发的情况下处理 ajaxToolkit ModalPopupExtender 中的 RadioButtonList SelectedIndexChanged 事件

javascript - 英特尔 xdk 数据库

javascript - 无法动态添加要选择的选项