Primefaces: Charts and Legend position

2019-04-16 14:03发布

I'm working on putting together a pretty basic stacked bar chart using Primefaces. The chart itself seems to build just fine. However, the legend is being placed over the chart data. I know you can move the legend to any compass ordinal (n, s, w, e, se, sw, ne, nw), but I'd actually like to move it OFF the data.

The code to get it in:

 <p:barChart id="stackedKWH" value="#{kwhSalesBeanManager.kwhSalesChart}" legendPosition="e" style="height:300px;width:800px" title="kWh Sales by Type" stacked="true" barMargin="10" min="0" max="125000000" />

And what it currently looks like. Would like to move that legend off the chart to the right.

Here's what it looks like now when set to 'e':

3条回答
Bombasti
2楼-- · 2019-04-16 14:22

Looks like you should use javascript for this purpose. something like that:

var legend = $('table.jqplot-table-legend')[0];
legend.setAttribute('style', 'position: absolute; z-index: 3; right: -100px; top: 25px;');

I tested it on this page: http://www.primefaces.org/showcase/ui/barChart.jsf

查看更多
爷的心禁止访问
3楼-- · 2019-04-16 14:30

Try to add this :

function extLegend() 
{
    this.cfg.legend= {
        show: true,
        location: 's',
        placement: 'outsideGrid'
    };
}

and add this inside your barChart component :

 extender="extLegend"
查看更多
叼着烟拽天下
4楼-- · 2019-04-16 14:37

Now the best solution would be defining the placemant on chart model:

...
 modelChart.setLegendPlacement(LegendPlacement.OUTSIDE);
...
查看更多
登录 后发表回答