<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ chartjs - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ chartjs - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Tue, 26 May 2026 04:44:18 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/chartjs/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Integrate Chart.js in Angular Using Data from a REST API ]]>
                </title>
                <description>
                    <![CDATA[ Charts are a great way of visually displaying large sets of data in formats that are easy to understand and analyze. They are a great way of showing the relationship that exists between two or more data sets. Different types of charts exist, some of ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-integrate-chart-js-in-angular-using-data-from-a-rest-api/</link>
                <guid isPermaLink="false">66d45e01f855545810e93415</guid>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ chartjs ]]>
                    </category>
                
                    <category>
                        <![CDATA[ REST API ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ deji adesoga ]]>
                </dc:creator>
                <pubDate>Thu, 30 Mar 2023 15:54:23 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/Copy-of-Copy-of-Orange-and-White-Vibrant-Food-YouTube-Thumbnail--1-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Charts are a great way of visually displaying large sets of data in formats that are easy to understand and analyze. They are a great way of showing the relationship that exists between two or more data sets.</p>
<p>Different types of charts exist, some of which includes bar chart, line chart, pie chart, radar chart, etc.</p>
<p>In this article, you will make use of a frontend framework called Angular as well as a JavaScript library called <a target="_blank" href="https://www.chartjs.org/">Chart.js</a> to display data from a cryptocurrency platform called <a target="_blank" href="https://coinranking.com/">Coinranking</a>. You will also make use of the Coinranking API to visualize a list of Cryptocurrencies along with their prices.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>To follow along with this tutorial, make sure you are familiar with the basics of the technologies listed below:</p>
<ul>
<li><p>HTML</p>
</li>
<li><p>JavaScript</p>
</li>
<li><p>TypeScript</p>
</li>
<li><p>npm</p>
</li>
</ul>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-how-to-install-and-create-the-angular-application">How to Install and Create the Angular Application</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-integrate-chartjs-in-angular">How to Integrate Chart.js in Angular</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-integrate-the-rest-api">How to Integrate the REST API</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-display-the-data-in-the-chart">How to Display the Data in the Chart</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<p>You can also watch the video version of this article below, or on my <a target="_blank" href="https://www.youtube.com/thecodeangle">YouTube channel</a>:</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/WCI4yvrzFwc" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p> </p>
<h2 id="heading-how-to-install-and-create-the-angular-application">How to Install and Create the Angular Application</h2>
<p>Firstly, you need to install and set up Angular using the following steps:</p>
<h3 id="heading-step-1-install-npm-node-package-manager">Step 1: Install NPM (Node Package Manager)</h3>
<p>To install npm, you need to download Node.js. This can be done through the Node.js <a target="_blank" href="https://nodejs.org/en/download/">website</a>.</p>
<p>Node.js is an open-source cross platform server environment that can run on Windows, Linux, Unix, macOS, and more. It allows us to make use of npm to install libraries like Chart.js into our Angular application.</p>
<h3 id="heading-step-2-install-the-angular-cli-command-line-interface">Step 2: Install the Angular CLI (Command Line Interface)</h3>
<p>Once Node.js is installed, you can now download Angular into your machine with the terminal/command line using the command below:</p>
<pre><code class="lang-javascript">npm install -g @angular/cli
</code></pre>
<p>To confirm Angular has been installed, you can view the version by running the command <code>ng v</code>, which will give us the result below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/capture2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Angular CLI Version</em></p>
<h3 id="heading-step-3-create-a-new-angular-project">Step 3: Create a New Angular Project</h3>
<p>Now that Angular has been installed, you can now create a new project by running the following commands in the terminal:</p>
<pre><code class="lang-javascript">ng <span class="hljs-keyword">new</span> ng-chart --routing=<span class="hljs-literal">false</span> --style=css
cd ng-chart
code .
</code></pre>
<p>With the commands above, we've created a new Angular project using the <code>ng new</code> command, disabled the routing and set the styling format to CSS.</p>
<p>Next, navigate into the project directory using the <code>cd</code> command, and open the project in Visual Studio Code.</p>
<p>You do not need to generate a new component for this project. You will use the two default files created by the Angular CLI — <code>app.component.ts</code> and <code>app.component.hmtl</code> — to render the chart. These files are in the <code>app</code> directory of your project.</p>
<p>The <code>app.component.html</code> file contains some boilerplate code which you need to get rid of. The Chart.js library can now be integrated into your application.</p>
<h2 id="heading-how-to-integrate-chartjs-in-angular">How to Integrate Chart.js in Angular</h2>
<p>To add the chart.js library to the Angular app, you need to run the following command inside your terminal:</p>
<pre><code class="lang-javascript">npm i chart.js
</code></pre>
<p>To confirm chart.js has been installed, you can check the <code>package.json</code> file in your project. You should see the chart.js version in the <code>dependencies</code> object shown in the image below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/capture4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Chart.js version</em></p>
<p>You can now import the chart.js library inside your project in the <code>app.component.ts</code> file as can be seen below:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> Chart <span class="hljs-keyword">from</span> <span class="hljs-string">'chart.js/auto'</span>;
</code></pre>
<p>Next, create a variable called <code>chart</code> and set it to an empty array:</p>
<pre><code class="lang-javascript">chart: any = []
</code></pre>
<p>Navigate to the getting started page of the chart.js <a target="_blank" href="https://www.chartjs.org/docs/latest/getting-started/">documentation</a> and grab the boilerplate code with static data, and paste it into the <code>ngOnInit</code> lifecycle hook:</p>
<p><strong>Note</strong>: The <code>ngOnInit</code> lifecycle hook in Angular gets triggered once after a component is initialized. That is, it gets called only when the first change detection occurs in an Angular component.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppComponent</span> </span>{
  title = <span class="hljs-string">'ng-chart'</span>;
  chart: any = [];

  <span class="hljs-keyword">constructor</span>() {}

  ngOnInit() {
    <span class="hljs-built_in">this</span>.chart = <span class="hljs-keyword">new</span> Chart(<span class="hljs-string">'canvas'</span>, {
      <span class="hljs-attr">type</span>: <span class="hljs-string">'bar'</span>,
      <span class="hljs-attr">data</span>: {
        <span class="hljs-attr">labels</span>: [<span class="hljs-string">'Red'</span>, <span class="hljs-string">'Blue'</span>, <span class="hljs-string">'Yellow'</span>, <span class="hljs-string">'Green'</span>, <span class="hljs-string">'Purple'</span>, <span class="hljs-string">'Orange'</span>],
        <span class="hljs-attr">datasets</span>: [
          {
            <span class="hljs-attr">label</span>: <span class="hljs-string">'# of Votes'</span>,
            <span class="hljs-attr">data</span>: [<span class="hljs-number">12</span>, <span class="hljs-number">19</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>],
            <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">1</span>,
          },
        ],
      },
      <span class="hljs-attr">options</span>: {
        <span class="hljs-attr">scales</span>: {
          <span class="hljs-attr">y</span>: {
            <span class="hljs-attr">beginAtZero</span>: <span class="hljs-literal">true</span>,
          },
        },
      },
    });
  }
}
</code></pre>
<p>In the code above, within the data object, there is a <code>labels</code> key which contains an array with different values. Beneath that you have the <code>datasets</code> array which contains an object.</p>
<p>Within the object, there is an array called <code>data</code> with values of different numbers. These values represent the data that will get displayed on the chart in the browser.</p>
<p>Before the chart gets displayed in the browser, you'll need to also grab the <code>&lt;canvas&gt;&lt;/canvas&gt;</code> html tag and paste it in the <code>app.component.html</code> file as seen below:</p>
<pre><code class="lang-javascript">&lt;div&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">canvas</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"canvas"</span>&gt;</span>{{chart}}<span class="hljs-tag">&lt;/<span class="hljs-name">canvas</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<p><strong>Note</strong>: In the <code>app.component.html</code> file, the <code>&lt;canvas&gt;</code> tag has an <code>id</code> called <code>canvas</code>. This must have the same name as the value inside the parentheses before the <code>new Chart</code> object in the <code>app.component.ts</code> file. If the names are not the same, the chart will not display.</p>
<p>You can now compile and serve the project by running the <code>ng serve --open</code> command in the terminal. You should have the following results:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/capture6.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Displaying the charts with static data</em></p>
<h2 id="heading-how-to-integrate-the-rest-api">How to Integrate the REST API</h2>
<p>To integrate the REST API, head over to the Coinranking website using this <a target="_blank" href="https://developers.coinranking.com/api">link</a>. You should see a page like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/capture8.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Coinranking website</em></p>
<p>Start by clicking on the "Get API Key" button. You will be directed to a page where you can create an account and have access to an <strong>API</strong> key.</p>
<p>Copy the API key and open the <code>environment.ts</code> file in your Angular project. Within the object, create a variable called <code>API_KEY</code> and paste in the API key generated from CoinRanking as seen below:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> environment = {
  <span class="hljs-attr">production</span>: <span class="hljs-literal">false</span>,
  <span class="hljs-attr">API_KEY</span>: <span class="hljs-string">'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'</span>,
};
</code></pre>
<p>Next, you need to copy the API to get all cryptocurrency coins from the CoinRanking <a target="_blank" href="https://developers.coinranking.com/api/documentation/coins">documentation</a>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/capture9.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Coinranking API documentation</em></p>
<p>To use the API link copied above, you need to create an Angular Service by running the command below:</p>
<pre><code class="lang-javascript">ng g service services/chart
</code></pre>
<p>With the <code>chart.service.ts</code> file created, you can now paste in the code that helps us consume the REST API as seen below:</p>
<p>To summarize the above code, here's what we did:</p>
<ul>
<li><p>We imported the <code>HttpClient</code>, <code>HttpHeaders</code> in <strong>line 2</strong>, as well as the <code>environment</code> file in <strong>line 3</strong>.</p>
</li>
<li><p>Between <strong>line 5</strong> and <strong>line 11</strong>, we created a variable called <code>httpOptions</code>. The <code>httpOptions</code> variable holds an object that contains the <code>HttpHeaders</code> configuration imported above. Here we set the <strong>content-type</strong>, passed the <code>API_KEY</code> variable from the <code>environment</code> file to the headers, and then set the <code>Access-Control-Allow-Origin</code> to a wildcard. This means the browser should allow request code from the origin.</p>
</li>
<li><p>In <strong>line 17</strong>, we created a private variable called <code>baseUrl</code> to hold the <strong>REST API</strong>, while in <strong>line 28</strong>, we created a variable called <code>proxyUrl</code> to hold the <strong>CORS Anywhere</strong> link. The <strong>CORS Anywhere</strong> link is a NodeJS proxy that adds CORS headers to the proxied request and helps to prevent CORS errors in the process.</p>
</li>
<li><p>In <strong>line 20</strong>, we injected the <code>HttpClient</code> into the constructor which makes it accessible within the Service.</p>
</li>
<li><p>Finally, we created a function called <code>cryptoData()</code> in <strong>line 22</strong>. Within this function lies a variable called <code>url</code>. We used the <code>url</code> variable to append both the <code>proxyUrl</code> and <code>baseUrl</code> variables to construct our API. Furthermore, in the <code>return</code> statement, we used the <code>get()</code> http method to fetch the data from the REST API.</p>
</li>
</ul>
<p>To make the <code>HttpClient</code> work, you need to import and inject the <code>HttpClientModule</code> into the <code>app.module.ts</code> file which can be seen below:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { NgModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;
<span class="hljs-keyword">import</span> { BrowserModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/platform-browser'</span>;
<span class="hljs-keyword">import</span> { HttpClientModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/common/http'</span>;

<span class="hljs-keyword">import</span> { AppComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./app.component'</span>;

@NgModule({
  <span class="hljs-attr">declarations</span>: [AppComponent],
  <span class="hljs-attr">imports</span>: [BrowserModule, HttpClientModule],
  <span class="hljs-attr">providers</span>: [],
  <span class="hljs-attr">bootstrap</span>: [AppComponent],
})
<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppModule</span> </span>{}
</code></pre>
<p>As seen above, the <code>HttpClientModule</code> was imported and injected into the <code>imports</code> array.</p>
<h2 id="heading-how-to-display-the-data-in-the-chart">How to Display the Data in the Chart</h2>
<p>To display the data on the chart, you need to head to the <code>app.component.ts</code> file and bring in your Chart service file as seen below:</p>
<ul>
<li><p>As seen above, we begin by importing the Chart Service and injecting it into our component in <strong>line 3</strong> and <strong>line 17</strong> respectively.</p>
</li>
<li><p>We created three variables called <code>result</code>, <code>coinPrice</code>, and <code>coinName</code>. These variables will be used later in the project to hold data.</p>
</li>
<li><p>Within the <code>ngOnInit()</code> lifecycle hook, we called the <code>cryptoData()</code> function in <code>line 20</code>, which returns an observable that gets subscribed to. This allows us to return a response from the data in the process.</p>
</li>
<li><p>In <strong>line 21</strong>, we called the <code>result</code> variable using the <code>this</code> keyword and set it to hold the data.</p>
</li>
<li><p>Next, in <strong>line 22</strong> and <strong>line 23</strong>, we called the <strong>coinPrice</strong> and <strong>coinName</strong> variables, mapped through the data, and attached the coin price and coin name to them respectively.</p>
</li>
<li><p>To see the results of the data in the browser, we used the <code>console.log()</code> function to display the data, as seen below:</p>
</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/capture10-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Data from the CoinRanking API displayed on the console.</em></p>
<p>The final thing to be done is to plot the data seen in the image above into chart.</p>
<ul>
<li><p>As seen above, we first replaced the static data in the labels in <strong>line 12</strong> with the coin name gotten from the CoinRanking API.</p>
</li>
<li><p>Next, we replaced the static data in <strong>line 15</strong> with the price gotten from the CoinRanking API.</p>
</li>
</ul>
<p>Now compile the project by running the <code>ng serve --open</code> command and see the results in the browser as seen below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/capture11.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Final results of the chart</em></p>
<p><strong>Note</strong>: There is a possibility that you'll run into an error that says <strong>(Failed to load resource: the server responded with a status of 403 (Forbidden))</strong> after compiling the project.</p>
<p>What you need to do is to click on the CORS Anywhere link in the console which will open up in a new tab, with a button that says <strong>Request temporary access to the demo server.</strong> Click the button and then refresh the page. The data should now reflect on the chart.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you learned how to install and integrate the Chart.js library and a REST API in an Angular application. If you want access to the code base, you can clone the repo <a target="_blank" href="https://github.com/desoga10/ng-chart">here</a> on GitHub.</p>
<p>If you enjoyed this article, you can show your support by subscribing to my <a target="_blank" href="https://www.youtube.com/TheCodeAngle">YouTube channel</a> where I create awesome tutorials on web development technologies like JavaScript, React, Angular, Node.js, and more.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Chart.js Tutorial – How to Make Marimekko Charts in Angular ]]>
                </title>
                <description>
                    <![CDATA[ By Swatej Patil Data visualization is an essential part of data analysis. And charts are one of the most effective ways to present data in a clear and concise manner.  Marimekko charts are an excellent choice for displaying complex data sets in a com... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/chart-js-tutorial-how-to-make-marimekko-charts-in-angular/</link>
                <guid isPermaLink="false">66d46149706b9fb1c166b9ab</guid>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ chartjs ]]>
                    </category>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ data visualization ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 21 Mar 2023 17:51:26 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/austin-distel-DfjJMVhwH_8-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Swatej Patil</p>
<p>Data visualization is an essential part of data analysis. And charts are one of the most effective ways to present data in a clear and concise manner. </p>
<p>Marimekko charts are an excellent choice for displaying complex data sets in a compact and visually appealing format. </p>
<p>A Marimekko chart, also known as a mosaic chart or mekko chart, is a combination of a stacked bar chart and a 100% stacked bar chart. The width of each bar represents the total value of the corresponding category, and the height of each bar represents the relative contribution of each sub-category to that total. </p>
<p>In this tutorial, we will explore how to create a Marimekko chart using Chart.js, a powerful charting library. We'll delve into the details of how to structure the data for Marimekko charts, as well as how to customize the chart's appearance and behavior using Chart.js options and APIs.</p>
<h1 id="heading-getting-started"><strong>Getting Started</strong></h1>
<p>I am assuming that you already have familiarity with creating simple line and bar charts using Chart.js in an Angular application. In this guide, there are certain concepts that will be easier to comprehend if you have prior knowledge.</p>
<p>Don't worry if you don't – I've got you covered. You can follow my <a target="_blank" href="https://www.freecodecamp.org/news/how-to-make-bar-and-line-charts-using-chartjs-in-angular/">Chart.js Tutorial – How to Make Bar and Line Charts in Angular</a> to get started.</p>
<h2 id="heading-how-to-structure-data-for-marimekko-charts">How to Structure Data for Marimekko Charts</h2>
<p>Let's discuss the data structure required for Marimekko charts before creating the chart. </p>
<p>Marimekko charts need an array of objects, with each object representing a category. Each object must have a label and a sub-array of objects, where each sub-object represents a sub-category. </p>
<p>Each sub-object must have a label and a value. The value represents the proportion of the sub-category in relation to the total of its category.</p>
<p>Here's an example of how to structure data for a Marimekko chart:</p>
<pre><code class="lang-jsx">data: [
  {
    <span class="hljs-attr">label</span>: <span class="hljs-string">'Category 1'</span>,
    <span class="hljs-attr">subcategories</span>: [
      { <span class="hljs-attr">label</span>: <span class="hljs-string">'Subcategory 1'</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">10</span> },
      { <span class="hljs-attr">label</span>: <span class="hljs-string">'Subcategory 2'</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">20</span> },
      { <span class="hljs-attr">label</span>: <span class="hljs-string">'Subcategory 3'</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">30</span> }
    ]
  },
  {
    <span class="hljs-attr">label</span>: <span class="hljs-string">'Category 2'</span>,
    <span class="hljs-attr">subcategories</span>: [
      { <span class="hljs-attr">label</span>: <span class="hljs-string">'Subcategory 1'</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">40</span> },
      { <span class="hljs-attr">label</span>: <span class="hljs-string">'Subcategory 2'</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">50</span> },
      { <span class="hljs-attr">label</span>: <span class="hljs-string">'Subcategory 3'</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">60</span> }
    ]
  }
]
</code></pre>
<p>In this example, we have two categories: Category 1 and Category 2, with three subcategories each. The values for the subcategories represent the proportion of the subcategory in relation to the total of its category. For example, in Category 1, Subcategory 1 represents 10 out of 60, or 16.7% of the total.</p>
<h2 id="heading-how-to-create-a-marimekko-chart-with-chartjs">How to Create a Marimekko Chart with Chart.js</h2>
<p>Now that we have our data structured correctly, let's move on to creating our Marimekko chart using Chart.js. </p>
<p>First, we need to create a canvas element in our HTML code to hold the chart:</p>
<pre><code class="lang-jsx">&lt;canvas id=<span class="hljs-string">"marimekkoChart"</span>&gt;&lt;/canvas&gt;
</code></pre>
<p>Next, we'll need to install Chart.js and import it into our Angular component:</p>
<pre><code class="lang-jsx">npm install chart.js
</code></pre>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Chart <span class="hljs-keyword">from</span> <span class="hljs-string">'chart.js/auto'</span>;
</code></pre>
<p>In our component class, we can then create a new Chart object and pass in our data and options:</p>
<pre><code class="lang-jsx">ngAfterViewInit() {
  <span class="hljs-keyword">const</span> canvas = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'marimekkoChart'</span>) <span class="hljs-keyword">as</span> HTMLCanvasElement;
  <span class="hljs-keyword">const</span> ctx = canvas.getContext(<span class="hljs-string">'2d'</span>) <span class="hljs-keyword">as</span> CanvasRenderingContext2D;

  <span class="hljs-keyword">const</span> data = {
    <span class="hljs-attr">labels</span>: [<span class="hljs-string">'Category 1'</span>, <span class="hljs-string">'Category 2'</span>],
    <span class="hljs-attr">datasets</span>: [
      {
        <span class="hljs-attr">label</span>: <span class="hljs-string">'Subcategory 1'</span>,
        <span class="hljs-attr">data</span>: [<span class="hljs-number">10</span>, <span class="hljs-number">40</span>],
        <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'rgba(255, 99, 132, 0.7)'</span>,
        <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">1</span>
      },
      {
        <span class="hljs-attr">label</span>: <span class="hljs-string">'Subcategory 2'</span>,
        <span class="hljs-attr">data</span>: [<span class="hljs-number">20</span>, <span class="hljs-number">50</span>],
        <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'rgba(54, 162, 235, 0.7)'</span>,
        <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">1</span>
      },
      {
        <span class="hljs-attr">label</span>: <span class="hljs-string">'Subcategory 3'</span>,
        <span class="hljs-attr">data</span>: [<span class="hljs-number">30</span>, <span class="hljs-number">60</span>],
        <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'rgba(255, 206, 86, 0.7)'</span>,
        <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">1</span>
      }
    ]
  };

  <span class="hljs-keyword">const</span> options = {
    <span class="hljs-attr">indexAxis</span>: <span class="hljs-string">'y'</span>,
    <span class="hljs-attr">plugins</span>: {
      <span class="hljs-attr">legend</span>: {
        <span class="hljs-attr">position</span>: <span class="hljs-string">'bottom'</span>
      }
    }
  };

  <span class="hljs-keyword">const</span> marimekkoChart = <span class="hljs-keyword">new</span> Chart(ctx, {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'bar'</span>,
    <span class="hljs-attr">data</span>: data,
  });
}
</code></pre>
<p>In this example, we're creating a Marimekko chart with three subcategories for two categories. We've set the type of chart to 'bar', and we're passing in our data and options. The 'indexAxis' option is set to 'y' to make the chart horizontal, and the 'legend' option is set to position the legend at the bottom of the chart.</p>
<p>Congratulations! If you have followed along carefully then you shouldn’t run into any errors and your output may look like the following:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/Mekkochart.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>And that's it! With this code, we can create a Marimekko chart in Chart.js in Angular. </p>
<p>You can further customize your chart using various Chart.js options and APIs, such as adjusting the colors, labels, and tooltip behaviors, to make it even more informative and engaging.</p>
<h1 id="heading-conclusion">C<strong>onclusion</strong></h1>
<p>Chart.js is a very useful and powerful library. In this quick tutorial, we've covered how to create a Marimekko chart in Chart.js in Angular. If you want to include any type of chart in your Angular application, then it is very easy to make them with Chart.js.</p>
<p>The full code for this Angular application is hosted on my <a target="_blank" href="https://github.com/SwatejPatil/Chart.js-Tutorial-How-to-Make-a-Marimekko-Chart-in-Angular">GitHub Repo</a>.</p>
<p>I hope you found this tutorial helpful and informative. If you have any questions or feedback, feel free to to contact me on <a target="_blank" href="http://www.linkedin.com/in/swatejpatil/">LinkedIn</a>!</p>
<h3 id="heading-related-articles-ive-written-about-chartjs"><strong>Related Articles I've Written about Chart.js:</strong></h3>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/chart-js-zooom-plugin/">How to Make Bar and Line Charts using Chart.js in Angular</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/how-to-make-pie-and-doughnut-charts-using-chartjs-in-angular/">Chart.js Tutorial – How to Make Pie and Doughnut Charts in Angular</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/chart-js-zooom-plugin/">How to Add the Chart.js Zoom Plugin to an Angular App</a></li>
</ul>
<p>Photo by <a target="_blank" href="https://unsplash.com/@austindistel?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Austin Distel</a> on <a target="_blank" href="https://unsplash.com/photos/DfjJMVhwH_8?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Chart.js Tutorial – How to Make Pie and Doughnut Charts in Angular ]]>
                </title>
                <description>
                    <![CDATA[ By Swatej Patil In this tutorial we will learn how to create a simple pie and doughnut chart using the Chart.js library in an Angular application. What is Chart.js? Chart.js is a JavaScript library for building charts. It's designed to be intuitive a... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-make-pie-and-doughnut-charts-using-chartjs-in-angular/</link>
                <guid isPermaLink="false">66d46150d14641365a050973</guid>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ chartjs ]]>
                    </category>
                
                    <category>
                        <![CDATA[ data analysis ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 31 Jan 2023 17:03:23 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/01/piechart-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Swatej Patil</p>
<p>In this tutorial we will learn how to create a simple pie and doughnut chart using the Chart.js library in an Angular application.</p>
<h2 id="heading-what-is-chartjs">What is Chart.js?</h2>
<p>Chart.js is a JavaScript library for building charts. It's designed to be intuitive and simple, but it's powerful enough to build complex visualizations. </p>
<p>It has a wide range of chart types, including bar charts, line charts, pie charts, scatter plots, and many more. Chart.js is open-source and can be used without any restrictions on private or commercial projects.</p>
<h3 id="heading-prerequisites"><strong>Prerequisites</strong></h3>
<p>Before we get started with making the charts, just make sure you meet the following prerequisites:</p>
<ul>
<li>Basic understanding of HTML /CSS and TypeScript especially the understanding of Object Oriented concepts of TypeScript classes and methods.</li>
<li>Node.js version 10 or higher and NPM or Yarn package manager. You can download node.js from <a target="_blank" href="https://nodejs.org/en/download/">here</a>.</li>
</ul>
<h2 id="heading-how-to-install-angular-cli">How to Install Angular CLI</h2>
<p>We will be using the Angular CLI to generate a starter application for our project. You can install Angular CLI by executing the following command on a terminal or Powershell window:</p>
<pre><code>npm install -g @angular/cli
</code></pre><p>If you have Angular CLI installed already, you can skip this step.</p>
<h2 id="heading-how-to-create-a-angular-application">How to Create a Angular Application</h2>
<p>Now let’s create an Angular application that will hold our graphs. In a terminal, execute the following commands:</p>
<pre><code class="lang-bash"> ng new angular-chartjs
</code></pre>
<p>Now that our Angular application has been created, navigate to the project folder and start the local development server using the following command:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> angular-chartjs
ng serve -o
</code></pre>
<p>A browser window will open on <code>http://localhost:4200/</code>  and you can see your running Angular application.</p>
<h2 id="heading-how-to-install-the-chartjs-package">How to Install the Chart.js Package</h2>
<p>Now open a new terminal window in the same directory and install the Chart.js library using the following:</p>
<pre><code class="lang-bash">npm install chart.js
</code></pre>
<p>Now we need to create two Angular components, one for the Pie graph and the other for the Doughnut chart. You can create them easily with the angular CLI by executing the following commands:</p>
<pre><code class="lang-bash">ng generate component pie-chart
ng generate component doughnut-chart
</code></pre>
<h2 id="heading-how-to-create-a-pie-chart-in-chartjs">How to Create a Pie Chart in Chart.js</h2>
<p>A pie chart is a circular graph that displays data in slices, where each slice represents a category of data. The size of each slice represents the proportion of the category in relation to the whole data set.</p>
<p>Pie charts are an effective way to show the breakdown of data into categories, especially when the data represents parts of a whole. They are useful when you want to show how much each category contributes to the total.</p>
<p>When to use pie charts:</p>
<ul>
<li>To show the proportions of different categories in a whole</li>
<li>To compare the relative sizes of different categories</li>
<li>To demonstrate how each category contributes to the whole </li>
</ul>
<p>Pie charts are commonly used in business and economics to show the distribution of expenditures, market shares, or other aspects of the company's performance.</p>
<p>Now that we have created the components, we will move on with creating the pie chart.</p>
<p>Navigate to the <code>/src/app/pie-chart</code> component and open the <code>pie-chart.component.html</code> file and paste the following code:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"chart-container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">canvas</span>  <span class="hljs-attr">id</span>=<span class="hljs-string">"MyChart"</span> &gt;</span>{{ chart }}<span class="hljs-tag">&lt;/<span class="hljs-name">canvas</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We have simply created a container, and inside that container a canvas with an id of <code>MyChart</code>. We have used Angular’s string interpolation to render the <code>chart</code> variable, which we will create soon.</p>
<p>Inside the pie-chart component, open the <code>pie-chart.component.ts</code> file and import the Chart.js library using the following command:</p>
<pre><code class="lang-bash">import Chart from <span class="hljs-string">'chart.js/auto'</span>;
</code></pre>
<p>Now let’s make the <code>chart</code> variable we mentioned earlier. This variable will hold the information of our graphs.</p>
<pre><code class="lang-tsx">public chart: any;
</code></pre>
<p>Now we will create a method for the pie chart. This will include the data and labels for our chart.  </p>
<p>Copy the following code and paste it in the <code>pie-chart.component.ts</code> file below the <code>ngOnInit()</code> function:</p>
<pre><code class="lang-tsx">createChart(){

    this.chart = new Chart("MyChart", {
      type: 'pie', //this denotes tha type of chart

      data: {// values on X-Axis
        labels: ['Red', 'Pink','Green','Yellow','Orange','Blue', ],
           datasets: [{
    label: 'My First Dataset',
    data: [300, 240, 100, 432, 253, 34],
    backgroundColor: [
      'red',
      'pink',
      'green',
            'yellow',
      'orange',
      'blue',            
    ],
    hoverOffset: 4
  }],
      },
      options: {
        aspectRatio:2.5
      }

    });
  }
</code></pre>
<p>Here we have set the <code>type</code> of chart as <code>pie</code>. We have given labels as the names of common colors. And we've added data values, which will be automatically allocated a portion of pie in the pie chart.</p>
<p>We want our <code>createChart()</code> function to run as soon as the page loads. In order to do that, we need to call the function in the <code>ngOnInit()</code>:</p>
<pre><code class="lang-tsx">ngOnInit(): void {
    this.createChart();
  }
</code></pre>
<p>Finally, we need to add the HTML selector of the pie-chart component to the <code>app.component.html</code>  file. Remove the initial template code of Angular and add the following:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">app-pie-chart</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">app-pie-chart</span>&gt;</span>
</code></pre>
<p>Save all the files and the browser window will refresh automatically. Your first pie chart is ready!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/piechart.png" alt="Image" width="600" height="400" loading="lazy">
<em>Our pie chart</em></p>
<h2 id="heading-how-to-create-a-doughnut-chart-in-chartjs">How to Create a Doughnut Chart in Chart.js</h2>
<p>A doughnut chart is a variation of the pie chart and serves the same purpose of representing data in a circular format. It is made up of multiple sectors, where each sector represents a data value, with the size of each sector proportional to its value.</p>
<p>You can use a doughnut chart:</p>
<ul>
<li>To show a parts-to-whole relationship in data</li>
<li>To compare different categories of data</li>
<li>To show progress towards a goal or target</li>
</ul>
<p>Now we will move on with creating the doughnut chart. Since Doughnut charts and Pie charts can be used interchangeably, we don’t need to modify anything except the type of chart. Just follow the same steps  you have done so far and make sure to do them on the doughnut chart component.</p>
<p>Paste the same code for <code>createChart()</code> method on the <code>doughnut-chart.component.ts</code> file below the <code>ngOnInit()</code> function. You just need to change the keyword for type of chart from <code>pie</code> to <code>doughnut</code>. Your code should look like the following:</p>
<pre><code class="lang-tsx">createChart(){

    this.chart = new Chart("MyChart", {
      type: 'doughnut', //this denotes tha type of chart

      data: {// values on X-Axis
        labels: ['Red', 'Pink','Green','Yellow','Orange','Blue', ],
           datasets: [{
    label: 'My First Dataset',
    data: [300, 240, 100, 432, 253, 34],
    backgroundColor: [
      'red',
      'pink',
      'green',
            'yellow',
      'orange',
      'blue',            
    ],
    hoverOffset: 4
  }],
      },
      options: {
        aspectRatio:2.5
      }

    });
  }
</code></pre>
<p>Call the <code>createChart()</code> function in <code>ngOnInit()</code> and your line chart will be ready.</p>
<pre><code class="lang-tsx">ngOnInit(): void {
    this.createChart();
  }
</code></pre>
<p>Finally, we need to add the HTML selector for the line-chart component to the <code>app.component.html</code> file.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">app-doughnut-chart</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">app-doughnut-chart</span>&gt;</span>
</code></pre>
<p>Your output may look like the following:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/doughtnutchart.png" alt="Image" width="600" height="400" loading="lazy">
<em>Our doughnut chart</em></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Chart.js is a very useful and powerful library. If you want to include any type of chart in your Angular application, then it is very easy to make them with Chart.js.</p>
<p>The full code for this Angular application is hosted on my <a target="_blank" href="https://github.com/SwatejPatil/Pie-and-Doughnut-Charts-using-ChartJs-in-Angular/upload/main">GitHub Repo</a>.</p>
<p>I hope you found this tutorial helpful. Thank you for reading!</p>
<h3 id="heading-related-articles">Related Articles</h3>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/chart-js-zooom-plugin/">How to Make Bar and Line Charts using Chart.js in Angular</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Add the Chart.js Zoom Plugin to an Angular App ]]>
                </title>
                <description>
                    <![CDATA[ By Swatej Patil In this tutorial you will learn how to the Add Chart.js Zoom plugin to an Angular application. When you have a lot of data in a chart, you may want to zoom in and see the details. Line charts are a good way to visualise large amounts ]]>
                </description>
                <link>https://www.freecodecamp.org/news/chart-js-zooom-plugin/</link>
                <guid isPermaLink="false">66d4614b4a0edd9b48e83591</guid>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ chartjs ]]>
                    </category>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 12 Sep 2022 18:38:17 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/09/chartjsZoom.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Swatej Patil</p>
<p>In this tutorial you will learn how to the Add Chart.js Zoom plugin to an Angular application.</p>
<p>When you have a lot of data in a chart, you may want to zoom in and see the details. Line charts are a good way to visualise large amounts of data. You can use the zooming feature in Chart.js to explore your data more closely.</p>
<p>Chart.js is an open-source library you can use to create beautiful charts in any part of your Angular application. The Zooming feature was created to allow you to magnify certain data points for closer inspection. You can quickly and easily zoom by scrolling with the mouse wheel.</p>
<p>Let's see how it works.</p>
<h2 id="heading-getting-started">Getting Started</h2>
<p>I'm going to assume that you already know how to use Chart.js in an Angular application to create simple line and bar charts. </p>
<p>Don't worry if you don't – I've got you covered. You can follow my <a target="_blank" href="https://www.freecodecamp.org/news/how-to-make-bar-and-line-charts-using-chartjs-in-angular/">Chart.js Tutorial – How to Make Bar and Line Charts in Angular</a> to get started.</p>
<p>We will create a new Angular component just for the line chart and then incorporate the zoom plugin into it. We will be using a large amount of data to see how useful the Zoom plugin is.</p>
<p>But first, in your Angular application, you'll need to install the Chart.js Zoom plugin if you don't already have it. </p>
<p>Open up a new terminal in the Angular project folder and paste in the following command:</p>
<pre><code class="lang-bash">npm install chartjs-plugin-zoom
</code></pre>
<p>This will install the plugin into your Angular application.</p>
<p>Now let’s create a new Angular component and make the line chart. Use the following to create a new Angular component:</p>
<pre><code class="lang-bash">ng g c line-chart
</code></pre>
<p>This will create a new Angular component in the <code>/src</code> directory. </p>
<p>Then open the <code>line-chart.component.html</code> file and paste in the following code:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"chart-container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">canvas</span>  <span class="hljs-attr">id</span>=<span class="hljs-string">"MyChart"</span> &gt;</span>{{ chart }}<span class="hljs-tag">&lt;/<span class="hljs-name">canvas</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Now open the <code>line-chart.component.ts</code> file and delete all the code inside it. Then paste in the following code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { Component, OnInit } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;
<span class="hljs-keyword">import</span> Chart <span class="hljs-keyword">from</span> <span class="hljs-string">'chart.js/auto'</span>;

@Component({
  <span class="hljs-attr">selector</span>: <span class="hljs-string">'app-line-chart'</span>,
  <span class="hljs-attr">templateUrl</span>: <span class="hljs-string">'./line-chart.component.html'</span>,
  <span class="hljs-attr">styleUrls</span>: [<span class="hljs-string">'./line-chart.component.css'</span>]
})
<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LineChartComponent</span> <span class="hljs-title">implements</span> <span class="hljs-title">OnInit</span> </span>{

  <span class="hljs-keyword">constructor</span>() { }

  ngOnInit(): <span class="hljs-keyword">void</span> {
    <span class="hljs-built_in">this</span>.createChart();
  }
  public chart: any;

  createChart() {

    <span class="hljs-built_in">this</span>.chart = <span class="hljs-keyword">new</span> Chart(<span class="hljs-string">"MyChart"</span>, {
      <span class="hljs-attr">type</span>: <span class="hljs-string">'line'</span>, <span class="hljs-comment">//this denotes tha type of chart</span>

      <span class="hljs-attr">data</span>: {<span class="hljs-comment">// values on X-Axis</span>
        <span class="hljs-attr">labels</span>: [<span class="hljs-string">"Monday"</span>, <span class="hljs-string">"Tuesday"</span>, <span class="hljs-string">"Wednesday"</span>, <span class="hljs-string">"Thursday"</span>, <span class="hljs-string">"Friday"</span>, <span class="hljs-string">"Saturday"</span>, <span class="hljs-string">"Sunday"</span>,
          <span class="hljs-string">"Monday"</span>, <span class="hljs-string">"Tuesday"</span>, <span class="hljs-string">"Wednesday"</span>, <span class="hljs-string">"Thursday"</span>, <span class="hljs-string">"Friday"</span>, <span class="hljs-string">"Saturday"</span>, <span class="hljs-string">"Sunday"</span>,
          <span class="hljs-string">"Monday"</span>, <span class="hljs-string">"Tuesday"</span>, <span class="hljs-string">"Wednesday"</span>, <span class="hljs-string">"Thursday"</span>, <span class="hljs-string">"Friday"</span>, <span class="hljs-string">"Saturday"</span>, <span class="hljs-string">"Sunday"</span>,
          <span class="hljs-string">"Monday"</span>, <span class="hljs-string">"Tuesday"</span>, <span class="hljs-string">"Wednesday"</span>, <span class="hljs-string">"Thursday"</span>, <span class="hljs-string">"Friday"</span>, <span class="hljs-string">"Saturday"</span>, <span class="hljs-string">"Sunday"</span>,
          <span class="hljs-string">"Monday"</span>, <span class="hljs-string">"Tuesday"</span>, <span class="hljs-string">"Wednesday"</span>, <span class="hljs-string">"Thursday"</span>, <span class="hljs-string">"Friday"</span>, <span class="hljs-string">"Saturday"</span>, <span class="hljs-string">"Sunday"</span>,
          <span class="hljs-string">"Monday"</span>, <span class="hljs-string">"Tuesday"</span>, <span class="hljs-string">"Wednesday"</span>, <span class="hljs-string">"Thursday"</span>, <span class="hljs-string">"Friday"</span>, <span class="hljs-string">"Saturday"</span>, <span class="hljs-string">"Sunday"</span>,
          <span class="hljs-string">"Monday"</span>, <span class="hljs-string">"Tuesday"</span>, <span class="hljs-string">"Wednesday"</span>, <span class="hljs-string">"Thursday"</span>, <span class="hljs-string">"Friday"</span>, <span class="hljs-string">"Saturday"</span>, <span class="hljs-string">"Sunday"</span>,
          <span class="hljs-string">"Monday"</span>, <span class="hljs-string">"Tuesday"</span>, <span class="hljs-string">"Wednesday"</span>, <span class="hljs-string">"Thursday"</span>, <span class="hljs-string">"Friday"</span>, <span class="hljs-string">"Saturday"</span>, <span class="hljs-string">"Sunday"</span>,
          <span class="hljs-string">"Monday"</span>, <span class="hljs-string">"Tuesday"</span>, <span class="hljs-string">"Wednesday"</span>, <span class="hljs-string">"Thursday"</span>, <span class="hljs-string">"Friday"</span>, <span class="hljs-string">"Saturday"</span>, <span class="hljs-string">"Sunday"</span>,
          <span class="hljs-string">"Monday"</span>, <span class="hljs-string">"Tuesday"</span>, <span class="hljs-string">"Wednesday"</span>, <span class="hljs-string">"Thursday"</span>, <span class="hljs-string">"Friday"</span>, <span class="hljs-string">"Saturday"</span>, <span class="hljs-string">"Sunday"</span>],
        <span class="hljs-attr">datasets</span>: [
          {
            <span class="hljs-attr">label</span>: <span class="hljs-string">"Sales"</span>,
            <span class="hljs-attr">data</span>: [<span class="hljs-string">'467'</span>, <span class="hljs-string">'576'</span>, <span class="hljs-string">'572'</span>, <span class="hljs-string">'79'</span>, <span class="hljs-string">'92'</span>, <span class="hljs-string">'574'</span>, <span class="hljs-string">'573'</span>,
            <span class="hljs-string">'467'</span>, <span class="hljs-string">'576'</span>, <span class="hljs-string">'572'</span>, <span class="hljs-string">'79'</span>, <span class="hljs-string">'92'</span>, <span class="hljs-string">'574'</span>, <span class="hljs-string">'573'</span>,
            <span class="hljs-string">'467'</span>, <span class="hljs-string">'576'</span>, <span class="hljs-string">'572'</span>, <span class="hljs-string">'79'</span>, <span class="hljs-string">'92'</span>, <span class="hljs-string">'574'</span>, <span class="hljs-string">'573'</span>,
            <span class="hljs-string">'467'</span>, <span class="hljs-string">'576'</span>, <span class="hljs-string">'572'</span>, <span class="hljs-string">'79'</span>, <span class="hljs-string">'92'</span>, <span class="hljs-string">'574'</span>, <span class="hljs-string">'573'</span>,
            <span class="hljs-string">'467'</span>, <span class="hljs-string">'576'</span>, <span class="hljs-string">'572'</span>, <span class="hljs-string">'79'</span>, <span class="hljs-string">'92'</span>, <span class="hljs-string">'574'</span>, <span class="hljs-string">'573'</span>,
            <span class="hljs-string">'467'</span>, <span class="hljs-string">'576'</span>, <span class="hljs-string">'572'</span>, <span class="hljs-string">'79'</span>, <span class="hljs-string">'92'</span>, <span class="hljs-string">'574'</span>, <span class="hljs-string">'573'</span>,
            <span class="hljs-string">'467'</span>, <span class="hljs-string">'576'</span>, <span class="hljs-string">'572'</span>, <span class="hljs-string">'79'</span>, <span class="hljs-string">'92'</span>, <span class="hljs-string">'574'</span>, <span class="hljs-string">'573'</span>,
            <span class="hljs-string">'467'</span>, <span class="hljs-string">'576'</span>, <span class="hljs-string">'572'</span>, <span class="hljs-string">'79'</span>, <span class="hljs-string">'92'</span>, <span class="hljs-string">'574'</span>, <span class="hljs-string">'573'</span>,
            <span class="hljs-string">'467'</span>, <span class="hljs-string">'576'</span>, <span class="hljs-string">'572'</span>, <span class="hljs-string">'79'</span>, <span class="hljs-string">'92'</span>, <span class="hljs-string">'574'</span>, <span class="hljs-string">'573'</span>,
            <span class="hljs-string">'467'</span>, <span class="hljs-string">'576'</span>, <span class="hljs-string">'572'</span>, <span class="hljs-string">'79'</span>, <span class="hljs-string">'92'</span>, <span class="hljs-string">'574'</span>, <span class="hljs-string">'573'</span>, ],
            <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'blue'</span>,
            <span class="hljs-attr">borderColor</span>: <span class="hljs-string">"#084de0"</span>
          }
        ]
      },
      <span class="hljs-attr">options</span>: {
        <span class="hljs-attr">aspectRatio</span>: <span class="hljs-number">3</span>
      }

    });
  }

}
</code></pre>
<p>Again, if you don’t know how this code works, you can always refer back to this <a target="_blank" href="https://www.freecodecamp.org/news/how-to-make-bar-and-line-charts-using-chartjs-in-angular/">Chart.js Tutorial</a>.</p>
<p>We need to add the HTML selector of the line-chart component to the <code>app.component.html</code> file. Remove the initial template code of Angular and add the following:</p>
<pre><code>&lt;app-line-chart&gt;&lt;/app-line-chart&gt;
</code></pre><p>Start the local development server using the following command:</p>
<pre><code class="lang-bash">ng serve -o
</code></pre>
<p> A browser window will open on http://localhost:4200/ and you can see your running Angular application.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/NotZoomLine.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>As you can see, in order to present the entire chart, this graph begins to skip some labels. It will seem more worse if the graph has more data.</p>
<p>Let's now add a zooming function to our chart so we can enlarge the data points for a closer look.</p>
<h2 id="heading-how-to-add-the-chartjs-zoom-plugin">How to Add the Chart.js Zoom Plugin</h2>
<p>Open the <code>line-chart.component.ts</code> file and import the chart.js Zoom plugin. Make sure you register it after importing it. The following code will show you how to do it:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> zoomPlugin <span class="hljs-keyword">from</span> <span class="hljs-string">'chartjs-plugin-zoom'</span>;
Chart.register(zoomPlugin);
</code></pre>
<p>Now that you have imported the plugin, in the Chart.js option we will add the plugin and enable the zoom on the wheel.</p>
<pre><code class="lang-js">options: {
        <span class="hljs-attr">aspectRatio</span>: <span class="hljs-number">3</span>,
        <span class="hljs-attr">plugins</span>: {
          <span class="hljs-attr">zoom</span>: {
            <span class="hljs-attr">zoom</span>: {
              <span class="hljs-attr">wheel</span>: {
                <span class="hljs-attr">enabled</span>: <span class="hljs-literal">true</span>,
              },
              <span class="hljs-attr">pinch</span>: {
                <span class="hljs-attr">enabled</span>: <span class="hljs-literal">true</span>
              },
              <span class="hljs-attr">mode</span>: <span class="hljs-string">'xy'</span>,
            }
          }
        }
      }
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ChartjsZoom.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>As you can see , now we can zoom in into the point we want to see closely. </p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>The full code for this Angular application is hosted on my <a target="_blank" href="https://github.com/SwatejPatil/ChartJs-Zoom-Plugin-Tutorial">GitHub</a>.</p>
<p>I hope you found this tutorial helpful.If you have any questions or comments, please do not hesitate to contact me on <a target="_blank" href="http://www.linkedin.com/in/swatejpatil/">LinkedIn</a>. I'll be more than pleased to assist you with your questions.</p>
<p>Thank you for reading!</p>
<p>Photo by <a href="https://unsplash.com/@isaacmsmith?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Isaac Smith</a> on <a href="https://unsplash.com/s/photos/line-chart?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to create serverless images using AWS lambda and ChartJS ]]>
                </title>
                <description>
                    <![CDATA[ By Martin van Vliet I’m working on a project to push sprint burndown information to distributed Scrum teams working in Slack. The entire application is serverless and running inside AWS. To generate burndown images I wanted to build a lambda function... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-serverless-images-using-aws-lambda-and-chartjs-3d18254b05ef/</link>
                <guid isPermaLink="false">66c351464f1fc448a367905f</guid>
                
                    <category>
                        <![CDATA[ AWS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ chartjs ]]>
                    </category>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ serverless ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 19 Nov 2018 21:40:57 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*UrOibhQ3ePj0qPlEQLsrhg.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Martin van Vliet</p>
<p>I’m working on a <a target="_blank" href="https://sprintlr.io">project to push sprint burndown information to distributed Scrum teams working in Slack</a>. The entire application is serverless and running inside AWS. To generate burndown images I wanted to build a lambda function that creates an image, then pushes it to an S3 bucket. In this article, I’ll describe the solution I came up with.</p>
<p>First, the charting library. After investigating a number of options, I settled on the use of the <a target="_blank" href="http://www.chartjs.org/">ChartJS library</a>. It’s a great charting library in Javascript that can render all sorts of graphs using the HTML5 canvas. Very flexible and configurable, too. But how do I run it, headless, inside of a lambda?</p>
<p>My first attempt was to create a simple lambda on my MacBook using three NPM packages: <strong>chart.js</strong> and two other libraries: <a target="_blank" href="https://www.npmjs.com/package/chartjs-node-canvas"><strong>chartjs-node-canvas</strong></a> and <a target="_blank" href="https://www.npmjs.com/package/canvas-prebuilt"><strong>canvas-prebuilt</strong></a>. The former is a wrapper around ChartJS that supports rendering charts in node. The latter is a prebuilt, native version of the HTML5 canvas that allows rendering a canvas headlessly without a browser. I built the lambda with the <a target="_blank" href="https://serverless.com/">serverless framework</a>, a very useful tool to create and deploy serverless applications.</p>
<p>Here’s my sample lambda:</p>
<p>My lambda generated a sample graph and all looked well when I ran it locally. On AWS, though, it failed. What gives?</p>
<p>As it turns out, ChartJS uses <em>native NPM modules</em> to render directly to the canvas. Obviously, the native modules I use on my MacBook don’t run inside of a lambda. So what to do?</p>
<p>To run native modules inside a lambda, I needed to use the native libraries that the <a target="_blank" href="https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html">lambda runtime environment</a> needs. So I fired up an instance of the lambda runtime AMI on EC2 and logged into it. There, I took the same lambda I built earlier and ran <strong>npm install</strong> on the EC2 instance. This downloads the native NPM libraries ChartJS needs but this time for the lambda runtime environment. With these libraries as part of the lambda function, ChartJS is able to render an image just as it normally would. Yay!</p>
<p>Contrary to normal NPM projects, I checked in the downloaded node modules into my git repository. When updating or building the graphing lambda, take care not to overwrite any of your dependencies. One side-effect of this approach is that I can no longer run the code on my local laptop to test.</p>
<p>Next up: store the image file generated in an S3 bucket. This was actually a fairly simple task with many code snippets available online to learn from. For completeness, here’s mine:</p>
<p>That’s it! Now you can generate images cheaply, on-demand!</p>
<p>Using native libraries is not without its problems though. I’ve had the entire setup work or fail depending on the exact version of <strong>canvas-prebuilt</strong> that I use. The current setup actually uses two. One transitive dependency via <strong>chartjs-node-canvas</strong> (2.0.0-alpha.14). One that I specify directly (1.6.5-prerelease.1). If I remove one of them the application fails to start due to some linking errors. YMMV.</p>
<p>I’ve put together a git repo with the sample code and binaries on it if you want to try it out. <a target="_blank" href="https://github.com/techfu-io/chartjs-lambda">Check out the source code</a>.</p>
<p><em>Martin works as VP Engineering for <a target="_blank" href="https://www.stackstate.com">StackState</a> where he and his teams are building a next-generation monitoring and AIOps product. As a side project, Martin is working on <a target="_blank" href="https://sprintlr.io">Sprintlr</a>, a tool to improve distributed Scrum teams, and <a target="_blank" href="https://techfu.io">techfu.io</a>, a tool to automatically assess technical talent.</em></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
