javascript - extjs饼图所有图例显示相同颜色

标签 javascript extjs charts legend

我正在使用 extjs 4.2 饼图并且在我的商店中有多个记录。 我想显示与每个切片颜色相同的图例颜色。目前每个图例颜色在我的生产版本中都是相同的,但在我的开发版本中这是正确的。这是我的代码。

开发快照

development snapshot

生产快照

production snapshot

{
        xtype: 'piechartattendancereport',
        itemId: 'studentattandencesummeryvise',
        title: 'Attendance Summary',
        width : 450,
        minHeight : 240,
        store: 'mystore.store.attendance.PendingAttendanceGridStore',
        countField: 'totalDays',
        valueField: 'programName'
    }




Ext.define('myapp.view.PieChartAttendanceReport', {
    extend: 'Ext.chart.Chart',
    alias: 'widget.piechartattendancereport',
    animate: true,
    shadow: true,
    legend: {
        position: 'right'
    },
    insetPadding: 30,
    theme: 'Base:gradients',
    initComponent: function() {
        var this$ = this;
        var countField = !isNullOrEmpty(this.countField)?this.countField:'number';
        var valueField = !isNullOrEmpty(this.valueField)?this.valueField:'category';
        var showLegend = (!isNullOrEmpty(this.legendField)&& this.legendField)?true:false;
        var chartStore = null;
        if(!isNullOrEmpty(this.store)){
            chartStore = Ext.create(this.store);
        }else{
            chartStore = Ext.create('Ext.data.JsonStore',{
                fields: ['number', 'category'],
                data: [{
                    number :0,
                    category : 'Category'
                }]
            });
        }
        Ext.apply(this$, {
            store: chartStore,
            series: [{
                type: 'pie',
                field: countField,
                showInLegend: true,
                donut: false,
                tips: {
                    trackMouse: true,
                    //width: 300,
                    height: 28,
                    layout: 'fit',
                    renderer: function(storeItem, item) {
                        var total = 0;
                        chartStore.each(function(rec) {
                            total += rec.get(countField);
                        });
                        var tipTitle = storeItem.get(valueField) + ': ' + storeItem.get(countField);
                        var length = (tipTitle.length)* 10;
                        this.setWidth(length);
                        this.setTitle(tipTitle);
                    }
                },
                highlight: {
                    segment: {
                        margin: 20
                    }
                },
                label: {
                    field: valueField,
                    display: 'rotate',
                    contrast: true,
                    font: '15px Arial',
                    renderer: function(value, label, storeItem, item, i, display, animate, index) {
                        var text;
                        if(storeItem.get(countField)!= undefined ||  storeItem.get(countField)!= null){
                            if(storeItem.get(countField) == 0){
                                text = '';
                            }else{
                                text = storeItem.get("Present")+ '%' ;
                                if(text.length > 12){
                                    text = text.substring(0, 10) + '..';
                                }
                            }

                        }else{
                            text = value;
                        }
                        label.setAttributes({
                            text: text
                        }, true);
                        return text;
                    }
                }
            }]
        });

        this$.callParent(arguments);
    }
});

最佳答案

尝试将label.setAttributes改为

label.setAttribute('text': text);

因为第三个参数avoidCopy有提示,object的内容可能会被破坏。

也可能是JS错误导致的,尝试查看控制台日志。我建议使用 typeof 而不是与 undefined 进行比较:

if (typeof storeItem.get(countField) !== 'undefined' ||  storeItem.get(countField) != null) {

关于javascript - extjs饼图所有图例显示相同颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44263291/

相关文章:

javascript - 将右键单击事件发送到 Internet Explorer 8 文档中 iframe 中的元素

charts - 如何在动态的谷歌图表表中居中数据

javascript - laravel 中的 Vue.js 错误

ios - PhoneGap 相机 API 不适用于 iOS

javascript - 如何解决 ' Uncaught TypeError: Converting circular structure to JSON '

excel - 两个数据集的堆积柱形图 - Excel

excel - 如何在excel VBA中为具有命名范围的图表设置源数据

javascript - 如何获取 Android WebView 中从顶部到 div block 的像素数量?

javascript - 如何在Google图像搜索API v2中获取图像URL

javascript - angularjs如何从 Controller 内的表单访问多个输入字段