Add demo functions
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||
<title>Updating Charts · GitBook</title>
|
||||
<title>Updating Charts · Chart.js documentation</title>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="description" content="">
|
||||
<meta name="generator" content="GitBook 3.2.2">
|
||||
@@ -195,7 +195,20 @@
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="1.3.2" data-path="../general/interactions/">
|
||||
<li class="chapter " data-level="1.3.2" data-path="../general/device-pixel-ratio.html">
|
||||
|
||||
<a href="../general/device-pixel-ratio.html">
|
||||
|
||||
|
||||
Pixel Ratio
|
||||
|
||||
</a>
|
||||
|
||||
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="1.3.3" data-path="../general/interactions/">
|
||||
|
||||
<a href="../general/interactions/">
|
||||
|
||||
@@ -209,7 +222,7 @@
|
||||
<ul class="articles">
|
||||
|
||||
|
||||
<li class="chapter " data-level="1.3.2.1" data-path="../general/interactions/events.html">
|
||||
<li class="chapter " data-level="1.3.3.1" data-path="../general/interactions/events.html">
|
||||
|
||||
<a href="../general/interactions/events.html">
|
||||
|
||||
@@ -222,7 +235,7 @@
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="1.3.2.2" data-path="../general/interactions/modes.html">
|
||||
<li class="chapter " data-level="1.3.3.2" data-path="../general/interactions/modes.html">
|
||||
|
||||
<a href="../general/interactions/modes.html">
|
||||
|
||||
@@ -240,7 +253,7 @@
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="1.3.3" data-path="../general/options.html">
|
||||
<li class="chapter " data-level="1.3.4" data-path="../general/options.html">
|
||||
|
||||
<a href="../general/options.html">
|
||||
|
||||
@@ -253,7 +266,7 @@
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="1.3.4" data-path="../general/colors.html">
|
||||
<li class="chapter " data-level="1.3.5" data-path="../general/colors.html">
|
||||
|
||||
<a href="../general/colors.html">
|
||||
|
||||
@@ -266,7 +279,7 @@
|
||||
|
||||
</li>
|
||||
|
||||
<li class="chapter " data-level="1.3.5" data-path="../general/fonts.html">
|
||||
<li class="chapter " data-level="1.3.6" data-path="../general/fonts.html">
|
||||
|
||||
<a href="../general/fonts.html">
|
||||
|
||||
@@ -866,7 +879,7 @@
|
||||
<section class="normal markdown-section">
|
||||
|
||||
<h1 id="updating-charts">Updating Charts</h1>
|
||||
<p>It's pretty common to want to update charts after they've been created. When the chart data is changed, Chart.js will animate to the new data values. </p>
|
||||
<p>It's pretty common to want to update charts after they've been created. When the chart data or options are changed, Chart.js will animate to the new data values and options.</p>
|
||||
<h2 id="adding-or-removing-data">Adding or Removing Data</h2>
|
||||
<p>Adding and removing data is supported by changing the data array. To add data, just add data into the data array as seen in this example.</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addData</span>(<span class="hljs-params">chart, label, data</span>) </span>{
|
||||
@@ -876,8 +889,8 @@
|
||||
});
|
||||
chart.update();
|
||||
}
|
||||
</code></pre>
|
||||
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">removeData</span>(<span class="hljs-params">chart</span>) </span>{
|
||||
|
||||
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">removeData</span>(<span class="hljs-params">chart</span>) </span>{
|
||||
chart.data.labels.pop();
|
||||
chart.data.datasets.forEach((dataset) => {
|
||||
dataset.data.pop();
|
||||
@@ -885,6 +898,67 @@
|
||||
chart.update();
|
||||
}
|
||||
</code></pre>
|
||||
<h2 id="updating-options">Updating Options</h2>
|
||||
<p>To update the options, mutating the options property in place or passing in a new options object are supported.</p>
|
||||
<ul>
|
||||
<li>If the options are mutated in place, other option properties would be preserved, including those calculated by Chart.js.</li>
|
||||
<li>If created as a new object, it would be like creating a new chart with the options - old options would be discarded.</li>
|
||||
</ul>
|
||||
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateConfigByMutating</span>(<span class="hljs-params">chart</span>) </span>{
|
||||
chart.options.title.text = <span class="hljs-string">'new title'</span>;
|
||||
chart.update();
|
||||
}
|
||||
|
||||
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateConfigAsNewObject</span>(<span class="hljs-params">chart</span>) </span>{
|
||||
chart.options = {
|
||||
responsive: <span class="hljs-literal">true</span>,
|
||||
title:{
|
||||
display:<span class="hljs-literal">true</span>,
|
||||
text: <span class="hljs-string">'Chart.js'</span>
|
||||
},
|
||||
scales: {
|
||||
xAxes: [{
|
||||
display: <span class="hljs-literal">true</span>
|
||||
}],
|
||||
yAxes: [{
|
||||
display: <span class="hljs-literal">true</span>
|
||||
}]
|
||||
}
|
||||
}
|
||||
chart.update();
|
||||
}
|
||||
</code></pre>
|
||||
<p>Scales can be updated separately without changing other options.
|
||||
To update the scales, pass in an object containing all the customization including those unchanged ones.</p>
|
||||
<p>Variables referencing any one from <code>chart.scales</code> would be lost after updating scales with a new <code>id</code> or the changed <code>type</code>.</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateScales</span>(<span class="hljs-params">chart</span>) </span>{
|
||||
<span class="hljs-keyword">var</span> xScale = chart.scales[<span class="hljs-string">'x-axis-0'</span>];
|
||||
<span class="hljs-keyword">var</span> yScale = chart.scales[<span class="hljs-string">'y-axis-0'</span>];
|
||||
chart.options.scales = {
|
||||
xAxes: [{
|
||||
id: <span class="hljs-string">'newId'</span>,
|
||||
display: <span class="hljs-literal">true</span>
|
||||
}],
|
||||
yAxes: [{
|
||||
display: <span class="hljs-literal">true</span>,
|
||||
type: <span class="hljs-string">'logarithmic'</span>
|
||||
}]
|
||||
}
|
||||
chart.update();
|
||||
<span class="hljs-comment">// need to update the reference</span>
|
||||
xScale = chart.scales[<span class="hljs-string">'newId'</span>];
|
||||
yScale = chart.scales[<span class="hljs-string">'y-axis-0'</span>];
|
||||
}
|
||||
</code></pre>
|
||||
<p>You can also update a specific scale either by specifying its index or id.</p>
|
||||
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateScale</span>(<span class="hljs-params">chart</span>) </span>{
|
||||
chart.options.scales.yAxes[<span class="hljs-number">0</span>] = {
|
||||
type: <span class="hljs-string">'logarithmic'</span>
|
||||
}
|
||||
chart.update();
|
||||
}
|
||||
</code></pre>
|
||||
<p>Code sample for updating options can be found in <a href="../../samples/scales/toggle-scale-type.html">toggle-scale-type.html</a>.</p>
|
||||
<h2 id="preventing-animations">Preventing Animations</h2>
|
||||
<p>Sometimes when a chart updates, you may not want an animation. To achieve this you can call <code>update</code> with a duration of <code>0</code>. This will render the chart synchronously and without an animation.</p>
|
||||
|
||||
@@ -930,7 +1004,7 @@
|
||||
<script>
|
||||
var gitbook = gitbook || [];
|
||||
gitbook.push(function() {
|
||||
gitbook.page.hasChanged({"page":{"title":"Updating Charts","level":"1.7.2","depth":2,"next":{"title":"Plugins","level":"1.7.3","depth":2,"path":"developers/plugins.md","ref":"developers/plugins.md","articles":[]},"previous":{"title":"Chart.js API","level":"1.7.1","depth":2,"path":"developers/api.md","ref":"developers/api.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-plus","anchorjs","chartjs","ga"],"root":"./docs","styles":{"website":"style.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"anchorjs":{"icon":"#","placement":"left","visible":"always"},"ga":{"configuration":"auto","token":"UA-28909194-3"},"theme-default":{"styles":{"website":"style.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{},"chartjs":{"defaults":null},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}},"theme":"default","author":"chartjs","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"gitbook":"3.2.2"},"file":{"path":"developers/updates.md","mtime":"2017-10-28T15:03:49.266Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-10-28T15:09:53.587Z"},"basePath":"..","book":{"language":""}});
|
||||
gitbook.page.hasChanged({"page":{"title":"Updating Charts","level":"1.7.2","depth":2,"next":{"title":"Plugins","level":"1.7.3","depth":2,"path":"developers/plugins.md","ref":"developers/plugins.md","articles":[]},"previous":{"title":"Chart.js API","level":"1.7.1","depth":2,"path":"developers/api.md","ref":"developers/api.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-plus","anchorjs","chartjs","ga"],"root":"./docs","styles":{"website":"style.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"anchorjs":{"icon":"#","placement":"left","visible":"always"},"ga":{"configuration":"auto","token":"UA-28909194-3"},"theme-default":{"styles":{"website":"style.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{},"chartjs":{"defaults":null},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}},"theme":"default","author":"chartjs","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Chart.js documentation","gitbook":"3.2.2"},"file":{"path":"developers/updates.md","mtime":"2018-03-01T21:46:31.679Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-03-01T21:50:47.970Z"},"basePath":"..","book":{"language":""}});
|
||||
});
|
||||
</script>
|
||||
</div>
|
||||
@@ -948,7 +1022,7 @@
|
||||
|
||||
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/3.1.1/anchor.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/4.1.0/anchor.min.js"></script>
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user