<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ricardo Nishimura</title>
    <description>The latest articles on DEV Community by Ricardo Nishimura (@ricnish).</description>
    <link>https://dev.to/ricnish</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F575214%2F2d2e6f11-a5b6-409f-9ac4-2cb3378c9cf4.jpeg</url>
      <title>DEV Community: Ricardo Nishimura</title>
      <link>https://dev.to/ricnish</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ricnish"/>
    <language>en</language>
    <item>
      <title>Chartjs with Gradient Background</title>
      <dc:creator>Ricardo Nishimura</dc:creator>
      <pubDate>Sun, 07 Feb 2021 21:43:27 +0000</pubDate>
      <link>https://dev.to/ricnish/chartjs-with-gradient-background-5fai</link>
      <guid>https://dev.to/ricnish/chartjs-with-gradient-background-5fai</guid>
      <description>&lt;p&gt;How to add a gradient background for the entire &lt;a href="https://www.chartjs.org/"&gt;Chart.js&lt;/a&gt; and not just for the dataset, that was my first question when I started to play with it, so here I am...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8mgew4fgl9zhsncw3xb.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8mgew4fgl9zhsncw3xb.PNG" alt="Alt Text" width="501" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Chart Plugins
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://www.chartjs.org/docs/latest/developers/plugins.html"&gt;Chartjs Plugins&lt;/a&gt; are the most efficient way to customize or change the default behavior of a chart. They have been introduced at version 2.1.0 (global plugins only) and extended at version 2.5.0 (per chart plugins and options).&lt;/p&gt;

&lt;p&gt;With a custom plugin we can draw a custom background implementing the function "beforeDraw".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There were some changes to the plugin interface in the ChartJs version 3, this code example was updated to reflect it, thanks to Hung Tran for pointing me out.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The following plugin draws a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient"&gt;custom gradient&lt;/a&gt; in the chart background:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var GradientBgPlugin = {
  beforeDraw: function(chart, args, options) {
    const ctx = chart.ctx;
    const canvas = chart.canvas;
    const chartArea = chart.chartArea;

    // Chart background
    var gradientBack = canvas.getContext("2d").createLinearGradient(0, 0, 0, 250);
    gradientBack.addColorStop(0, "rgba(60, 174, 163, 0.7)");
    gradientBack.addColorStop(0.5, "rgba(255, 255, 255, 0)");
    gradientBack.addColorStop(1, "rgba(32, 99, 155, 0.7)");

    ctx.fillStyle = gradientBack;
    ctx.fillRect(chartArea.left, chartArea.bottom,
      chartArea.right - chartArea.left, chartArea.top - chartArea.bottom);
  }
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then insert the custom plugin in the Chart configuration and voilà:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://jsfiddle.net/ricnish/6284o57q/28/embedded/js,html,result//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>chartjs</category>
    </item>
  </channel>
</rss>
