Options configuration

Defaults are configured per chart with an options configuration object included in the chart object (the second parameter of the constructor), as demonstrated in the following block of code:

new Chart("ocean-volume-bar-chart",
{
type: "bar",
data: dataObj,
options: {} // configure options here
});

There are many defaults that you can change. You might, for example, wish to have more control over the size of your chart, which resizes automatically. That happens because charts are responsive by default. You can turn responsiveness off by overriding the responsive property, which has a default true value, as follows:

options: {
responsive: false
}

Now, your chart no longer resizes automatically. However, what if you do want to undertake resizing, but don't care about the aspect ratio? Then, you can override the maintainAspectRatio property, as follows:

options: {
maintainAspectRatio: false
}

You might want this if your canvas object is located inside a parent <div> container, which controls its size and is configured with CSS. In the following code (Pages/BarChart4.html), the canvas will occupy 80% of the size of its parent container, as follows:

<style>
#canvas-container {
position: relative;
height: 80%;
width: 80%;
}
</style>
<div id="canvas-container">
<canvas id="ocean-volume-bar-chart" width="200" height="200"></canvas>
</div>

If you try to resize the chart, it will, by default, maintain its aspect ratio (and no longer fit in the page), unless the maintainAspectRatio property is set to false, as follows:

Configuring the aspect ratio of a chart to make it fit in a canvas (code: Pages/BarChart4.html)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset