Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a different text in Legend

This is about version 4.2.2 We have a chart like this

var store = Ext.create('Ext.data.JsonStore', {
    fields: ['name', 'data'],
    data: [
        { 'name': 'metric one',   'data':10 },
        { 'name': 'metric two',  'data':27 }
    ]
});

Ext.create('Ext.chart.Chart', {
    renderTo: Ext.getBody(),
    width: 500,
    height: 300,
    animate: true,
    store: store,
    legend: {
        position: 'right'
    },
    axes: [
        {
            type: 'Numeric',
            position: 'left',
            fields: ['data'],
            label: {
                renderer: Ext.util.Format.numberRenderer('0,0')
            },
            title: 'Sample Values',
            grid: true,
            minimum: 0
        },
        {
            type: 'Category',
            position: 'bottom',
            fields: ['name'],
            title: 'Sample Metrics'
        }
    ],
    series: [
        {
            type: 'column',
            axis: 'left',
            highlight: true,
            tips: {
              trackMouse: true,
              width: 140,
              height: 28,
              renderer: function(storeItem, item) {
                this.setTitle(storeItem.get('name') + ': ' + storeItem.get('data') + ' $');
              }
            },
            label: {
              display: 'insideEnd',
              'text-anchor': 'middle',
                field: 'data',
                renderer: Ext.util.Format.numberRenderer('0'),
                orientation: 'vertical',
                color: '#333'
            },
            xField: 'name',
            yField: 'data'
        }
    ]
});

But Legend only displays the field name which is not quite helpful. We need to apply a custom value here which should be static typed. But we've found nothing to do so. There is a showInLegend property but we found no legendText or displayText prop which may allow us to change the name from, in this case data to A much better Name.

Edit Example with more date to display within the legend

Ext.application({
    name : 'Fiddle',

    launch : function() {
        var store = Ext.create('Ext.data.JsonStore', {
    fields: ['name', 'data', 'data2'],
    data: [
        { 'name': 'metric one',   'data':10, 'data2':2 },
        { 'name': 'metric two',  'data':27, 'data2': 5 }
    ]
});

Ext.create('Ext.chart.Chart', {
    renderTo: Ext.getBody(),
    width: 500,
    height: 300,
    animate: true,
    store: store,
    legend: {
        position: 'right'
    },
    axes: [
        {
            type: 'Numeric',
            position: 'left',
            fields: ['data','data2'],
            label: {
                renderer: Ext.util.Format.numberRenderer('0,0')
            },
            title: 'Sample Values',
            grid: true,
            minimum: 0
        },
        {
            type: 'Category',
            position: 'bottom',
            fields: ['name'],
            title: 'Sample Metrics'
        }
    ],
    series: [
        {
            type: 'column',
            axis: 'left',
            highlight: true,
            stacked: true,
            tips: {
              trackMouse: true,
              width: 140,
              height: 28,
              renderer: function(storeItem, item) {
                this.setTitle(storeItem.get('name') + ': ' + storeItem.get('data') + ' $');
              }
            },
            label: {
              display: 'insideEnd',
              'text-anchor': 'middle',
                field: 'data',
                renderer: Ext.util.Format.numberRenderer('0'),
                orientation: 'vertical',
                color: '#333'
            },
            xField: 'name',
            yField: ['data','data2'],
            title:'YourNewLabel'
        }
    ]
});
    }
});
like image 954
JJR Avatar asked Feb 17 '14 12:02

JJR


People also ask

Can you change the text in a legend on Excel?

Select your chart in Excel, and click Design > Select Data. Click on the legend name you want to change in the Select Data Source dialog box, and click Edit. Note: You can update Legend Entries and Axis Label names from this view, and multiple Edit options might be available.

How do you change the text in a legend entry?

On the Design tab, in the Data group, click Select Data. In the Select Data Source dialog box, in the Legend Entries (Series) box, select the legend entry that you want to change. Click Edit. Tip: To add a new legend entry, click Add, or to remove a legend entry, click Remove.

How do you change the text of a legend in Arcgis?

Double-click the legend to bring up the Legend Properties dialog box. Click the Legend tab on the Legend Properties dialog box. Right-click the item from Legend Items in which you want to edit and click Properties. Make your changes using the Arrangement and General tabs.

How do I format text in a legend in Excel?

Click the chart, and then click the Chart Design tab. Click Add Chart Element > Legend. To change the position of the legend, choose Right, Top, Left, or Bottom. To change the format of the legend, click More Legend Options, and then make the format changes that you want.


1 Answers

You can use title to change the label in the legend. Check out this fiddle i created from your example code for use.

series: [
        {
            type: 'column',
            axis: 'left',
            highlight: true,
            tips: {
              trackMouse: true,
              width: 140,
              height: 28,
              renderer: function(storeItem, item) {
                this.setTitle(storeItem.get('name') + ': ' + storeItem.get('data') + ' $');
              }
            },
            label: {
              display: 'insideEnd',
              'text-anchor': 'middle',
                field: 'data',
                renderer: Ext.util.Format.numberRenderer('0'),
                orientation: 'vertical',
                color: '#333'
            },
            xField: 'name',
            yField: 'data',
            title:'YourNewLabel'
        }

Update: If you have more than one field than supply an array of the corresponding titles. For instance:

title:['My First Field','My Second Field']

Here is a fiddle demonstrating multiple titles

like image 137
weeksdev Avatar answered Oct 06 '22 19:10

weeksdev