Extjs 4 : Customize Legend of Pie Chart

Unlike extjs 3, pie chart of Extjs 4 can be customized according to your requirment.

For example, in my application, I  need to customize pie chart's legend to show a combination of both category and its count.(Default it will show only category.)

 

So I hacked extjs library, create my own Legend, I'm a newbie in extjs, but my solution works!!!

 

 

Define my own Legend MyPieChartLegend :

 

Ext.define('Ext.chart.MyPieChartLegend', {
    extend: 'Ext.chart.Legend',

 override the createItems function:

 

Ext.each([].concat(series.yField), function(field, j) {
		var count = [];                                            //1
		var store = chart.store;                             //2
		store.each(function(rec){                          //3
			count.push(rec.get(series.field));     //4
		});                                                            //5
		item = new Ext.chart.NewLegendItem({ //6 
                        legend: this,
                        series: series,
                        surface: chart.surface,
			yFieldCount: count[j],                     //6
                        yFieldIndex: j
                    });

 Note that, the lines ending with comments are newly added or changed code. Here I also use my own NewLegendItem which extends 'Ext.chart.LegendItem' . In original code, yField of the legend stores categories the store provides, e.g., ['apple', 'orange', ...], when creating legend item, yFieldIndex is used to get corresponding category name,  so we can similiarly add an extra yFieldCount field to the config object.

 

My own NewLegendItem :

 

Ext.define('Ext.chart.NewLegendItem', {

 mainly modifies the createLegend function:

 

createLegend: function(config) {
        var me = this,
            index = config.yFieldIndex,
           count = config.yFieldCount,     // line 1
            series = me.series,
            seriesType = series.type,
            idx = me.yFieldIndex,
            legend = me.legend,
            surface = me.surface,
            refX = legend.x + me.x,
            refY = legend.y + me.y,
            bbox, z = me.zIndex,
            markerConfig, label, mask,
            radius, toggle = false,
            seriesStyle = Ext.apply(series.seriesStyle, series.style);

        function getSeriesProp(name) {
            var val = series[name];
			var ret = Ext.isArray(val) ? val[idx] : val;      // line 2
			ret = ret? (ret + " (" + count + ")") : ret;     // line 3
            return ret;
        }
        
        label = me.add('label', surface.add({
            type: 'text',
            x: 20,
            y: 0,
            zIndex: z || 0,
            font: legend.labelFont,
            text: getSeriesProp('title') || getSeriesProp('yField')
        }));
 

 

You can see the change is very simple, I just append (count) to the category name. So now I get things like

 

apple (10)

orange (5)

 

See the picture below for illustration.

 

Also, you can customize other things I think, for instance, the label.


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值