Resizing a Chart

Best Practices

Introduction

KeyLines chart and time bar components adapt to fit the size of their parent element in the DOM. Any changes in the size of the parent will be adopted by the chart or time bar, making it easy to manipulate their size and shape.

The KeyLines container

The KeyLines.create() function creates a container that the chart sits inside. So when you create a chart or time bar, that element will be a child of the specified container.

var chart;
KeyLines.create({ container: 'kl-chart' }, function (err, result) {
  chart = result;
  // Load your chart here!
});

Your html element structure should then look something like this:

<div id='kl-chart' style='width:600px; height:500px;'>
  <canvas height="500" width="600" id="KeyLines-chart-1" style="-ms-touch-action: none; touch-action: none; outline: none;" tabindex="8888"></canvas>
</div>

Note that a KeyLines component will not render to a container if the container's css style property has display: none, width = 0 or height = 0.

Styling and CSS

As the chart resizes automatically you should make sure you target your CSS at the container, and not the KeyLines element itself.

As KeyLines elements adapt to fill the size of their parent, we advise using box-sizing to make sizing the chart simpler:

* {
  box-sizing: border-box;
}

KeyLines elements adapt to fit the size of their parent, so you should be aware of other rules in css modules that may interfere with this. Modules such as css grid - where parent elements are sized based on their children - can cause unexpected behaviour.

Fullscreen mode

When you want to put the chart in fullscreen mode, you can use the hierarchy rules of css to control the size of the chart:

HTML

<div id='kl-main' style="width: 800px; height: 600px;">
  <div id='fullScreen'>
    <div id='kl-chart'>
      <canvas id="KeyLines-chart-1"></canvas>
    </div>
  </div>
  <button id='fullscreenBtn'>Fullscreen</button>
</div>

JavaScript

// When the window is loaded
$(window).on('load', function() {

  var chart;

  // Create KeyLines chart as a child of the DOM element with id 'kl-chart'
  KeyLines.create({ container: 'kl-chart', options:{backColour: 'white' } }, function (err, result) {
    chart = result;
    // Load your chart here!
  });

  // Toggle fullscreen mode when the button is clicked
  $('#fullscreenBtn').click(function(evt) {
    if(KeyLines.fullScreenCapable()) {
      KeyLines.toggleFullScreen(document.getElementById('fullScreen'));
    }
  });
});

You can then set the styling for all child elements to inherit from the kl-main div, or select only the fullscreen div to make the chart fill the screen.

Terms of use

These terms do not alter or supersede any existing agreements between you (or your employer) and us.

By accessing or using any Content you agree to be bound by these Terms of Use. Please review these terms carefully before using the website.

The contents of this website, including but not limited to any text, code samples, API references, schemas, interactive tools, and other materials (collectively, the 'Content'), are made available for informational and internal evaluation purposes only. All intellectual property rights in the Content are reserved. No licence is granted to use the Content for any commercial purpose, or to copy, distribute, modify, reverse-engineer, or incorporate any part of the Content into any product or service, without our prior written consent.

This Content is provided “as is” and “as available,” without any representations, warranties, or guarantees of any kind, whether express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, non-infringement, or accuracy. To the fullest extent permitted by applicable law, we expressly exclude and disclaim all implied warranties, conditions, and other terms that might otherwise be implied.

We disclaim all liability for any loss or damage, whether direct, indirect, incidental, consequential, or otherwise, arising from any reliance placed on the Content or from your use of it, to the fullest extent permitted by applicable law. By continuing to access or use the Content, you acknowledge and agree to these terms.