Pie chart legend in dc.js 1.7 not showing up

2019-07-27 09:50发布

I'm trying to create a pie chart legend in dc.js. However, there is no legend. It just... doesn't show up. Everything appears the same as before I used the legend command.

Here is the JSBin

Here is the legend code:

.legend(dc.legend().x(80).y(70).itemHeight(13).gap(5));

Here is the rest of the relevant code:

  var companyDimension = data.dimension(function(d) {return d.company;});
  var totalSalesByCompany = companyDimension.group().reduceSum(function(d) {return d.total;});
  var pieChartCompanySales = dc.pieChart("#pie-chart-sales-by-company");
  pieChartCompanySales
    .width(150).height(150)
    .dimension(companyDimension)
    .group(totalSalesByCompany)
    .legend(dc.legend().x(80).y(70).itemHeight(13).gap(5));

  dc.renderAll();

I'm getting my legend code from this tutorial, and it seems to check out with the official docs. What am I doing wrong?

2条回答
我只想做你的唯一
2楼-- · 2019-07-27 10:02

I think you are not doing anything wrong. Its the problem with the version of dc.js you are using. Please check http://jsbin.com/xasenusu/1/. I have changed the version of dc.js as well as gave the div a little more space to svg.

 <!DOCTYPE html>
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.7/crossfilter.min.js"></script>
  <script type="text/javascript" src="http://dc-js.github.io/dc.js/js/dc.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
    <style>
    #pie-chart-sales-by-company svg{width:350px;}</style>
</head>
<body>

<div id="pie-chart-sales-by-company"></div>
</body>
</html>
查看更多
Explosion°爆炸
3楼-- · 2019-07-27 10:09

For me, this solutions doesn't works. So I developed the legend manually. For each value, I put some CSS to indicate the color and the percentage there. This is not the best solution but works. I hope the version 2.0 stable come on very soon!

CSS

#precos-legenda {
    float:left;
    width: 150px;
    height: 150px;
    text-align:left;
}

.quadradinho{
    width: 13px;
    height: 13px;
    background: #ff7373;
    float:left;
}

.elemento-legenda{
    display: table-row;

}

JS

f.forEach(function(item) {
        $("#precos-legenda").append("<div id=" + item.key + " class='elemento-legenda'><div class='quadradinho' style='background: " + cores_legenda[item.key] + "'></div> <div class='texto-legenda'>" + item.key + "\t\t\t (" + (item.value / total * 100).toFixed(1) + "%) " + "</div></div>");

    });

HTML

    <div class="chart-stage">
      <div id="precos-chart"></div>
      <div id="precos-legenda">

    </div>
查看更多
登录 后发表回答