[FIXED] [#1543] [4.5.0] Problem in chart with legend text is long

  1. #1

    [FIXED] [#1543] [4.5.0] Problem in chart with legend text is long

    Hi,
    see my example, if the Titles of LineSeries is long and the width of chart is short the javascript goes in loop.
    Thank you
    Jimmy

    <%@ Page Language="C#" %>
    
    <script runat="server">
        private List<object> GenerateData()
        {
            List<object> data = new List<object>();
            Random random = new Random();
            double p = (random.NextDouble() * 11) + 1;
            DateTime date = DateTime.Today;
    
            for (int i = 0; i < 15; i++)
            {
                data.Add(new
                {
                    Date = date.AddDays(i),
                    Data1 = Math.Round(random.NextDouble() * 10),
                    Data2 = Math.Round(random.NextDouble() * 100)
                });
            }
    
            return data;
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.Chart1.GetStore().DataSource = this.GenerateData();
            }
        }
    
    </script>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <title></title>
        <link href="/resources/css/examples.css" rel="stylesheet" />    
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
    
            <ext:Panel
                runat="server"
                Title="Line Chart"
                Width="200"
                Height="400"
                Layout="FitLayout">
                <Items>
                    <ext:CartesianChart
                        ID="Chart1"
                        runat="server"
                        Animate="true">
                        <Store>
                            <ext:Store runat="server">
                                <Model>
                                    <ext:Model runat="server">
                                        <Fields>
                                            <ext:ModelField Name="Date" Type="Date" />
                                            <ext:ModelField Name="Data1" />
                                            <ext:ModelField Name="Data2" />
                                        </Fields>
                                    </ext:Model>
                                </Model>
                            </ext:Store>
                        </Store>
                        <Axes>
                            <ext:TimeAxis
                                Title="Date"
                                Fields="Date"
                                Position="Bottom"
                                DateFormat="MMM dd"
                                FromDate="<%# DateTime.Today %>"
                                ToDate="<%# DateTime.Today.AddDays(14) %>"
                                AutoDataBind="true" />
    
                            <ext:NumericAxis
                                Title="Data (blue)"
                                Fields="Data1"
                                Position="Left"
                                Maximum="10">
                                <TitleConfig FillStyle="#115fa6" />
                                <Label FillStyle="#115fa6" />
                            </ext:NumericAxis>
    
                            <ext:NumericAxis
                                Title="Data (green)"
                                Fields="Data2"
                                Position="Right"
                                Maximum="100">
                                <TitleConfig FillStyle="#94ae0a" />
                                <Label FillStyle="#94ae0a" />
                            </ext:NumericAxis>
                        </Axes>
                        <Series>
                            <ext:LineSeries
                                Titles="Blue Line lorem ipsum lorem ipsum"
                                XField="Date"
                                YField="Data1"
                                Smooth="3">
                                <HighlightConfig>
                                    <ext:CircleSprite Radius="7" />
                                </HighlightConfig>
                                <Marker>
                                    <ext:CircleSprite Radius="4" LineWidth="0" />
                                </Marker>
                            </ext:LineSeries>
    
                            <ext:LineSeries
                                Titles="Green Line"
                                XField="Date"
                                YField="Data2"
                                Smooth="3">
                                <HighlightConfig>
                                    <ext:CircleSprite Radius="7" />
                                </HighlightConfig>
                                <Marker>
                                    <ext:CircleSprite Radius="4" LineWidth="0" />
                                </Marker>
                            </ext:LineSeries>
                        </Series>
                        <Plugins>
                            <ext:VerticalMarker runat="server">
                                <XLabelRenderer Handler="return Ext.util.Format.date(value, 'M d');" />
                            </ext:VerticalMarker>
                        </Plugins>
                        <LegendConfig runat="server" Dock="Bottom" />
                    </ext:CartesianChart>
                </Items>
            </ext:Panel>
        </form>
    </body>
    </html>
    Last edited by fabricio.murta; Sep 13, 2017 at 12:39 AM.
  2. #2
    Hello @xeo4.it!

    Thanks for this bug report! I see the infinite loop here, and this may be the reason text in legend does not wrap when placed right/left to the chart as well.

    I understand in your situation you may have arbitrary data and data contents as well as, maybe arbitrary chart width depending on the user browser size. And an infinite loop like that is clearly not acceptable.

    We have logged this issue under #1543 and we'll update here as soon as we fix it.

    Well, for the time being, I believe best that can be done would be triggering an error whenever this issue will happen, i. e.:

    Ext.define('Ext.chart.legend.SpriteLegend', {
        override: 'Ext.chart.legend.SpriteLegend',
        performLayout: function () {
            var me = this,
                sprites = me.getSprites(),
                surface = me.getSurface(),
                surfaceRect = surface.getRect(),
                // gap is 4x for the theme-driven borders
                gap = 4 * me.getPadding(),
                i, sprite;
    
            if (!surface || !surfaceRect) {
                return false;
            }
    
            var docked = me.getDocked(),
                surfaceWidth = surfaceRect[2],
                surfaceHeight = surfaceRect[3],
                bboxes = [];
    
            Ext.each(sprites, function (sprite) {
                bboxes.push(sprite.getBBox());
            })
    
            switch (docked) {
                case 'bottom':
                case 'top':
                    if (!surfaceWidth) {
                        return false;
                    }
    
                    var bboxWidth = 0;
                    Ext.each(bboxes, function (bbox) {
                        console.log(bbox.width + " vs " + bboxWidth + " [" + surfaceWidth + "]");
                        if (bboxWidth < bbox.width) bboxWidth = bbox.width;
                    });
    
                    if ((bboxWidth + gap) > surfaceWidth) {
                        Ext.Error.raise("Legend Sprite width exceeds chart surface width. Minimum width should be: " + bboxWidth + ".");
                        return false;
                    }
    
                    break;
            }
    
            return me.callParent(arguments);
        }
    });
    I have tested the edge cases and this will trigger the error and tell the exact minimum width the chart must have in order for it to work. Of course, this is not a fix in a grade we feel like merging to Ext.NET. Some more thought should be put into, maybe cropping the legend entries that exceed the minimum width so that everything fits in.

    I hope this helps for the time being, and thanks again for the report!
    Fabrício Murta
    Developer & Support Expert
  3. #3
    Hi Fabricio,
    your solution works fine.
    Thank you for your help.

    Jimmy
  4. #4
    Hello again!

    We've just implemented a solution and merged it into Ext.NET. This solution will comprehensively crop the text until it fits the chart. It will only throw an error if the text is completely removed and still it does not fit the chart (which will only happen in situations the chart is just a few pixels wide).

    We'd appreciate if you could grab latest sources from github and check it out, providing us feedback whether this works for you or not.
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. Replies: 6
    Last Post: Sep 18, 2015, 7:40 AM
  2. [CLOSED] Update Legend Text in Chart
    By PriceRightHTML5team in forum 2.x Legacy Premium Help
    Replies: 0
    Last Post: Aug 27, 2014, 12:38 PM
  3. Hide Legend text on Pie chart
    By kavitha in forum 2.x Help
    Replies: 0
    Last Post: Apr 01, 2014, 11:21 AM
  4. [CLOSED] chart / text at the bottom of a column too long
    By tMp in forum 2.x Legacy Premium Help
    Replies: 3
    Last Post: Jan 17, 2014, 10:30 AM
  5. Replies: 7
    Last Post: Feb 17, 2011, 11:45 AM

Posting Permissions