<!DOCTYPE HTML> <html lang="" > <head> <meta charset="UTF-8"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Legend ยท 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"> <meta name="author" content="chartjs"> <link rel="stylesheet" href="../gitbook/style.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css"> <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css"> <link rel="stylesheet" href="../style.css"> <meta name="HandheldFriendly" content="true"/> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png"> <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon"> <link rel="next" href="title.html" /> <link rel="prev" href="layout.html" /> <link rel="stylesheet" href="../gitbook/gitbook-plugin-chartjs/style.css"> <script src="../gitbook/gitbook-plugin-chartjs/Chart.bundle.js"></script> <script src="../gitbook/gitbook-plugin-chartjs/chartjs-plugin-deferred.js"></script> </head> <body> <div class="book"> <div class="book-summary"> <div id="book-search-input" role="search"> <input type="text" placeholder="Type to search" /> </div> <nav role="navigation"> <ul class="summary"> <li class="chapter " data-level="1.1" data-path="../"> <a href="../"> Chart.js </a> </li> <li class="chapter " data-level="1.2" data-path="../getting-started/"> <a href="../getting-started/"> Getting Started </a> <ul class="articles"> <li class="chapter " data-level="1.2.1" data-path="../getting-started/installation.html"> <a href="../getting-started/installation.html"> Installation </a> </li> <li class="chapter " data-level="1.2.2" data-path="../getting-started/integration.html"> <a href="../getting-started/integration.html"> Integration </a> </li> <li class="chapter " data-level="1.2.3" data-path="../getting-started/usage.html"> <a href="../getting-started/usage.html"> Usage </a> </li> </ul> </li> <li class="chapter " data-level="1.3" data-path="../general/"> <a href="../general/"> General </a> <ul class="articles"> <li class="chapter " data-level="1.3.1" data-path="../general/responsive.html"> <a href="../general/responsive.html"> Responsive </a> </li> <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/"> Interactions </a> <ul class="articles"> <li class="chapter " data-level="1.3.3.1" data-path="../general/interactions/events.html"> <a href="../general/interactions/events.html"> Events </a> </li> <li class="chapter " data-level="1.3.3.2" data-path="../general/interactions/modes.html"> <a href="../general/interactions/modes.html"> Modes </a> </li> </ul> </li> <li class="chapter " data-level="1.3.4" data-path="../general/options.html"> <a href="../general/options.html"> Options </a> </li> <li class="chapter " data-level="1.3.5" data-path="../general/colors.html"> <a href="../general/colors.html"> Colors </a> </li> <li class="chapter " data-level="1.3.6" data-path="../general/fonts.html"> <a href="../general/fonts.html"> Fonts </a> </li> </ul> </li> <li class="chapter " data-level="1.4" data-path="./"> <a href="./"> Configuration </a> <ul class="articles"> <li class="chapter " data-level="1.4.1" data-path="animations.html"> <a href="animations.html"> Animations </a> </li> <li class="chapter " data-level="1.4.2" data-path="layout.html"> <a href="layout.html"> Layout </a> </li> <li class="chapter active" data-level="1.4.3" data-path="legend.html"> <a href="legend.html"> Legend </a> </li> <li class="chapter " data-level="1.4.4" data-path="title.html"> <a href="title.html"> Title </a> </li> <li class="chapter " data-level="1.4.5" data-path="tooltip.html"> <a href="tooltip.html"> Tooltip </a> </li> <li class="chapter " data-level="1.4.6" data-path="elements.html"> <a href="elements.html"> Elements </a> </li> </ul> </li> <li class="chapter " data-level="1.5" data-path="../charts/"> <a href="../charts/"> Charts </a> <ul class="articles"> <li class="chapter " data-level="1.5.1" data-path="../charts/line.html"> <a href="../charts/line.html"> Line </a> </li> <li class="chapter " data-level="1.5.2" data-path="../charts/bar.html"> <a href="../charts/bar.html"> Bar </a> </li> <li class="chapter " data-level="1.5.3" data-path="../charts/radar.html"> <a href="../charts/radar.html"> Radar </a> </li> <li class="chapter " data-level="1.5.4" data-path="../charts/doughnut.html"> <a href="../charts/doughnut.html"> Doughnut & Pie </a> </li> <li class="chapter " data-level="1.5.5" data-path="../charts/polar.html"> <a href="../charts/polar.html"> Polar Area </a> </li> <li class="chapter " data-level="1.5.6" data-path="../charts/bubble.html"> <a href="../charts/bubble.html"> Bubble </a> </li> <li class="chapter " data-level="1.5.7" data-path="../charts/scatter.html"> <a href="../charts/scatter.html"> Scatter </a> </li> <li class="chapter " data-level="1.5.8" data-path="../charts/area.html"> <a href="../charts/area.html"> Area </a> </li> <li class="chapter " data-level="1.5.9" data-path="../charts/mixed.html"> <a href="../charts/mixed.html"> Mixed </a> </li> </ul> </li> <li class="chapter " data-level="1.6" data-path="../axes/"> <a href="../axes/"> Axes </a> <ul class="articles"> <li class="chapter " data-level="1.6.1" data-path="../axes/cartesian/"> <a href="../axes/cartesian/"> Cartesian </a> <ul class="articles"> <li class="chapter " data-level="1.6.1.1" data-path="../axes/cartesian/category.html"> <a href="../axes/cartesian/category.html"> Category </a> </li> <li class="chapter " data-level="1.6.1.2" data-path="../axes/cartesian/linear.html"> <a href="../axes/cartesian/linear.html"> Linear </a> </li> <li class="chapter " data-level="1.6.1.3" data-path="../axes/cartesian/logarithmic.html"> <a href="../axes/cartesian/logarithmic.html"> Logarithmic </a> </li> <li class="chapter " data-level="1.6.1.4" data-path="../axes/cartesian/time.html"> <a href="../axes/cartesian/time.html"> Time </a> </li> </ul> </li> <li class="chapter " data-level="1.6.2" data-path="../axes/radial/"> <a href="../axes/radial/"> Radial </a> <ul class="articles"> <li class="chapter " data-level="1.6.2.1" data-path="../axes/radial/linear.html"> <a href="../axes/radial/linear.html"> Linear </a> </li> </ul> </li> <li class="chapter " data-level="1.6.3" data-path="../axes/labelling.html"> <a href="../axes/labelling.html"> Labelling </a> </li> <li class="chapter " data-level="1.6.4" data-path="../axes/styling.html"> <a href="../axes/styling.html"> Styling </a> </li> </ul> </li> <li class="chapter " data-level="1.7" data-path="../developers/"> <a href="../developers/"> Developers </a> <ul class="articles"> <li class="chapter " data-level="1.7.1" data-path="../developers/api.html"> <a href="../developers/api.html"> Chart.js API </a> </li> <li class="chapter " data-level="1.7.2" data-path="../developers/updates.html"> <a href="../developers/updates.html"> Updating Charts </a> </li> <li class="chapter " data-level="1.7.3" data-path="../developers/plugins.html"> <a href="../developers/plugins.html"> Plugins </a> </li> <li class="chapter " data-level="1.7.4" data-path="../developers/charts.html"> <a href="../developers/charts.html"> New Charts </a> </li> <li class="chapter " data-level="1.7.5" data-path="../developers/axes.html"> <a href="../developers/axes.html"> New Axes </a> </li> <li class="chapter " data-level="1.7.6" data-path="../developers/contributing.html"> <a href="../developers/contributing.html"> Contributing </a> </li> </ul> </li> <li class="chapter " data-level="1.8" data-path="../notes/"> <a href="../notes/"> Additional Notes </a> <ul class="articles"> <li class="chapter " data-level="1.8.1" data-path="../notes/comparison.html"> <a href="../notes/comparison.html"> Comparison Table </a> </li> <li class="chapter " data-level="1.8.2" data-path="../notes/extensions.html"> <a href="../notes/extensions.html"> Popular Extensions </a> </li> <li class="chapter " data-level="1.8.3" data-path="../notes/license.html"> <a href="../notes/license.html"> License </a> </li> </ul> </li> <li class="divider"></li> <li> <a href="https://www.gitbook.com" target="blank" class="gitbook-link"> Published with GitBook </a> </li> </ul> </nav> </div> <div class="book-body"> <div class="body-inner"> <div class="book-header" role="navigation"> <!-- Title --> <h1> <i class="fa fa-circle-o-notch fa-spin"></i> <a href=".." >Legend</a> </h1> </div> <div class="page-wrapper" tabindex="-1" role="main"> <div class="page-inner"> <div class="search-plus" id="book-search-results"> <div class="search-noresults"> <section class="normal markdown-section"> <h1 id="legend-configuration">Legend Configuration</h1> <p>The chart legend displays data about the datasets that area appearing on the chart.</p> <h2 id="configuration-options">Configuration options</h2> <p>The legend configuration is passed into the <code>options.legend</code> namespace. The global options for the chart legend is defined in <code>Chart.defaults.global.legend</code>.</p> <table> <thead> <tr> <th>Name</th> <th>Type</th> <th>Default</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>display</code></td> <td><code>Boolean</code></td> <td><code>true</code></td> <td>is the legend shown</td> </tr> <tr> <td><code>position</code></td> <td><code>String</code></td> <td><code>'top'</code></td> <td>Position of the legend. <a href="#position">more...</a></td> </tr> <tr> <td><code>fullWidth</code></td> <td><code>Boolean</code></td> <td><code>true</code></td> <td>Marks that this box should take the full width of the canvas (pushing down other boxes). This is unlikely to need to be changed in day-to-day use.</td> </tr> <tr> <td><code>onClick</code></td> <td><code>Function</code></td> <td></td> <td>A callback that is called when a click event is registered on a label item</td> </tr> <tr> <td><code>onHover</code></td> <td><code>Function</code></td> <td></td> <td>A callback that is called when a 'mousemove' event is registered on top of a label item</td> </tr> <tr> <td><code>reverse</code></td> <td><code>Boolean</code></td> <td><code>false</code></td> <td>Legend will show datasets in reverse order.</td> </tr> <tr> <td><code>labels</code></td> <td><code>Object</code></td> <td></td> <td>See the <a href="#legend-label-configuration">Legend Label Configuration</a> section below.</td> </tr> </tbody> </table> <h2 id="position">Position</h2> <p>Position of the legend. Options are:</p> <ul> <li><code>'top'</code></li> <li><code>'left'</code></li> <li><code>'bottom'</code></li> <li><code>'right'</code></li> </ul> <h2 id="legend-label-configuration">Legend Label Configuration</h2> <p>The legend label configuration is nested below the legend configuration using the <code>labels</code> key.</p> <table> <thead> <tr> <th>Name</th> <th>Type</th> <th>Default</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>boxWidth</code></td> <td><code>Number</code></td> <td><code>40</code></td> <td>width of coloured box</td> </tr> <tr> <td><code>fontSize</code></td> <td><code>Number</code></td> <td><code>12</code></td> <td>font size of text</td> </tr> <tr> <td><code>fontStyle</code></td> <td><code>String</code></td> <td><code>'normal'</code></td> <td>font style of text</td> </tr> <tr> <td><code>fontColor</code></td> <td><code>Color</code></td> <td><code>'#666'</code></td> <td>Color of text</td> </tr> <tr> <td><code>fontFamily</code></td> <td><code>String</code></td> <td><code>"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"</code></td> <td>Font family of legend text.</td> </tr> <tr> <td><code>padding</code></td> <td><code>Number</code></td> <td><code>10</code></td> <td>Padding between rows of colored boxes.</td> </tr> <tr> <td><code>generateLabels</code></td> <td><code>Function</code></td> <td></td> <td>Generates legend items for each thing in the legend. Default implementation returns the text + styling for the color box. See <a href="#legend-item-interface">Legend Item</a> for details.</td> </tr> <tr> <td><code>filter</code></td> <td><code>Function</code></td> <td><code>null</code></td> <td>Filters legend items out of the legend. Receives 2 parameters, a <a href="#legend-item-interface">Legend Item</a> and the chart data.</td> </tr> <tr> <td><code>usePointStyle</code></td> <td><code>Boolean</code></td> <td><code>false</code></td> <td>Label style will match corresponding point style (size is based on fontSize, boxWidth is not used in this case).</td> </tr> </tbody> </table> <h2 id="legend-item-interface">Legend Item Interface</h2> <p>Items passed to the legend <code>onClick</code> function are the ones returned from <code>labels.generateLabels</code>. These items must implement the following interface.</p> <pre><code class="lang-javascript">{ <span class="hljs-comment">// Label that will be displayed</span> text: <span class="hljs-built_in">String</span>, <span class="hljs-comment">// Fill style of the legend box</span> fillStyle: Color, <span class="hljs-comment">// If true, this item represents a hidden dataset. Label will be rendered with a strike-through effect</span> hidden: <span class="hljs-built_in">Boolean</span>, <span class="hljs-comment">// For box border. See https://developer.mozilla.org/en/docs/Web/API/CanvasRenderingContext2D/lineCap</span> lineCap: <span class="hljs-built_in">String</span>, <span class="hljs-comment">// For box border. See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash</span> lineDash: <span class="hljs-built_in">Array</span>[<span class="hljs-built_in">Number</span>], <span class="hljs-comment">// For box border. See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset</span> lineDashOffset: <span class="hljs-built_in">Number</span>, <span class="hljs-comment">// For box border. See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineJoin</span> lineJoin: <span class="hljs-built_in">String</span>, <span class="hljs-comment">// Width of box border</span> lineWidth: <span class="hljs-built_in">Number</span>, <span class="hljs-comment">// Stroke style of the legend box</span> strokeStyle: Color <span class="hljs-comment">// Point style of the legend box (only used if usePointStyle is true)</span> pointStyle: <span class="hljs-built_in">String</span> } </code></pre> <h2 id="example">Example</h2> <p>The following example will create a chart with the legend enabled and turn all of the text red in color.</p> <pre><code class="lang-javascript"><span class="hljs-keyword">var</span> chart = <span class="hljs-keyword">new</span> Chart(ctx, { type: <span class="hljs-string">'bar'</span>, data: data, options: { legend: { display: <span class="hljs-literal">true</span>, labels: { fontColor: <span class="hljs-string">'rgb(255, 99, 132)'</span> } } } }); </code></pre> <h2 id="custom-on-click-actions">Custom On Click Actions</h2> <p>It can be common to want to trigger different behaviour when clicking an item in the legend. This can be easily achieved using a callback in the config object.</p> <p>The default legend click handler is:</p> <pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e, legendItem</span>) </span>{ <span class="hljs-keyword">var</span> index = legendItem.datasetIndex; <span class="hljs-keyword">var</span> ci = <span class="hljs-keyword">this</span>.chart; <span class="hljs-keyword">var</span> meta = ci.getDatasetMeta(index); <span class="hljs-comment">// See controller.isDatasetVisible comment</span> meta.hidden = meta.hidden === <span class="hljs-literal">null</span>? !ci.data.datasets[index].hidden : <span class="hljs-literal">null</span>; <span class="hljs-comment">// We hid a dataset ... rerender the chart</span> ci.update(); } </code></pre> <p>Lets say we wanted instead to link the display of the first two datasets. We could change the click handler accordingly.</p> <pre><code class="lang-javascript"><span class="hljs-keyword">var</span> defaultLegendClickHandler = Chart.defaults.global.legend.onClick; <span class="hljs-keyword">var</span> newLegendClickHandler = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e, legendItem</span>) </span>{ <span class="hljs-keyword">var</span> index = legendItem.datasetIndex; <span class="hljs-keyword">if</span> (index > <span class="hljs-number">1</span>) { <span class="hljs-comment">// Do the original logic</span> defaultLegendClickHandler(e, legendItem); } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">let</span> ci = <span class="hljs-keyword">this</span>.chart; [ci.getDatasetMeta(<span class="hljs-number">0</span>), ci.getDatasetMeta(<span class="hljs-number">1</span>)].forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">meta</span>) </span>{ meta.hidden = meta.hidden === <span class="hljs-literal">null</span>? !ci.data.datasets[index].hidden : <span class="hljs-literal">null</span>; }); ci.update(); } }; <span class="hljs-keyword">var</span> chart = <span class="hljs-keyword">new</span> Chart(ctx, { type: <span class="hljs-string">'line'</span>, data: data, options: { legend: { } } }); </code></pre> <p>Now when you click the legend in this chart, the visibility of the first two datasets will be linked together.</p> <h2 id="html-legends">HTML Legends</h2> <p>Sometimes you need a very complex legend. In these cases, it makes sense to generate an HTML legend. Charts provide a <code>generateLegend()</code> method on their prototype that returns an HTML string for the legend.</p> <p>To configure how this legend is generated, you can change the <code>legendCallback</code> config property.</p> <pre><code class="lang-javascript"><span class="hljs-keyword">var</span> chart = <span class="hljs-keyword">new</span> Chart(ctx, { type: <span class="hljs-string">'line'</span>, data: data, options: { legendCallback: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">chart</span>) </span>{ <span class="hljs-comment">// Return the HTML string here.</span> } } }); </code></pre> <p>Note that legendCallback is not called automatically and you must call <code>generateLegend()</code> yourself in code when creating a legend using this method.</p> </section> </div> <div class="search-results"> <div class="has-results"> <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1> <ul class="search-results-list"></ul> </div> <div class="no-results"> <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1> </div> </div> </div> </div> </div> </div> <a href="layout.html" class="navigation navigation-prev " aria-label="Previous page: Layout"> <i class="fa fa-angle-left"></i> </a> <a href="title.html" class="navigation navigation-next " aria-label="Next page: Title"> <i class="fa fa-angle-right"></i> </a> </div> <script> var gitbook = gitbook || []; gitbook.push(function() { gitbook.page.hasChanged({"page":{"title":"Legend","level":"1.4.3","depth":2,"next":{"title":"Title","level":"1.4.4","depth":2,"path":"configuration/title.md","ref":"configuration/title.md","articles":[]},"previous":{"title":"Layout","level":"1.4.2","depth":2,"path":"configuration/layout.md","ref":"configuration/layout.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":"configuration/legend.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> <script src="../gitbook/gitbook.js"></script> <script src="../gitbook/theme.js"></script> <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script> <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/4.1.0/anchor.min.js"></script> <script src="../gitbook/gitbook-plugin-anchorjs/anchor-style.js"></script> <script src="../gitbook/gitbook-plugin-ga/plugin.js"></script> <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script> <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script> </body> </html>