javascript - 如何为plot.ly js图例着色以及为什么它是黑色的?

标签 javascript d3.js plotly

当我的图表渲染时,一切都很好,除了图例仅显示黑色。分组条形图显示了我的预期。所有的数据和颜色都很棒,但图例不正确。

我正在将绘图加载到 salesforce Visualforce 页面,如下所示:

    <apex:panelGroup styleClass="charts" layout="block">
    <div id="myDiv" style="width: 480px; height: 400px;"><!-- Plotly chart will be drawn inside this DIV --></div>
</apex:panelGroup>
<script>
    var trace1 = {
      y: ['B2B', 'Commercial', 'Retail'], 
      x: [20, 14, 23],
      marker: {
          color: ['#f8b98d', '#f8b98d', '#f8b98d']  
      },
      name: 'Annual Quota', 
      type: 'bar',
      orientation: 'h'  

    };

    var trace2 = {
      y: ['B2B', 'Commercial', 'Retail'], 
      x: [12, 18, 29],
      marker: {
          color: ['#f58b3f', '#f58b3f', '#f58b3f']  
      },
      name: 'Current Market Size', 
      type: 'bar',
      orientation: 'h'

    };

    var data = [trace1, trace2];

    var layout = {barmode: 'group'};

    Plotly.newPlot('myDiv', data, layout, {displayModeBar: false});
</script>

最佳答案

我遇到了与此类似的问题 - 堆叠条上的自定义颜色看起来很棒,但图例样本全是黑色。

我找不到官方解决方案,因此使用 Plotly 内置的 d3 生成图表后手动设置图例中的颜色,例如:

var legendBoxes = document.getElementById("div-id-for-plot").getElementsByClassName("legendbar");
Plotly.d3.select(legendBoxes[0]).style("fill", "put your colour definition here");

(此示例仅设置单个框的样式)

关于javascript - 如何为plot.ly js图例着色以及为什么它是黑色的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35658551/

相关文章:

javascript - 在 D3 库中将 html 附加到 .text() 方法

python - 在 Plotly 3.0 中设置图例文本颜色

javascript - plotly js : location and length of color scale in heatmap

python - Plotly:如何消除x轴上的空白

javascript - HTML 表格解决方案通过增加表格宽度来限制行或单元格的最大高度,Javascript

javascript - 由于函数表达式和函数声明基本上做同样的事情,我什么时候应该使用哪个?

javascript - D3 力向图添加新节点导致 x & y 为 NaN

javascript - 如何在 d3js 中制作基本柱形(垂直)图表?

javascript - 正确链接 Promise

javascript - 从自定义按钮禁用 Bootstrap