问题
I'm trying to add a legend to my pie chart but I keep getting the error TypeError: dc.legend is not a function
.
I'm not sure where to begin on fixing this. I've tried the solution here and here, but no luck.
Any help would be greatly appreciated!
var geoValue = facts.dimension(function (d) {
return d.geo;
});
var geoGroup = geoValue.group();
geoChart
.width(480)
.radius(100)
.innerRadius(90)
.dimension(geoValue)
.group(geoGroup)
.transitionDuration(500)
.legend(dc.legend().x(250).y(100)) //LEGEND CODE
.title(function(d){return d.geo;});
回答1:
The syntax looks correct.
I have had several cases where my legend did not show up either. It was usually because I specified .x() or .y() outside the container size of the graph. Start by putting the legend at .x(0).y(0) to make sure it fits inside the container.
A working fiddle of the basic .legend() for the pieChart can be found here http://jsfiddle.net/za8ksj45/39/
Working example of original .legend(): line 1018
Working example of new .legend2(): line 1030
It is a bit lengthy because the example includes a modification to legend.js in the beginning of the section that lets you add (%) to it. If you just need the standard legend, look at line 1018.
来源:https://stackoverflow.com/questions/30041565/dc-js-adding-a-legend-to-pie-chart