How to properly render the kendo chart custom labels

Viewed 1415

I neede to customize labels (like a bubbletalk) of a kendo line chart. I build a template (like in this kendo test: http://dojo.telerik.com/@PMcDonou/URiZA. I took this demo from an admin solution in a kendo forum thread) and it work when load the page first time, but in some situations kendo UI not render the labels. The problem appears in this two cases:

  1. When I refresh manually chart with a button that to click on it you need to scroll the page.

  2. When i refresh the page(F5) after that i scrolled down the page.

I noticed that the position of the labels moves upwards based on the offset from top given by scrolling.

I did this fiddle for you to see the first case. For see an example of the second case look the fiddle in a full screen page, try to scroll down and refresh the page(F5) and try this in multiple positions of scroll. Thank you for attention and help me please.

function createChart(){
      var dataSource = new kendo.data.DataSource({
        data: [{
          category: "A", value: 10
        }, {
          category: "B", value: 20
        }, {
          category: "C", value: 30
        }]
      });

      var labelTemplate = kendo.template($("#labelTemplate").html());
      $("#chart").kendoChart({
        dataSource: dataSource, series: [{
          type: "line", style: "smooth", field: "value", categoryField: "category", labels: {
            visible: true, template: "#= category #", visual: function(e) {
              var dataItem = $.grep(dataSource.data(), function(item) {
                return item.category === e.text;
              })[0];
                // Label origin position (same as where the original label would be)
                var origin = e.rect.origin;
                var content = $('<div/>')
                  .css({
                    position: "absolute",
                    font: "11px Arial,Helvetica,sans-serif",
                    left: 0,
                    top: 0
                  })
                  .appendTo(document.body)
                  .html(labelTemplate(dataItem));
                var visual = new kendo.drawing.Group();

                kendo.drawing.drawDOM(content).done(function(group) {
                  // Place drawn shapes on original label position
                  group.transform(kendo.geometry.transform().translate(origin.x, origin.y));
                  visual.append(group);

                  // Remove element from DOM
                  content.remove();
                });

                return visual;
            }
          }
        }]
      });
    }

    $(function(){
        createChart();
        $("#refreshChart").on("click", function(){
            $("#chart").data("kendoChart").refresh();
        })
    });
.talk-bubble {
      width: auto;
      height: auto;
      background-color: red;
      color: white;
      border-radius: 30px;
    }

    .talktext {
      padding: 1em;
      text-align: left;
      line-height: 1.5em;
    }

    .talktext p {
      /* remove webkit p margins */
      -webkit-margin-before: 0em;
      -webkit-margin-after: 0em;
    }
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>Untitled</title>
 <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.2.621/styles/kendo.common.min.css">
 <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.2.621/styles/kendo.rtl.min.css">
 <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.2.621/styles/kendo.default.min.css">
 <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.2.621/styles/kendo.mobile.all.min.css">
 <script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
 <script src="https://kendo.cdn.telerik.com/2017.2.621/js/angular.min.js"></script>
 <script src="https://kendo.cdn.telerik.com/2017.2.621/js/jszip.min.js"></script>
 <script src="https://kendo.cdn.telerik.com/2017.2.621/js/kendo.all.min.js"></script>
    </head>
    <body>
    <!-- Div for chart -->
    <div id="chart"></div>
      
    <!-- Example div container -->
    <h1>Go down and click button</h1>
    <div style="height:3000px; background-color: yellow;"></div>
    <!-- Button that refresh chart -->
    <h3>Click button</h3>
    <button type="button" id="refreshChart">Refresh chart</button>
    <h1>Now go up, label not rendered</h1>
        
    <!-- Label Template Custom -->
    <script id="labelTemplate" type="text/x-kendo-template">
    <div class="talk-bubble">
    <div class="talktext">
    <p>Value: #= kendo.format('{0:n2}', data.value) #</p>
    </div>
    </div>
    </script>
    </body>
    </html>

0 Answers
Related