<?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[ charts - 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[ charts - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Wed, 27 May 2026 16:21:25 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/charts/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Build an Interactive University Ranking System Using React and Data Viz Tools ]]>
                </title>
                <description>
                    <![CDATA[ Hi! I'm Daria, and I'm a software engineering student with a keen interest in data visualization. I've been actively exploring various visualization tools through small pet projects, and I'd like to s ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-an-interactive-ranking-system-using-react-and-data-viz-tools/</link>
                <guid isPermaLink="false">69c44d7d10e664c5daefbf5c</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ #data visualisation ]]>
                    </category>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pivottable ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Daria Filozop ]]>
                </dc:creator>
                <pubDate>Wed, 25 Mar 2026 21:02:53 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/5f31dd6f-e199-4cf3-a76a-daf985a4e7d7.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Hi! I'm Daria, and I'm a software engineering student with a keen interest in data visualization. I've been actively exploring various visualization tools through small pet projects, and I'd like to share my latest demo with you.</p>
<p>In this tutorial, we'll build a project that displays the historical rankings of universities around the world. It's interesting to check out and analyze which institutions consistently maintain their top-tier positions and which ones experience significant movement up or down the rankings over the years.</p>
<p>While building the dashboard, we'll load and structure the dataset, display all important information in a pivot table, and then create charts to visualize the top 10 universities and their ranking trends over time.</p>
<p>Even though we'll walk through this specific example here, you can apply this same approach to many other datasets to build data viz dashboards.</p>
<h2 id="heading-what-well-cover">What We’ll Cover:</h2>
<ol>
<li><p><a href="#heading-tech-stack">Tech Stack</a></p>
</li>
<li><p><a href="#heading-flexmonster-pivot-table-setup">Flexmonster Pivot Table Setup</a></p>
</li>
<li><p><a href="#heading-loading-and-displaying-the-data">Loading and Displaying the Data</a></p>
</li>
<li><p><a href="#heading-creating-charts">Creating Charts</a></p>
</li>
<li><p><a href="#heading-adding-year-filtering-buttons">Adding Year Filtering Buttons</a></p>
</li>
<li><p><a href="#heading-styling">Styling</a></p>
</li>
</ol>
<h2 id="heading-tech-stack">Tech Stack</h2>
<p>Let’s talk about the tools we’ll be using so you know what you’ll need to have before following along.</p>
<p>First, we’ll use <a href="https://react.dev/"><strong>React</strong></a>, which is a popular JavaScript library for building interactive web interfaces. It helps create reusable components and manage data efficiently. According to <a href="https://survey.stackoverflow.co/2025/technology"><strong>the 2025 Stack Overflow Developer Survey</strong></a>, React is the second most popular web framework.</p>
<p>We’ll also use <a href="https://www.flexmonster.com/"><strong>Flexmonster Pivot Table</strong></a>, a web component for displaying data in a table format. In general, pivot tables are widely used for data visualization because they allow you to quickly group, aggregate, filter, and explore large datasets from different perspectives.</p>
<p>With Flexmonster, you can easily create reports and customize your information. It also integrates smoothly with almost all popular modern frameworks (like React, which we’re using here!).</p>
<p>Next, we have <a href="https://echarts.apache.org/en/index.html"><strong>ECharts</strong></a>, which complements the detailed data provided by the pivot table. It’s a powerful, open-source charting library that’ll give us the visual insights we need, offering over 20 chart types to effectively visualize historical university ranking trends.</p>
<p>Finally, we’ll use the <a href="https://www.kaggle.com/datasets/mylesoneill/world-university-rankings"><strong>World University Rankings Dataset</strong></a>. It contains data from three global rankings (<a href="https://www.timeshighereducation.com/world-university-rankings"><strong>THE</strong></a>, <a href="https://www.shanghairanking.com/"><strong>ARWU</strong></a>, and <a href="https://cwur.org/"><strong>CWUR</strong></a>), providing information about well-known universities from 2012 to 2015 for detailed analytical research. The dataset size is 186.38 kB.</p>
<p>As a small disclaimer, this tutorial uses React, so some familiarity with it will help you follow along. But actually, you can use any other framework that's convenient for you. Flexmonster Pivot Table offers <a href="https://www.flexmonster.com/doc/available-tutorials-integration/">many integrations with popular frameworks</a>, including Angular, Vue, Svelte, and more.</p>
<p>When we’re done with the project, we’ll get an interactive dashboard like this:</p>
<img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXe49D_OM3dE-Q8bLmhsYyejXE0CF0PZTZeXhm_3vJfCDOO8JmQMCbLk8x_d_FBAWJjcsKFd0Mm3uTntW2zcp6NYGMXUVnyfjpwWI0izXInLvUhcHgtoSPoCt4EFtHpVm5WOzP-khwnxPryrgO1FjEUqWXSEWtI?key=44ff_UPj0hN24gZs8A8Dew" alt="Interactive dashboard for visualizing university ratings" width="600" height="400" loading="lazy">

<p>So now that you’re familiar with the tools, let’s get started!</p>
<h2 id="heading-flexmonster-pivot-table-setup">Flexmonster Pivot Table Setup</h2>
<p>To get started, you’ll need to integrate Flexmonster into your React project. I’ll walk you through how it works with React, but you can use other frameworks, too. You can find complete instructions in <a href="https://www.flexmonster.com/doc/available-tutorials-integration/"><strong>the Flexmonster docs</strong></a>.</p>
<p>First, create a React application using Vite:</p>
<pre><code class="language-shell">npm create vite@latest flexmonster-project -- --template react
</code></pre>
<p>Also, don’t forget to install npm dependencies:</p>
<pre><code class="language-shell">cd flexmonster-project
npm install
</code></pre>
<p>Next, we’ll install the Flexmonster wrapper for React. To do this, use this command:</p>
<pre><code class="language-shell">npm install -g flexmonster-cli
flexmonster add react-flexmonster
</code></pre>
<p>Then add Flexmonster styles and the component to your App.jsx file:</p>
<pre><code class="language-javascript">import FlexmonsterReact from "react-flexmonster";
import "flexmonster/flexmonster.css";
</code></pre>
<h2 id="heading-loading-and-displaying-the-data">Loading and Displaying the Data</h2>
<p>Now it’s time to create a report object. This is a configuration that defines how the pivot table should load and display data. It describes how the fields should be interpreted, and how the table should organize and aggregate the information.</p>
<p>The first part of this is <code>dataSource</code>. It defines where the data comes from and how it should be read by the pivot table (format of the dataset, location of the file, and the structure of the fields that will be used).</p>
<p>In our case, we'll load a CSV file that contains the university rankings dataset and define the fields that will appear in the pivot table:</p>
<pre><code class="language-javascript">&nbsp;&nbsp;const report = {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataSource: {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: "csv",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename: "/data/world-university-rankings.csv",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mapping: {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;world_rank: { type: "number", caption: "World Rank" },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;institution: { type: "string" },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;country: { type: "string" },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;score: { type: "number", caption: "Score" },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;year: { type: "number", caption: "Year" },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
…
}
</code></pre>
<p>The second part is the slice section. It defines which subset of the dataset will be displayed and how it should be organized. There, you'll render a table by setting measures, rows, and columns. You can also set the <code>flatOrder</code> property, where you define the order of the fields in the flat form.</p>
<p>You can find more detailed information about the <a href="https://www.flexmonster.com/api/slice-object/"><strong>Slice Object here</strong></a>. There are lots of interesting functional possibilities!</p>
<pre><code class="language-javascript">const report = {
…
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slice: {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rows: [{ uniqueName: "institution" }],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;columns: [{ uniqueName: "[Measures]" }],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;measures: [
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ uniqueName: "world_rank", aggregation: "min" },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uniqueName: "year",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aggregation: "none",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filter: { members: [`year.[${selectedYear}]`] },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flatOrder: ["institution", "world_rank"],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options: {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grid: { type: "flat", showGrandTotals: "off" },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;};
</code></pre>
<p>You’ll also need to include Flexmonster inside the React component. In this example, we'll include the <code>FlexmonsterReact</code> tag in JSX and pass the report object (which we defined earlier) as a property. You can do that with this code snippet:</p>
<pre><code class="language-javascript">&lt;FlexmonsterReact
&nbsp;&nbsp;&nbsp;&nbsp;ref={pivotRef}
&nbsp;&nbsp;&nbsp;&nbsp;toolbar={true}
&nbsp;&nbsp;&nbsp;&nbsp;report={report}
&nbsp;&nbsp;&nbsp;&nbsp;width="100%"
&nbsp;&nbsp;&nbsp;&nbsp;height="100%"
&nbsp;&nbsp;&nbsp;&nbsp;reportcomplete={createChart}
/&gt;
</code></pre>
<p>As a result, we've got pivot table with all the info about the universities:</p>
<img src="https://cdn.hashnode.com/uploads/covers/690da61ea51d4259bd4a849b/ab96b9fc-c322-482c-b95a-33d17887a94e.png" alt="Pivot table with university data and rankings" style="display:block;margin:0 auto" width="2792" height="942" loading="lazy">

<h2 id="heading-creating-charts">Creating Charts</h2>
<p>For some users, charts are easier to understand than tables, so let’s also create some now. I decided to display the top 10 universities in the world using bar charts. Bar charts are commonly used to compare values between categories, and they're quite useful for highlighting rankings or top performers.</p>
<p>We'll use ECharts here, but you can easily integrate Flexmonster with <a href="https://www.flexmonster.com/doc/available-tutorials-charts/"><strong>the most convenient chart library for you</strong></a>.</p>
<p>As a first step, make sure to install ECharts in your project:</p>
<pre><code class="language-shell">npm install echarts
</code></pre>
<p>To prepare our data for display in the charts, we’ll create the <code>prepareData()</code> function. This function picks out the universities' names and their ranks, removes any invalid data, sorts it by rank, and keeps only the top 10. It returns two arrays: one with the names (for chart labels) and another with the rankings (for chart values):</p>
<pre><code class="language-javascript">const prepareData = (rawData) =&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;const rows = rawData.data
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map((r) =&gt; ({ name: r.r0, rank: r.v0 }))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filter((r) =&gt; r.name &amp;&amp; !isNaN(r.rank))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.sort((a, b) =&gt; a.rank - b.rank)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.slice(0, 10);
&nbsp;&nbsp;&nbsp;&nbsp;return {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;labels: rows.map((r) =&gt; r.name).reverse(),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values: rows.map((r) =&gt; r.rank).reverse(),
&nbsp;&nbsp;&nbsp;&nbsp;};
};
</code></pre>
<p>Next, we'll set up chart options. You’ll need to decide in what form your dataset will be displayed: in this example, we'll choose a bar chart. We'll also set the title (here "Top 10 Universities by World Rank"), x-axis (shows the rank numbers) and y-axis (shows universities names), and tooltip, which shows info when you hover over a bar.</p>
<p>Also, don't forget to initialize the chart and apply these options. You can do all this with this code snippet:</p>
<pre><code class="language-javascript">const drawChart = (rawData) =&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;const { labels, values } = prepareData(rawData);
&nbsp;&nbsp;&nbsp;&nbsp;const options = {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;title: {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text: "Top 10 Universities by World Rank",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;left: "center",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textStyle: { fontSize: 20, fontWeight: "bold" },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tooltip: { trigger: "axis", axisPointer: { type: "shadow" } },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xAxis: { type: "value", name: "Rank" },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yAxis: { type: "category", data: labels },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;series: [{ type: "bar", data: values, barMaxWidth: 30 }],
&nbsp;&nbsp;&nbsp;&nbsp;};
&nbsp;&nbsp;&nbsp;&nbsp;chartInstance = echarts.init(chartRef.current);
&nbsp;&nbsp;&nbsp;&nbsp;chartInstance.setOption(options);
};
</code></pre>
<p>Also, an important part of chart configuration is the <code>updateCharts()</code> function, which redraws the chart when needed:</p>
<pre><code class="language-javascript">const updateChart = (rawData) =&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;if (chartInstance) chartInstance.dispose();
&nbsp;&nbsp;&nbsp;&nbsp;drawChart(rawData);
};
</code></pre>
<p>So now we can see the charts we've created! This might look a bit basic and really hard to read, but don’t worry – we’ll make it look nicer and easier to understand in a later section.</p>
<img src="https://cdn.hashnode.com/uploads/covers/690da61ea51d4259bd4a849b/65cf5e06-f0dd-4d45-b1eb-e78797b44d2b.png" alt="Bar charts showing the top 10 universities by world rank" style="display:block;margin:0 auto" width="2642" height="604" loading="lazy">

<h2 id="heading-adding-year-filtering-buttons">Adding Year Filtering Buttons</h2>
<p>You might have noticed that the dataset contains ratings for different years (from 2012 to 2015). It would be great if we could use the whole dataset, not just information for one year.</p>
<p>To manage this, we'll create filtering buttons for each year to provide more straightforward navigation.</p>
<p>First, we’ll add a div element which contains buttons for each year:</p>
<pre><code class="language-javascript">&lt;div className="years-container"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;{[2012, 2013, 2014, 2015].map((year) =&gt; (
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;button
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key={year}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onClick={() =&gt; handleYearChange(year)}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;className={`year-btn ${selectedYear === year ? "active" : ""}`}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{year}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/button&gt;
&nbsp;&nbsp;&nbsp;&nbsp;))}
&lt;/div&gt;
</code></pre>
<p>In the above code snippet, you can see that each button calls <code>handleYearChange(year)</code> when being clicked. Let’s now examine what this handler does:</p>
<pre><code class="language-javascript">const handleYearChange = (year) =&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;setSelectedYear(year);

&nbsp;&nbsp;&nbsp;&nbsp;const pivot = pivotRef.current?.flexmonster;
&nbsp;&nbsp;&nbsp;&nbsp;if (pivot) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const newReport = {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...report,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slice: {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...report.slice,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;measures: [
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ uniqueName: "world_rank", aggregation: "min" },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uniqueName: "year",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aggregation: "none",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filter: { members: [`year.[${year}]`] },
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pivot.setReport(newReport);
&nbsp;&nbsp;&nbsp;&nbsp;}
};&nbsp;
</code></pre>
<p>This function modifies the pivot table report to filter by that year, and refreshes the table. This way, clicking a button instantly shows only the data for the chosen year.</p>
<p>And now we've got buttons which display years from our dataset:</p>
<img src="https://cdn.hashnode.com/uploads/covers/690da61ea51d4259bd4a849b/184608a5-6207-44d8-b5fe-80fd355201c0.png" alt="Buttons to filter data by year" style="display:block;margin:0 auto" width="571" height="99" loading="lazy">

<h2 id="heading-styling">Styling</h2>
<p>Finally, here’s my favorite part of every project: customization! I love experimenting with different styles and choosing the most appropriate one.</p>
<p>For this dashboard, I chose light violet and white colors to make the interface clean and easy to read. I personally associate the university vibe with these colors, so I think they match our dashboard perfectly. So let's go step by step through it.</p>
<p>The main container defines the overall layout and background. It centers the content on the page, adds some spacing, and sets the base font and colors.</p>
<pre><code class="language-javascript">.app-container {
    min-height: 100vh;
    width: 100vw;
    padding: 32px;
    display: flex;
    flex-direction: column;
    align-items: center;

    background: #ebe6f7;
    font-family: "Inter", system-ui, -apple-system, sans-serif;
    color: #1b2a4e;
}
</code></pre>
<p>Next, we'll style the year filter buttons. They have rounded corners, a soft shadow, and a small hover effect so the interface feels interactive. The active button gets a violet background so it’s easy to see which year is selected.</p>
<pre><code class="language-javascript">.year-btn {
    padding: 10px 20px;
    background: #ffffff;
    border: 1px solid #cdd2e0;
    border-radius: 8px;
    cursor: pointer;
}

.year-btn:hover {
    background: #e1dffa;
}

.year-btn.active {
    background: #6c5ce7;
    color: white;
}
</code></pre>
<p>Also, the pivot table and the chart are located inside simple containers with rounded corners and slight shadows. This visually separates the components and keeps the layout logically structured.</p>
<pre><code class="language-javascript">.pivot-container,
.chart-container {
    width: 90%;
    background: #ffffff;
    border-radius: 12px;
    padding: 14px;
    box-shadow: 0 6px 20px rgba(15, 35, 95, 0.12);
}

.pivot-container {
    height: 56vh;
    margin-bottom: 24px;
}

.chart-container {
    height: 40vh;
}
</code></pre>
<p>And now, here's the result of our work!</p>
<img src="https://cdn.hashnode.com/uploads/covers/690da61ea51d4259bd4a849b/ed35dbdd-9c5d-4f7e-b6e1-4f0546a0d660.png" alt="Universities ranking pivot table with interactive year filters" style="display:block;margin:0 auto" width="2826" height="1130" loading="lazy">

<img src="https://cdn.hashnode.com/uploads/covers/690da61ea51d4259bd4a849b/1a0b0f1c-9c26-40e2-8c28-bd45517ea145.png" alt="Charts showing the top 10 universities by world rank" style="display:block;margin:0 auto" width="2826" height="698" loading="lazy">

<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>In this tutorial, we've build an interactive dashboard for visualizing the world's top university rankings over the years. You learned how to load and show data in pivot table in a convenient and compact way, make bar charts to show comparative data, and add buttons to filter info by year.</p>
<p>You can now use these skills to visualize other datasets and play with different charts and customization options.</p>
<p>If you want to look closer at my code and get detailed styling code, you can check out my GitHub: <a href="https://github.com/filozopdasha/universities-dashboard"><strong>https://github.com/filozopdasha/universities-dashboard</strong></a></p>
<p>I would be delighted to hear your thoughts about this small project. I’m curious to see what you build!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use Chart js for Interactive Data Visualization ]]>
                </title>
                <description>
                    <![CDATA[ Data surrounds us, but its raw form can be overwhelming and difficult to interpret. That's where data visualization comes in. It can help you take your data and turn it into charts and graphs that make sense at a glance. Among the many data visualiza... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-chart-js-for-interactive-data-visualization/</link>
                <guid isPermaLink="false">66e2e5406bc43703e551036f</guid>
                
                    <category>
                        <![CDATA[ #data visualisation ]]>
                    </category>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Oluwadamisi Samuel ]]>
                </dc:creator>
                <pubDate>Thu, 12 Sep 2024 12:57:36 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1725821135508/79767987-8760-4626-a924-212e402b051f.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Data surrounds us, but its raw form can be overwhelming and difficult to interpret. That's where data visualization comes in. It can help you take your data and turn it into charts and graphs that make sense at a glance.</p>
<p>Among the many data visualization libraries available, Chart.js stands out for its simplicity, flexibility, and interactivity.</p>
<p>This guide is like a roadmap to making charts with Chart.js. It doesn't matter if you're a coding expert or just getting started – I'll show you everything you need to know. We'll break down the basics of Chart.js, show you different types of charts (like bars and lines), and teach you how to make them look great and even respond to clicks.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-what-is-chartjs">What is Chart.js?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-benefits-of-using-chartjs-for-data-visualization">Benefits of Using Chart.js for Data Visualization</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-get-started-with-chartjs">How to Get Started with Chart.js</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-chart-types-in-chartjs">Chart Types in Chart.js</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-customization-and-interactivity-in-chartjs">Customization and Interactivity in Chart.js</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-work-with-data-in-chartjs">How to Work with Data in Chart.js</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-advanced-chartjs-features">Advanced Chart.js Features</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-best-practices-for-chart-design">Best Practices for Chart Design</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-what-is-chartjs">What is Chart.js?</h2>
<p>Chart.js is a popular open-source JavaScript library that allows you to create beautiful and interactive charts on the web. It's easy to use and supports various types of charts, such as line, bar, pie, radar, and more.</p>
<p>Chart.js is highly customizable, allowing you to modify the appearance and behavior of the charts to fit your specific needs. It uses the HTML5 <code>&lt;canvas&gt;</code> element to render the charts, making it compatible with modern web browsers.</p>
<h2 id="heading-benefits-of-using-chartjs-for-data-visualization">Benefits of Using Chart.js for Data Visualization</h2>
<p>Using Chart.js for data visualization offers numerous benefits that make it an excellent choice for developers and non-developers alike. The primary advantages include:</p>
<h3 id="heading-easy-to-use">Easy to Use</h3>
<p>Chart.js is known for its simplicity and ease of use. Even if you're new to JavaScript, you can quickly create and customize charts with minimal code.</p>
<p>The library's clear and concise documentation provides step-by-step instructions and examples, making it accessible for beginners and experienced developers alike.</p>
<h3 id="heading-versatile-chart-types">Versatile Chart Types</h3>
<p>Chart.js supports a wide range of chart types, including line, bar, pie, doughnut, radar, polar area, bubble, and scatter charts. This versatility allows you to choose the best chart type to represent your data effectively.</p>
<p>Whether you need to show trends over time, compare different categories, or display proportions, Chart.js has you covered.</p>
<h3 id="heading-highly-customizable">Highly Customizable</h3>
<p>One of the standout features of Chart.js is its high level of customization. You can tweak almost every aspect of your charts, from colors, fonts, and sizes to tooltips, legends, and animations. This flexibility ensures that your charts match the look and feel of your website or application perfectly.</p>
<h3 id="heading-responsive-design">Responsive Design</h3>
<p>Chart.js charts are responsive by default, meaning they automatically adjust their size and layout based on the screen size. This is particularly important in today's world, where users access websites and applications from various devices, including desktops, tablets, and smartphones. With Chart.js, you can be confident that your charts will look great on any device.</p>
<h3 id="heading-interactive-features">Interactive Features</h3>
<p>Interactivity is a key component of modern data visualization, and Chart.js excels in this area. Charts created with Chart.js can include interactive features like tooltips, which display detailed information when users hover over data points, and clickable legends, which allow users to toggle the visibility of different datasets. These features make your charts more engaging and informative.</p>
<h3 id="heading-lightweight-and-fast">Lightweight and Fast</h3>
<p>Chart.js is a lightweight library, meaning it doesn't add significant load time to your website or application. Despite its small size, it is highly efficient and capable of rendering complex charts quickly. This performance is crucial for maintaining a smooth user experience, especially when dealing with large datasets.</p>
<h3 id="heading-open-source-and-community-support">Open Source and Community Support</h3>
<p>As an open-source project, Chart.js is free to use and benefits from a vibrant community of developers who contribute to its improvement and extension.</p>
<p>The active community support means you can find numerous plugins, extensions, and third-party integrations to enhance the functionality of Chart.js. You can also rely on community forums and resources for troubleshooting and inspiration.</p>
<h3 id="heading-compatibility-with-modern-web-technologies">Compatibility with Modern Web Technologies</h3>
<p>Chart.js leverages the HTML5 <code>&lt;canvas&gt;</code> element to render charts, making it compatible with modern web browsers. This compatibility ensures that your charts will display correctly across different platforms and devices.</p>
<p>Also, you can easily integrate Chart.js with popular JavaScript frameworks and libraries, such as React, Angular, and Vue.js, allowing you to incorporate charts seamlessly into your projects.</p>
<h3 id="heading-accessibility-features">Accessibility Features</h3>
<p>Accessibility is a crucial consideration in web development, and Chart.js includes features to support this. You can add alternative text descriptions and ARIA labels to your charts, making them more accessible to users with disabilities.</p>
<p>This commitment to accessibility helps you create inclusive data visualizations that can be enjoyed by a wider audience.</p>
<h3 id="heading-continuous-improvement">Continuous Improvement</h3>
<p>The development team behind Chart.js is committed to continuously improving the library. Regular updates bring new features, performance enhancements, and bug fixes, ensuring that Chart.js remains a cutting-edge tool for data visualization.</p>
<h2 id="heading-how-to-get-started-with-chartjs">How to Get Started with Chart.js</h2>
<p>Creating interactive and visually appealing charts is straightforward thanks to Chart.js. In this section, I'll walk you through the initial steps to get Chart.js up and running in your project, including setting up the library and creating your first chart.</p>
<h3 id="heading-set-up-your-project">Set Up Your Project</h3>
<p>To get started with Chart.js, you need to include the library in your project. You can do this by either downloading the Chart.js library or linking to it via a Content Delivery Network (CDN). Using a CDN is often the easiest way to get started.</p>
<h4 id="heading-include-chartjs-via-cdn">Include Chart.js via CDN</h4>
<p>Add the following <code>&lt;script&gt;</code> tag to the <code>&lt;head&gt;</code> or <code>&lt;body&gt;</code> section of your HTML file:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Chart.js Example<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/chart.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- Chart container --&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> <span class="hljs-attr">width</span>=<span class="hljs-string">"400"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">canvas</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- Your JavaScript code will go here --&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="heading-create-your-first-chart">Create Your First Chart</h3>
<p>Next, you need to create a <code>&lt;canvas&gt;</code> element in your HTML file where the chart will be rendered. This element acts as a container for the chart. In the example above, we already added a <code>&lt;canvas&gt;</code> element with the <code>id</code> of <code>myChart</code>.</p>
<h3 id="heading-write-the-javascript-code">Write the JavaScript Code</h3>
<p>Now, let's write some JavaScript to create a basic chart. Place the following script in the <code>&lt;body&gt;</code> section of your HTML file, below the <code>&lt;canvas&gt;</code> element, or in an external JavaScript file:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
    <span class="hljs-comment">// Get the context of the canvas element we want to select</span>
    <span class="hljs-keyword">var</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);

    <span class="hljs-comment">// Create a new Chart object</span>
    <span class="hljs-keyword">var</span> myChart = <span class="hljs-keyword">new</span> Chart(ctx, {
        <span class="hljs-attr">type</span>: <span class="hljs-string">'bar'</span>, <span class="hljs-comment">// The type of chart we want to create</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-comment">// Labels for the chart</span>
            <span class="hljs-attr">datasets</span>: [{
                <span class="hljs-attr">label</span>: <span class="hljs-string">'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-comment">// Data points for the chart</span>
                <span class="hljs-attr">backgroundColor</span>: [
                    <span class="hljs-string">'rgba(255, 99, 132, 0.2)'</span>,
                    <span class="hljs-string">'rgba(54, 162, 235, 0.2)'</span>,
                    <span class="hljs-string">'rgba(255, 206, 86, 0.2)'</span>,
                    <span class="hljs-string">'rgba(75, 192, 192, 0.2)'</span>,
                    <span class="hljs-string">'rgba(153, 102, 255, 0.2)'</span>,
                    <span class="hljs-string">'rgba(255, 159, 64, 0.2)'</span>
                ],
                <span class="hljs-attr">borderColor</span>: [
                    <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
                    <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                    <span class="hljs-string">'rgba(255, 206, 86, 1)'</span>,
                    <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
                    <span class="hljs-string">'rgba(153, 102, 255, 1)'</span>,
                    <span class="hljs-string">'rgba(255, 159, 64, 1)'</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> <span class="hljs-comment">// Start the y-axis at 0</span>
                }
            }
        }
    });
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>Alright, let's understand what's going on in this code:</p>
<ol>
<li><p><strong>Get the Canvas Context</strong>: The first line of the script selects the <code>&lt;canvas&gt;</code> element by its <code>id</code> and gets its 2D drawing context. This context is necessary for creating the chart.</p>
<pre><code class="lang-javascript"> <span class="hljs-keyword">var</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
</code></pre>
</li>
<li><p><strong>Create a New Chart</strong>: The <code>Chart</code> constructor creates a new chart. You need to pass two arguments: the context and a configuration object.</p>
<pre><code class="lang-javascript"> <span class="hljs-keyword">var</span> myChart = <span class="hljs-keyword">new</span> Chart(ctx, {
     <span class="hljs-attr">type</span>: <span class="hljs-string">'bar'</span>, <span class="hljs-comment">// The type of chart we want to create</span>
</code></pre>
</li>
<li><p><strong>Data Object</strong>: The <code>data</code> property of the configuration object defines the chart's data and labels. In this example, we use an array of colors as labels and an array of numbers as data points.</p>
<pre><code class="lang-javascript"> data: {
     <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">'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>],
</code></pre>
</li>
<li><p><strong>Styling</strong>: The <code>backgroundColor</code> and <code>borderColor</code> properties specify the colors for the bars and their borders. The <code>borderWidth</code> sets the width of the borders.</p>
<pre><code class="lang-javascript"> backgroundColor: [
     <span class="hljs-string">'rgba(255, 99, 132, 0.2)'</span>,
     <span class="hljs-string">'rgba(54, 162, 235, 0.2)'</span>,
     <span class="hljs-string">'rgba(255, 206, 86, 0.2)'</span>,
     <span class="hljs-string">'rgba(75, 192, 192, 0.2)'</span>,
     <span class="hljs-string">'rgba(153, 102, 255, 0.2)'</span>,
     <span class="hljs-string">'rgba(255, 159, 64, 0.2)'</span>
 ],
 <span class="hljs-attr">borderColor</span>: [
     <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
     <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
     <span class="hljs-string">'rgba(255, 206, 86, 1)'</span>,
     <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
     <span class="hljs-string">'rgba(153, 102, 255, 1)'</span>,
     <span class="hljs-string">'rgba(255, 159, 64, 1)'</span>
 ],
 <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">1</span>
</code></pre>
</li>
<li><p><strong>Options Object</strong>: The <code>options</code> property contains configuration options for the chart. In this example, we set the <code>beginAtZero</code> option to <code>true</code> to start the y-axis at 0.</p>
<pre><code class="lang-javascript"> options: {
     <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>
</li>
</ol>
<h3 id="heading-view-your-chart">View Your Chart</h3>
<p>Once you've added the code, open the HTML file in a web browser. You should see a bar chart displaying the data you provided.</p>
<div class="embed-wrapper"><iframe height="300" style="width:100%" src="https://codepen.io/joanayebola/embed/YzowGeQ?default-tab=html%2Cresult" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/joanayebola/pen/YzowGeQ">
  Basic Chart.js example</a> by joan? (<a href="https://codepen.io/joanayebola">@joanayebola</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<p>Congratulations, you've created your first chart with Chart.js!</p>
<p>Feel free to experiment with different chart types, data, and customization options to explore the full potential of Chart.js.</p>
<h2 id="heading-chart-types-in-chartjs">Chart Types in Chart.js</h2>
<p>Chart.js supports a variety of chart types, each designed to visualize data in different ways. Below are some of the most commonly used chart types available in Chart.js:</p>
<h3 id="heading-1-line-chart">1. Line Chart</h3>
<p>A line chart is used to show trends over time or to demonstrate continuous data. It's effective for displaying data that changes continuously over a period. Here's an example of a simple line chart:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
    <span class="hljs-keyword">var</span> lineChart = <span class="hljs-keyword">new</span> Chart(ctx, {
      <span class="hljs-attr">type</span>: <span class="hljs-string">'line'</span>,
      <span class="hljs-attr">data</span>: {
        <span class="hljs-attr">labels</span>: [<span class="hljs-string">'January'</span>, <span class="hljs-string">'February'</span>, <span class="hljs-string">'March'</span>, <span class="hljs-string">'April'</span>, <span class="hljs-string">'May'</span>, <span class="hljs-string">'June'</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-number">30</span>, <span class="hljs-number">45</span>, <span class="hljs-number">60</span>, <span class="hljs-number">35</span>, <span class="hljs-number">50</span>, <span class="hljs-number">40</span>],
          <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
          <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">2</span>,
          <span class="hljs-attr">fill</span>: <span class="hljs-literal">false</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>
<div class="embed-wrapper"><iframe height="300" style="width:100%" src="https://codepen.io/joanayebola/embed/eYwJdKY?default-tab=html%2Cresult" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/joanayebola/pen/eYwJdKY">
  Line Chart</a> by joan? (<a href="https://codepen.io/joanayebola">@joanayebola</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<h3 id="heading-2-bar-chart">2. Bar Chart</h3>
<p>A bar chart is used to compare different categories of data. It's ideal for showing discrete data points and comparing magnitudes between categories. Here's how you can create one:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
<span class="hljs-keyword">var</span> barChart = <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>: {
        <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">'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">backgroundColor</span>: [
                <span class="hljs-string">'rgba(255, 99, 132, 0.2)'</span>,
                <span class="hljs-string">'rgba(54, 162, 235, 0.2)'</span>,
                <span class="hljs-string">'rgba(255, 206, 86, 0.2)'</span>,
                <span class="hljs-string">'rgba(75, 192, 192, 0.2)'</span>,
                <span class="hljs-string">'rgba(153, 102, 255, 0.2)'</span>,
                <span class="hljs-string">'rgba(255, 159, 64, 0.2)'</span>
            ],
            <span class="hljs-attr">borderColor</span>: [
                <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
                <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                <span class="hljs-string">'rgba(255, 206, 86, 1)'</span>,
                <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
                <span class="hljs-string">'rgba(153, 102, 255, 1)'</span>,
                <span class="hljs-string">'rgba(255, 159, 64, 1)'</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>
<div class="embed-wrapper"><iframe height="300" style="width:100%" src="https://codepen.io/joanayebola/embed/BagjLOO?default-tab=html%2Cresult" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/joanayebola/pen/BagjLOO">
  Bar Chart</a> by joan? (<a href="https://codepen.io/joanayebola">@joanayebola</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<h3 id="heading-3-pie-chart">3. Pie Chart</h3>
<p>A pie chart is used to show proportions or percentages of a whole. It's effective for illustrating how parts contribute to the whole. Here's what one looks like in code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
<span class="hljs-keyword">var</span> pieChart = <span class="hljs-keyword">new</span> Chart(ctx, {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'pie'</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">'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">backgroundColor</span>: [
                <span class="hljs-string">'rgba(255, 99, 132, 0.2)'</span>,
                <span class="hljs-string">'rgba(54, 162, 235, 0.2)'</span>,
                <span class="hljs-string">'rgba(255, 206, 86, 0.2)'</span>,
                <span class="hljs-string">'rgba(75, 192, 192, 0.2)'</span>,
                <span class="hljs-string">'rgba(153, 102, 255, 0.2)'</span>,
                <span class="hljs-string">'rgba(255, 159, 64, 0.2)'</span>
            ],
            <span class="hljs-attr">borderColor</span>: [
                <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
                <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                <span class="hljs-string">'rgba(255, 206, 86, 1)'</span>,
                <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
                <span class="hljs-string">'rgba(153, 102, 255, 1)'</span>,
                <span class="hljs-string">'rgba(255, 159, 64, 1)'</span>
            ],
            <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">1</span>
        }]
    },
    <span class="hljs-attr">options</span>: {
        <span class="hljs-attr">responsive</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-attr">plugins</span>: {
            <span class="hljs-attr">legend</span>: {
                <span class="hljs-attr">position</span>: <span class="hljs-string">'top'</span>,
            },
            <span class="hljs-attr">tooltip</span>: {
                <span class="hljs-attr">callbacks</span>: {
                    <span class="hljs-attr">label</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">tooltipItem</span>) </span>{
                        <span class="hljs-keyword">return</span> tooltipItem.label + <span class="hljs-string">': '</span> + tooltipItem.raw.toFixed(<span class="hljs-number">2</span>);
                    }
                }
            }
        }
    }
});
</code></pre>
<div class="embed-wrapper"><iframe height="300" style="width:100%" src="https://codepen.io/joanayebola/embed/vYqLXQW?default-tab=html%2Cresult" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/joanayebola/pen/vYqLXQW">
  Pie Chart</a> by joan? (<a href="https://codepen.io/joanayebola">@joanayebola</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<h3 id="heading-4-doughnut-chart">4. Doughnut Chart</h3>
<p>A doughnut chart is similar to a pie chart but has a hollow center. It's useful for comparing proportions while also displaying total values.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
<span class="hljs-keyword">var</span> doughnutChart = <span class="hljs-keyword">new</span> Chart(ctx, {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'doughnut'</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">'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">backgroundColor</span>: [
                <span class="hljs-string">'rgba(255, 99, 132, 0.2)'</span>,
                <span class="hljs-string">'rgba(54, 162, 235, 0.2)'</span>,
                <span class="hljs-string">'rgba(255, 206, 86, 0.2)'</span>,
                <span class="hljs-string">'rgba(75, 192, 192, 0.2)'</span>,
                <span class="hljs-string">'rgba(153, 102, 255, 0.2)'</span>,
                <span class="hljs-string">'rgba(255, 159, 64, 0.2)'</span>
            ],
            <span class="hljs-attr">borderColor</span>: [
                <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
                <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                <span class="hljs-string">'rgba(255, 206, 86, 1)'</span>,
                <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
                <span class="hljs-string">'rgba(153, 102, 255, 1)'</span>,
                <span class="hljs-string">'rgba(255, 159, 64, 1)'</span>
            ],
            <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">1</span>
        }]
    },
    <span class="hljs-attr">options</span>: {
        <span class="hljs-attr">responsive</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-attr">plugins</span>: {
            <span class="hljs-attr">legend</span>: {
                <span class="hljs-attr">position</span>: <span class="hljs-string">'top'</span>,
            },
            <span class="hljs-attr">tooltip</span>: {
                <span class="hljs-attr">callbacks</span>: {
                    <span class="hljs-attr">label</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">tooltipItem</span>) </span>{
                        <span class="hljs-keyword">return</span> tooltipItem.label + <span class="hljs-string">': '</span> + tooltipItem.raw.toFixed(<span class="hljs-number">2</span>);
                    }
                }
            }
        }
    }
});
</code></pre>
<div class="embed-wrapper"><iframe height="300" style="width:100%" src="https://codepen.io/joanayebola/embed/VwJeKgM?default-tab=html%2Cresult" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/joanayebola/pen/VwJeKgM">
  Doughnut Chart</a> by joan? (<a href="https://codepen.io/joanayebola">@joanayebola</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<h3 id="heading-5-scatter-charts">5. Scatter Charts</h3>
<p>A scatter chart is used to display relationships between two or more variables. It's effective for showing correlations and distributions of data points.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
<span class="hljs-keyword">var</span> scatterChart = <span class="hljs-keyword">new</span> Chart(ctx, {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'scatter'</span>,
    <span class="hljs-attr">data</span>: {
        <span class="hljs-attr">datasets</span>: [{
            <span class="hljs-attr">label</span>: <span class="hljs-string">'Scatter Dataset'</span>,
            <span class="hljs-attr">data</span>: [
                { <span class="hljs-attr">x</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">20</span> },
                { <span class="hljs-attr">x</span>: <span class="hljs-number">15</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">25</span> },
                { <span class="hljs-attr">x</span>: <span class="hljs-number">7</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">10</span> },
                { <span class="hljs-attr">x</span>: <span class="hljs-number">12</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">18</span> },
                { <span class="hljs-attr">x</span>: <span class="hljs-number">20</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">30</span> }
            ],
            <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'rgba(54, 162, 235, 0.5)'</span>,
            <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(54, 162, 235, 1)'</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">x</span>: {
                <span class="hljs-attr">type</span>: <span class="hljs-string">'linear'</span>, <span class="hljs-comment">// Scatter charts support only 'linear' scale type for x-axis</span>
                <span class="hljs-attr">position</span>: <span class="hljs-string">'bottom'</span>
            },
            <span class="hljs-attr">y</span>: {
                <span class="hljs-attr">type</span>: <span class="hljs-string">'linear'</span>, <span class="hljs-comment">// Scatter charts support only 'linear' scale type for y-axis</span>
                <span class="hljs-attr">position</span>: <span class="hljs-string">'left'</span>
            }
        }
    }
});
</code></pre>
<div class="embed-wrapper"><iframe height="300" style="width:100%" src="https://codepen.io/joanayebola/embed/dyBGOPJ?default-tab=html%2Cresult" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/joanayebola/pen/dyBGOPJ">
  Scatter Chart</a> by joan? (<a href="https://codepen.io/joanayebola">@joanayebola</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<h3 id="heading-6-bubble-charts">6. Bubble Charts</h3>
<p>A bubble chart is similar to a scatter chart but uses bubble-like markers to represent data points. It's useful for comparing relationships between variables and showing data distribution with sizes of bubbles.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
<span class="hljs-keyword">var</span> bubbleChart = <span class="hljs-keyword">new</span> Chart(ctx, {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'bubble'</span>,
    <span class="hljs-attr">data</span>: {
        <span class="hljs-attr">datasets</span>: [{
            <span class="hljs-attr">label</span>: <span class="hljs-string">'Bubble Dataset'</span>,
            <span class="hljs-attr">data</span>: [
                { <span class="hljs-attr">x</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">20</span>, <span class="hljs-attr">r</span>: <span class="hljs-number">5</span> },
                { <span class="hljs-attr">x</span>: <span class="hljs-number">15</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">25</span>, <span class="hljs-attr">r</span>: <span class="hljs-number">8</span> },
                { <span class="hljs-attr">x</span>: <span class="hljs-number">7</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">r</span>: <span class="hljs-number">6</span> },
                { <span class="hljs-attr">x</span>: <span class="hljs-number">12</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">18</span>, <span class="hljs-attr">r</span>: <span class="hljs-number">10</span> },
                { <span class="hljs-attr">x</span>: <span class="hljs-number">20</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">30</span>, <span class="hljs-attr">r</span>: <span class="hljs-number">7</span> }
            ],
            <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'rgba(255, 99, 132, 0.5)'</span>,
            <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(255, 99, 132, 1)'</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">x</span>: {
                <span class="hljs-attr">type</span>: <span class="hljs-string">'linear'</span>, <span class="hljs-comment">// Bubble charts support only 'linear' scale type for x-axis</span>
                <span class="hljs-attr">position</span>: <span class="hljs-string">'bottom'</span>
            },
            <span class="hljs-attr">y</span>: {
                <span class="hljs-attr">type</span>: <span class="hljs-string">'linear'</span>, <span class="hljs-comment">// Bubble charts support only 'linear' scale type for y-axis</span>
                <span class="hljs-attr">position</span>: <span class="hljs-string">'left'</span>
            }
        }
    }
});
</code></pre>
<div class="embed-wrapper"><iframe height="300" style="width:100%" src="https://codepen.io/joanayebola/embed/ExBPNjZ?default-tab=html%2Cresult" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/joanayebola/pen/ExBPNjZ">
  Bubble Chart</a> by joan? (<a href="https://codepen.io/joanayebola">@joanayebola</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<h3 id="heading-7-radar-chart">7. Radar Chart</h3>
<p>A radar chart (or spider chart) is used to display multivariate data in the form of a two-dimensional chart of three or more quantitative variables represented on axes starting from the same point. Here's how you can create one:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
<span class="hljs-keyword">var</span> radarChart = <span class="hljs-keyword">new</span> Chart(ctx, {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'radar'</span>,
    <span class="hljs-attr">data</span>: {
        <span class="hljs-attr">labels</span>: [<span class="hljs-string">'Math'</span>, <span class="hljs-string">'Physics'</span>, <span class="hljs-string">'Chemistry'</span>, <span class="hljs-string">'Biology'</span>, <span class="hljs-string">'English'</span>, <span class="hljs-string">'History'</span>],
        <span class="hljs-attr">datasets</span>: [{
            <span class="hljs-attr">label</span>: <span class="hljs-string">'Student A'</span>,
            <span class="hljs-attr">data</span>: [<span class="hljs-number">85</span>, <span class="hljs-number">90</span>, <span class="hljs-number">75</span>, <span class="hljs-number">80</span>, <span class="hljs-number">70</span>, <span class="hljs-number">85</span>],
            <span class="hljs-attr">fill</span>: <span class="hljs-literal">true</span>,
            <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'rgba(54, 162, 235, 0.2)'</span>,
            <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
            <span class="hljs-attr">pointBackgroundColor</span>: <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
            <span class="hljs-attr">pointBorderColor</span>: <span class="hljs-string">'#fff'</span>,
            <span class="hljs-attr">pointHoverBackgroundColor</span>: <span class="hljs-string">'#fff'</span>,
            <span class="hljs-attr">pointHoverBorderColor</span>: <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
            <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">1</span>
        }, {
            <span class="hljs-attr">label</span>: <span class="hljs-string">'Student B'</span>,
            <span class="hljs-attr">data</span>: [<span class="hljs-number">70</span>, <span class="hljs-number">85</span>, <span class="hljs-number">80</span>, <span class="hljs-number">75</span>, <span class="hljs-number">85</span>, <span class="hljs-number">90</span>],
            <span class="hljs-attr">fill</span>: <span class="hljs-literal">true</span>,
            <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'rgba(255, 99, 132, 0.2)'</span>,
            <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
            <span class="hljs-attr">pointBackgroundColor</span>: <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
            <span class="hljs-attr">pointBorderColor</span>: <span class="hljs-string">'#fff'</span>,
            <span class="hljs-attr">pointHoverBackgroundColor</span>: <span class="hljs-string">'#fff'</span>,
            <span class="hljs-attr">pointHoverBorderColor</span>: <span class="hljs-string">'rgba(255, 99, 132, 1)'</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">r</span>: {
                <span class="hljs-attr">suggestedMin</span>: <span class="hljs-number">0</span>,
                <span class="hljs-attr">suggestedMax</span>: <span class="hljs-number">100</span>
            }
        }
    }
});
</code></pre>
<div class="embed-wrapper"><iframe height="300" style="width:100%" src="https://codepen.io/joanayebola/embed/Yzowpya?default-tab=html%2Cresult" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/joanayebola/pen/Yzowpya">
  Untitled</a> by joan? (<a href="https://codepen.io/joanayebola">@joanayebola</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<h3 id="heading-8-polar-area-chart">8. Polar Area Chart</h3>
<p>A polar area chart is similar to a pie chart, but the sectors are equal angles and differ in how far they extend from the center of the circle. It's useful for showing data distributions with the proportion of each category.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
<span class="hljs-keyword">var</span> polarAreaChart = <span class="hljs-keyword">new</span> Chart(ctx, {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'polarArea'</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">'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">backgroundColor</span>: [
                <span class="hljs-string">'rgba(255, 99, 132, 0.5)'</span>,
                <span class="hljs-string">'rgba(54, 162, 235, 0.5)'</span>,
                <span class="hljs-string">'rgba(255, 206, 86, 0.5)'</span>,
                <span class="hljs-string">'rgba(75, 192, 192, 0.5)'</span>,
                <span class="hljs-string">'rgba(153, 102, 255, 0.5)'</span>,
                <span class="hljs-string">'rgba(255, 159, 64, 0.5)'</span>
            ],
            <span class="hljs-attr">borderColor</span>: [
                <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
                <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                <span class="hljs-string">'rgba(255, 206, 86, 1)'</span>,
                <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
                <span class="hljs-string">'rgba(153, 102, 255, 1)'</span>,
                <span class="hljs-string">'rgba(255, 159, 64, 1)'</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">r</span>: {
                <span class="hljs-attr">suggestedMin</span>: <span class="hljs-number">0</span>,
                <span class="hljs-attr">suggestedMax</span>: <span class="hljs-number">20</span>
            }
        }
    }
});
</code></pre>
<div class="embed-wrapper"><iframe height="300" style="width:100%" src="https://codepen.io/joanayebola/embed/zYVroqN?default-tab=html%2Cresult" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/joanayebola/pen/zYVroqN">
  Polar Area Chart</a> by joan? (<a href="https://codepen.io/joanayebola">@joanayebola</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<p>These are just a few examples of the chart types available in Chart.js. Each chart type has its own unique features and is suitable for different types of data visualization tasks. Experiment with these chart types and explore Chart.js documentation for more advanced options and customization.</p>
<h2 id="heading-customization-and-interactivity-in-chartjs">Customization and Interactivity in Chart.js</h2>
<p>Chart.js offers extensive options for customizing the appearance and interactivity of your charts. This section covers customizing chart appearance, adding tooltips and legends, and making charts interactive with features like zooming and hovering.</p>
<h3 id="heading-customizing-chart-appearance">Customizing Chart Appearance</h3>
<p>Customizing the appearance of your charts helps make them more visually appealing and tailored to your specific needs. You can customize colors, fonts, borders, and other properties.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> customChart = <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>: {
        <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">'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">backgroundColor</span>: [
                <span class="hljs-string">'rgba(255, 99, 132, 0.8)'</span>,
                <span class="hljs-string">'rgba(54, 162, 235, 0.8)'</span>,
                <span class="hljs-string">'rgba(255, 206, 86, 0.8)'</span>,
                <span class="hljs-string">'rgba(75, 192, 192, 0.8)'</span>,
                <span class="hljs-string">'rgba(153, 102, 255, 0.8)'</span>,
                <span class="hljs-string">'rgba(255, 159, 64, 0.8)'</span>
            ],
            <span class="hljs-attr">borderColor</span>: [
                <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
                <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                <span class="hljs-string">'rgba(255, 206, 86, 1)'</span>,
                <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
                <span class="hljs-string">'rgba(153, 102, 255, 1)'</span>,
                <span class="hljs-string">'rgba(255, 159, 64, 1)'</span>
            ],
            <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">2</span>
        }]
    },
    <span class="hljs-attr">options</span>: {
        <span class="hljs-attr">plugins</span>: {
            <span class="hljs-attr">legend</span>: {
                <span class="hljs-attr">display</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-attr">labels</span>: {
                    <span class="hljs-attr">color</span>: <span class="hljs-string">'rgb(255, 99, 132)'</span>,
                    <span class="hljs-attr">font</span>: {
                        <span class="hljs-attr">size</span>: <span class="hljs-number">16</span>,
                        <span class="hljs-attr">family</span>: <span class="hljs-string">"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"</span>
                    }
                }
            },
            <span class="hljs-attr">tooltip</span>: {
                <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'rgba(0, 0, 0, 0.8)'</span>,
                <span class="hljs-attr">titleFont</span>: {
                    <span class="hljs-attr">size</span>: <span class="hljs-number">18</span>
                },
                <span class="hljs-attr">bodyFont</span>: {
                    <span class="hljs-attr">size</span>: <span class="hljs-number">14</span>
                },
                <span class="hljs-attr">callbacks</span>: {
                    <span class="hljs-attr">label</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">context</span>) </span>{
                        <span class="hljs-keyword">return</span> context.dataset.label + <span class="hljs-string">': '</span> + context.raw + <span class="hljs-string">' votes'</span>;
                    }
                }
            }
        },
        <span class="hljs-attr">scales</span>: {
            <span class="hljs-attr">x</span>: {
                <span class="hljs-attr">ticks</span>: {
                    <span class="hljs-attr">color</span>: <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                    <span class="hljs-attr">font</span>: {
                        <span class="hljs-attr">size</span>: <span class="hljs-number">14</span>
                    }
                }
            },
            <span class="hljs-attr">y</span>: {
                <span class="hljs-attr">ticks</span>: {
                    <span class="hljs-attr">color</span>: <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                    <span class="hljs-attr">font</span>: {
                        <span class="hljs-attr">size</span>: <span class="hljs-number">14</span>
                    }
                }
            }
        }
    }
});
</code></pre>
<p>In this example:</p>
<ul>
<li><p><strong>Chart Type:</strong> The chart is defined as a <code>bar</code> chart.</p>
</li>
<li><p><strong>Background and Border Colors:</strong> The <code>backgroundColor</code> and <code>borderColor</code> arrays are customized for each bar. For example, the "Red" bar is colored with a semi-transparent background (<code>rgba(255, 99, 132, 0.8)</code>) and a solid border (<code>rgba(255, 99, 132, 1)</code>).</p>
</li>
<li><p><strong>Font and Color for Legends:</strong> The <code>legend</code> configuration customizes the display of the chart’s legend, where the font size is set to 16, and the font family is <code>'Helvetica Neue', 'Helvetica', 'Arial', sans-serif'</code>. Additionally, the text color of the legend labels is customized to <code>rgb(255, 99, 132)</code>.</p>
</li>
<li><p><strong>Font and Color for Axes:</strong> The <code>scales</code> section customizes the appearance of both the X and Y axes. The font size for axis labels is set to 14, and their color is <code>rgba(54, 162, 235, 1)</code>.</p>
</li>
</ul>
<h3 id="heading-adding-tooltips-and-legends">Adding Tooltips and Legends</h3>
<p>Tooltips provide additional information when you hover over chart elements. Legends help users understand the data by showing which dataset each color represents. Both can be customized extensively, like this for example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> chartWithTooltipsAndLegend = <span class="hljs-keyword">new</span> Chart(ctx, {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'line'</span>,
    <span class="hljs-attr">data</span>: {
        <span class="hljs-attr">labels</span>: [<span class="hljs-string">'January'</span>, <span class="hljs-string">'February'</span>, <span class="hljs-string">'March'</span>, <span class="hljs-string">'April'</span>, <span class="hljs-string">'May'</span>, <span class="hljs-string">'June'</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-number">30</span>, <span class="hljs-number">45</span>, <span class="hljs-number">60</span>, <span class="hljs-number">35</span>, <span class="hljs-number">50</span>, <span class="hljs-number">40</span>],
            <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
            <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">2</span>,
            <span class="hljs-attr">fill</span>: <span class="hljs-literal">false</span>
        }]
    },
    <span class="hljs-attr">options</span>: {
        <span class="hljs-attr">plugins</span>: {
            <span class="hljs-attr">legend</span>: {
                <span class="hljs-attr">display</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-attr">position</span>: <span class="hljs-string">'top'</span>,
                <span class="hljs-attr">labels</span>: {
                    <span class="hljs-attr">font</span>: {
                        <span class="hljs-attr">size</span>: <span class="hljs-number">14</span>
                    }
                }
            },
            <span class="hljs-attr">tooltip</span>: {
                <span class="hljs-attr">enabled</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-attr">callbacks</span>: {
                    <span class="hljs-attr">label</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">tooltipItem</span>) </span>{
                        <span class="hljs-keyword">return</span> <span class="hljs-string">'Sales: $'</span> + tooltipItem.raw;
                    }
                }
            }
        }
    }
});
</code></pre>
<p>In this example:</p>
<ul>
<li><p><strong>Tooltips:</strong> Tooltips appear when the user hovers over elements of the chart. The tooltip background is customized to be black (<code>rgba(0, 0, 0, 0.8)</code>) with font sizes for both the title (18) and body text (14). The content of the tooltip is dynamically generated by a <code>callback</code> function, which appends the word "votes" to the dataset value.</p>
</li>
<li><p><strong>Legends:</strong> The legends describe the data on the chart and are placed at the top. The customization here includes setting the font size to 14 and making the legend displayable by configuring <code>display: true</code>.</p>
</li>
</ul>
<h3 id="heading-making-charts-interactive-zooming-hovering-and-so-on">Making Charts Interactive (Zooming, Hovering, and so on)</h3>
<p>Adding interactivity to your charts can enhance the user experience. Features like zooming, panning, and custom hover effects can be implemented using additional Chart.js plugins such as <code>chartjs-plugin-zoom</code>.</p>
<p>First, include the zoom plugin in your project:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@1.0.0"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>Then, configure your chart to enable zooming and panning:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> interactiveChart = <span class="hljs-keyword">new</span> Chart(ctx, {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'line'</span>,
    <span class="hljs-attr">data</span>: {
        <span class="hljs-attr">labels</span>: [<span class="hljs-string">'January'</span>, <span class="hljs-string">'February'</span>, <span class="hljs-string">'March'</span>, <span class="hljs-string">'April'</span>, <span class="hljs-string">'May'</span>, <span class="hljs-string">'June'</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-number">30</span>, <span class="hljs-number">45</span>, <span class="hljs-number">60</span>, <span class="hljs-number">35</span>, <span class="hljs-number">50</span>, <span class="hljs-number">40</span>],
            <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
            <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">2</span>,
            <span class="hljs-attr">fill</span>: <span class="hljs-literal">false</span>
        }]
    },
    <span class="hljs-attr">options</span>: {
        <span class="hljs-attr">plugins</span>: {
            <span class="hljs-attr">zoom</span>: {
                <span class="hljs-attr">pan</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>
                },
                <span class="hljs-attr">zoom</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>
                }
            }
        },
        <span class="hljs-attr">hover</span>: {
            <span class="hljs-attr">mode</span>: <span class="hljs-string">'nearest'</span>,
            <span class="hljs-attr">intersect</span>: <span class="hljs-literal">true</span>
        }
    }
});
</code></pre>
<p>In this example:</p>
<ul>
<li><p><strong>Zoom and Pan</strong>: The <code>chartjs-plugin-zoom</code> plugin is used to add zooming and panning capabilities. Users can zoom in and out using the mouse wheel or pan by dragging.</p>
</li>
<li><p><strong>Hover Mode</strong>: The <code>hover</code> option is set to <code>nearest</code>, ensuring the nearest data point is highlighted when hovered over.</p>
</li>
</ul>
<h3 id="heading-complete-html-example">Complete HTML Example</h3>
<p>Here's the complete HTML file incorporating the examples above:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Chart.js Customization and Interactivity<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/chart.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@1.0.0"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Chart.js Customization and Interactivity<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Customizing Chart Appearance<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">canvas</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"customChart"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"400"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">canvas</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Adding Tooltips and Legends<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">canvas</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"tooltipsAndLegendChart"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"400"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">canvas</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Making Charts Interactive (Zooming, Hovering, etc.)<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">canvas</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"interactiveChart"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"400"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"200"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">canvas</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>
        // Customizing Chart Appearance
        var customCtx = document.getElementById('customChart').getContext('2d');
        var customChart = new Chart(customCtx, {
            type: 'bar',
            data: {
                labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
                datasets: [{
                    label: 'Votes',
                    data: [12, 19, 3, 5, 2, 3],
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.8)',
                        'rgba(54, 162, 235, 0.8)',
                        'rgba(255, 206, 86, 0.8)',
                        'rgba(75, 192, 192, 0.8)',
                        'rgba(153, 102, 255, 0.8)',
                        'rgba(255, 159, 64, 0.8)'
                    ],
                    borderColor: [
                        'rgba(255, 99, 132, 1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(153, 102, 255, 1)',
                        'rgba(255, 159, 64, 1)'
                    ],
                    borderWidth: 2
                }]
            },
            options: {
                plugins: {
                    legend: {
                        display: true,
                        labels: {
                            color: 'rgb(255, 99, 132)',
                            font: {
                                size: 16,
                                family: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
                            }
                        }
                    },
                    tooltip: {
                        backgroundColor: 'rgba(0, 0, 0, 0.8)',
                        titleFont: {
                            size: 18
                        },
                        bodyFont: {
                            size: 14
                        },
                        callbacks: {
                            label: function(context) {
                                return context.dataset.label + ': ' + context.raw + ' votes';
                            }
                        }
                    }
                },
                scales: {
                    x: {
                        ticks: {
                            color: 'rgba(54, 162, 235, 1)',
                            font: {
                                size: 14
                            }
                        }
                    },
                    y: {
                        ticks: {
                            color: 'rgba(54, 162, 235, 1)',
                            font: {
                                size: 14
                            }
                        }
                    }
                }
            }
        });

        // Adding Tooltips and Legends
        var tooltipsAndLegendCtx = document.getElementById('

tooltipsAndLegendChart').getContext('2d');
        var chartWithTooltipsAndLegend = new Chart(tooltipsAndLegendCtx, {
            type: 'line',
            data: {
                labels: ['January', 'February', 'March', 'April', 'May', 'June'],
                datasets: [{
                    label: 'Sales',
                    data: [30, 45, 60, 35, 50, 40],
                    borderColor: 'rgba(54, 162, 235, 1)',
                    borderWidth: 2,
                    fill: false
                }]
            },
            options: {
                plugins: {
                    legend: {
                        display: true,
                        position: 'top',
                        labels: {
                            font: {
                                size: 14
                            }
                        }
                    },
                    tooltip: {
                        enabled: true,
                        callbacks: {
                            label: function(tooltipItem) {
                                return 'Sales: $' + tooltipItem.raw;
                            }
                        }
                    }
                }
            }
        });

        // Making Charts Interactive (Zooming, Hovering, etc.)
        var interactiveCtx = document.getElementById('interactiveChart').getContext('2d');
        var interactiveChart = new Chart(interactiveCtx, {
            type: 'line',
            data: {
                labels: ['January', 'February', 'March', 'April', 'May', 'June'],
                datasets: [{
                    label: 'Sales',
                    data: [30, 45, 60, 35, 50, 40],
                    borderColor: 'rgba(54, 162, 235, 1)',
                    borderWidth: 2,
                    fill: false
                }]
            },
            options: {
                plugins: {
                    zoom: {
                        pan: {
                            enabled: true,
                            mode: 'xy'
                        },
                        zoom: {
                            enabled: true,
                            mode: 'xy'
                        }
                    }
                },
                hover: {
                    mode: 'nearest',
                    intersect: true
                }
            }
        });
    <span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<div class="embed-wrapper"><iframe height="300" style="width:100%" src="https://codepen.io/joanayebola/embed/QWXyGqb?default-tab=html%2Cresult" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/joanayebola/pen/QWXyGqb">
  Untitled</a> by joan? (<a href="https://codepen.io/joanayebola">@joanayebola</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<p>This HTML file includes examples of customizing chart appearance, adding tooltips and legends, and making charts interactive. You can view the results by opening this HTML file in a web browser.</p>
<h2 id="heading-how-to-work-with-data-in-chartjs">How to Work with Data in Chart.js</h2>
<p>Chart.js provides versatile capabilities for handling data, accommodating various formats and requirements for data visualization:</p>
<h3 id="heading-data-formats-supported-by-chartjs">Data Formats Supported by Chart.js</h3>
<p>Chart.js provides flexible support for various data formats to accommodate different data structures and needs:</p>
<ul>
<li><p><strong>Arrays</strong>: Simple arrays of values are the most basic format and can be directly used to plot data points.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> data = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>];
</code></pre>
</li>
<li><p><strong>Objects</strong>: Arrays of objects are useful for more complex data where each object represents a data point with multiple properties.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> data = [
      { <span class="hljs-attr">x</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">20</span> },
      { <span class="hljs-attr">x</span>: <span class="hljs-number">15</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">25</span> },
      { <span class="hljs-attr">x</span>: <span class="hljs-number">20</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">30</span> }
  ];
</code></pre>
</li>
<li><p><strong>JSON</strong>: JSON (JavaScript Object Notation) is ideal for structured data interchange, allowing for clear organization of labels and datasets.</p>
<pre><code class="lang-json">  {
      <span class="hljs-attr">"labels"</span>: [<span class="hljs-string">"January"</span>, <span class="hljs-string">"February"</span>, <span class="hljs-string">"March"</span>, <span class="hljs-string">"April"</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-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>]
      }]
  }
</code></pre>
</li>
<li><p><strong>CSV</strong>: Comma-Separated Values (CSV) are commonly used for tabular data and can be parsed into arrays or objects for Chart.js.</p>
</li>
</ul>
<h3 id="heading-how-to-load-data-from-external-files-json-csv">How to Load Data from External Files (JSON, CSV)</h3>
<p>Loading data from external files is essential for handling dynamic or large datasets efficiently:</p>
<ul>
<li><p><strong>Loading JSON Data</strong>: Use the <code>fetch</code> API to retrieve JSON data and integrate it into Chart.js.</p>
<pre><code class="lang-html">  <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
      fetch(<span class="hljs-string">'data.json'</span>)
          .then(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> response.json())
          .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> {
              <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
              <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,
                  <span class="hljs-attr">options</span>: {}
              });
          });
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
</li>
<li><p><strong>Loading CSV Data</strong>: Utilize libraries like PapaParse to parse CSV files into usable data formats for Chart.js visualization.</p>
<pre><code class="lang-html">  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
      Papa.parse(<span class="hljs-string">'data.csv'</span>, {
          <span class="hljs-attr">download</span>: <span class="hljs-literal">true</span>,
          <span class="hljs-attr">header</span>: <span class="hljs-literal">true</span>,
          <span class="hljs-attr">complete</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">results</span>) </span>{
              <span class="hljs-keyword">const</span> labels = results.data.map(<span class="hljs-function"><span class="hljs-params">row</span> =&gt;</span> row[<span class="hljs-string">'Month'</span>]);
              <span class="hljs-keyword">const</span> data = results.data.map(<span class="hljs-function"><span class="hljs-params">row</span> =&gt;</span> <span class="hljs-built_in">parseFloat</span>(row[<span class="hljs-string">'Sales'</span>]));

              <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
              <span class="hljs-keyword">new</span> Chart(ctx, {
                  <span class="hljs-attr">type</span>: <span class="hljs-string">'line'</span>,
                  <span class="hljs-attr">data</span>: {
                      <span class="hljs-attr">labels</span>: labels,
                      <span class="hljs-attr">datasets</span>: [{
                          <span class="hljs-attr">label</span>: <span class="hljs-string">'Sales'</span>,
                          <span class="hljs-attr">data</span>: data
                      }]
                  },
                  <span class="hljs-attr">options</span>: {}
              });
          }
      });
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
</li>
</ul>
<h3 id="heading-dynamic-data-updates">Dynamic Data Updates</h3>
<p>Chart.js supports dynamic data updates, which is crucial for real-time data visualization:</p>
<ul>
<li><p><strong>Updating Data Dynamically</strong>: Use Chart.js methods to update chart data dynamically and re-render the chart as needed.</p>
<pre><code class="lang-html">  <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
      <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'myChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
      <span class="hljs-keyword">const</span> myChart = <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>: {
              <span class="hljs-attr">labels</span>: [<span class="hljs-string">'January'</span>, <span class="hljs-string">'February'</span>, <span class="hljs-string">'March'</span>, <span class="hljs-string">'April'</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-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>]
              }]
          },
          <span class="hljs-attr">options</span>: {}
      });

      <span class="hljs-comment">// Example function to update the chart data</span>
      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateChartData</span>(<span class="hljs-params"></span>) </span>{
          myChart.data.datasets[<span class="hljs-number">0</span>].data = [<span class="hljs-number">50</span>, <span class="hljs-number">60</span>, <span class="hljs-number">70</span>, <span class="hljs-number">80</span>];
          myChart.update();
      }

      <span class="hljs-comment">// Call the update function after 2 seconds</span>
      <span class="hljs-built_in">setTimeout</span>(updateChartData, <span class="hljs-number">2000</span>);
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
</li>
</ul>
<p>In this section, we've explored the various data formats supported by Chart.js, methods for loading data from external files such as JSON and CSV, and how to implement dynamic data updates for real-time visualization. These features make Chart.js a powerful tool for interactive data visualization in web applications.</p>
<h2 id="heading-advanced-chartjs-features">Advanced Chart.js Features</h2>
<p>Chart.js offers advanced features that enhance data visualization capabilities beyond basic charts. Let's delve into these features:</p>
<h3 id="heading-how-to-combine-different-chart-types">How to Combine Different Chart Types</h3>
<p>Chart.js allows you to combine different chart types within a single chart, offering flexibility in visualizing complex data sets:</p>
<ul>
<li><p><strong>Mixed Chart Types</strong>: You can mix line, bar, radar, and other chart types in a single chart to represent diverse datasets effectively.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'mixedChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
  <span class="hljs-keyword">const</span> mixedChart = <span class="hljs-keyword">new</span> Chart(ctx, {
      <span class="hljs-attr">type</span>: <span class="hljs-string">'bar'</span>, <span class="hljs-comment">// Default type for the main dataset</span>
      <span class="hljs-attr">data</span>: {
          <span class="hljs-attr">labels</span>: [<span class="hljs-string">'January'</span>, <span class="hljs-string">'February'</span>, <span class="hljs-string">'March'</span>, <span class="hljs-string">'April'</span>, <span class="hljs-string">'May'</span>, <span class="hljs-string">'June'</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-number">50</span>, <span class="hljs-number">60</span>, <span class="hljs-number">70</span>, <span class="hljs-number">80</span>, <span class="hljs-number">90</span>, <span class="hljs-number">100</span>],
              <span class="hljs-attr">type</span>: <span class="hljs-string">'line'</span>, <span class="hljs-comment">// Specific type for this dataset</span>
              <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
              <span class="hljs-attr">tension</span>: <span class="hljs-number">0.1</span>
          }]
      },
      <span class="hljs-attr">options</span>: {}
  });
</code></pre>
</li>
</ul>
<h3 id="heading-how-to-create-chart-animations">How to Create Chart Animations</h3>
<p>Animations in Chart.js can bring your data visualizations to life, providing a dynamic and engaging user experience:</p>
<ul>
<li><p><strong>Animated Transitions</strong>: Configure animations to smoothly transition between different states, enhancing the clarity of data changes over time.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'animatedChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
  <span class="hljs-keyword">const</span> animatedChart = <span class="hljs-keyword">new</span> Chart(ctx, {
      <span class="hljs-attr">type</span>: <span class="hljs-string">'line'</span>,
      <span class="hljs-attr">data</span>: {
          <span class="hljs-attr">labels</span>: [<span class="hljs-string">'January'</span>, <span class="hljs-string">'February'</span>, <span class="hljs-string">'March'</span>, <span class="hljs-string">'April'</span>, <span class="hljs-string">'May'</span>, <span class="hljs-string">'June'</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-number">50</span>, <span class="hljs-number">60</span>, <span class="hljs-number">70</span>, <span class="hljs-number">80</span>, <span class="hljs-number">90</span>, <span class="hljs-number">100</span>],
              <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
              <span class="hljs-attr">tension</span>: <span class="hljs-number">0.1</span>
          }]
      },
      <span class="hljs-attr">options</span>: {
          <span class="hljs-attr">animation</span>: {
              <span class="hljs-attr">duration</span>: <span class="hljs-number">2000</span>, <span class="hljs-comment">// Animation duration in milliseconds</span>
              <span class="hljs-attr">easing</span>: <span class="hljs-string">'easeInOutQuart'</span> <span class="hljs-comment">// Easing function for smooth animation</span>
          }
      }
  });
</code></pre>
</li>
</ul>
<h3 id="heading-how-to-use-third-party-plugins">How to Use Third-Party Plugins</h3>
<p>Extend Chart.js functionality with third-party plugins to add custom features and enhance visualization capabilities:</p>
<ul>
<li><p><strong>Integration of Plugins</strong>: Integrate plugins like zoom, tooltip enhancements, or data label customization to tailor charts to specific needs.</p>
<pre><code class="lang-html">  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
      <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'pluginChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
      <span class="hljs-keyword">const</span> pluginChart = <span class="hljs-keyword">new</span> Chart(ctx, {
          <span class="hljs-attr">type</span>: <span class="hljs-string">'line'</span>,
          <span class="hljs-attr">data</span>: {
              <span class="hljs-attr">labels</span>: [<span class="hljs-string">'January'</span>, <span class="hljs-string">'February'</span>, <span class="hljs-string">'March'</span>, <span class="hljs-string">'April'</span>, <span class="hljs-string">'May'</span>, <span class="hljs-string">'June'</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-number">50</span>, <span class="hljs-number">60</span>, <span class="hljs-number">70</span>, <span class="hljs-number">80</span>, <span class="hljs-number">90</span>, <span class="hljs-number">100</span>],
                  <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
                  <span class="hljs-attr">tension</span>: <span class="hljs-number">0.1</span>
              }]
          },
          <span class="hljs-attr">options</span>: {
              <span class="hljs-attr">plugins</span>: {
                  <span class="hljs-attr">zoom</span>: {
                      <span class="hljs-attr">pan</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>
                      },
                      <span class="hljs-attr">zoom</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>
                      }
                  }
              }
          }
      });
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
</li>
</ul>
<h2 id="heading-best-practices-for-chart-design">Best Practices for Chart Design</h2>
<p>Effective chart design ensures that your data is presented clearly and accurately, making it easy for users to understand and interpret. Here are some best practices to consider:</p>
<h3 id="heading-choose-the-right-chart-type">Choose the Right Chart Type</h3>
<p>Selecting the appropriate chart type is crucial for conveying your data effectively:</p>
<ul>
<li><p><strong>Bar Charts</strong>: Ideal for comparing different categories or tracking changes over time. Use bar charts when you have discrete data points.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'barChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
  <span class="hljs-keyword">const</span> barChart = <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>: {
          <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">'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">backgroundColor</span>: [
                  <span class="hljs-string">'rgba(255, 99, 132, 0.2)'</span>,
                  <span class="hljs-string">'rgba(54, 162, 235, 0.2)'</span>,
                  <span class="hljs-string">'rgba(255, 206, 86, 0.2)'</span>,
                  <span class="hljs-string">'rgba(75, 192, 192, 0.2)'</span>,
                  <span class="hljs-string">'rgba(153, 102, 255, 0.2)'</span>,
                  <span class="hljs-string">'rgba(255, 159, 64, 0.2)'</span>
              ],
              <span class="hljs-attr">borderColor</span>: [
                  <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
                  <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                  <span class="hljs-string">'rgba(255, 206, 86, 1)'</span>,
                  <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
                  <span class="hljs-string">'rgba(153, 102, 255, 1)'</span>,
                  <span class="hljs-string">'rgba(255, 159, 64, 1)'</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>
</li>
<li><p><strong>Line Charts</strong>: Best for showing trends over time or continuous data. Line charts are useful when you want to highlight changes and patterns.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'lineChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
  <span class="hljs-keyword">const</span> lineChart = <span class="hljs-keyword">new</span> Chart(ctx, {
      <span class="hljs-attr">type</span>: <span class="hljs-string">'line'</span>,
      <span class="hljs-attr">data</span>: {
          <span class="hljs-attr">labels</span>: [<span class="hljs-string">'January'</span>, <span class="hljs-string">'February'</span>, <span class="hljs-string">'March'</span>, <span class="hljs-string">'April'</span>, <span class="hljs-string">'May'</span>, <span class="hljs-string">'June'</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-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>, <span class="hljs-number">60</span>],
              <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
              <span class="hljs-attr">tension</span>: <span class="hljs-number">0.1</span>
          }]
      },
      <span class="hljs-attr">options</span>: {}
  });
</code></pre>
</li>
<li><p><strong>Pie and Doughnut Charts</strong>: Suitable for displaying proportions and parts of a whole. Use these charts when you want to highlight distribution.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'doughnutChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
  <span class="hljs-keyword">const</span> doughnutChart = <span class="hljs-keyword">new</span> Chart(ctx, {
      <span class="hljs-attr">type</span>: <span class="hljs-string">'doughnut'</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-attr">datasets</span>: [{
              <span class="hljs-attr">label</span>: <span class="hljs-string">'Votes'</span>,
              <span class="hljs-attr">data</span>: [<span class="hljs-number">300</span>, <span class="hljs-number">50</span>, <span class="hljs-number">100</span>],
              <span class="hljs-attr">backgroundColor</span>: [
                  <span class="hljs-string">'rgba(255, 99, 132, 0.2)'</span>,
                  <span class="hljs-string">'rgba(54, 162, 235, 0.2)'</span>,
                  <span class="hljs-string">'rgba(255, 206, 86, 0.2)'</span>
              ],
              <span class="hljs-attr">borderColor</span>: [
                  <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
                  <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                  <span class="hljs-string">'rgba(255, 206, 86, 1)'</span>
              ],
              <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">1</span>
          }]
      },
      <span class="hljs-attr">options</span>: {}
  });
</code></pre>
</li>
</ul>
<h3 id="heading-design-for-clarity-and-readability">Design for Clarity and Readability</h3>
<p>Clear and readable charts help users understand data quickly and accurately:</p>
<ul>
<li><p><strong>Use Appropriate Labels</strong>: Ensure that all axes, data points, and legends are clearly labeled. Avoid cluttering the chart with too much information.</p>
</li>
<li><p><strong>Color Choices</strong>: Use contrasting colors to differentiate between data points or categories. Ensure that color choices are accessible to those with color vision deficiencies.</p>
</li>
<li><p><strong>Simplify Data</strong>: Avoid overloading the chart with too much data. Focus on the key message you want to convey and use additional charts for supplementary information.</p>
</li>
<li><p><strong>Consistent Scale</strong>: Use a consistent scale across charts when comparing similar data sets. This helps users make accurate comparisons without recalibrating their understanding of the scale.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'clarityChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
  <span class="hljs-keyword">const</span> clarityChart = <span class="hljs-keyword">new</span> Chart(ctx, {
      <span class="hljs-attr">type</span>: <span class="hljs-string">'line'</span>,
      <span class="hljs-attr">data</span>: {
          <span class="hljs-attr">labels</span>: [<span class="hljs-string">'January'</span>, <span class="hljs-string">'February'</span>, <span class="hljs-string">'March'</span>, <span class="hljs-string">'April'</span>, <span class="hljs-string">'May'</span>, <span class="hljs-string">'June'</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-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>, <span class="hljs-number">60</span>],
              <span class="hljs-attr">borderColor</span>: <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
              <span class="hljs-attr">tension</span>: <span class="hljs-number">0.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>
</li>
</ul>
<h3 id="heading-make-charts-accessible">Make Charts Accessible</h3>
<p>Accessibility ensures that your charts can be understood by all users, including those with disabilities:</p>
<ul>
<li><p><strong>Use ARIA Labels</strong>: Implement ARIA (Accessible Rich Internet Applications) labels to provide additional context to screen readers.</p>
<pre><code class="lang-html">  <span class="hljs-tag">&lt;<span class="hljs-name">canvas</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"accessibleChart"</span> <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"Sales Data"</span> <span class="hljs-attr">role</span>=<span class="hljs-string">"img"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">canvas</span>&gt;</span>
</code></pre>
</li>
<li><p><strong>Provide Alternative Text</strong>: Include descriptive alternative text for charts, especially if the chart is complex. This helps users who rely on screen readers to understand the chart's content.</p>
</li>
<li><p><strong>Keyboard Navigation</strong>: Ensure that all interactive elements of the chart, such as tooltips and legends, are accessible via keyboard navigation.</p>
</li>
<li><p><strong>High Contrast Colors</strong>: Use high contrast colors for better visibility, especially for users with low vision or color blindness.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> ctx = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'accessibleChart'</span>).getContext(<span class="hljs-string">'2d'</span>);
  <span class="hljs-keyword">const</span> accessibleChart = <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>: {
          <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">'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">backgroundColor</span>: [
                  <span class="hljs-string">'rgba(255, 99, 132, 0.8)'</span>,
                  <span class="hljs-string">'rgba(54, 162, 235, 0.8)'</span>,
                  <span class="hljs-string">'rgba(255, 206, 86, 0.8)'</span>,
                  <span class="hljs-string">'rgba(75, 192, 192, 0.8)'</span>,
                  <span class="hljs-string">'rgba(153, 102, 255, 0.8)'</span>,
                  <span class="hljs-string">'rgba(255, 159, 64, 0.8)'</span>
              ],
              <span class="hljs-attr">borderColor</span>: [
                  <span class="hljs-string">'rgba(255, 99, 132, 1)'</span>,
                  <span class="hljs-string">'rgba(54, 162, 235, 1)'</span>,
                  <span class="hljs-string">'rgba(255, 206, 86, 1)'</span>,
                  <span class="hljs-string">'rgba(75, 192, 192, 1)'</span>,
                  <span class="hljs-string">'rgba(153, 102, 255, 1)'</span>,
                  <span class="hljs-string">'rgba(255, 159, 64, 1)'</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>
</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Using Chart.js for interactive data visualization is a great way to turn raw data into clear, engaging charts. This tool is easy to use and offers many types of charts, like bar, line, and pie charts. You can also customize your charts to make them look exactly how you want.</p>
<p>Chart.js is not only good for basic charts but also has advanced features. You can combine different types of charts, add animations, and use plugins to add extra functions like zooming. It supports various data formats, can load data from files, and update data in real-time.</p>
<p>When designing your charts, it’s important to choose the right type of chart for your data, keep your charts clear and easy to read, and make sure they are accessible to everyone, including people with disabilities.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Realtime Chart with React, HighCharts, and Pusher ]]>
                </title>
                <description>
                    <![CDATA[ In today's tutorial, you are going to learn about WebSockets and how you can use them to create interactive realtime data applications.  To illustrate just how innovative real time technologies are, we will build a chart application which automatical... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-a-realtime-chart-with-react-and-pusher/</link>
                <guid isPermaLink="false">66be00bfb51b2616d39af871</guid>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ websocket ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Andrew Baisden ]]>
                </dc:creator>
                <pubDate>Thu, 02 May 2024 00:07:30 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/05/pusher-banner.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In today's tutorial, you are going to learn about WebSockets and how you can use them to create interactive realtime data applications. </p>
<p>To illustrate just how innovative real time technologies are, we will build a chart application which automatically updates with new dynamic online data.</p>
<p>This is going to be a really good example of how we can implement user data into useful commercial products such as sports leaderboards, social media analytics, financial money trackers, medical instruments, games and many more.</p>
<p>As you can see, there are numerous use cases for this technology. So let's get under way and first learn about the main platform that we will be using to create our realtime data: <a target="_blank" href="https://pusher.com/">Pusher</a>.</p>
<p>You can find the <a target="_blank" href="https://github.com/andrewbaisden/realtime-chart-pusher">codebase online here</a>.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ul>
<li>An IDE/code editor installed</li>
<li>Basic knowledge of JavaScript and React</li>
<li>An understanding of Node.js and npm</li>
<li>An account on <a target="_blank" href="https://pusher.com/">Pusher</a></li>
</ul>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><a class="post-section-overview" href="#heading-what-are-websockets">What are WebSockets?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-pusher">What is Pusher?</a></li>
<li><a class="post-section-overview" href="#heading-what-we-will-be-building">What we will be building</a></li>
<li><a class="post-section-overview" href="#heading-how-to-create-an-account-on-pusher">How to Create an Account on Pusher</a></li>
<li><a class="post-section-overview" href="#heading-how-to-build-the-company-annual-income-application">How to Build the Company Annual Income Application</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ol>
<h2 id="heading-what-are-websockets">What are WebSockets?</h2>
<p>WebSockets are a communications protocol which allows for data to travel bidirectionally between a client and a server. This essentially means that it's possible for a client and server to simultaneously send data back and forth independently of each other. </p>
<p>Being able to send data this way brings many advantages, such as preventing blocking. This can occur if the system is only capable of either sending or receiving and not both at the same time.</p>
<p>Another advantage is the fact that the Transmission Control Protocol (TCP) connection remains long-lived and connected. This is a drawback of traditional Hypertext Transfer Protocol (HTTP) connections, as they are regularly opening and closing each time there is a request or a response. This prevents communication from being instant and realtime. </p>
<p>Not having to worry about the opening and closing of connections results in dramatically less network traffic – so in terms of speed and resources, this is a welcome bonus.</p>
<p>Now that you know why WebSockets are helpful, let's take a look at Pusher and see what the platform is capable of.</p>
<h2 id="heading-what-is-pusher">What is Pusher?</h2>
<p>Pusher is an online platform you can use to build and develop applications that require real-time communication. These communications typically take place between web browsers, mobile phones, and other internet connected devices. </p>
<p>The platform is designed to make it easy to implement real-time communication systems which are nowhere near as complicated as raw WebSocket connections in terms of setup and management. This allows for better scaling and even a way to handle fallbacks for legacy environments that do not support WebSockets.</p>
<p>Pusher is capable of using numerous technologies such as WebSockets, HTTP fallbacks, and even its own propriety protocol called <code>ws-longpolling</code>. This enables the platform to work in places where WebSockets is not supported.</p>
<p>Pusher also has a lot of other useful features. For example, it fully supports WebSockets, so bidirectional communication works as you would expect. The platform also follows the publish-subscribe (Pub/Sub) pattern which is a popular messaging pattern used in the industry. It pretty much guarantees that anyone subscribed to any of the channels can receive the messages in realtime.</p>
<p>A good example of this would be everyone getting the latest news notification who is subscribed to that social media channel. Subscriptions can be public and private, which requires authentication to gain access to those messages. </p>
<p>There's also wide platform support, as the Pusher SDK is available in different programming languages like JavaScript, Python, iOS, Android and many others. It's available as a service online, so all of the scaling and communications infrastructure is already handled. This means one less system for developers to handle when building an application.</p>
<p>Pusher is a great option, as I hope you can tell. But there are other tools out there that do similar things:</p>
<ul>
<li><a target="_blank" href="https://www.pubnub.com/">PubNub</a></li>
<li><a target="_blank" href="https://deepstreamhub.com/">deepstreamHub</a></li>
<li><a target="_blank" href="https://aws.amazon.com/iot/">AWS IoT</a></li>
<li><a target="_blank" href="https://aws.amazon.com/sns/">AWS SNS</a></li>
<li><a target="_blank" href="https://cloud.google.com/pubsub">Google Cloud Pub/Sub</a></li>
<li><a target="_blank" href="https://fanout.io/">Fanout</a></li>
</ul>
<p>Ok, you've learned quite a lot about WebSockets and Pusher. Now, we'll take a look at the application that we are going to build.</p>
<h2 id="heading-what-we-will-be-building">What We Will Be Building</h2>
<p>To help you learn how Pusher works, we are going to build a Company Annual Income dashboard application. The application has a graph, a 3D Mode toggle, and three buttons.</p>
<p>We'll build our application using <a target="_blank" href="https://nextjs.org/">Next.js</a>, <a target="_blank" href="https://pusher.com/">Pusher</a>, and <a target="_blank" href="https://www.highcharts.com/docs/index">HighCharts</a> (the tool that we'll use to create our graph component). This will be a full stack application with one API endpoint for our Pusher REST API. We just need one GET endpoint which will connect to the Pusher API.</p>
<p>The graph data will come directly from Pusher, and the 3D Mode toggle essentially lets us switch our chart between 2D and 3D mode. The buttons let us change the type of chart that is displayed on screen, and all 3 charts can work in 2D and 3D mode. The chart has some starting data, and new data is automatically added to the end for each new year.</p>
<p>Here you can see our Company Annual Income Application in 2D Graph Mode.</p>
<p><img src="https://res.cloudinary.com/d74fh3kw/image/upload/v1714407050/company-annual-income_ww5d4g.png" alt="Company Annual Income Application in 2D Graph Mode" width="2754" height="1204" loading="lazy">
<em>Company Annual Income Application 2D Mode</em></p>
<p>And here you can see our Company Annual Income Application in 3D Graph Mode.</p>
<p><img src="https://res.cloudinary.com/d74fh3kw/image/upload/v1714407667/company-annual-income-3d_fxjm1p.png" alt="Company Annual Income Application in 3D Graph Mode" width="2754" height="1194" loading="lazy">
<em>Company Annual Income Application 3D Mode</em></p>
<p>As you can see, the HighCharts library is very good when it comes to building applications that require data visualisation. If you are interested in other libraries which work just as well, I have put together a list of some of them here. Each one has its own unique benefits and it's possible to use more than one library if you want to:</p>
<ul>
<li><a target="_blank" href="https://www.chartjs.org/">Chart.js</a></li>
<li><a target="_blank" href="https://d3js.org/">D3</a></li>
<li><a target="_blank" href="https://recharts.org/en-US">Recharts</a></li>
<li><a target="_blank" href="https://developers.google.com/chart">Google Charts</a></li>
<li><a target="_blank" href="https://apexcharts.com/">Apex Charts</a></li>
<li><a target="_blank" href="https://dygraphs.com/">dyagraphs</a></li>
<li><a target="_blank" href="https://commerce.nearform.com/open-source/victory/">Victory</a></li>
</ul>
<p>Alright, we know what we are going to build. In the next section, we will quickly go through how to create an account on Pusher followed by actually building our application afterwards.</p>
<h2 id="heading-how-to-create-an-account-on-pusher">How to Create an Account on Pusher</h2>
<p>Creating an account on Pusher only requires a few steps. Depending on when you follow this tutorial, the website might look a bit different – but the sign up process should be the same.</p>
<p>Start by going to the <a target="_blank" href="https://pusher.com/">Pusher</a> website as shown below:</p>
<p><img src="https://res.cloudinary.com/d74fh3kw/image/upload/v1714409546/pusher-website-home_fvngyq.png" alt="Pusher website home page" width="600" height="400" loading="lazy">
<em>Pusher website home page</em></p>
<p>Click on either the Get your free account button in the middle of the page, or the Sign up button in the top right hand corner.</p>
<p>You should now be on the Sign up page as shown here:</p>
<p><img src="https://res.cloudinary.com/d74fh3kw/image/upload/v1714409766/pusher-website-signup_zpihwa.png" alt="Pusher website sign up page" width="600" height="400" loading="lazy">
<em>Pusher website sign up for account page</em></p>
<p>Either use your GitHub, Google, or email to create an account.</p>
<p>The next page to load should be your main dashboard after you have completed the sign up as shown in the image below:</p>
<p><img src="https://res.cloudinary.com/d74fh3kw/image/upload/v1714410240/pusher-channels_wqpt2l.png" alt="Pusher channels page" width="1305" height="535" loading="lazy">
<em>Pusher website main dashboard page</em></p>
<p>Click on the Manage button in the Channels section in the image shown above to get to the Channels page.</p>
<p>The Channels page can be seen below which displays all of the data for our app.</p>
<p><img src="https://res.cloudinary.com/d74fh3kw/image/upload/v1714410477/pusher-website-create-app_iorisl.png" alt="Pusher website Channels page" width="2454" height="1940" loading="lazy">
<em>Pusher website Channels page</em></p>
<p>We need to create an app, so click on the Create app button in the top right hand corner.</p>
<p>Clicking on the button should give you this Create your Channels app box shown here:</p>
<p><img src="https://res.cloudinary.com/d74fh3kw/image/upload/v1714410628/pusher-website-create-channels-app_gsyb8z.png" alt="Pusher website Create your Channels app screen" width="533" height="627" loading="lazy">
<em>Pusher website Create your channels app page</em></p>
<p>Give your app a name and then choose React for the front end and Node.js for the back end. Then click the Create app button at the bottom of the form.</p>
<p>That's it – you should now see the main screen for your app as shown in the example image below (with data for connections and messages shown in the middle). The sidebar on the left has the features and App keys which we need to use later.</p>
<p><img src="https://res.cloudinary.com/d74fh3kw/image/upload/v1714411607/pusher-website-app-overview_lsrhxl.png" alt="Pusher website app overview page" width="1260" height="892" loading="lazy">
<em>Pusher website app page</em></p>
<p>Great! Up next we will start building our app...so lets get to it.</p>
<h2 id="heading-how-to-build-the-company-annual-income-application">How to Build the Company Annual Income Application</h2>
<p>The first thing we have to do is setup our project architecture and the folder structure so let's start with that. Create a folder on your computer called <code>realtime-chart-pusher</code> and then <code>cd</code> into it.</p>
<p>Create a Next.js project by running the usual install and setup command here:</p>
<pre><code class="lang-shell">npx create-next-app .
</code></pre>
<p>On the configuration screen, our project needs to use Tailwind CSS for styling and its recommended that you use the App Router. The other defaults should be fine.</p>
<p>Now we have to install our dependencies for the project. Go ahead and do that with this command:</p>
<pre><code class="lang-shell">npm i axios highcharts-react-official pusher pusher-js
</code></pre>
<p>Here is a quick breakdown of the purpose of each package we are installing:</p>
<ul>
<li>Axios: for making API requests to our backend API which is connected to the Pusher API</li>
<li>highcharts-react-official: for building our realtime chart</li>
<li>pusher and pusher-js: for connecting to our Pusher account and the Pusher API</li>
</ul>
<p>Right, with that done, we just have to setup our project files. You can use this run script in your terminal while still in the project root folder:</p>
<pre><code class="lang-shell">touch .env.local
cd src/app
mkdir -p api/pusher components/ChartButton components/CompanyIncomeChart components/Toggle3DButton
touch api/pusher/route.js
touch components/ChartButton/ChartButton.js components/CompanyIncomeChart/CompanyIncomeChart.js components/Toggle3DButton/Toggle3DButton.js
</code></pre>
<p>This script creates a <code>.env.local</code> file for our Pusher App keys. It also creates files and folders for our backend and frontend.</p>
<p>Before we begin adding the code to our codebase, we need to get our Pusher App Keys. You can find them on your Pusher account as shown in this example below:</p>
<p><img src="https://res.cloudinary.com/d74fh3kw/image/upload/v1714580536/pusher-app-keys_rihysv.png" alt="Pusher App Keys Screen" width="810" height="537" loading="lazy">
<em>Pusher website App keys page</em></p>
<p>Add your app keys to the <code>.env.local</code> file like in this code example here:</p>
<pre><code class="lang-shell">NEXT_SECRET_PUSHER_APP_ID = "yourid"
NEXT_SECRET_PUSHER_KEY = "yourkey"
NEXT_SECRET_PUSHER_SECRET = "yoursecret"
NEXT_SECRET_PUSHER_CLUSTER = "yourlocation"
</code></pre>
<p>We just added <code>NEXT_SECRET</code> at the start of all of the variables because it is a Next.js convention and ensures that the variables work properly.</p>
<p>Now we can start with the bulk of our codebase. First we will do the <code>route.js</code> file inside of the <code>api/route</code> folder. This is the code needed for the file:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> Pusher <span class="hljs-keyword">from</span> <span class="hljs-string">'pusher'</span>;

<span class="hljs-keyword">const</span> pusher = <span class="hljs-keyword">new</span> Pusher({
  <span class="hljs-attr">appId</span>: process.env.NEXT_SECRET_PUSHER_APP_ID,
  <span class="hljs-attr">key</span>: process.env.NEXT_SECRET_PUSHER_KEY,
  <span class="hljs-attr">secret</span>: process.env.NEXT_SECRET_PUSHER_SECRET,
  <span class="hljs-attr">cluster</span>: process.env.NEXT_SECRET_PUSHER_CLUSTER,
  <span class="hljs-attr">useTLS</span>: <span class="hljs-literal">true</span>,
});

<span class="hljs-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">GET</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Define the initial value</span>
  <span class="hljs-keyword">const</span> value = <span class="hljs-built_in">Math</span>.random() * <span class="hljs-number">800000</span> + <span class="hljs-number">200000</span>;

  <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =&gt;</span> {
    pusher.trigger(<span class="hljs-string">'company-income'</span>, <span class="hljs-string">'new-price'</span>, {
      <span class="hljs-attr">value</span>: value,
    });

    <span class="hljs-keyword">return</span> Response.json({ <span class="hljs-attr">value</span>: value }, { <span class="hljs-attr">status</span>: <span class="hljs-number">200</span> });
    <span class="hljs-comment">// Every ten seconds, the setInterval method will get data from the API because its value is set to ten seconds. Pusher's free plans are limited to 200,000 messages a day, so be careful while reducing the interval or you risk exceeding your limit too soon.</span>
  }, <span class="hljs-number">10000</span>);
  <span class="hljs-keyword">return</span> Response.json({ <span class="hljs-attr">value</span>: value }, { <span class="hljs-attr">status</span>: <span class="hljs-number">200</span> });
}
</code></pre>
<p>The code in this file lets us connect to Pusher using our App Keys. The <code>value</code> is a randomly generated number that gets sent as JSON to our Pusher channel. This number is what we use to show the company income in each new year which is automatically generated in our chart.</p>
<p>This update occurs every 10 seconds and can be changed in the <code>setInterval</code> function. Pusher's free plans are limited to 200,000 messages a day, so be careful while reducing the interval or you risk exceeding your limit too soon.</p>
<p>The next file we will work on is the <code>ChartButton.js</code> file which you can find inside its folder <code>ChartButton</code>.</p>
<p>Add this code to the file:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ChartButton</span>(<span class="hljs-params">{ chartRef, type, name }</span>) </span>{
  <span class="hljs-keyword">const</span> switchToLineChart = <span class="hljs-function">() =&gt;</span> {
    chartRef.current.update({
      <span class="hljs-attr">chart</span>: {
        <span class="hljs-attr">type</span>: type,
      },
    });
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span>
      <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> switchToLineChart()}
      className="bg-indigo-500 hover:bg-indigo-700 p-2 rounded text-white shadow-md"
    &gt;
      {name}
    <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
  );
}
</code></pre>
<p>Our chart application has three buttons for Area Chart, Bar Chart, and Line Chart. This is basically just the component that creates the buttons.</p>
<p>Good, up next will be our <code>CompanyIncomeChart.js</code> file which you can find in the <code>CompanyIncomeChart</code> folder. This file will be taking this code here:</p>
<pre><code class="lang-javascript"><span class="hljs-string">'use client'</span>;

<span class="hljs-keyword">import</span> { useEffect, useRef, useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">'axios'</span>;
<span class="hljs-keyword">import</span> ChartButton <span class="hljs-keyword">from</span> <span class="hljs-string">'../ChartButton/ChartButton'</span>;
<span class="hljs-keyword">import</span> Toggle3DButton <span class="hljs-keyword">from</span> <span class="hljs-string">'../Toggle3DButton/Toggle3DButton'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">CompanyIncome</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> initialData = [
    [<span class="hljs-number">1965</span>, <span class="hljs-number">360202</span>],
    [<span class="hljs-number">1966</span>, <span class="hljs-number">400123</span>],
    [<span class="hljs-number">1967</span>, <span class="hljs-number">460331</span>],
    [<span class="hljs-number">1968</span>, <span class="hljs-number">460346</span>],
    [<span class="hljs-number">1969</span>, <span class="hljs-number">460339</span>],
    [<span class="hljs-number">1970</span>, <span class="hljs-number">460370</span>],
  ];

  <span class="hljs-keyword">const</span> chartRef = useRef(<span class="hljs-literal">null</span>);
  <span class="hljs-keyword">const</span> [chartData, setChartData] = useState([...initialData]);
  <span class="hljs-keyword">const</span> [toggle3D, setToggle3D] = useState(<span class="hljs-literal">false</span>);

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-comment">// Dynamically import Highcharts and its 3D module</span>
    <span class="hljs-keyword">import</span>(<span class="hljs-string">'highcharts/highcharts-3d'</span>).then(<span class="hljs-function">(<span class="hljs-params">Highcharts3D</span>) =&gt;</span> {
      <span class="hljs-keyword">import</span>(<span class="hljs-string">'highcharts'</span>).then(<span class="hljs-function">(<span class="hljs-params">Highcharts</span>) =&gt;</span> {
        Highcharts3D.default(Highcharts.default);

        chartRef.current = Highcharts.default.chart(<span class="hljs-string">'chart-container'</span>, {
          <span class="hljs-attr">colors</span>: [<span class="hljs-string">'#F3F7FB'</span>, <span class="hljs-string">'#F3F7FB'</span>],
          <span class="hljs-attr">chart</span>: {
            <span class="hljs-attr">style</span>: {
              <span class="hljs-attr">fontFamily</span>: [<span class="hljs-string">'Prompt'</span>, <span class="hljs-string">'sans-serif'</span>],
              <span class="hljs-attr">fontSize</span>: <span class="hljs-string">'16px'</span>,
            },
            <span class="hljs-attr">type</span>: <span class="hljs-string">'column'</span>,
            <span class="hljs-attr">options3d</span>: {
              <span class="hljs-attr">enabled</span>: toggle3D,
              <span class="hljs-attr">alpha</span>: <span class="hljs-number">10</span>,
              <span class="hljs-attr">beta</span>: <span class="hljs-number">25</span>,
              <span class="hljs-attr">depth</span>: <span class="hljs-number">70</span>,
              <span class="hljs-attr">viewDistance</span>: <span class="hljs-number">25</span>,
            },
            <span class="hljs-attr">backgroundColor</span>: {
              <span class="hljs-attr">linearGradient</span>: [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">500</span>, <span class="hljs-number">500</span>],
              <span class="hljs-attr">stops</span>: [
                [<span class="hljs-number">0</span>, <span class="hljs-string">'rgb(128, 130, 221)'</span>],
                [<span class="hljs-number">1</span>, <span class="hljs-string">'rgb(128, 130, 221)'</span>],
              ],
            },
          },
          <span class="hljs-attr">title</span>: {
            <span class="hljs-attr">text</span>: <span class="hljs-string">'COMPANY ANNUAL INCOME'</span>,
            <span class="hljs-attr">style</span>: {
              <span class="hljs-attr">fontSize</span>: <span class="hljs-string">'27px'</span>,
            },
          },
          <span class="hljs-attr">xAxis</span>: {
            <span class="hljs-attr">title</span>: {
              <span class="hljs-attr">text</span>: <span class="hljs-string">'Year'</span>,
              <span class="hljs-attr">style</span>: {
                <span class="hljs-attr">color</span>: <span class="hljs-string">'white'</span>,
              },
            },
            <span class="hljs-attr">labels</span>: {
              <span class="hljs-attr">style</span>: {
                <span class="hljs-attr">color</span>: <span class="hljs-string">'white'</span>,
              },
            },
            <span class="hljs-attr">type</span>: <span class="hljs-string">'category'</span>,
          },
          <span class="hljs-attr">yAxis</span>: {
            <span class="hljs-attr">title</span>: {
              <span class="hljs-attr">text</span>: <span class="hljs-string">'Income'</span>,
              <span class="hljs-attr">style</span>: {
                <span class="hljs-attr">color</span>: <span class="hljs-string">'white'</span>,
              },
            },
            <span class="hljs-attr">labels</span>: {
              <span class="hljs-attr">style</span>: {
                <span class="hljs-attr">color</span>: <span class="hljs-string">'white'</span>,
              },
            },
            <span class="hljs-attr">min</span>: <span class="hljs-number">0</span>,
          },
          <span class="hljs-attr">credits</span>: {
            <span class="hljs-attr">text</span>: <span class="hljs-string">''</span>,
          },
          <span class="hljs-attr">series</span>: [
            {
              <span class="hljs-attr">name</span>: <span class="hljs-string">'Income'</span>,
              <span class="hljs-attr">data</span>: chartData,
            },
          ],
          <span class="hljs-attr">animation</span>: {
            <span class="hljs-attr">duration</span>: <span class="hljs-number">100</span>,
          },
        });
      });
    });

    <span class="hljs-keyword">const</span> interval = <span class="hljs-built_in">setInterval</span>(<span class="hljs-keyword">async</span> () =&gt; {
      <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> axios.get(<span class="hljs-string">'/api/pusher'</span>);
        <span class="hljs-keyword">const</span> newDataPoint = [
          chartData[chartData.length - <span class="hljs-number">1</span>][<span class="hljs-number">0</span>] + <span class="hljs-number">1</span>,
          response.data.value,
        ];
        chartRef.current.series[<span class="hljs-number">0</span>].addPoint(newDataPoint, <span class="hljs-literal">true</span>, <span class="hljs-literal">true</span>);
        setChartData(<span class="hljs-function">(<span class="hljs-params">prevData</span>) =&gt;</span> [...prevData, newDataPoint]);
      } <span class="hljs-keyword">catch</span> (error) {
        <span class="hljs-built_in">console</span>.error(<span class="hljs-string">'Error fetching data:'</span>, error);
      }
      <span class="hljs-comment">// Every ten seconds, the setInterval method will get data from the API because its value is set to ten seconds. Pusher's free plans are limited to 200,000 messages a day, so be careful while reducing the interval or you risk exceeding your limit too soon.</span>
    }, <span class="hljs-number">10000</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> {
      <span class="hljs-built_in">clearInterval</span>(interval);
    };
  }, [toggle3D]);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"chart-container"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-full h-96"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"border-solid border-2 border-indigo-500 m-10"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"grid justify-center mt-5 text-center"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>3D Mode<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-20"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Toggle3DButton</span> <span class="hljs-attr">toggle3D</span>=<span class="hljs-string">{toggle3D}</span> <span class="hljs-attr">setToggle3D</span>=<span class="hljs-string">{setToggle3D}</span> /&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"grid gap-4 justify-center mt-5 xl:grid-cols-3 p-4"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">ChartButton</span> <span class="hljs-attr">chartRef</span>=<span class="hljs-string">{chartRef}</span> <span class="hljs-attr">type</span>=<span class="hljs-string">{</span>'<span class="hljs-attr">area</span>'} <span class="hljs-attr">name</span>=<span class="hljs-string">{</span>'<span class="hljs-attr">Area</span> <span class="hljs-attr">Chart</span>'} /&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">ChartButton</span> <span class="hljs-attr">chartRef</span>=<span class="hljs-string">{chartRef}</span> <span class="hljs-attr">type</span>=<span class="hljs-string">{</span>'<span class="hljs-attr">bar</span>'} <span class="hljs-attr">name</span>=<span class="hljs-string">{</span>'<span class="hljs-attr">Bar</span> <span class="hljs-attr">Chart</span>'} /&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">ChartButton</span> <span class="hljs-attr">chartRef</span>=<span class="hljs-string">{chartRef}</span> <span class="hljs-attr">type</span>=<span class="hljs-string">{</span>'<span class="hljs-attr">line</span>'} <span class="hljs-attr">name</span>=<span class="hljs-string">{</span>'<span class="hljs-attr">Line</span> <span class="hljs-attr">Chart</span>'} /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>This component creates our Company Income Chart and is already set with some initial loading data so that the chart loads some data when it first starts. </p>
<p>The file contains our chart configuration settings. There is a function that does a GET request to our Next.js backend which then connects to our Pusher account. This is how the data is retrieved for our chart file. Like on the backend, there is a <code>setInterval</code> function that runs every 10 seconds to get the latest data from our backend (we we can increase or decrease).</p>
<p>Next is the <code>Toggle3DButton.js</code> file in the <code>Toggle3DButton</code> folder. Here is the code for the file:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Toggle3DButton</span>(<span class="hljs-params">{ toggle3D, setToggle3D }</span>) </span>{
  <span class="hljs-keyword">const</span> toggle3DMode = <span class="hljs-function">() =&gt;</span> {
    setToggle3D(!toggle3D);
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center cursor-pointer border border-gray-400 rounded-full p-1 relative"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
        <span class="hljs-attr">type</span>=<span class="hljs-string">"checkbox"</span>
        <span class="hljs-attr">className</span>=<span class="hljs-string">"hidden"</span>
        <span class="hljs-attr">checked</span>=<span class="hljs-string">{toggle3D}</span>
        <span class="hljs-attr">onChange</span>=<span class="hljs-string">{toggle3DMode}</span>
      /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">span</span>
        <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">toggle__line</span> <span class="hljs-attr">w-full</span> <span class="hljs-attr">h-4</span> <span class="hljs-attr">bg-gray-400</span> <span class="hljs-attr">rounded-full</span> <span class="hljs-attr">shadow-inner</span> ${
          <span class="hljs-attr">toggle3D</span> ? '<span class="hljs-attr">bg-green-500</span>' <span class="hljs-attr">:</span> '<span class="hljs-attr">bg-gray-400</span>'
        }`}
      &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">span</span>
        <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">toggle__dot</span> <span class="hljs-attr">absolute</span> <span class="hljs-attr">w-6</span> <span class="hljs-attr">h-6</span> <span class="hljs-attr">bg-white</span> <span class="hljs-attr">rounded-full</span> <span class="hljs-attr">shadow</span> <span class="hljs-attr">inset-y-0</span> ${
          <span class="hljs-attr">toggle3D</span> ? '<span class="hljs-attr">right-0</span>' <span class="hljs-attr">:</span> '<span class="hljs-attr">left-0</span>'
        }`}
      &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span></span>
  );
}
</code></pre>
<p>Our application has a 3D Mode toggle and this component is used to create it.</p>
<p>The main files are completed – we just have three left and then we're done. Next is the <code>globals.css</code> file which needs this code to replace the existing code:</p>
<pre><code class="lang-css"><span class="hljs-keyword">@import</span> url(<span class="hljs-string">'https://fonts.googleapis.com/css2?family=Bebas+Neue&amp;family=Prompt:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&amp;display=swap'</span>);

<span class="hljs-keyword">@tailwind</span> base;
<span class="hljs-keyword">@tailwind</span> components;
<span class="hljs-keyword">@tailwind</span> utilities;

<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">'Prompt'</span>, sans-serif;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">400</span>;
  <span class="hljs-attribute">font-style</span>: normal;
}
</code></pre>
<p>We just imported the <code>Prompt</code> font to use in our application.</p>
<p>Continuing on from before, now we have to update the <code>layout.js</code> file so that it also uses the <code>Prompt</code> font. Add this code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { Prompt } <span class="hljs-keyword">from</span> <span class="hljs-string">'next/font/google'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'./globals.css'</span>;

<span class="hljs-keyword">const</span> prompt = Prompt({ <span class="hljs-attr">subsets</span>: [<span class="hljs-string">'latin'</span>], <span class="hljs-attr">weight</span>: <span class="hljs-string">'400'</span> });

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> metadata = {
  <span class="hljs-attr">title</span>: <span class="hljs-string">'Create Next App'</span>,
  <span class="hljs-attr">description</span>: <span class="hljs-string">'Generated by create next app'</span>,
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">RootLayout</span>(<span class="hljs-params">{ children }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">body</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{prompt.className}</span>&gt;</span>{children}<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></span>
  );
}
</code></pre>
<p>Now we can access the <code>Prompt</code> font throughout our application with this import and update.</p>
<p>All that remains is our <code>page.js</code> file which is the main entry point for all of our components. Replace all the code in that file with this final code shown here:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> CompanyIncomeChart <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/CompanyIncomeChart/CompanyIncomeChart'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">CompanyIncomeChart</span> /&gt;</span></span>;
}
</code></pre>
<p>Alright all done – our project codebase is complete!</p>
<p>Just return to the root project folder and run the command below. Your application should be up and running in a web browser.</p>
<pre><code class="lang-shell">npm run dev
</code></pre>
<p>Everything should be working, and now you should have a realtime chart with live random dates.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Realtime communication platforms are essential for all of the tech that we use. Today you learned about Pusher and how it can help you build realtime data applications that can integrate with a chart library like HighCharts.</p>
<p>There are lots of potential applications you can build with these tools. Today's introduction should open the door and give you many ideas for future projects.</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[ How to Create a Pivot Table in Excel ]]>
                </title>
                <description>
                    <![CDATA[ In Excel, pivot tables let you analyze and visualize your data in an easy way.  With pivot tables, you can make comparisons and create calculations more quickly. You can even create charts to visualize your data. Creating pivot tables might be intimi... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-a-pivot-table-in-excel/</link>
                <guid isPermaLink="false">66adf1106778e7bd69427bfc</guid>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Datatables ]]>
                    </category>
                
                    <category>
                        <![CDATA[ excel ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kolade Chris ]]>
                </dc:creator>
                <pubDate>Wed, 14 Sep 2022 22:35:36 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/09/pivotTable.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In Excel, pivot tables let you analyze and visualize your data in an easy way. </p>
<p>With pivot tables, you can make comparisons and create calculations more quickly. You can even create charts to visualize your data.</p>
<p>Creating pivot tables might be intimidating if you're doing it for the first time. But in this article, I’m going to explain everything you need to start creating pivot tables.</p>
<p>It doesn’t end there – I will also show you how to add charts so you can visualize your data.</p>
<p>In addition, the version of Excel you’re using doesn’t matter. You can even create a Pivot table in Excel 2013. In fact, I used Excel 13 to get set for this article.</p>
<h2 id="heading-what-well-cover">What We'll Cover</h2>
<ul>
<li><a class="post-section-overview" href="#heading-how-to-create-a-pivot-table-in-excel">How to Create a Pivot Table in Excel</a><ul>
<li><a class="post-section-overview" href="#heading-how-to-create-rows-and-make-calculations-with-a-pivot-table">How to Create Rows and Make Calculations with a Pivot Table</a></li>
<li><a class="post-section-overview" href="#heading-how-to-create-entirely-new-rows-with-a-pivot-table">How to Create Entirely New Rows with a Pivot Table</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-how-to-implement-graphical-visualization-for-a-pivot-table">How to Implement Graphical Visualization for a Pivot Table</a></li>
<li><a class="post-section-overview" href="#heading-wrapping-up">Wrapping Up</a></li>
</ul>
<h2 id="heading-how-to-create-a-pivot-table-in-excel">How to Create a Pivot Table in Excel</h2>
<p>To show you how to create a pivot table, I have created a table of some fictional footballers showing:</p>
<ul>
<li>their names</li>
<li>the number of matches they've played</li>
<li>their assists and goals</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss1-3.png" alt="ss1-3" width="600" height="400" loading="lazy"> </p>
<p>I will be creating extra rows of <code>Goal Contributions</code> and <code>Goal Ratio</code>, also called Goals per Game.</p>
<p>In football (Soccer), goal contributions is the total number of goals and assists. The goal ratio is derived when the number of goals is divided by the number of matches played.</p>
<p><strong>To create a pivot table, follow the steps below</strong>:</p>
<p><strong>Step 1</strong>: In the menu bar, click “Insert” and select “Pivot Table”:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss2-3.png" alt="ss2-3" width="600" height="400" loading="lazy"> </p>
<p><strong>Step 2</strong>: Leave everything as it is and select “OK”:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss3-3.png" alt="ss3-3" width="600" height="400" loading="lazy"> </p>
<p>You should use a new worksheet so you can have a dedicated sheet for your pivot table.</p>
<p>The next interface you’ll see looks like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss4-3.png" alt="ss4-3" width="600" height="400" loading="lazy"></p>
<p>You’ll be working with the part where you see “PivotTable Fields”. You will even see the columns of your table there.</p>
<h3 id="heading-how-to-create-rows-and-make-calculations-with-a-pivot-table">How to Create Rows and Make Calculations with a Pivot Table</h3>
<p>This is the part where you can create rows, columns, and make calculations.</p>
<p>To create rows for your pivot table, drag one of the rows in the existing table to the part where you see “ROWS”. </p>
<p>For instance, I want to create a row for the pivot table with the name row of the original table. That means I have to drag the name row to the ROWS area:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss5-3.png" alt="ss5-3" width="600" height="400" loading="lazy"></p>
<p>You can see I’ve created a row with the name row of the original table.</p>
<p>To make calculations easily, you can use the “VALUES” area.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss6-1.png" alt="ss6-1" width="600" height="400" loading="lazy"> </p>
<p>I want to see the number of goals scored by each player. So, I’ll drag the “Goal scored” row to the “VALUES” area:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss7-1.png" alt="ss7-1" width="600" height="400" loading="lazy"></p>
<p>You can see I can directly visualize the number of goals scored by each footballer.</p>
<p>You can also make other calculations in the Values area. Just click the dropdown in front of the column right there and select “Value Field Settings…”:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss8-1.png" alt="ss8-1" width="600" height="400" loading="lazy"></p>
<p>I want to see the highest goal scored instead of the total goals scored by all the players. So I’ll select “MAX” and click “OK”:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss9-1.png" alt="ss9-1" width="600" height="400" loading="lazy"></p>
<p>Now I can see the maximum goals scored instead of the total of all goals scored:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss10-1.png" alt="ss10-1" width="600" height="400" loading="lazy"> </p>
<h3 id="heading-how-to-create-entirely-new-rows-with-a-pivot-table">How to Create Entirely New Rows with a Pivot Table</h3>
<p>Remember I said I would create extra rows of Goal Contributions and Goal Ratio, also called Goals per Game? So, let’s do it.</p>
<p>I need the Assists Created and Goal Scored rows to calculate goal contributions. So, I’ll make sure both of them are in the Values area:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss11-1.png" alt="ss11-1" width="600" height="400" loading="lazy"> </p>
<p>Now, I’ll make sure the “Analyze” tab is selected, click “Fields, Items, &amp; Sets”, then select “Calculated Field…”:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss12-1.png" alt="ss12-1" width="600" height="400" loading="lazy"> </p>
<p>The next interface you’ll see looks like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss13-1.png" alt="ss13-1" width="600" height="400" loading="lazy"> </p>
<p>Here, I’ll do three things:</p>
<ul>
<li>type the name of the row in the name field</li>
<li>write the formula – in this case, “Assists Created + Goal Scored”</li>
<li>click Add and OK</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/new-pivot-table-row.gif" alt="new-pivot-table-row" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss14-1.png" alt="ss14-1" width="600" height="400" loading="lazy"></p>
<p>Now, I have successfully created the Goal Contributions row:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss15-1.png" alt="ss15-1" width="600" height="400" loading="lazy"></p>
<p>To create the Goal Ratio, I have to make sure the matches played row is in the VALUES area:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss16-1.png" alt="ss16-1" width="600" height="400" loading="lazy"> </p>
<p>The formula I’ll use is Goal Scored / Matches played. So, I’ll implement calculated fields again:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss17-1.png" alt="ss17-1" width="600" height="400" loading="lazy"> </p>
<p>I can now see the goal ratio of each footballer:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss18-1.png" alt="ss18-1" width="600" height="400" loading="lazy"> </p>
<h2 id="heading-how-to-implement-graphical-visualization-for-a-pivot-table">How to Implement Graphical Visualization for a Pivot Table</h2>
<p>It’s nice to create a pivot table and implement calculations easily, but it’s nicer to see the graphical representation of that pivot table in a chart.</p>
<p>To represent the pivot table in a chart:</p>
<p><strong>Step 1</strong>: Make sure the “Analyze” tab is selected, then select PivotChart:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss19-1.png" alt="ss19-1" width="600" height="400" loading="lazy"> </p>
<p><strong>Step 2</strong>: Select the type of chart you want on the right. It could be a column, pie chart, or bar chart. Also, select the format in the upper part. It could be 2D or 3D.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss20-1.png" alt="ss20-1" width="600" height="400" loading="lazy"> </p>
<p>Click OK when you are satisfied.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/ss21.png" alt="ss21" width="600" height="400" loading="lazy"></p>
<p>That’s the chart representing the data.</p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>Pivot tables are one of the most powerful features of Excel. If you have large datasets you have to work with, a pivot table can save you a lot of time when it comes to analysis and visualization.</p>
<p>Pivot tables are nice, but being able to create different types of charts to represent the data is really helpful, too.</p>
<p>I hope this article helps you create a pivot table and charts for the data you’re working with.</p>
<p>If you find this article helpful, don’t hesitate to pass it along to others.</p>
 ]]>
                </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[ Line Charts Tutorial – How to Create a Line Graph in JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ By Shachee Swadia Data visualization is a vast field with so many different types of charts to learn and create.  But there are several basic, evergreen graphs that every data designer and web developer dealing with analytics should know how to build... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-make-line-charts-in-javascript/</link>
                <guid isPermaLink="false">66d460f44a0edd9b48e83585</guid>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ data visualization ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 08 Sep 2022 19:58:03 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/09/line-chart.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Shachee Swadia</p>
<p>Data visualization is a vast field with so many different <a target="_blank" href="https://datavizcatalogue.com/">types of charts</a> to learn and create. </p>
<p>But there are several basic, evergreen graphs that every data designer and web developer dealing with analytics should know how to build. </p>
<p>One of them is a <strong>Line Chart</strong> (or <strong>Line Graph</strong>). It's primarily designed to represent data over time.</p>
<p>You can follow along with this tutorial to learn how to quickly create beautiful interactive line (and step-line) charts using JavaScript. We'll look at some cool examples and build them step by step, which will make the process both clear and entertaining. </p>
<p>For your convenience, you can find all of them on <a target="_blank" href="https://codepen.io/collection/pgPwyr">CodePen</a> so you could play with the line charting code further without limits.</p>
<h3 id="heading-our-dataset">Our Dataset</h3>
<p>The last two decades have been nothing short of spectacular in the world of tennis. The <a target="_blank" href="https://en.wikipedia.org/wiki/Big_Three_(tennis)">Big Three</a> — Roger Federer, Rafael Nadal, and Novak Djokovic — have won an astonishing combined 63 (of the past 78) Grand Slam tournaments. These are the most prestigious championships. </p>
<p>I decided to plot their outstanding rivalry. So, the JS line graphs in this tutorial will visualize <strong>the Big Three’s Grand Slam title race</strong>. And the first serve is already coming!</p>
<h1 id="heading-how-to-build-line-charts-in-4-steps"><strong>How to Build Line Charts in 4 Steps</strong></h1>
<p>Generally speaking, the whole process of creating any chart in JavaScript is broken down into four steps, and a line chart is no exception:</p>
<ol>
<li>Make an HTML page with a container.</li>
<li>Include JavaScript files.</li>
<li>Add your data.</li>
<li>Code a visualization.</li>
</ol>
<p>Let's go through each of these steps now.</p>
<h3 id="heading-1-make-an-html-page-with-a-container">1. Make an HTML page with a container</h3>
<p>To start with, you need a place where you want your chart to appear.</p>
<p>If you don’t have one yet, create a basic web page. Then create a container for the line chart — add an HTML block-level element and give it a unique ID for further reference.</p>
<p>Here’s what such a page might look like:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Line Chart JS<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span>&gt;</span><span class="css">      
      <span class="hljs-selector-tag">html</span>, <span class="hljs-selector-tag">body</span>, <span class="hljs-selector-id">#container</span> { 
        <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>; <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>; 
      } 
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>The <code>width</code> and <code>height</code> parameters of the element are set as 100%. As a result, the line chart will render all over the web page. Of course you can set the style settings to your own liking and needs.</p>
<h3 id="heading-2-include-javascript-files">2. Include JavaScript files</h3>
<p>Next, include all JavaScript files, which we'll use to create the line chart, in the <code>&lt;head&gt;</code> section.</p>
<p>There are a whole lot of different <a target="_blank" href="https://en.wikipedia.org/wiki/Comparison_of_JavaScript_charting_libraries">JavaScript charting libraries</a> which let you visualize data in a fast and simple way. Many of them support line charts, and you can choose one or another depending on your project requirements. </p>
<p>For illustration purposes, in this tutorial, I am using <a target="_blank" href="https://www.anychart.com/">AnyChart JS Charts</a>. It’s flexible, comes with extensive <a target="_blank" href="https://docs.anychart.com">charting docs</a> and <a target="_blank" href="https://api.anychart.com">API references</a>, and you can use it for free (unless you are building something for a business.)</p>
<p>For the line chart, I add the “Base” module from the <a target="_blank" href="https://www.anychart.com/download/cdn/">CDN</a>. (Of course, you can download it, put it in a folder on your website, and use your own link in that case.)</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Line Chart JS<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.anychart.com/releases/8.11.0/js/anychart-base.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span>&gt;</span><span class="css">      
      <span class="hljs-selector-tag">html</span>, <span class="hljs-selector-tag">body</span>, <span class="hljs-selector-id">#container</span> { 
        <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>; <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>; 
      } 
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
      <span class="hljs-comment">// JavaScript code for the line chart.</span>
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>The JavaScript code for the line graph will be inserted between <code>&lt;script&gt;</code> and <code>&lt;/script&gt;</code> tags located in the <code>&lt;body&gt;</code> section (you may put those in the <code>&lt;head&gt;</code> section if you want).</p>
<h3 id="heading-3-add-your-data">3. Add your data</h3>
<p>Then, add the data you want to visualize in your line chart.</p>
<p>I counted all <a target="_blank" href="https://en.wikipedia.org/wiki/Big_Three_(tennis)#Grand_Slam_tournaments">Grand Slam singles titles won by Federer, Nadal, and Djokovic</a>, by year. I will add it just like that as an array of arrays. </p>
<p>If you prefer other formats in your particular case, such as JSON, XML, CSV, or something else, check the <a target="_blank" href="https://docs.anychart.com/Working_with_Data/Overview">ways to work with data</a>.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> data = [
  [<span class="hljs-string">"2003"</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
  [<span class="hljs-string">"2004"</span>, <span class="hljs-number">4</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
  [<span class="hljs-string">"2005"</span>, <span class="hljs-number">6</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
  [<span class="hljs-string">"2006"</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>],
  [<span class="hljs-string">"2007"</span>, <span class="hljs-number">12</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>],
  [<span class="hljs-string">"2008"</span>, <span class="hljs-number">13</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>],
  [<span class="hljs-string">"2009"</span>, <span class="hljs-number">15</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1</span>],
  [<span class="hljs-string">"2010"</span>, <span class="hljs-number">16</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>],
  [<span class="hljs-string">"2011"</span>, <span class="hljs-number">16</span>, <span class="hljs-number">10</span>, <span class="hljs-number">4</span>],
  [<span class="hljs-string">"2012"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">11</span>, <span class="hljs-number">5</span>],
  [<span class="hljs-string">"2013"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">13</span>, <span class="hljs-number">6</span>],
  [<span class="hljs-string">"2014"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>, <span class="hljs-number">7</span>],
  [<span class="hljs-string">"2015"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>, <span class="hljs-number">10</span>],
  [<span class="hljs-string">"2016"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>, <span class="hljs-number">12</span>],
  [<span class="hljs-string">"2017"</span>, <span class="hljs-number">19</span>, <span class="hljs-number">16</span>, <span class="hljs-number">12</span>],
  [<span class="hljs-string">"2018"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>],
  [<span class="hljs-string">"2019"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">19</span>, <span class="hljs-number">16</span>],
  [<span class="hljs-string">"2020"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>, <span class="hljs-number">17</span>],
  [<span class="hljs-string">"2021"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>],
  [<span class="hljs-string">"2022"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">22</span>, <span class="hljs-number">20</span>]
];
</code></pre>
<p>In each array, the year is the first parameter (column #0). Then comes the number of titles won by the three players subsequently (cumulative for each).</p>
<h3 id="heading-4-code-a-visualization">4. Code a visualization</h3>
<p>Now, the warm-up session is done, and the court is all set. So let’s get the match started and do some quick JavaScript coding!</p>
<p>First, add <code>anychart.onDocumentReady()</code> as shown below:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
  anychart.onDocumentReady(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// The main JS line charting code will be here.</span>
  });
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>Everything else goes inside of that function.</p>
<p>So, second, include the data (from the previous step).</p>
<p>Third, create a data set and map it for each series (one for each player):</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// create a data set</span>
<span class="hljs-keyword">var</span> dataSet = anychart.data.set(data);

<span class="hljs-comment">// map the data for all series</span>
<span class="hljs-keyword">var</span> firstSeriesData = dataSet.mapAs({<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">1</span>});
<span class="hljs-keyword">var</span> secondSeriesData = dataSet.mapAs({<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">2</span>});
<span class="hljs-keyword">var</span> thirdSeriesData = dataSet.mapAs({<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">3</span>});
</code></pre>
<p>Fourth, create a line chart instance and three series with the mapped data:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// create a line chart</span>
<span class="hljs-keyword">var</span> chart = anychart.line();

<span class="hljs-comment">// create the series and name them</span>
<span class="hljs-keyword">var</span> firstSeries = chart.line(firstSeriesData);
firstSeries.name(<span class="hljs-string">"Roger Federer"</span>);
<span class="hljs-keyword">var</span> secondSeries = chart.line(secondSeriesData);
secondSeries.name(<span class="hljs-string">"Rafael Nadal"</span>);
<span class="hljs-keyword">var</span> thirdSeries = chart.line(thirdSeriesData);
thirdSeries.name(<span class="hljs-string">"Novak Djokovic"</span>);
</code></pre>
<p>Fifth, to make it clear at a glance what is shown in the line chart, a good idea is to add a legend and a title:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// add a legend</span>
chart.legend().enabled(<span class="hljs-literal">true</span>);

<span class="hljs-comment">// add a title</span>
chart.title(<span class="hljs-string">"Big Three's Grand Slam Title Race"</span>);
</code></pre>
<p>Finally, reference the container element ID and draw the resulting line chart:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// specify where to display the chart</span>
chart.container(<span class="hljs-string">"container"</span>);

<span class="hljs-comment">// draw the resulting chart</span>
chart.draw();
</code></pre>
<p>That’s it! A fully functional line graph built with JS is ready. Feels like a <a target="_blank" href="https://sports.answers.com/Q/What_does_straight_sets_mean_in_a_game_of_tennis">straight-sets</a> victory, doesn’t it?</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/line-chart-1.png" alt="A basic multi-line chart, JS." width="600" height="400" loading="lazy">
<em>Line chart showing the Big 3't title race - created with AnyChart</em></p>
<p>Check out this basic version of the line chart with the full HTML/CSS/JS code on <a target="_blank" href="https://codepen.io/shacheeswadia/pen/gOvjVaK">CodePen</a>. Just in case, here’s the code too:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Line Chart JS<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.anychart.com/releases/8.11.0/js/anychart-base.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span>&gt;</span><span class="css">      
      <span class="hljs-selector-tag">html</span>, <span class="hljs-selector-tag">body</span>, <span class="hljs-selector-id">#container</span> { 
        <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>; <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>; 
      } 
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
      anychart.onDocumentReady(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{

        <span class="hljs-comment">// add data</span>
        <span class="hljs-keyword">var</span> data = [
          [<span class="hljs-string">"2003"</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
          [<span class="hljs-string">"2004"</span>, <span class="hljs-number">4</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
          [<span class="hljs-string">"2005"</span>, <span class="hljs-number">6</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
          [<span class="hljs-string">"2006"</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>],
          [<span class="hljs-string">"2007"</span>, <span class="hljs-number">12</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>],
          [<span class="hljs-string">"2008"</span>, <span class="hljs-number">13</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>],
          [<span class="hljs-string">"2009"</span>, <span class="hljs-number">15</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1</span>],
          [<span class="hljs-string">"2010"</span>, <span class="hljs-number">16</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>],
          [<span class="hljs-string">"2011"</span>, <span class="hljs-number">16</span>, <span class="hljs-number">10</span>, <span class="hljs-number">4</span>],
          [<span class="hljs-string">"2012"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">11</span>, <span class="hljs-number">5</span>],
          [<span class="hljs-string">"2013"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">13</span>, <span class="hljs-number">6</span>],
          [<span class="hljs-string">"2014"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>, <span class="hljs-number">7</span>],
          [<span class="hljs-string">"2015"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>, <span class="hljs-number">10</span>],
          [<span class="hljs-string">"2016"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>, <span class="hljs-number">12</span>],
          [<span class="hljs-string">"2017"</span>, <span class="hljs-number">19</span>, <span class="hljs-number">16</span>, <span class="hljs-number">12</span>],
          [<span class="hljs-string">"2018"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>],
          [<span class="hljs-string">"2019"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">19</span>, <span class="hljs-number">16</span>],
          [<span class="hljs-string">"2020"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>, <span class="hljs-number">17</span>],
          [<span class="hljs-string">"2021"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>],
          [<span class="hljs-string">"2022"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">22</span>, <span class="hljs-number">20</span>]
        ];

        <span class="hljs-comment">// create a data set</span>
        <span class="hljs-keyword">var</span> dataSet = anychart.data.set(data);

        <span class="hljs-comment">// map the data for all series</span>
        <span class="hljs-keyword">var</span> firstSeriesData = dataSet.mapAs({<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">1</span>});
        <span class="hljs-keyword">var</span> secondSeriesData = dataSet.mapAs({<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">2</span>});
        <span class="hljs-keyword">var</span> thirdSeriesData = dataSet.mapAs({<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">3</span>});

        <span class="hljs-comment">// create a line chart</span>
        <span class="hljs-keyword">var</span> chart = anychart.line();

        <span class="hljs-comment">// create the series and name them</span>
        <span class="hljs-keyword">var</span> firstSeries = chart.line(firstSeriesData);
        firstSeries.name(<span class="hljs-string">"Roger Federer"</span>);
        <span class="hljs-keyword">var</span> secondSeries = chart.line(secondSeriesData);
        secondSeries.name(<span class="hljs-string">"Rafael Nadal"</span>);
        <span class="hljs-keyword">var</span> thirdSeries = chart.line(thirdSeriesData);
        thirdSeries.name(<span class="hljs-string">"Novak Djokovic"</span>);

        <span class="hljs-comment">// add a legend</span>
        chart.legend().enabled(<span class="hljs-literal">true</span>);

        <span class="hljs-comment">// add a title</span>
        chart.title(<span class="hljs-string">"Big Three's Grand Slam Title Race"</span>);

        <span class="hljs-comment">// specify where to display the chart</span>
        chart.container(<span class="hljs-string">"container"</span>);

        <span class="hljs-comment">// draw the resulting chart</span>
        chart.draw();

      });
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h1 id="heading-how-to-customize-your-line-charts"><strong>How to Customize Your Line Charts</strong></h1>
<p>The basic line chart we created by following the four steps above already looks good. But what if you want to customize it?</p>
<p>Let me show you how to make some changes in the same quick and easy manner.</p>
<h3 id="heading-1-name-the-axes">1. Name the axes</h3>
<p>It is always a good idea to explain what each axis of the line chart represents, even if it seems quite obvious. To add titles to both the X and Y axes, use the following:</p>
<pre><code class="lang-javascript">chart.yAxis().title(<span class="hljs-string">"Titles won"</span>);
chart.xAxis().title(<span class="hljs-string">"Year"</span>);
</code></pre>
<h3 id="heading-2-customize-the-markers">2. Customize the markers</h3>
<p>By default, when you move the mouse pointer over the plot, markers show up on each line series, and their shapes are different. Why not give the markers the same shape? Plus, it would be great to make them smaller.</p>
<p>Look how you can customize the appearance of the line series markers:</p>
<pre><code class="lang-javascript">firstSeries.hovered().markers().type(<span class="hljs-string">"circle"</span>).size(<span class="hljs-number">4</span>);
secondSeries.hovered().markers().type(<span class="hljs-string">"circle"</span>).size(<span class="hljs-number">4</span>);
thirdSeries.hovered().markers().type(<span class="hljs-string">"circle"</span>).size(<span class="hljs-number">4</span>);
</code></pre>
<h3 id="heading-3-enable-crosshairs">3. Enable crosshairs</h3>
<p>Crosshairs are a pair of perpendicular lines following the mouse pointer to help you better understand the X and Y values at any currently hovered point. </p>
<p>In this case, it could be enough to get only one such line, vertical, to highlight the year. Here’s how it’s done:</p>
<pre><code class="lang-javascript">chart.crosshair().enabled(<span class="hljs-literal">true</span>).yStroke(<span class="hljs-literal">null</span>).yLabel(<span class="hljs-literal">false</span>);
</code></pre>
<h3 id="heading-4-change-the-tooltip-position">4. Change the tooltip position</h3>
<p>Currently, the tooltip is following the mouse pointer. But in this situation, it could be better to make it stick to the data points. </p>
<p>To achieve that kind of behavior, just define the line chart tooltip position mode as “point” and fine-tune other position settings to your liking. For example:</p>
<pre><code class="lang-javascript">chart.tooltip().positionMode(<span class="hljs-string">"point"</span>);
chart.tooltip().position(<span class="hljs-string">"right"</span>).anchor(<span class="hljs-string">"left-center"</span>).offsetX(<span class="hljs-number">5</span>).offsetY(<span class="hljs-number">5</span>);
</code></pre>
<p>Check out how the JavaScript line chart looks now after all these customizations. (See it live with the full code on <a target="_blank" href="https://codepen.io/shacheeswadia/pen/vYdaoyR">CodePen</a>.)</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/line-chart-2.png" alt="A custom line chart, JS." width="600" height="400" loading="lazy"></p>
<h3 id="heading-5-change-the-colors">5. Change the colors</h3>
<p>One of the simplest, yet most effective ways to personalize a data visualization is by playing with the colors. </p>
<p>The code below changes the color of each player’s line to the main color of the Grand Slam tournament he has won the most times: Wimbledon’s purple for Federer, French Open’s brown for Nadal, and Australian Open’s blue for Djokovic. Additionally, the thickness of the lines is adjusted.</p>
<pre><code class="lang-javascript">firstSeries.normal().stroke(<span class="hljs-string">"#7b60a2"</span>, <span class="hljs-number">2.5</span>);
secondSeries.normal().stroke(<span class="hljs-string">"#db7346"</span>, <span class="hljs-number">2.5</span>);
thirdSeries.normal().stroke(<span class="hljs-string">"#43a7dc"</span>, <span class="hljs-number">2.5</span>);
</code></pre>
<h3 id="heading-6-improve-the-title-and-legend-text">6. Improve the title and legend text</h3>
<p>The last changes I want to demonstrate in this tutorial — and make the interactive line chart complete — are title and legend customizations.</p>
<p>You can add a subtitle to provide more context, and you can make the text styling more attractive in a few quick tweaks with the help of HTML:</p>
<pre><code class="lang-javascript">chart
  .title()
  .enabled(<span class="hljs-literal">true</span>)
  .useHtml(<span class="hljs-literal">true</span>)
  .text(
    <span class="hljs-string">'&lt;span style="color: #006331; font-size:20px;"&gt;Big Three&amp;#39;s Grand Slam Title Race&lt;/span&gt;'</span> +
      <span class="hljs-string">'&lt;br/&gt;&lt;span style="font-size: 16px;"&gt;(Triumphs at Australian Open, French Open, Wimbledon, U.S. Open)&lt;/span&gt;'</span>
  );
</code></pre>
<p>For the chart legend, it’s easy to modify the font size and the padding:</p>
<pre><code class="lang-javascript">chart.legend().enabled(<span class="hljs-literal">true</span>).fontSize(<span class="hljs-number">14</span>).padding([<span class="hljs-number">10</span>, <span class="hljs-number">0</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>]);
</code></pre>
<p>See what we’ve got! (Check out this JS line chart on <a target="_blank" href="https://codepen.io/shacheeswadia/pen/wvyxVqZ">CodePen</a>.)</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/line-chart-3.png" alt="Advanced line chart built with JavaScript." width="600" height="400" loading="lazy"></p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/shacheeswadia/embed/wvyxVqZ" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<h2 id="heading-how-to-create-a-step-line-chart">How to Create a Step Line Chart</h2>
<p>Just like it’s always more exciting when a tennis match is a five-setter one, here is something extra to make this tutorial and this line graph visualization even more awesome.</p>
<p>From the point of view of data visualization, a stepped line chart will actually work better in this case. And we can make one with just one small modification. </p>
<p>Just change the <code>line()</code> function to <code>stepLine()</code> and your line chart will become your stepped line chart:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// create a stepped line chart</span>
<span class="hljs-keyword">var</span> chart = anychart.stepLine();

<span class="hljs-comment">// create the series and name them</span>
<span class="hljs-keyword">var</span> firstSeries = chart.stepLine(firstSeriesData);
firstSeries.name(<span class="hljs-string">"Roger Federer"</span>);
<span class="hljs-keyword">var</span> secondSeries = chart.stepLine(secondSeriesData);
secondSeries.name(<span class="hljs-string">"Rafael Nadal"</span>);
<span class="hljs-keyword">var</span> thirdSeries = chart.stepLine(thirdSeriesData);
thirdSeries.name(<span class="hljs-string">"Novak Djokovic"</span>);
</code></pre>
<p>Enjoy the elegant JavaScript-powered stepped line chart visualizing the Grand Slam title race between the Big Three in tennis. (Feel free to explore and continue playing with its full source code on <a target="_blank" href="https://codepen.io/shacheeswadia/pen/zYRmXpv">CodePen</a>.)</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/09/line-chart-4.png" alt="A stepped line chart visualizing the Grand Slam title race of Federer, Nadal, and Djokovic. JavaScript HTML5." width="600" height="400" loading="lazy"></p>
<p>And here's the full code:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Line Chart JS<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.anychart.com/releases/8.11.0/js/anychart-base.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span>&gt;</span><span class="css">      
      <span class="hljs-selector-tag">html</span>, <span class="hljs-selector-tag">body</span>, <span class="hljs-selector-id">#container</span> { 
        <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>; <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>; 
      } 
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
      anychart.onDocumentReady(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{

        <span class="hljs-comment">// add data</span>
        <span class="hljs-keyword">var</span> data = [
          [<span class="hljs-string">"2003"</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
          [<span class="hljs-string">"2004"</span>, <span class="hljs-number">4</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
          [<span class="hljs-string">"2005"</span>, <span class="hljs-number">6</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>],
          [<span class="hljs-string">"2006"</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>],
          [<span class="hljs-string">"2007"</span>, <span class="hljs-number">12</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>],
          [<span class="hljs-string">"2008"</span>, <span class="hljs-number">13</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>],
          [<span class="hljs-string">"2009"</span>, <span class="hljs-number">15</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1</span>],
          [<span class="hljs-string">"2010"</span>, <span class="hljs-number">16</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>],
          [<span class="hljs-string">"2011"</span>, <span class="hljs-number">16</span>, <span class="hljs-number">10</span>, <span class="hljs-number">4</span>],
          [<span class="hljs-string">"2012"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">11</span>, <span class="hljs-number">5</span>],
          [<span class="hljs-string">"2013"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">13</span>, <span class="hljs-number">6</span>],
          [<span class="hljs-string">"2014"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>, <span class="hljs-number">7</span>],
          [<span class="hljs-string">"2015"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>, <span class="hljs-number">10</span>],
          [<span class="hljs-string">"2016"</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>, <span class="hljs-number">12</span>],
          [<span class="hljs-string">"2017"</span>, <span class="hljs-number">19</span>, <span class="hljs-number">16</span>, <span class="hljs-number">12</span>],
          [<span class="hljs-string">"2018"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">17</span>, <span class="hljs-number">14</span>],
          [<span class="hljs-string">"2019"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">19</span>, <span class="hljs-number">16</span>],
          [<span class="hljs-string">"2020"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>, <span class="hljs-number">17</span>],
          [<span class="hljs-string">"2021"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>],
          [<span class="hljs-string">"2022"</span>, <span class="hljs-number">20</span>, <span class="hljs-number">22</span>, <span class="hljs-number">20</span>]
        ];

        <span class="hljs-comment">// create a data set</span>
        <span class="hljs-keyword">var</span> dataSet = anychart.data.set(data);

        <span class="hljs-comment">// map the data for all series</span>
        <span class="hljs-keyword">var</span> firstSeriesData = dataSet.mapAs({<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">1</span>});
        <span class="hljs-keyword">var</span> secondSeriesData = dataSet.mapAs({<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">2</span>});
        <span class="hljs-keyword">var</span> thirdSeriesData = dataSet.mapAs({<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">value</span>: <span class="hljs-number">3</span>});

        <span class="hljs-comment">// create a stepped line chart</span>
        <span class="hljs-keyword">var</span> chart = anychart.stepLine();

        <span class="hljs-comment">// create the series and name them</span>
        <span class="hljs-keyword">var</span> firstSeries = chart.stepLine(firstSeriesData);
        firstSeries.name(<span class="hljs-string">"Roger Federer"</span>);
        <span class="hljs-keyword">var</span> secondSeries = chart.stepLine(secondSeriesData);
        secondSeries.name(<span class="hljs-string">"Rafael Nadal"</span>);
        <span class="hljs-keyword">var</span> thirdSeries = chart.stepLine(thirdSeriesData);
        thirdSeries.name(<span class="hljs-string">"Novak Djokovic"</span>);

        <span class="hljs-comment">// add a legend and customize it</span>
        chart.legend().enabled(<span class="hljs-literal">true</span>).fontSize(<span class="hljs-number">14</span>).padding([<span class="hljs-number">10</span>, <span class="hljs-number">0</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>]);

        <span class="hljs-comment">// add a title and customize it</span>
        chart
          .title()
          .enabled(<span class="hljs-literal">true</span>)
          .useHtml(<span class="hljs-literal">true</span>)
          .text(
            <span class="hljs-string">'&lt;span style="color: #006331; font-size:20px;"&gt;Big Three&amp;#39;s Grand Slam Title Race&lt;/span&gt;'</span> +
              <span class="hljs-string">'&lt;br/&gt;&lt;span style="font-size: 16px;"&gt;(Triumphs at Australian Open, French Open, Wimbledon, U.S. Open)&lt;/span&gt;'</span>
          );

        <span class="hljs-comment">// name the axes</span>
        chart.yAxis().title(<span class="hljs-string">"Titles won"</span>);
        chart.xAxis().title(<span class="hljs-string">"Year"</span>);

        <span class="hljs-comment">// customize the series markers</span>
        firstSeries.hovered().markers().type(<span class="hljs-string">"circle"</span>).size(<span class="hljs-number">4</span>);
        secondSeries.hovered().markers().type(<span class="hljs-string">"circle"</span>).size(<span class="hljs-number">4</span>);
        thirdSeries.hovered().markers().type(<span class="hljs-string">"circle"</span>).size(<span class="hljs-number">4</span>);

        <span class="hljs-comment">// turn on crosshairs and remove the y hair</span>
        chart.crosshair().enabled(<span class="hljs-literal">true</span>).yStroke(<span class="hljs-literal">null</span>).yLabel(<span class="hljs-literal">false</span>);

        <span class="hljs-comment">// change the tooltip position</span>
        chart.tooltip().positionMode(<span class="hljs-string">"point"</span>);
        chart.tooltip().position(<span class="hljs-string">"right"</span>).anchor(<span class="hljs-string">"left-center"</span>).offsetX(<span class="hljs-number">5</span>).offsetY(<span class="hljs-number">5</span>);

        <span class="hljs-comment">// customize the series stroke in the normal state</span>
        firstSeries.normal().stroke(<span class="hljs-string">"#7b60a2"</span>, <span class="hljs-number">2.5</span>);
        secondSeries.normal().stroke(<span class="hljs-string">"#db7346"</span>, <span class="hljs-number">2.5</span>);
        thirdSeries.normal().stroke(<span class="hljs-string">"#43a7dc"</span>, <span class="hljs-number">2.5</span>);

        <span class="hljs-comment">// specify where to display the chart</span>
        chart.container(<span class="hljs-string">"container"</span>);

        <span class="hljs-comment">// draw the resulting chart</span>
        chart.draw();

      });
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/shacheeswadia/embed/zYRmXpv" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<h1 id="heading-conclusion"><strong>Conclusion</strong></h1>
<p>As you can see in this tutorial, creating interactive line (and stepped line) charts with JavaScript can be pretty straightforward. Let me know if you have any questions or suggestions.</p>
<p>It is motivating to see how these greats have achieved so much in their professional life. </p>
<p>Let’s use this inspiration to get ahead in the field of data visualization development by building even more (and all the more awesome) charts and graphs!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Chart.js Tutorial – How to Make Bar and Line Charts in Angular ]]>
                </title>
                <description>
                    <![CDATA[ By Swatej Patil In this tutorial we will learn how to create simple bar and line charts using the Chart.js library in an Angular application. But first of all, what is Chart.js and what does it do? What is Chart.js? Chart.js is a JavaScript library f... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-make-bar-and-line-charts-using-chartjs-in-angular/</link>
                <guid isPermaLink="false">66d4614da326133d12440a8e</guid>
                
                    <category>
                        <![CDATA[ Angular ]]>
                    </category>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 23 Aug 2022 15:35:01 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/08/Line-Bar-4.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Swatej Patil</p>
<p>In this tutorial we will learn how to create simple bar and line charts using the Chart.js library in an Angular application.</p>
<p>But first of all, what is Chart.js and what does it do?</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 powerful enough to build complex visualizations. It has a wide range of chart types including bar charts, line charts, pie charts, scatter plots, and many more. </p>
<p>ChartJs is open-source and you can use it without any restrictions on private or commercial projects. It's written in pure JavaScript and has no dependencies on other libraries such as jQuery or D3. </p>
<p>It also provides options for animating data updates and adding interactivity to charts such as tooltips and click events.</p>
<p>It was created by the team at Plotly and it’s free to use.</p>
<p>Now we can move on with creating our first bar graph in Chart.js</p>
<p>##Prerequisites
Before we get started making the charts, just make sure you meet the following prerequisites:</p>
<ul>
<li>You have a basic understanding of HTML, CSS, and TypeScript, especially Object Oriented concepts of TypeScript classes and methods.</li>
<li>You have 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>Angular CLI is an official tool from the developers of Angular. It is a command-line interface tool which is very useful for initialising and developing Angular applications.  </p>
<p>You can install Angular CLI by executing the following command in a terminal or powershell window:</p>
<pre><code class="lang-shell">npm install -g @angular/cli
</code></pre>
<h2 id="heading-how-to-create-an-angular-application">How to Create an 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 you've created your angular application, 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 there.</p>
<p>Now open a new terminal window in the same directory and install the Chart.js library using the following command:</p>
<pre><code class="lang-bash">npm install chart.js
</code></pre>
<p>Now we need to create two angular components: one for the Bar graph and the other for the line chart. </p>
<p>You can create them easily with the Angular CLI by executing the following commands:</p>
<pre><code class="lang-bash">ng generate component bar-chart
ng generate component line-chart
</code></pre>
<h2 id="heading-how-to-create-a-bar-graph-with-chartjs">How to Create a Bar Graph with Chart.js</h2>
<p>A bar graph is a graphical representation of data, in which the horizontal and vertical axes represent the values, and the length of each bar represents the values specified on the axis. </p>
<p>Bar graphs are one of the most common forms of graphs when it comes to representing data visually. We use them to identify trends and patterns in various data sets.</p>
<p>When to use bar charts:</p>
<ul>
<li>To show multiple values at once</li>
<li>To provide a graphical representation of data</li>
<li>To compare datasets</li>
<li>To examine relationships between different variables.</li>
</ul>
<p>A bar chart is often used in business and economics to show comparisons between products, services, or companies. In our example, we have taken the data of sales and profits made on each day during an 8-day period.</p>
<p>Now that we have created the components, we will move on with creating the bar chart.</p>
<p>Inside the bar-chart component, open the <code>bar-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 in the canvas.</p>
<p>Inside the bar-chart component, open the <code>bar-chart.component.ts</code> file and import the Chart.js library using the following commands:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Chart <span class="hljs-keyword">from</span> <span class="hljs-string">'chart.js/auto'</span>;
<span class="hljs-comment">//or</span>
<span class="hljs-keyword">import</span> Chart <span class="hljs-keyword">from</span> <span class="hljs-string">'chart.js'</span>;
</code></pre>
<p>Now let’s make the <code>chart</code> veritable we mentioned earlier. This variable will hold the information of our graphs.</p>
<pre><code class="lang-js">public chart: any;
</code></pre>
<p>Now we will create a method for the bar chart. This will include the data and labels for our bar chart. Copy the following code and paste it in the <code>bar-chart.component.ts</code> file below the <code>ngOnInit()</code> function:</p>
<pre><code class="lang-js">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">'bar'</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">'2022-05-10'</span>, <span class="hljs-string">'2022-05-11'</span>, <span class="hljs-string">'2022-05-12'</span>,<span class="hljs-string">'2022-05-13'</span>,
                                 <span class="hljs-string">'2022-05-14'</span>, <span class="hljs-string">'2022-05-15'</span>, <span class="hljs-string">'2022-05-16'</span>,<span class="hljs-string">'2022-05-17'</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">'576'</span>],
            <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'blue'</span>
          },
          {
            <span class="hljs-attr">label</span>: <span class="hljs-string">"Profit"</span>,
            <span class="hljs-attr">data</span>: [<span class="hljs-string">'542'</span>, <span class="hljs-string">'542'</span>, <span class="hljs-string">'536'</span>, <span class="hljs-string">'327'</span>, <span class="hljs-string">'17'</span>,
                                     <span class="hljs-string">'0.00'</span>, <span class="hljs-string">'538'</span>, <span class="hljs-string">'541'</span>],
            <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'limegreen'</span>
          }  
        ]
      },
      <span class="hljs-attr">options</span>: {
        <span class="hljs-attr">aspectRatio</span>:<span class="hljs-number">2.5</span>
      }

    });
  }
</code></pre>
<p>The above code might look daunting at first, but it's quite simple. We are providing the type of chart, the labels, and the data.</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> like this:</p>
<pre><code class="lang-ts">ngOnInit(): <span class="hljs-built_in">void</span> {
    <span class="hljs-built_in">this</span>.createChart();
  }
</code></pre>
<p>Finally, we need to add the HTML selector of the bar-chart component to the <code>app.component.html file.</code>. Remove the initial Angular template code and add the following:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">app-bar-chart</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">app-bar-chart</span>&gt;</span>
</code></pre>
<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/2022/08/barchart.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-create-a-line-graph-with-chartjs">How to Create a Line Graph with Chart.js</h2>
<p>Line charts similar to bar charts are often used to show trends in data over time, such as economic growth or changes in stock prices. </p>
<p>Line charts are also useful for showing changes in quantities, such as population over time or weight loss over a period of weeks. </p>
<p>With a line graph, as opposed to a bar graph, you draw individual points on the two axes and connect nearby points with straight lines.</p>
<p>When to use bar charts:</p>
<ul>
<li>When the differences in data points are smaller</li>
<li>To show trends over the course of time</li>
</ul>
<p>If you have created the bar graph, then creating a line graph is quite simple. Just follow the same steps you have done so far (and make sure to do them on the line chart component).</p>
<p>Paste the same code for <code>createChart()</code> method on the <code>line-chart.component.ts</code> file below the <code>ngOnInit()</code> function. You just need to change the keyword for type of chart from <code>bar</code> to <code>line</code>. </p>
<p>Your code should look like the following:</p>
<pre><code class="lang-ts">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-keyword">type</span>: <span class="hljs-string">'line'</span>, <span class="hljs-comment">//this denotes tha type of chart</span>

      data: {<span class="hljs-comment">// values on X-Axis</span>
        labels: [<span class="hljs-string">'2022-05-10'</span>, <span class="hljs-string">'2022-05-11'</span>, <span class="hljs-string">'2022-05-12'</span>,<span class="hljs-string">'2022-05-13'</span>,
                                 <span class="hljs-string">'2022-05-14'</span>, <span class="hljs-string">'2022-05-15'</span>, <span class="hljs-string">'2022-05-16'</span>,<span class="hljs-string">'2022-05-17'</span>, ], 
           datasets: [
          {
            label: <span class="hljs-string">"Sales"</span>,
            data: [<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">'576'</span>],
            backgroundColor: <span class="hljs-string">'blue'</span>
          },
          {
            label: <span class="hljs-string">"Profit"</span>,
            data: [<span class="hljs-string">'542'</span>, <span class="hljs-string">'542'</span>, <span class="hljs-string">'536'</span>, <span class="hljs-string">'327'</span>, <span class="hljs-string">'17'</span>,
                                     <span class="hljs-string">'0.00'</span>, <span class="hljs-string">'538'</span>, <span class="hljs-string">'541'</span>],
            backgroundColor: <span class="hljs-string">'limegreen'</span>
          }  
        ]
      },
      options: {
        aspectRatio:<span class="hljs-number">2.5</span>
      }

    });
  }
</code></pre>
<p>Call the <code>createChart()</code> function in <code>ngOnInit()</code> and your line chart is ready.</p>
<pre><code class="lang-ts">ngOnInit(): <span class="hljs-built_in">void</span> {
    <span class="hljs-built_in">this</span>.createChart();
  }
</code></pre>
<p>Finally, we need to add the HTML selector of 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-line-chart</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">app-line-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/2022/08/linechart.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The full code for this Angular application is hosted on my <a target="_blank" href="https://github.com/SwatejPatil/Bar-and-Line-Charts-using-ChartJs-in-Angular">GitHub Repo</a>. </p>
<p>I hope you found this tutorial helpful. Thank you for reading!</p>
<p>Photo by <a target="_blank" href="https://unsplash.com/@lukechesser">Luke Chesser</a> | Unsplash</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Think Less About Data Visualization ]]>
                </title>
                <description>
                    <![CDATA[ By Allan Campopiano In your native tongue, you are not aware of the translation between thought and speech. You think about what you want to say and say it. The pathway from thought to speech feels direct. It's an instantaneous, 1:1 mapping. When it ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/data-viz-from-thought-to-chart/</link>
                <guid isPermaLink="false">66d84dd6e86088251dd27baf</guid>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ data visualization ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 26 Jan 2022 19:57:11 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/02/cover_image_campo_viz.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Allan Campopiano</p>
<p>In your native tongue, you are not aware of the translation between thought and speech. You think about what you want to say and say it. The pathway from thought to speech feels direct. It's an instantaneous, 1:1 mapping.</p>
<p>When it comes to data visualization, however, there are often many stages of conscious translation between your idea of a chart to the actual graphics on the screen. </p>
<p>Consider these common examples of translation:</p>
<ul>
<li>The dataset that undergirds your hypothesis requires transformation before you can display it as a chart.</li>
<li>Your mental image of the chart has to be translated into drop-down menus and checkboxes in Excel.</li>
<li>You feel yourself scrolling through a mental database of function signatures and APIs (how do I make a pie chart in Matplotlib again?). Good thing you bookmarked the docs, but still...</li>
</ul>
<p>Is there a way we can think less about the process of producing charts? Are there heuristics we can follow that make data visualization feel more like speaking our first language? </p>
<p>Thanks to the pioneering work of the late Leland Wilkinson, the answer to these questions is <em>Yes</em>. </p>
<p>In 1999, Leland wrote the book "<a target="_blank" href="https://link.springer.com/book/10.1007/0-387-28695-0">The Grammar of Graphics</a>" which not only introduced a fresh way of reasoning about charts but it also provided the mathematical underpinnings we need to write powerful, high-level data visualization programs. </p>
<p>Leland forever changed how we think about data visualization. </p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/1X93Sum_SyM" 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>In this article, I will show by example how to approach data visualization using the concepts of The Grammar of Graphics. The goal is to provide a structured and reusable way of thinking that supports getting from the chart in your mind's eye to the chart on the screen. </p>
<p>In addition, I provide a <a target="_blank" href="https://deepnote.com/project/Intro-to-Altair-challenges-with-code-oiylaVDoSXu1a75xovuFRw/%2Fintro_to_altair.ipynb/#00013-e392a6f0-9399-4e81-884d-de75fcd01cc1"><strong>this notebook</strong></a> in <a target="_blank" href="http://deepnote.com/">Deepnote</a> that contains 30 challenges, with increasing difficulty, to help you test your new data visualization knowledge. The notebook runs in the cloud so it's ready to use immediately (no installation or setup required).</p>
<h2 id="heading-comparing-visualization-paradigms">Comparing Visualization Paradigms</h2>
<p>When thinking of data visualization, consider the following paradigms, especially in terms of how they affect <strong>data exploration</strong>. </p>
<p>For the sake of example, suppose you wanted to plot three categories as points on the X-Y plane (that is, a colored scatter chart).</p>
<p><strong>Imperative visualization</strong> specifies <em>how</em> something should be done. For example, begin by instantiating a blank canvas. Use a <em>for loop</em> statement to cycle through a list that maps categories to colors. Draw onto the canvas with each iteration, selecting the correct data based on the mapping. Set the axis labels and legend location.</p>
<p>Here's an example of imperative visualization using <a target="_blank" href="https://matplotlib.org/">Matplotlib</a>:</p>
<figure>
  <div class="embed-wrapper"><iframe src="https://embed.deepnote.com/b1f93d15-b954-486c-8dd0-098f619c912d/dc93c85d-ec8d-4e37-aee7-44bbdf56c95a/1278ef92-dadd-40d6-848b-da7cfe1451f7?height=557.9857788085938" height="557.9857788085938" width="100%" title="Embedded content" loading="lazy"></iframe></div>
</figure>


<p><strong>Declarative visualization</strong> specifies <em>what</em> should be done. For example, tell the program that you want to represent two quantitative columns as X-Y points. A third categorial column will be used to encode color. </p>
<p>Here's an example of declarative visualization using <a target="_blank" href="https://altair-viz.github.io/">Altair</a>:</p>
<figure>
  <div class="embed-wrapper"><iframe src="https://embed.deepnote.com/b1f93d15-b954-486c-8dd0-098f619c912d/dc93c85d-ec8d-4e37-aee7-44bbdf56c95a/a8bd3657-5e74-4415-aaef-df05c61e7b3d?height=491.960205078125" height="491.960205078125" width="100%" title="Embedded content" loading="lazy"></iframe></div>
</figure>


<p>As you can see above, both examples represent the data in similar ways. The path to getting there, however, is very different. </p>
<p>The imperative paradigm is low level. It provides fine-grained control over the chart at the cost of being verbose. Notice that many details have to be specified manually (for example, legend location, axis labels, and so on).  </p>
<p>The declarative paradigm, by contrast, is high level. It does not require much information in order to produce a chart (notice that sensible defaults are chosen by the underlying program rather than having to be explicitly written in code). </p>
<p>This may come at the cost of having ultimate control over every detail on the canvas. But a good implementation of The Grammar of Graphics can produce a <a target="_blank" href="https://vega.github.io/vega-lite/examples/">staggering variety of charts</a> for many use cases. High-level, declarative visualization libraries allow users to get from thought to chart with fewer programmatic steps. </p>
<p>As you will see in the next section, the benefits don't stop at reducing the lines of code. They extend to the way we think about visualization to begin with.</p>
<blockquote>
<p><strong>Declarative visualization</strong> lets you think about <strong>data and relationships</strong>, rather than incidental details. – Jake VanderPlas (<a target="_blank" href="https://speakerdeck.com/jakevdp/bespoke-visualizations-with-a-declarative-twist?slide=28">source</a>)</p>
</blockquote>
<h2 id="heading-why-you-should-avoid-taxonomies-of-charts">Why You Should Avoid Taxonomies of Charts</h2>
<p>A taxonomy of charts is a way of organizing charts into groups based on their function and appearance. They may seem helpful at first, but chart taxonomies can be insidious. They way in which they group charts together is inconsistent, though, perceptually this isn't clear at first sight. </p>
<p>Put another way, this method of grouping charts does not follow a well-defined set of logical (mathematical) rules. I will explain <strong>why this matters</strong>, but first, let me provide two canonical examples of how a chart taxonomy fails.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/01/Screen-Shot-2022-01-24-at-2.59.42-PM.png" alt="Image" width="600" height="400" loading="lazy">
<em>Chart taxonomy (<a target="_blank" href="https://datavizproject.com/">source</a>)</em></p>
<blockquote>
<p>... and I came to realize that these taxonomies of charts are not only useless, they're <strong>harmful</strong>. – Leland Wilkinson (<a target="_blank" href="https://www.slideshare.net/0xdata/leland-wilkinson-h2oai-the-grammar-of-graphics-and-the-future-of-big-data-visualization-h2o-world-2019-nyc">source</a>)</p>
</blockquote>
<p>The following two charts are considered <em>different</em> according to chart taxonomies:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/01/v1-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>However, both charts are actually more similar than they are different. The left chart uses Cartesian coordinates and the top chart uses polar coordinates. That's the only difference. </p>
<p>Let's look at the opposite problem. The following two charts are considered <em>similar</em> according to chart taxonomies:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/01/v2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>However, upon closer inspection, these charts are not similar at all in terms of how they represent data. The left chart uses angles to represent quantities in the data. The right chart, instead, uses the length of the segments to represent the data while keeping the angles the same.</p>
<h3 id="heading-why-does-this-matter">Why does this matter?</h3>
<p>Imagine a visualization program that uses its own idea of a chart taxonomy to map onto GUI menus and/or function calls. For each arbitrarily defined type of chart, the program would have to have a corresponding menu/function (for example, a function for a pie chart, a function for a bar chart, a function for a histogram, and so on). </p>
<p>Sure, some of these functions could be combined but the devil's in the details (that is, which rules govern whether or not to give charts their own function?).  Such a program will eventually become inconsistent and bloated, as noted by Wilkinson.</p>
<p>If, on the other hand, we follow The Grammar of Graphics approach, which provides rigorous, mathematical rules for reasoning about data visualization, we may just end up with libraries and GUIs that are more internally consistent and less bloated. This makes them easier to use and maintain. </p>
<p>For example, take a look at these two charts and their associated code:</p>
<figure>
  <div class="embed-wrapper"><iframe src="https://embed.deepnote.com/b1f93d15-b954-486c-8dd0-098f619c912d/dc93c85d-ec8d-4e37-aee7-44bbdf56c95a/a8bd3657-5e74-4415-aaef-df05c61e7b3d?height=491.960205078125" height="491.960205078125" width="100%" title="Embedded content" loading="lazy"></iframe></div>
</figure>


<figure>
  <div class="embed-wrapper"><iframe src="https://embed.deepnote.com/b1f93d15-b954-486c-8dd0-098f619c912d/dc93c85d-ec8d-4e37-aee7-44bbdf56c95a/936d014c-42cf-4b40-aa23-915bdc10032f?height=491.960205078125" height="491.960205078125" width="100%" title="Embedded content" loading="lazy"></iframe></div>
</figure>

<p>Both charts are based on the same underlying data. Despite the charts having a very different visual appearance (one is a scatter chart, one is a bar chart with aggregation), they are produced with remarkably similar code. </p>
<p>Notice that the API is not governed by a chart taxonomy that maps onto a set of function calls. As a result, if implemented well, the grammar-based approach allows for consistent and easy-to-remember APIs. </p>
<p>For example, for many statistical charts in Altair, it is often enough to remember only <strong>two statements</strong> for translating thought into charts.</p>
<p><strong>Which geometric shape do I want?</strong> </p>
<p>Bar, circle, line, arc, etc...</p>
<p><strong>How do I want to represent the columns in my data?</strong></p>
<p>Color, shape, size, position, etc...</p>
<p>Using the scatter chart example above, you could say the following:</p>
<blockquote>
<p>I want to use a <strong>circle</strong> as my geometric shape  </p>
<p>I want to <strong>represent</strong> petalLength on the <strong>X axis,</strong> petalWidth on the <strong>Y axis,</strong> and species as <strong>color</strong></p>
</blockquote>
<p>If you compare the associated code to these statements, you can see that they map naturally onto each other, without the need for much translation between thought, code, and chart.</p>
<pre><code class="lang-python">alt.Chart(df).mark_circle().encode(
    x=<span class="hljs-string">'petalLength'</span>,
    y=<span class="hljs-string">'petalWidth'</span>,
    color=<span class="hljs-string">'species'</span>
)
</code></pre>
<h2 id="heading-call-to-action">Call To Action</h2>
<p>Grammar-based visualization is not a panacea. But once you learn to think in this way, making charts can feel a lot more like speaking a language. </p>
<p>The graphics you see in your mind can end up on the screen without first having to go through several stages of translation where the resulting code ends up looking very different from how you would describe the chart with words. </p>
<p>Once The Grammar of Graphics is a part of your repertoire, you'll not only be faster at data exploration but your reasoning about data visualization will change, becoming more efficient and consistent. </p>
<p>This is <strong>not just about learning a new tool</strong> but rather, it's about changing the way we think about data visualization. After all, extending an idea from a famous quote:</p>
<blockquote>
<p>A language that doesn't affect the way you think about programming, is not worth knowing. – Alan J. Perlis</p>
</blockquote>
<p>To get you started with this new way of thinking, I've prepared <a target="_blank" href="https://deepnote.com/project/Intro-to-Altair-challenges-with-code-oiylaVDoSXu1a75xovuFRw/%2Fintro_to_altair.ipynb"><strong>this notebook</strong></a> in <a target="_blank" href="http://deepnote.com/">Deepnote</a>. The notebook runs in the cloud so it's ready to use immediately (no installation or setup). </p>
<p>You'll find 30 Grammar Of Graphics challenges, with increasing difficulty, waiting for you. Good luck!</p>
<p><a target="_blank" href="https://altair-viz.github.io/">Cover image source</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ What is a Helm Chart? A Tutorial for Kubernetes Beginners ]]>
                </title>
                <description>
                    <![CDATA[ By Lucas Santos Kubernetes is a very helpful tool for cloud-native developers. But it doesn't cover all the bases on its own – there are some things that Kubernetes cannot solve or that are outside its scope. This is one of the reasons why open sourc... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-a-helm-chart-tutorial-for-kubernetes-beginners/</link>
                <guid isPermaLink="false">66d419ac5f4570de9ac0c420</guid>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ containers ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Helm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Kubernetes ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 09 Mar 2021 18:44:46 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/604640a8a7946308b768453d.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Lucas Santos</p>
<p><a target="_blank" href="https://azure.microsoft.com/services/kubernetes-service/?WT.mc_id=containers-19838-ludossan">Kubernetes</a> is a very helpful tool for cloud-native developers. But it doesn't cover all the bases on its own – there are some things that Kubernetes cannot solve or that are outside its scope.</p>
<p>This is one of the reasons why open source projects are so great. They help amazing tools become even more amazing when we combine them with other awesome open-source tools. And often these tools were developed for the sole purpose of filling the gaps. One of these tools is Helm.</p>
<h2 id="heading-what-is-helm">What is Helm?</h2>
<p><a target="_blank" href="https://helm.sh">Helm</a> is widely known as "the package manager for <a target="_blank" href="https://azure.microsoft.com/services/kubernetes-service/?WT.mc_id=containers-19838-ludossan">Kubernetes</a>". Although it presents itself like this, its scope goes way beyond that of a simple package manager. However, let's start at the beginning.</p>
<p>Helm is an open-source project which was originally created by <a target="_blank" href="https://deislabs.io/">DeisLabs</a> and donated to <a target="_blank" href="https://azure.microsoft.com/blog/announcing-cncf/?WT.mc_id=containers-19838-ludossan">CNCF</a>, which now maintains it. The original goal of Helm was to provide users with a better way to manage all the <a target="_blank" href="https://azure.microsoft.com/services/kubernetes-service/?WT.mc_id=containers-19838-ludossan">Kubernetes</a> YAML files we create on <a target="_blank" href="https://azure.microsoft.com/services/kubernetes-service/?WT.mc_id=containers-19838-ludossan">Kubernetes</a> projects.</p>
<p>The path <a target="_blank" href="https://docs.microsoft.com/azure/aks/kubernetes-helm?WT.mc_id=containers-19838-ludossan">Helm</a> took to solve this issue was to create Helm <strong><a target="_blank" href="https://docs.microsoft.com/azure/aks/kubernetes-helm?WT.mc_id=containers-19838-ludossan">Charts</a></strong>. Each chart is a bundle with one or more <a target="_blank" href="https://azure.microsoft.com/services/kubernetes-service/?WT.mc_id=containers-19838-ludossan">Kubernetes</a> manifests – a chart can have child charts and dependent charts as well. </p>
<p>This means that Helm installs the whole dependency tree of a project if you run the install command for the top-level chart. You just a single command to install your entire application, instead of listing the files to install via <code>kubectl</code>.</p>
<p><a target="_blank" href="https://docs.microsoft.com/azure/aks/kubernetes-helm?WT.mc_id=containers-19838-ludossan">Charts</a> allow you to version your manifest files too, just like we do with Node.js or any other package. This lets you install specific chart versions, which means keeping specific configurations for your infrastructure in the form of code. </p>
<p>Helm also keeps a release history of all deployed charts, so you can go back to a previous release if something went wrong.</p>
<p><a target="_blank" href="https://docs.microsoft.com/azure/aks/kubernetes-helm?WT.mc_id=containers-19838-ludossan">Helm</a> supports <a target="_blank" href="https://azure.microsoft.com/services/kubernetes-service/?WT.mc_id=containers-19838-ludossan">Kubernetes</a> natively, which means you don't have to write any complex syntax files or anything to start using Helm. Just drop your template files into a new chart and you're good to go.</p>
<p>But why should we use it? Managing application manifests can be easily done with a few combinations of commands.</p>
<h2 id="heading-why-should-you-use-helm">Why Should You Use Helm?</h2>
<p>Helm really shines where Kubernetes didn't go. For instance, templating. The scope of the Kubernetes project is to deal with your containers for you, not your template files. </p>
<p>This makes it overly difficult to create truly generic files to be used across a large team or a large organization with many different parameters that need to be set for each file. </p>
<p>And also, how do you version sensitive information using Git when template files are plain text?</p>
<p>The answer: Go templates. Helm allows you to add variables and use functions inside your template files. This makes it perfect for scalable applications that'll eventually need to have their parameters changed. Let's look at an example.</p>
<p>I have an open-source project called <a target="_blank" href="https://github.com/khaosdoctor/zaqar/">Zaqar</a>, a simple email microservice for Node.js which communicates with SendGrid. The project is basically composed of a service, a deployment and an autoscaler. </p>
<p>Let's take the deployment file as the example. I'd have something like this:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">apiVersion:</span> <span class="hljs-string">apps/v1</span>
<span class="hljs-attr">kind:</span> <span class="hljs-string">Deployment</span>
<span class="hljs-attr">metadata:</span>
  <span class="hljs-attr">name:</span> <span class="hljs-string">zaqar</span>
  <span class="hljs-attr">namespace:</span> <span class="hljs-string">default</span>
  <span class="hljs-attr">labels:</span>
    <span class="hljs-attr">app:</span> <span class="hljs-string">zaqar</span>
    <span class="hljs-attr">version:</span> <span class="hljs-string">v1.0.0</span>
    <span class="hljs-attr">env:</span> <span class="hljs-string">production</span>
<span class="hljs-attr">spec:</span>
  <span class="hljs-attr">replicas:</span> <span class="hljs-number">1</span>
  <span class="hljs-attr">selector:</span>
    <span class="hljs-attr">matchLabels:</span>
      <span class="hljs-attr">app:</span> <span class="hljs-string">zaqar</span>
      <span class="hljs-attr">env:</span> <span class="hljs-string">production</span>
  <span class="hljs-attr">template:</span>
    <span class="hljs-attr">metadata:</span>
      <span class="hljs-attr">labels:</span>
        <span class="hljs-attr">app:</span> <span class="hljs-string">zaqar</span>
        <span class="hljs-attr">version:</span> <span class="hljs-string">v1.0.0</span>
        <span class="hljs-attr">env:</span> <span class="hljs-string">production</span>
    <span class="hljs-attr">spec:</span>
      <span class="hljs-attr">containers:</span>
        <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">zaqar</span>
          <span class="hljs-attr">image:</span> <span class="hljs-string">"khaosdoctor/zaqar:v1.0.0"</span>
          <span class="hljs-attr">imagePullPolicy:</span> <span class="hljs-string">IfNotPresent</span>
          <span class="hljs-attr">env:</span>
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">SENDGRID_APIKEY</span>
              <span class="hljs-attr">value:</span> <span class="hljs-string">"MY_SECRET_KEY"</span>
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">DEFAULT_FROM_ADDRESS</span>
              <span class="hljs-attr">value:</span> <span class="hljs-string">"my@email.com"</span>
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">DEFAULT_FROM_NAME</span>
              <span class="hljs-attr">value:</span> <span class="hljs-string">"Lucas Santos"</span>
          <span class="hljs-attr">ports:</span>
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">http</span>
              <span class="hljs-attr">containerPort:</span> <span class="hljs-number">3000</span>
              <span class="hljs-attr">protocol:</span> <span class="hljs-string">TCP</span>
          <span class="hljs-attr">resources:</span>
            <span class="hljs-attr">requests:</span>
              <span class="hljs-attr">cpu:</span> <span class="hljs-string">100m</span>
              <span class="hljs-attr">memory:</span> <span class="hljs-string">128Mi</span>
            <span class="hljs-attr">limits:</span>
              <span class="hljs-attr">cpu:</span> <span class="hljs-string">250m</span>
              <span class="hljs-attr">memory:</span> <span class="hljs-string">256Mi</span>
</code></pre>
<p>If I wanted to use this template on a <a target="_blank" href="https://docs.microsoft.com/learn/modules/aks-deployment-pipeline-github-actions/?WT.mc_id=containers-19838-ludossan">CI pipeline</a>, or publish it on my <a target="_blank" href="https://github.com/khaosdoctor">GitHub</a>, I'd need to replace the variable parts with placeholders. So we can replace these texts with the required information. </p>
<p>In this case, both the version tag, the <code>env</code> label, and the environment variables would be replaced by placeholders, like this:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">apiVersion:</span> <span class="hljs-string">apps/v1</span>
<span class="hljs-attr">kind:</span> <span class="hljs-string">Deployment</span>
<span class="hljs-attr">metadata:</span>
  <span class="hljs-attr">name:</span> <span class="hljs-string">zaqar</span>
  <span class="hljs-attr">namespace:</span> <span class="hljs-string">default</span>
  <span class="hljs-attr">labels:</span>
    <span class="hljs-attr">app:</span> <span class="hljs-string">zaqar</span>
    <span class="hljs-attr">version:</span> <span class="hljs-comment">#!VERSION!#</span>
    <span class="hljs-attr">env:</span> <span class="hljs-comment">#!ENV!#</span>
<span class="hljs-attr">spec:</span>
  <span class="hljs-attr">replicas:</span> <span class="hljs-number">1</span>
  <span class="hljs-attr">selector:</span>
    <span class="hljs-attr">matchLabels:</span>
      <span class="hljs-attr">app:</span> <span class="hljs-string">zaqar</span>
      <span class="hljs-attr">env:</span> <span class="hljs-comment">#!ENV!#</span>
  <span class="hljs-attr">template:</span>
    <span class="hljs-attr">metadata:</span>
      <span class="hljs-attr">labels:</span>
        <span class="hljs-attr">app:</span> <span class="hljs-string">zaqar</span>
        <span class="hljs-attr">version:</span> <span class="hljs-comment">#!VERSION!#</span>
        <span class="hljs-attr">env:</span> <span class="hljs-comment">#!ENV!#</span>
    <span class="hljs-attr">spec:</span>
      <span class="hljs-attr">containers:</span>
        <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">zaqar</span>
          <span class="hljs-attr">image:</span> <span class="hljs-string">"khaosdoctor/zaqar:#!VERSION!#"</span>
          <span class="hljs-attr">imagePullPolicy:</span> <span class="hljs-string">IfNotPresent</span>
          <span class="hljs-attr">env:</span>
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">SENDGRID_APIKEY</span>
              <span class="hljs-attr">value:</span> <span class="hljs-string">"#!SENDGRID_KEY!#"</span>
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">DEFAULT_FROM_ADDRESS</span>
              <span class="hljs-attr">value:</span> <span class="hljs-string">"#!FROM_ADDR!#"</span>
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">DEFAULT_FROM_NAME</span>
              <span class="hljs-attr">value:</span> <span class="hljs-string">"#!FROM_NAME!#"</span>
          <span class="hljs-attr">ports:</span>
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">http</span>
              <span class="hljs-attr">containerPort:</span> <span class="hljs-number">3000</span>
              <span class="hljs-attr">protocol:</span> <span class="hljs-string">TCP</span>
          <span class="hljs-attr">resources:</span>
            <span class="hljs-attr">requests:</span>
              <span class="hljs-attr">cpu:</span> <span class="hljs-string">100m</span>
              <span class="hljs-attr">memory:</span> <span class="hljs-string">128Mi</span>
            <span class="hljs-attr">limits:</span>
              <span class="hljs-attr">cpu:</span> <span class="hljs-string">250m</span>
              <span class="hljs-attr">memory:</span> <span class="hljs-string">256Mi</span>
</code></pre>
<p>We can now run our <a target="_blank" href="https://docs.microsoft.com/learn/modules/aks-deployment-pipeline-github-actions/?WT.mc_id=containers-19838-ludossan">CI pipeline</a>. But before we do that, we need to replace our placeholders with the actual values. </p>
<p>For this we can use <code>sed</code> and its "super easy" syntax <code>sed 's/#!PLACEHOLDER!#/replacement/g'</code>, and pipe this down until we finish all the placeholders. The final command would be something like this:</p>
<pre><code class="lang-bash">cat deploy.yaml | \
    sed <span class="hljs-string">'s/#!ENV!#/production/g'</span> | \
    sed <span class="hljs-string">'s/#!VERSION!#/v1.0.0/g'</span> | \
    sed <span class="hljs-string">'s/#!SENDGRID_KEY!#/MyKey/g'</span> | \
    sed <span class="hljs-string">'s/#!FROM_ADDR!#/my@email.com/g'</span> | \
    sed <span class="hljs-string">'s/#!FROM_NAME!#/Lucas Santos/g'</span>
</code></pre>
<p>By default, sed outputs everything to the <code>stdout</code>, so we can add another pipe to <code>kubectl -f</code>, like <code>&lt;all the command from before&gt; | kubectl -f -</code>. Then we'll have our deployment in place. The only problem is that we need to do the same for <strong>all</strong> the other files. </p>
<p>Now imagine a larger project, with lots of other variables and placeholders. You'd probably write a script to do it for you, wouldn't you? That script is Helm.</p>
<p>When you create a Chart (more on this later on), we have a specific directory tree that we must follow so Helm understands what we want to do. Inside the <code>templates</code> directory, we can add our manifest files, <strong>with native go templating,</strong> like this:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">apiVersion:</span> <span class="hljs-string">apps/v1</span>
<span class="hljs-attr">kind:</span> <span class="hljs-string">Deployment</span>
<span class="hljs-attr">metadata:</span>
  <span class="hljs-attr">name:</span> {{ <span class="hljs-string">.Values.name</span> }}
  <span class="hljs-attr">namespace:</span> {{ <span class="hljs-string">default</span> <span class="hljs-string">.Release.Namespace</span> <span class="hljs-string">.Values.namespace</span> }}
  <span class="hljs-attr">labels:</span>
    <span class="hljs-attr">app:</span> {{ <span class="hljs-string">.Values.name</span> }}
    <span class="hljs-attr">version:</span> {{ <span class="hljs-string">.Values.image.tag</span> }}
    <span class="hljs-attr">env:</span> {{ <span class="hljs-string">.Values.env</span> }}
<span class="hljs-attr">spec:</span>
  <span class="hljs-attr">replicas:</span> {{ <span class="hljs-string">.Values.replicaCount</span> }}
  <span class="hljs-attr">selector:</span>
    <span class="hljs-attr">matchLabels:</span>
      <span class="hljs-attr">app:</span> {{ <span class="hljs-string">.Values.name</span> }}
      <span class="hljs-attr">env:</span> {{ <span class="hljs-string">.Values.env</span> }}
  <span class="hljs-attr">template:</span>
    <span class="hljs-attr">metadata:</span>
      <span class="hljs-attr">labels:</span>
        <span class="hljs-attr">app:</span> {{ <span class="hljs-string">.Values.name</span> }}
        <span class="hljs-attr">version:</span> {{ <span class="hljs-string">.Values.image.tag</span> }}
        <span class="hljs-attr">env:</span> {{ <span class="hljs-string">.Values.env</span> }}
    <span class="hljs-attr">spec:</span>
      <span class="hljs-attr">containers:</span>
        <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> {{ <span class="hljs-string">.Chart.Name</span> }}
          <span class="hljs-attr">image:</span> <span class="hljs-string">"khaosdoctor/zaqar:<span class="hljs-template-variable">{{ .Values.image.tag }}</span>"</span>
          <span class="hljs-attr">imagePullPolicy:</span> {{ <span class="hljs-string">.Values.image.pullPolicy</span> }}
          <span class="hljs-attr">env:</span>
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">SENDGRID_APIKEY</span>
              <span class="hljs-attr">value:</span> {{ <span class="hljs-string">required</span> <span class="hljs-string">"You must set a valid Sendgrid API key"</span> <span class="hljs-string">.Values.environment.SENDGRID_APIKEY</span> <span class="hljs-string">|</span> <span class="hljs-string">quote</span> }}
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">DEFAULT_FROM_ADDRESS</span>
              <span class="hljs-attr">value:</span> {{ <span class="hljs-string">required</span> <span class="hljs-string">"You must set a default from address"</span> <span class="hljs-string">.Values.environment.DEFAULT_FROM_ADDRESS</span> <span class="hljs-string">|</span> <span class="hljs-string">quote</span> }}
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">DEFAULT_FROM_NAME</span>
              <span class="hljs-attr">value:</span> {{ <span class="hljs-string">required</span> <span class="hljs-string">"You must set a default from name"</span> <span class="hljs-string">.Values.environment.DEFAULT_FROM_NAME</span> <span class="hljs-string">|</span> <span class="hljs-string">quote</span> }}
          <span class="hljs-attr">ports:</span>
            <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">http</span>
              <span class="hljs-attr">containerPort:</span> <span class="hljs-number">3000</span>
              <span class="hljs-attr">protocol:</span> <span class="hljs-string">TCP</span>
          <span class="hljs-attr">resources:</span>
            {{<span class="hljs-bullet">-</span> <span class="hljs-string">toYaml</span> <span class="hljs-string">.Values.resources</span> <span class="hljs-string">|</span> <span class="hljs-string">nindent</span> <span class="hljs-number">12</span> }}
</code></pre>
<p>All those values can be obtained from a <code>Values.yaml</code> file (for default values), or you can set them in the CLI using the <code>--set &lt;path&gt; value</code> flag. </p>
<p>If we want to install our chart we can issue the following command:</p>
<pre><code class="lang-bash">helm upgrade --install --create-namespace myChart ./path/to/my/chart \
  --<span class="hljs-built_in">set</span> image.tag=v1.0.0 \
  --<span class="hljs-built_in">set</span> env=production \
  --<span class="hljs-built_in">set</span> environment.SENDGRID_APIKEY=myKey \
  --<span class="hljs-built_in">set</span> environment.DEFAULT_FROM_ADDRESS=<span class="hljs-string">"my@email.com"</span> \
  --<span class="hljs-built_in">set</span> environment.DEFAULT_FROM_NAME=<span class="hljs-string">"Lucas Santos"</span>
</code></pre>
<p>Helm also allows us to use functions inside our deployments. So we can have <code>default</code> functions to fallback to default values if they're not filled, like the namespace. Or we can have <code>required</code> which displays a message and fails to install the chart if the value is not provided, which is the case of our environment variables.</p>
<p>There are a lot of other useful functions in their <a target="_blank" href="https://helm.sh/docs/chart_template_guide/">docs</a> - check 'em out.</p>
<p>Now we are able to not only more efficiently manage our application's resources, but also to publish these resources in an open-source version system without any hassle or security issue.</p>
<h2 id="heading-how-to-create-a-helm-chart">How to Create a Helm Chart</h2>
<p>It's pretty easy to create a chart in Helm. First, you need to have <a target="_blank" href="https://helm.sh/docs/intro/quickstart/">Helm installed</a>. Then, just type in <code>helm create &lt;chart name&gt;</code> and it will create a directory filled with files and other directories. Those files are required for Helm to create a chart. </p>
<p>Let's take a closer look at what this file tree looks like and what the files are within it:</p>
<ul>
<li><strong>chart.yaml:</strong> This is where you'll put the information related to your chart. That includes the chart version, name, and description so you can find it if you publish it on an open repository. Also in this file you'll be able to set external <a target="_blank" href="https://helm.sh/docs/topics/charts/#chart-dependencies">dependencies</a> using the <code>dependencies</code> key.</li>
<li><strong>values.yaml</strong>: Like we saw before, this is the file that contains defaults for variables.</li>
<li><strong>templates (dir):</strong> This is the place where you'll put all your manifest files. Everything in here will be passed on and created in Kubernetes.</li>
<li><strong>charts:</strong> If your chart depends on another chart you own, or if you don't want to rely on Helm's default library (the default registry where Helm pull charts from), you can bring this same structure inside this directory. Chart dependencies are installed from the bottom to the top, which means if chart A depends on chart B, and B depends on C, the installation order will be C -&gt;B -&gt;A.</li>
</ul>
<p>There are other fields, but these are the most common, and they're the required ones. You can take a quick look at <a target="_blank" href="https://github.com/khaosdoctor/zaqar/tree/master/helm">Zaqar's repository</a> to check on how we can publish open source charts.</p>
<p>A quick note: When installing Helm, make sure you're installing version 3. Version 2 still works, but it needs a server-side component called Tiller, which ties your helm installation to a single cluster. Helm 3 removed this need with the addition of several CRDs, but it's not supported in all Kubernetes versions.</p>
<h2 id="heading-how-to-host-a-helm-chart">How to Host a Helm Chart</h2>
<p>Ok, you created your chart, now what? Do we have to download the entire repository to install those charts? No! Helm has a <a target="_blank" href="https://artifacthub.io/">public library for the most used charts</a>, which kind of works like Docker Hub. </p>
<p>You can also create your own chart repository and <a target="_blank" href="https://helm.sh/docs/topics/chart_repository/">host it online</a>. Helm drinks from the same fountain as HomeBrew, or Linux. You can tap these repositories to download charts contained in them. </p>
<p>Since a chart repository is basically an <code>index.yaml</code> file served from a static web server, you can pretty much create a chart repository out of anywhere.</p>
<p>Take <a target="_blank" href="https://github.com/khaosdoctor/zaqar/tree/master/helm">Zaqar</a>, for example – it's hosted on GitHub Pages and is accessible through <a target="_blank" href="https://lsantos.me/zaqar/helm/index.yaml">my domain</a>. When Helm looks for an <code>index.yaml</code> file it's actually looking for the list of available versions of that chart, their SHA256 digests, and the location of the packaged <code>.tgz</code> file to download the chart itself. This is pretty much what NPM does under the hood (overly simplified).</p>
<p>This means you don't need to have your repository cloned forever, and your charts can be private as well. You only need to create a chart repository. </p>
<p>You can even use <a target="_blank" href="https://docs.microsoft.com/azure/container-registry/container-registry-helm-repos?WT.mc_id=containers-19838-ludossan">hosted services like Azure CR</a> to do the job, or you can have a full solution called <a target="_blank" href="https://github.com/helm/chartmuseum">Chart Museum</a>, which allows you to store your charts and provides you with a neat UI.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Helm is here to stay. It has helped and will help a lot of Kubernetes developers out there for a long time. </p>
<p>If you want to know how to use Helm, you can refer to their <a target="_blank" href="https://helm.sh">docs</a>, or you can take this <a target="_blank" href="https://docs.microsoft.com/learn/modules/aks-app-package-management-using-helm/?WT.mc_id=containers-19838-ludossan">free learn module</a> on how to deploy your applications on Kubernetes the easy way with Helm.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create a Diverging Bar Chart with a JavaScript Charting Library ]]>
                </title>
                <description>
                    <![CDATA[ By Shachee Swadia This article is a step-by-step guide that'll show you how to build an interactive JavaScript range chart that visualizes 20 years of the LA Lakers’ performance with Kobe Bryant. The year 2020 was pretty poignant for obvious reasons.... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/diverging-bar-chart-javascript/</link>
                <guid isPermaLink="false">66d460f2ffe6b1f641b5fa7d</guid>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ data analysis ]]>
                    </category>
                
                    <category>
                        <![CDATA[ data visualization ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 24 Feb 2021 17:09:07 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/02/diverging-bar-chart-javascript-1500.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Shachee Swadia</p>
<p>This article is a step-by-step guide that'll show you how to build an interactive JavaScript range chart that visualizes 20 years of the LA Lakers’ performance with Kobe Bryant.</p>
<p>The year 2020 was pretty poignant for obvious reasons. But even before the pandemic, the year started on a sad note with the death of basketball legend <a target="_blank" href="https://en.wikipedia.org/wiki/Kobe_Bryant">Kobe Bryant</a>. He was a star NBA athlete who had played for 20 years with one and only one team — the Los Angeles Lakers. </p>
<p>Remembering Kobe one year after that horrible crash, I wondered how the Lakers had performed in his two-decade era. So, I visualized that in an interactive Diverging Bar Chart with the help of pure JavaScript. </p>
<p>Thinking that this project might be helpful for those new to web charting, I also logged the entire process and made a tutorial. Check it out!</p>
<h2 id="heading-what-is-a-diverging-bar-chart">What Is a Diverging Bar Chart?</h2>
<p>First things first, I will give you a brief explanation about what diverging bar charts are and then we'll dive into the tutorial.  </p>
<p>A diverging bar chart shows two or more measures that are plotted from a middle baseline, extending either to the right and left (horizontal range bars) or top and bottom (vertical range columns). </p>
<p>The key point of data visualization in such diverging charts is to facilitate the comparison of multiple categories by means of displaying them against a bifurcating midpoint.</p>
<p>In this tutorial, I use the diverging bar chart technique to showcase the wins and losses of the LA Lakers through the 20 years of Kobe Bryant’s career.</p>
<p>Here's a sneak peek at the final chart to get you ready for the start of the game! Follow along with me to learn how I create this beautiful range bar chart with JavaScript.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/02/js-diverging-bar-chart.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-build-a-javascript-diverging-bar-chart-in-4-basic-steps">How to Build a JavaScript Diverging Bar Chart in 4 Basic Steps</h2>
<p>There are <a target="_blank" href="https://en.wikipedia.org/wiki/Comparison_of_JavaScript_charting_libraries">multiple</a> JavaScript libraries out there providing pre-written JS code for commonly needed functions that can make the interactive data visualization process quite quick and straightforward. </p>
<p>I picked one called <a target="_blank" href="https://www.anychart.com">AnyChart</a> to create this diverging bar chart. This JS charting library appeared to support (particularly useful in this case) range charts out of the box and was also flexible enough to do what I wanted. </p>
<p>Also, it is quite easy to get started with AnyChart even for beginners because there are many ready-to-use examples and it has intensive <a target="_blank" href="https://docs.anychart.com">documentation</a>.</p>
<p>Of course, having good HTML and JavaScript skills gives you an edge when visualizing data on the web. But anyway, the best part about making use of good charting libraries is that they make it quite uncomplicated to create interactive charts even without much experience.</p>
<p>The entire process of creating literally any JS chart, including a diverging bar chart like this one, can be broken down into four fundamental steps:</p>
<ol>
<li>Create an HTML page.</li>
<li>Reference the necessary JS files.</li>
<li>Set the data.</li>
<li>Write the JS code for the chart.</li>
</ol>
<p>Let's go through each step in detail now.</p>
<h3 id="heading-1-create-a-basic-html-page">1. Create a basic HTML page</h3>
<p>The first thing we need to do is create a basic HTML page. Let’s give it a title and create an HTML block element to hold the chart. To identify this <code>&lt;div&gt;</code> later in the code, we should also give it an id attribute (let it be “container”).</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>JavaScript Diverging Bar Chart<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span>&gt;</span><span class="css">      
        <span class="hljs-selector-tag">html</span>, <span class="hljs-selector-tag">body</span>, <span class="hljs-selector-id">#container</span> { 
            <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>; <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>; 
        } 
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Note that it is possible to specify the width and height parameters inside the <code>&lt;style&gt;</code> block to modify the space that your chart will occupy. I have put 100% in both parameters so the chart fills the whole page.</p>
<h3 id="heading-2-include-the-necessary-javascript-files">2. Include the necessary JavaScript files</h3>
<p>Next, we need to add the charting library scripts that will help create the data visualization. Since we are working with the AnyChart library here, let’s include the corresponding files from its <a target="_blank" href="https://www.anychart.com/download/cdn/">CDN</a>. (Keep in mind that you can always download the scripts if you want.)</p>
<p>For the diverging bar chart, we need the <a target="_blank" href="https://docs.anychart.com/Quick_Start/Modules#base">base module script</a> which is to be added to the <code>&lt;head&gt;</code> section of the HTML page.</p>
<pre><code class="lang-js">&lt;html&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>JavaScript Diverging Bar Chart<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span>&gt;</span><span class="css">
      <span class="hljs-selector-tag">html</span>,
      <span class="hljs-selector-tag">body</span>,
      <span class="hljs-selector-id">#container</span> {
        <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
        <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
        <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
        <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
      }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
      <span class="hljs-comment">// All the code for the JS diverging bar chart will come here</span>
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span></span>
&lt;/html&gt;
</code></pre>
<h3 id="heading-3-add-the-data">3. Add the data</h3>
<p>I wanted to visualize the number of wins and losses of the LA Lakers team across all seasons from 1996 to 2016. So, I got the data from the <a target="_blank" href="https://www.nba.com/lakers/history/seasonbyseason/">NBA website</a> and created an array with the year, wins, and losses.</p>
<p>Since the amount of data is not huge, we can add it just like that:</p>
<pre><code><span class="hljs-keyword">var</span> winlossData = [
  [<span class="hljs-number">65</span>, <span class="hljs-number">17</span>, <span class="hljs-string">"2015-16"</span>],
  [<span class="hljs-number">61</span>, <span class="hljs-number">21</span>, <span class="hljs-string">"2014-15"</span>],
  [<span class="hljs-number">55</span>, <span class="hljs-number">27</span>, <span class="hljs-string">"2013-14"</span>],
  [<span class="hljs-number">37</span>, <span class="hljs-number">45</span>, <span class="hljs-string">"2012-13"</span>],
  [<span class="hljs-number">25</span>, <span class="hljs-number">41</span>, <span class="hljs-string">"2011-12"</span>],
  [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2010-11"</span>],
  [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2009-10"</span>],
  [<span class="hljs-number">17</span>, <span class="hljs-number">65</span>, <span class="hljs-string">"2008-09"</span>],
  [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2007-08"</span>],
  [<span class="hljs-number">40</span>, <span class="hljs-number">42</span>, <span class="hljs-string">"2006-07"</span>],
  [<span class="hljs-number">37</span>, <span class="hljs-number">45</span>, <span class="hljs-string">"2005-06"</span>],
  [<span class="hljs-number">48</span>, <span class="hljs-number">34</span>, <span class="hljs-string">"2004-05"</span>],
  [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"2003-04"</span>],
  [<span class="hljs-number">32</span>, <span class="hljs-number">50</span>, <span class="hljs-string">"2002-03"</span>],
  [<span class="hljs-number">24</span>, <span class="hljs-number">58</span>, <span class="hljs-string">"2001-02"</span>],
  [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"2000-01"</span>],
  [<span class="hljs-number">15</span>, <span class="hljs-number">67</span>, <span class="hljs-string">"1999-00"</span>],
  [<span class="hljs-number">19</span>, <span class="hljs-number">31</span>, <span class="hljs-string">"1998-99"</span>],
  [<span class="hljs-number">21</span>, <span class="hljs-number">61</span>, <span class="hljs-string">"1997-98"</span>],
  [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"1996-97"</span>]
];
</code></pre><p>Now that the stage is set, let’s start playing around by adding the JavaScript code that will create the interactive Diverging Bar Chart!</p>
<h3 id="heading-4-write-the-javascript-code-for-your-chart">4. Write the JavaScript code for your chart</h3>
<p>Before anything else, we need to add a function enclosing all the JS code, which makes sure that the entire code inside of it will only execute once the page is loaded.</p>
<pre><code class="lang-js">&lt;script&gt;
  anychart.onDocumentReady(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// The place for the JS diverging bar chart code</span>
  });
&lt;/script&gt;
</code></pre>
<p>In general, a JS diverging bar chart is pretty simple to create and I will walk you through each action. So get ready to wiggle, block, and shoot!</p>
<p>Firstly, we create a bar chart and enter the data, all inside the enclosing <code>anychart.onDocumentReady()</code> function.</p>
<pre><code class="lang-js"><span class="hljs-comment">// create a bar chart</span>
<span class="hljs-keyword">var</span> chart = anychart.bar();

<span class="hljs-comment">// data</span>
<span class="hljs-keyword">var</span> winlossData = [
  [<span class="hljs-number">65</span>, <span class="hljs-number">17</span>, <span class="hljs-string">"2015-16"</span>],
  [<span class="hljs-number">61</span>, <span class="hljs-number">21</span>, <span class="hljs-string">"2014-15"</span>],
  [<span class="hljs-number">55</span>, <span class="hljs-number">27</span>, <span class="hljs-string">"2013-14"</span>],
  [<span class="hljs-number">37</span>, <span class="hljs-number">45</span>, <span class="hljs-string">"2012-13"</span>],
  [<span class="hljs-number">25</span>, <span class="hljs-number">41</span>, <span class="hljs-string">"2011-12"</span>],
  [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2010-11"</span>],
  [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2009-10"</span>],
  [<span class="hljs-number">17</span>, <span class="hljs-number">65</span>, <span class="hljs-string">"2008-09"</span>],
  [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2007-08"</span>],
  [<span class="hljs-number">40</span>, <span class="hljs-number">42</span>, <span class="hljs-string">"2006-07"</span>],
  [<span class="hljs-number">37</span>, <span class="hljs-number">45</span>, <span class="hljs-string">"2005-06"</span>],
  [<span class="hljs-number">48</span>, <span class="hljs-number">34</span>, <span class="hljs-string">"2004-05"</span>],
  [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"2003-04"</span>],
  [<span class="hljs-number">32</span>, <span class="hljs-number">50</span>, <span class="hljs-string">"2002-03"</span>],
  [<span class="hljs-number">24</span>, <span class="hljs-number">58</span>, <span class="hljs-string">"2001-02"</span>],
  [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"2000-01"</span>],
  [<span class="hljs-number">15</span>, <span class="hljs-number">67</span>, <span class="hljs-string">"1999-00"</span>],
  [<span class="hljs-number">19</span>, <span class="hljs-number">31</span>, <span class="hljs-string">"1998-99"</span>],
  [<span class="hljs-number">21</span>, <span class="hljs-number">61</span>, <span class="hljs-string">"1997-98"</span>],
  [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"1996-97"</span>]
];
</code></pre>
<p>Next, we create a function that accepts two parameters — a column number and a name. The column number indicates the column in the dataset and the name indicates the series. In our case, we have two series — one for the number of wins and one for the number of losses. </p>
<p>Since we want a diverging bar chart, let’s take the center and plot the bars for wins to the right and bars for losses to the left. Then, we should prepare the dataset by adding all the required values through a 'for' loop.</p>
<p>Don’t worry if this sounds a bit complicated. It is just about making our data ready to be plotted, and when you look into the code below, you’ll likely see that it's all completely logical.</p>
<p>There are two more things we need to include in the function. We define a series with the rangeBar function and add a line to indicate the names of the series and a separator line between the left and right bars.</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> createSeries = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">columnNumber, name</span>) </span>{
  <span class="hljs-keyword">var</span> data = [];
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; winlossData.length; i++) {
    <span class="hljs-keyword">var</span> value = winlossData[i][columnNumber];
    <span class="hljs-keyword">var</span> center = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">if</span> (name === <span class="hljs-string">"Wins"</span>) {
      data.push({
        <span class="hljs-attr">x</span>: winlossData[i][<span class="hljs-number">2</span>],
        <span class="hljs-attr">low</span>: center,
        <span class="hljs-attr">high</span>: center + value,
        <span class="hljs-attr">value</span>: value
      });
    } <span class="hljs-keyword">else</span> {
      data.push({
        <span class="hljs-attr">x</span>: winlossData[i][<span class="hljs-number">2</span>],
        <span class="hljs-attr">low</span>: -center,
        <span class="hljs-attr">high</span>: -center - value,
        <span class="hljs-attr">value</span>: value
      });
    }
  }

  <span class="hljs-keyword">var</span> series = chart.rangeBar(data);
  series.name(name);
};
</code></pre>
<p>Now, we create the two series with the desired arguments using the function just defined.</p>
<pre><code class="lang-js">createSeries(<span class="hljs-number">0</span>, <span class="hljs-string">"Losses"</span>);
createSeries(<span class="hljs-number">1</span>, <span class="hljs-string">"Wins"</span>);
</code></pre>
<p>It’s halftime and the most complicated parts are over! Now we just have the setup of the chart.</p>
<p>Add the title to the diverging bar chart:</p>
<pre><code class="lang-js">chart
  .title()
  .enabled(<span class="hljs-literal">true</span>)
  .text(<span class="hljs-string">"20 Years of LA Lakers Win-Loss Record with Kobe Bryant (1996-2016)"</span>);
</code></pre>
<p>And enable the chart’s legend:</p>
<pre><code class="lang-js">chart
  .legend()
  .enabled(<span class="hljs-literal">true</span>);
</code></pre>
<p>To make the wins and losses for each year show up adjacent to each other, we should convert the multi-series bar chart into a stacked bar chart. Next, to emphasize divergence, let’s add a line marker at 0. Finally, we assign the container div and draw the chart:</p>
<pre><code class="lang-js"><span class="hljs-comment">// create a stacked bar chart from the multi-series bar chart</span>
chart.yScale().stackMode(<span class="hljs-string">"value"</span>);

<span class="hljs-comment">// set a container id for the chart</span>
chart.container(<span class="hljs-string">"container"</span>);

<span class="hljs-comment">// initiate chart drawing</span>
chart.draw();
</code></pre>
<p>That’s the whistle and there you have it — a very basic, yet fully functional interactive diverging bar chart built with JavaScript!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/02/initial-chart-default-colors-1.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Although Kobe may have been spectacular in the final games of his career in the NBA, we can see that the Lakers struggled during his last few years with more losses than wins. But the overall record is definitely many more triumphs than losses.</p>
<p><strong>Take a look at this initial version of the diverging bar chart with the full JS/CSS/HTML code <a target="_blank" href="https://codepen.io/shacheeswadia/pen/jOVrqLQ">on CodePen</a>.</strong></p>
<pre><code class="lang-js">&lt;html&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>JavaScript Diverging Bar Chart<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span>&gt;</span><span class="css">
      <span class="hljs-selector-tag">html</span>,
      <span class="hljs-selector-tag">body</span>,
      <span class="hljs-selector-id">#container</span> {
        <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
        <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
        <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
        <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
      }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">

    anychart.onDocumentReady(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{

      <span class="hljs-comment">// create a bar chart</span>
      <span class="hljs-keyword">var</span> chart = anychart.bar();

      <span class="hljs-comment">// data</span>
      <span class="hljs-keyword">var</span> winlossData = [
        [<span class="hljs-number">65</span>, <span class="hljs-number">17</span>, <span class="hljs-string">"2015-16"</span>],
        [<span class="hljs-number">61</span>, <span class="hljs-number">21</span>, <span class="hljs-string">"2014-15"</span>],
        [<span class="hljs-number">55</span>, <span class="hljs-number">27</span>, <span class="hljs-string">"2013-14"</span>],
        [<span class="hljs-number">37</span>, <span class="hljs-number">45</span>, <span class="hljs-string">"2012-13"</span>],
        [<span class="hljs-number">25</span>, <span class="hljs-number">41</span>, <span class="hljs-string">"2011-12"</span>],
        [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2010-11"</span>],
        [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2009-10"</span>],
        [<span class="hljs-number">17</span>, <span class="hljs-number">65</span>, <span class="hljs-string">"2008-09"</span>],
        [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2007-08"</span>],
        [<span class="hljs-number">40</span>, <span class="hljs-number">42</span>, <span class="hljs-string">"2006-07"</span>],
        [<span class="hljs-number">37</span>, <span class="hljs-number">45</span>, <span class="hljs-string">"2005-06"</span>],
        [<span class="hljs-number">48</span>, <span class="hljs-number">34</span>, <span class="hljs-string">"2004-05"</span>],
        [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"2003-04"</span>],
        [<span class="hljs-number">32</span>, <span class="hljs-number">50</span>, <span class="hljs-string">"2002-03"</span>],
        [<span class="hljs-number">24</span>, <span class="hljs-number">58</span>, <span class="hljs-string">"2001-02"</span>],
        [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"2000-01"</span>],
        [<span class="hljs-number">15</span>, <span class="hljs-number">67</span>, <span class="hljs-string">"1999-00"</span>],
        [<span class="hljs-number">19</span>, <span class="hljs-number">31</span>, <span class="hljs-string">"1998-99"</span>],
        [<span class="hljs-number">21</span>, <span class="hljs-number">61</span>, <span class="hljs-string">"1997-98"</span>],
        [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"1996-97"</span>]
      ];

      <span class="hljs-comment">// configure a function to create series</span>
      <span class="hljs-keyword">var</span> createSeries = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">columnNumber, name</span>) </span>{
        <span class="hljs-keyword">var</span> data = [];
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; winlossData.length; i++) {
          <span class="hljs-keyword">var</span> value = winlossData[i][columnNumber];
          <span class="hljs-keyword">var</span> center = <span class="hljs-number">0</span>;
          <span class="hljs-keyword">if</span> (name === <span class="hljs-string">"Wins"</span>) {
            data.push({
              <span class="hljs-attr">x</span>: winlossData[i][<span class="hljs-number">2</span>],
              <span class="hljs-attr">low</span>: center,
              <span class="hljs-attr">high</span>: center + value,
              <span class="hljs-attr">value</span>: value
            });
          } <span class="hljs-keyword">else</span> {
            data.push({
              <span class="hljs-attr">x</span>: winlossData[i][<span class="hljs-number">2</span>],
              <span class="hljs-attr">low</span>: -center,
              <span class="hljs-attr">high</span>: -center - value,
              <span class="hljs-attr">value</span>: value
            });
          }
        }

        <span class="hljs-keyword">var</span> series = chart.rangeBar(data);
        series.name(name);
      };

      <span class="hljs-comment">// create series</span>
      createSeries(<span class="hljs-number">0</span>, <span class="hljs-string">"Losses"</span>);
      createSeries(<span class="hljs-number">1</span>, <span class="hljs-string">"Wins"</span>);

      <span class="hljs-comment">// set the chart title</span>
     chart
        .title()
        .enabled(<span class="hljs-literal">true</span>)
        .text(<span class="hljs-string">"20 Years of LA Lakers Win-Loss Record with Kobe Bryant (1996-2016)"</span>);

      <span class="hljs-comment">// enable the chart legend</span>
      chart
        .legend()
        .enabled(<span class="hljs-literal">true</span>);

      <span class="hljs-comment">// create a stacked bar chart from the multi-series bar chart</span>
      chart.yScale().stackMode(<span class="hljs-string">"value"</span>);

      <span class="hljs-comment">// set a container id for the chart</span>
      chart.container(<span class="hljs-string">"container"</span>);

      <span class="hljs-comment">// initiate chart drawing</span>
      chart.draw();

    });

    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span></span>
&lt;/html&gt;
</code></pre>
<h2 id="heading-how-to-customize-our-javascript-diverging-bar-chart">How to Customize Our JavaScript Diverging Bar Chart</h2>
<p>A slam dunk thing about interactive data visualization with JavaScript is the freedom we have to customize it so that our data tells better stories. I will show you how to push some quick changes to the basic JS-based diverging bar chart to make it more engaging and informative.</p>
<p>Now I'll throw a 3-pointer and customize the chart to improve some of its functionalities and aesthetics.</p>
<h3 id="heading-1-basic-styling-and-axes-settings">1. Basic styling and axes settings</h3>
<p>To start with, let’s change some basic styling and settings for the X and Y axes to make things more readable. </p>
<p>Just remember that in AnyChart, a <a target="_blank" href="https://docs.anychart.com/Basic_Charts/Range_Bar_Chart">range bar chart</a> is the vertical version of a <a target="_blank" href="https://docs.anychart.com/Basic_Charts/Range_Column_Chart">range column chart</a>. Consequently, in our diverging bar chart, the horizontal axis is the Y-axis, and the vertical axis is called the X-axis.</p>
<p>So, let’s get rid of ticks, configure the axis title, and customize the labels on the vertical axis. We'll also set 80 as the maximum and remove the minus sign from the labels on the horizontal axis:</p>
<pre><code class="lang-js">chart
  .xAxis()
  .ticks(<span class="hljs-literal">false</span>);
chart
  .xAxis()
  .title()
  .enabled(<span class="hljs-literal">true</span>)
  .text(<span class="hljs-string">"Years"</span>)
  .padding([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>]);
chart
  .xAxis()
  .labels()
  .fontSize(<span class="hljs-number">11</span>)
  .fontColor(<span class="hljs-string">"#474747"</span>)
  .padding([<span class="hljs-number">0</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]);
chart.yScale().maximum(<span class="hljs-number">80</span>);
chart
  .yAxis(<span class="hljs-number">0</span>)
  .labels()
  .format(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.abs(<span class="hljs-built_in">this</span>.value);
  });
</code></pre>
<p>Next, to emphasize divergence, it would be great to add a white stroke between the two series and a line marker at 0.</p>
<pre><code class="lang-js"><span class="hljs-comment">// add the stroke by setting it in this line</span>
series.name(name).stroke(<span class="hljs-string">"3 #fff 1"</span>);

...

<span class="hljs-comment">// create a line marker at 0</span>
chart
  .lineMarker()
  .value(<span class="hljs-number">0</span>)
  .stroke(<span class="hljs-string">"#CECECE"</span>);
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/02/intermediate-chart-1-1.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Ah, doesn’t the chart look more polished and easier to read now?</p>
<p><strong>Check out the code for this version of the diverging bar chart <a target="_blank" href="https://codepen.io/shacheeswadia/pen/zYoEMEd">on CodePen</a>.</strong></p>
<p>Before we make more customizations, there is one small digression I want to make. I also thought of making the horizontal axis display the wins and losses for every season in percentages rather than absolute values. It’s pretty easy but the result did not offer any extra insights. </p>
<p>Also, the absolute values do represent when the Lakers played more or fewer games through the year. That’s ultimately why I decided to keep the absolute values. But you are welcome to check out the version with percentages <a target="_blank" href="https://codepen.io/shacheeswadia/pen/jOVrqKd">on CodePen</a>. </p>
<p>Well, let's move on from that missed shot and back into focus mode.</p>
<h3 id="heading-2-tooltip-customization">2. Tooltip customization</h3>
<p>Next, I customized the tooltip to make it more informative and interesting.</p>
<p>Here, I also got the idea of showcasing the previously calculated percentage values (see the example from the digression just above) as an extra bit of information in the tooltip of our diverging bar chart.</p>
<p>So, the first step is to implement the calculation of percentage values:</p>
<pre><code class="lang-js"><span class="hljs-comment">// calculate percentages for the tooltip</span>
<span class="hljs-keyword">var</span> val = winlossData[i][columnNumber] * <span class="hljs-number">100</span>;
<span class="hljs-keyword">if</span> (columnNumber == <span class="hljs-number">0</span>) {
  <span class="hljs-keyword">var</span> percentValue =
    val / (winlossData[i][columnNumber] + winlossData[i][columnNumber + <span class="hljs-number">1</span>]);
} <span class="hljs-keyword">else</span> {
  <span class="hljs-keyword">var</span> percentValue =
    val / (winlossData[i][columnNumber] + winlossData[i][columnNumber - <span class="hljs-number">1</span>]);
}
percentValue = percentValue.toFixed(<span class="hljs-number">2</span>);
</code></pre>
<p>The percentage calculation goes as part of the series configuration function — look at how it is included there:</p>
<pre><code class="lang-js"><span class="hljs-comment">// configure a function to create series</span>
<span class="hljs-keyword">var</span> createSeries = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">columnNumber, name</span>) </span>{
  <span class="hljs-keyword">var</span> data = [];
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; winlossData.length; i++) {

    <span class="hljs-comment">// calculate percentages for the tooltip</span>
    <span class="hljs-keyword">var</span> val = winlossData[i][columnNumber] * <span class="hljs-number">100</span>;
    <span class="hljs-keyword">if</span> (columnNumber == <span class="hljs-number">0</span>) {
      <span class="hljs-keyword">var</span> percentValue =
        val / (winlossData[i][columnNumber] + winlossData[i][columnNumber + <span class="hljs-number">1</span>]);
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-keyword">var</span> percentValue =
        val / (winlossData[i][columnNumber] + winlossData[i][columnNumber - <span class="hljs-number">1</span>]);
    }
    percentValue = percentValue.toFixed(<span class="hljs-number">2</span>);     

    <span class="hljs-keyword">var</span> value = winlossData[i][columnNumber];
    <span class="hljs-keyword">var</span> center = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">if</span> (name === <span class="hljs-string">"Wins"</span>) {
      data.push({
        <span class="hljs-attr">x</span>: winlossData[i][<span class="hljs-number">2</span>],
        <span class="hljs-attr">low</span>: center,
        <span class="hljs-attr">high</span>: center + value,
        <span class="hljs-attr">value</span>: value,
        <span class="hljs-comment">// add the calculated percentage value</span>
        <span class="hljs-attr">percentValue</span>: percentValue
      });
    } <span class="hljs-keyword">else</span> {
      data.push({
        <span class="hljs-attr">x</span>: winlossData[i][<span class="hljs-number">2</span>],
        <span class="hljs-attr">low</span>: -center,
        <span class="hljs-attr">high</span>: -center - value,
        <span class="hljs-attr">value</span>: value,
        <span class="hljs-comment">// add the calculated percentage value</span>
        <span class="hljs-attr">percentValue</span>: percentValue
      });
    }
  }
</code></pre>
<p>Then we have additional tooltip formatting to make it all look neat and beautiful:</p>
<pre><code class="lang-js"><span class="hljs-comment">// customize the tooltip</span>
chart
  .tooltip()
  .useHtml(<span class="hljs-literal">true</span>)
  .fontSize(<span class="hljs-number">12</span>)
  .titleFormat(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.getData(<span class="hljs-string">"x"</span>) + <span class="hljs-string">" "</span> + <span class="hljs-built_in">this</span>.seriesName;
  })
  .format(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> (
      <span class="hljs-string">"&lt;h6 style='font-size:12px; font-weight:400; margin: 0.25rem 0;'&gt;Total games: "</span> +
      <span class="hljs-string">"&lt;b&gt;"</span> +
      <span class="hljs-built_in">this</span>.getData(<span class="hljs-string">"value"</span>) +
      <span class="hljs-string">"&lt;/b&gt;&lt;/h6&gt;"</span> +
      <span class="hljs-string">"&lt;h6 style='font-size:12px; font-weight:400; margin: 0.25rem 0;'&gt;Percentage games: "</span> +
      <span class="hljs-string">"&lt;b&gt;"</span> +
      <span class="hljs-built_in">this</span>.getData(<span class="hljs-string">"percentValue"</span>) +
      <span class="hljs-string">" %&lt;/b&gt;&lt;/h6&gt;"</span>
    );
  });
</code></pre>
<h3 id="heading-3-color-palette-change">3. Color palette change</h3>
<p>Well, this last customization is definitely a dagger — the shot that is going to make the chart look completely awesome and win the game! It is simply changing the color palette to match the LA Lakers' jersey colors. So simple:</p>
<pre><code class="lang-js">chart.palette(
  anychart.palettes.distinctColors().items([<span class="hljs-string">"#FDB827"</span>, <span class="hljs-string">"#542583"</span>])
);
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/02/final-chart-without-tooltip-2.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You see, at the very last second, I also turned off the selection mode by adding the corresponding command to this line:</p>
<pre><code class="lang-js">series.name(name).stroke(<span class="hljs-string">"3 #fff 1"</span>).selectionMode(<span class="hljs-string">"none"</span>);
</code></pre>
<p><strong>Okay! This final interactive JavaScript diverging range bar chart is available <a target="_blank" href="https://codepen.io/shacheeswadia/pen/NWbrpYj">on CodePen</a>.</strong></p>
<p>Just in case, the full code for the HTML page is right here:</p>
<pre><code class="lang-js">&lt;html&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>JavaScript Diverging Bar Chart<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span>&gt;</span><span class="css">
      <span class="hljs-selector-tag">html</span>,
      <span class="hljs-selector-tag">body</span>,
      <span class="hljs-selector-id">#container</span> {
        <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
        <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
        <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
        <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
      }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">

    anychart.onDocumentReady(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{

      <span class="hljs-comment">// create a bar chart</span>
      <span class="hljs-keyword">var</span> chart = anychart.bar();

      <span class="hljs-comment">// data</span>
      <span class="hljs-keyword">var</span> winlossData = [
        [<span class="hljs-number">65</span>, <span class="hljs-number">17</span>, <span class="hljs-string">"2015-16"</span>],
        [<span class="hljs-number">61</span>, <span class="hljs-number">21</span>, <span class="hljs-string">"2014-15"</span>],
        [<span class="hljs-number">55</span>, <span class="hljs-number">27</span>, <span class="hljs-string">"2013-14"</span>],
        [<span class="hljs-number">37</span>, <span class="hljs-number">45</span>, <span class="hljs-string">"2012-13"</span>],
        [<span class="hljs-number">25</span>, <span class="hljs-number">41</span>, <span class="hljs-string">"2011-12"</span>],
        [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2010-11"</span>],
        [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2009-10"</span>],
        [<span class="hljs-number">17</span>, <span class="hljs-number">65</span>, <span class="hljs-string">"2008-09"</span>],
        [<span class="hljs-number">25</span>, <span class="hljs-number">57</span>, <span class="hljs-string">"2007-08"</span>],
        [<span class="hljs-number">40</span>, <span class="hljs-number">42</span>, <span class="hljs-string">"2006-07"</span>],
        [<span class="hljs-number">37</span>, <span class="hljs-number">45</span>, <span class="hljs-string">"2005-06"</span>],
        [<span class="hljs-number">48</span>, <span class="hljs-number">34</span>, <span class="hljs-string">"2004-05"</span>],
        [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"2003-04"</span>],
        [<span class="hljs-number">32</span>, <span class="hljs-number">50</span>, <span class="hljs-string">"2002-03"</span>],
        [<span class="hljs-number">24</span>, <span class="hljs-number">58</span>, <span class="hljs-string">"2001-02"</span>],
        [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"2000-01"</span>],
        [<span class="hljs-number">15</span>, <span class="hljs-number">67</span>, <span class="hljs-string">"1999-00"</span>],
        [<span class="hljs-number">19</span>, <span class="hljs-number">31</span>, <span class="hljs-string">"1998-99"</span>],
        [<span class="hljs-number">21</span>, <span class="hljs-number">61</span>, <span class="hljs-string">"1997-98"</span>],
        [<span class="hljs-number">26</span>, <span class="hljs-number">56</span>, <span class="hljs-string">"1996-97"</span>]
      ];

      <span class="hljs-comment">// configure a function to create series</span>
      <span class="hljs-keyword">var</span> createSeries = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">columnNumber, name</span>) </span>{
        <span class="hljs-keyword">var</span> data = [];
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; winlossData.length; i++) {

          <span class="hljs-comment">// calculate percentages for the tooltip</span>
          <span class="hljs-keyword">var</span> val = winlossData[i][columnNumber] * <span class="hljs-number">100</span>;
          <span class="hljs-keyword">if</span> (columnNumber == <span class="hljs-number">0</span>) {
            <span class="hljs-keyword">var</span> percentValue =
              val / (winlossData[i][columnNumber] + winlossData[i][columnNumber + <span class="hljs-number">1</span>]);
         } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">var</span> percentValue =
              val / (winlossData[i][columnNumber] + winlossData[i][columnNumber - <span class="hljs-number">1</span>]);
          }
          percentValue = percentValue.toFixed(<span class="hljs-number">2</span>);     

          <span class="hljs-keyword">var</span> value = winlossData[i][columnNumber];
          <span class="hljs-keyword">var</span> center = <span class="hljs-number">0</span>;
          <span class="hljs-keyword">if</span> (name === <span class="hljs-string">"Wins"</span>) {
            data.push({
              <span class="hljs-attr">x</span>: winlossData[i][<span class="hljs-number">2</span>],
              <span class="hljs-attr">low</span>: center,
              <span class="hljs-attr">high</span>: center + value,
              <span class="hljs-attr">value</span>: value,
              <span class="hljs-comment">// add the calculated percentage value</span>
              <span class="hljs-attr">percentValue</span>: percentValue
            });
          } <span class="hljs-keyword">else</span> {
            data.push({
              <span class="hljs-attr">x</span>: winlossData[i][<span class="hljs-number">2</span>],
              <span class="hljs-attr">low</span>: -center,
              <span class="hljs-attr">high</span>: -center - value,
              <span class="hljs-attr">value</span>: value,
              <span class="hljs-comment">// add the calculated percentage value</span>
              <span class="hljs-attr">percentValue</span>: percentValue
            });
          }
        }

        <span class="hljs-keyword">var</span> series = chart.rangeBar(data);
        series.name(name).stroke(<span class="hljs-string">"3 #fff 1"</span>).selectionMode(<span class="hljs-string">"none"</span>);
      };

      <span class="hljs-comment">// create series</span>
      createSeries(<span class="hljs-number">0</span>, <span class="hljs-string">"Losses"</span>);
      createSeries(<span class="hljs-number">1</span>, <span class="hljs-string">"Wins"</span>);

      <span class="hljs-comment">// set the chart title</span>
      chart
        .title()
        .enabled(<span class="hljs-literal">true</span>)
        .text(<span class="hljs-string">"20 Years of LA Lakers Win-Loss Record with Kobe Bryant (1996-2016)"</span>);

      <span class="hljs-comment">// enable the chart legend</span>
      chart
        .legend()
        .enabled(<span class="hljs-literal">true</span>);

      <span class="hljs-comment">// create a stacked bar chart from the multi-series bar chart</span>
      chart.yScale().stackMode(<span class="hljs-string">"value"</span>);

      <span class="hljs-comment">// customize the settings of the axes</span>
      chart
        .xAxis()
        .ticks(<span class="hljs-literal">false</span>);
      chart
        .xAxis()
        .title()
        .enabled(<span class="hljs-literal">true</span>)
        .text(<span class="hljs-string">"Years"</span>)
        .padding([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>]);
      chart
        .xAxis()
        .labels()
        .fontSize(<span class="hljs-number">11</span>)
        .fontColor(<span class="hljs-string">"#474747"</span>)
        .padding([<span class="hljs-number">0</span>, <span class="hljs-number">10</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]);
      chart.yScale().maximum(<span class="hljs-number">80</span>);
      chart
        .yAxis(<span class="hljs-number">0</span>)
        .labels()
        .format(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
          <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.abs(<span class="hljs-built_in">this</span>.value);
        });

      <span class="hljs-comment">// create a line marker at 0</span>
      chart
        .lineMarker()
        .value(<span class="hljs-number">0</span>)
        .stroke(<span class="hljs-string">"#CECECE"</span>);

      <span class="hljs-comment">// customize the tooltip</span>
      chart
        .tooltip()
        .useHtml(<span class="hljs-literal">true</span>)
        .fontSize(<span class="hljs-number">12</span>)
        .titleFormat(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
          <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.getData(<span class="hljs-string">"x"</span>) + <span class="hljs-string">" "</span> + <span class="hljs-built_in">this</span>.seriesName;
        })
        .format(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
          <span class="hljs-keyword">return</span> (
            <span class="hljs-string">"&lt;h6 style='font-size:12px; font-weight:400; margin: 0.25rem 0;'&gt;Total games: "</span> +
            <span class="hljs-string">"&lt;b&gt;"</span> +
            <span class="hljs-built_in">this</span>.getData(<span class="hljs-string">"value"</span>) +
            <span class="hljs-string">"&lt;/b&gt;&lt;/h6&gt;"</span> +
            <span class="hljs-string">"&lt;h6 style='font-size:12px; font-weight:400; margin: 0.25rem 0;'&gt;Percentage games: "</span> +
            <span class="hljs-string">"&lt;b&gt;"</span> +
            <span class="hljs-built_in">this</span>.getData(<span class="hljs-string">"percentValue"</span>) +
            <span class="hljs-string">" %&lt;/b&gt;&lt;/h6&gt;"</span>
          );
        });

      <span class="hljs-comment">// set a custom color palette</span>
      chart.palette(
        anychart.palettes.distinctColors().items([<span class="hljs-string">"#FDB827"</span>, <span class="hljs-string">"#542583"</span>])
      );

      <span class="hljs-comment">// set a container id for the chart</span>
      chart.container(<span class="hljs-string">"container"</span>);

      <span class="hljs-comment">// initiate chart drawing</span>
      chart.draw();

    });

    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span></span>
&lt;/html&gt;
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, I have shown you how fast and easy it is to get a diverging bar chart up and running using JavaScript. We've also seen how a little bit of effort makes the graphic look really cool and lets you get more out of the underlying data. Please let me know if you have any questions.</p>
<p>If you are feeling motivated to work more with interactive JS-based data visualization, go ahead and play around with the diverging bar charts on CodePen (I added links throughout the tutorial), check out <a target="_blank" href="https://www.anychart.com/chartopedia/">other chart options</a>, or try <a target="_blank" href="https://en.wikipedia.org/wiki/Comparison_of_JavaScript_charting_libraries">other JavaScript libraries</a>.</p>
<p>Also, as we fondly look back at the statistics of the basketball legend’s team here, remember to do more sports and create more visualizations!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Integrate ApexCharts with Vue.js – a Detailed Guide ]]>
                </title>
                <description>
                    <![CDATA[ By Oluwaseyi Bello Charts and graphs are a great way to display information/data to your app's users. In this article I will show you exactly how to do that with vue-apexcharts. Getting Started Using the Vue CLI we can easily create a starter applica... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/a-detailed-guide-to-integrating-apexcharts-with-vuejs/</link>
                <guid isPermaLink="false">66d460893a8352b6c5a2aac7</guid>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Vue.js ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 15 Jun 2020 23:14:04 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/06/vueApex-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Oluwaseyi Bello</p>
<p>Charts and graphs are a great way to display information/data to your app's users. In this article I will show you exactly how to do that with vue-apexcharts.</p>
<h2 id="heading-getting-started">Getting Started</h2>
<p>Using the Vue CLI we can easily create a starter application. First, we need to install the Vue CLI with the command below. You can skip this if you have it installed already.</p>
<p>Note: In order to use the CLI, you’ll need to have <a target="_blank" href="https://nodejs.org/">Node.js</a> version 8 or above installed (8.10.0+ is recommended).</p>
<p>To install the CLI, run this command in your terminal:</p>
<pre><code>npm install @vue/cli
</code></pre><p>Now we can use it to create our project. We will create the project using this command:</p>
<pre><code>vue create vue-apexcharts-demo
</code></pre><p>We’ll then be prompted with the option to pick a default preset or to manually select features. Select <code>**default**</code>.</p>
<p>A new project directory will be created, and you can navigate into it using this command:</p>
<pre><code>cd vue-apexcharts-demo
</code></pre><h2 id="heading-installing-apexcharts">Installing Apexcharts</h2>
<p>Before we continue, you might be wondering…</p>
<p><strong>What is apexcharts?!</strong></p>
<p>ApexCharts is a modern charting library that helps developers create beautiful and interactive visualizations for web pages. You can view their demo <a target="_blank" href="https://apexcharts.com/javascript-chart-demos/">here</a>.</p>
<p>We can easily use ApexCharts with our Vue application by integrating its wrapper component for Vue called <strong>vue-apexcharts</strong>.</p>
<p>To install the vue-apexcharts component in our application, use this command:</p>
<pre><code>npm install --save apexcharts 
npm install --save vue-apexcharts
</code></pre><h2 id="heading-configuring-vue-apexcharts">Configuring vue-apexcharts</h2>
<p>Now that we have vue-apexcharts installed we need to install it in our application. Open up the <code>src</code> directory and create a new directory called <code>plugins</code>. Inside the new plugins directory create a file called <code>apexcharts.js</code>.</p>
<p>Next, we will create a Vue component for apexcharts in our <code>apexcharts.js</code>. This will make the component globally available in our application. </p>
<p>To do this, we'll be importing both Vue and vue-apexcharts. Next, we will create a global component called <code>apexchart</code>. Here is what your <code>apexcharts.js</code> file should look like:</p>
<pre><code><span class="hljs-keyword">import</span> Vue <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>
<span class="hljs-keyword">import</span> VueApexCharts <span class="hljs-keyword">from</span> <span class="hljs-string">'vue-apexcharts'</span>

Vue.component(<span class="hljs-string">'apexchart'</span>, VueApexCharts)
</code></pre><h2 id="heading-importing-our-plugin-file">Importing our plugin file</h2>
<p>We have to make Vue aware of the file we just created. To do that, we import it in the <code>main.js</code> file. Open up the main.js file and add the following line after the last import statement:</p>
<pre><code><span class="hljs-keyword">import</span> <span class="hljs-string">'@/plugins/apexcharts'</span>
</code></pre><p>Now we are ready to create our first chart.</p>
<h2 id="heading-creating-our-first-chart">Creating our First Chart</h2>
<p>We will be creating our charts in the HelloWorld component. This component was created automatically when we used the Vue CLI to create our application.</p>
<p>Open up the file <code>HelloWorld.vue</code> and delete all the demo code there.</p>
<p>Your file should look like this:</p>
<pre><code>&lt;template&gt;
&lt;/template&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">  
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
 <span class="hljs-attr">name</span>: <span class="hljs-string">'HelloWorld'</span>
}
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span></span>
</code></pre><p>Remember in our plugin, we called our component <code>apexchart</code>.</p>
<p>Vue-Apexcharts builds charts by using the data you pass into it using props, as seen in the code below. We will use that to create the HTML for our first chart. In the first example, we will start with a very basic bar chart.</p>
<pre><code>&lt;template&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">apexchart</span> 
     <span class="hljs-attr">width</span>=<span class="hljs-string">"500"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"bar"</span> 
     <span class="hljs-attr">:options</span>=<span class="hljs-string">"options"</span> <span class="hljs-attr">:series</span>=<span class="hljs-string">"series"</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">apexchart</span>&gt;</span>  
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
&lt;/template&gt;
</code></pre><p>As you can see in the above template, the <code>apexchart</code> component holds 4 props. Now, let’s jump to the script part.</p>
<pre><code>&lt;script&gt;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
  <span class="hljs-attr">name</span>: <span class="hljs-string">'HelloWorld'</span>,
  <span class="hljs-attr">data</span>: <span class="hljs-function">() =&gt;</span> ({
    <span class="hljs-attr">options</span>: {
      <span class="hljs-attr">chart</span>: {
        <span class="hljs-attr">id</span>: <span class="hljs-string">'vuechart-example'</span>
      },
      <span class="hljs-attr">xaxis</span>: {
        <span class="hljs-attr">categories</span>: [
         <span class="hljs-string">"Jan"</span>,
         <span class="hljs-string">"Feb"</span>,
         <span class="hljs-string">"Mar"</span>,
         <span class="hljs-string">"Apr"</span>,
         <span class="hljs-string">"May"</span>,
         <span class="hljs-string">"Jun"</span>,
         <span class="hljs-string">"Jul"</span>,
         <span class="hljs-string">"Aug"</span>,
         <span class="hljs-string">"Sep"</span>,
         <span class="hljs-string">"Oct"</span>,
         <span class="hljs-string">"Nov"</span>,
         <span class="hljs-string">"Dec"</span>
        ]
      }
    },
    <span class="hljs-attr">series</span>: [{
      <span class="hljs-attr">name</span>: <span class="hljs-string">'series-1'</span>,
      <span class="hljs-attr">data</span>: [<span class="hljs-number">55</span>, <span class="hljs-number">62</span>, <span class="hljs-number">89</span>, <span class="hljs-number">66</span>, <span class="hljs-number">98</span>, <span class="hljs-number">72</span>, <span class="hljs-number">101</span>, <span class="hljs-number">75</span>, <span class="hljs-number">94</span>, <span class="hljs-number">120</span>, <span class="hljs-number">117</span>, <span class="hljs-number">139</span>]
    }]
  })
}
&lt;/script&gt;
</code></pre><h2 id="heading-explaining-chart-data">Explaining Chart Data</h2>
<p>Our bar chart contains monthly stock pricing for a fictional organization. We have 12 data points in our bar chart. Each month will be displayed on the x-axis of the chart. Their pricing is displayed on the y-axis of the chart.</p>
<p>To provide the data that will be displayed in our bar chart, we will add a series array. A series is an array of objects. Each object defines the type of chart to be created and will have a data array of values to be plotted on the graph.</p>
<p>You can start your server with the command:</p>
<pre><code>npm run serve
</code></pre><p>Now, open your browser to localhost:8080 and you should see your chart:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/firstapexchart.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-styling-our-charts">Styling our charts</h3>
<p>We can place our chart in the center of the page. To do this I am going to add a class <code>chart-wrapper</code> inside our div. I also want to increase the width of the chart to 800px. My template now looks like this:</p>
<pre><code>&lt;template&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"chart-wrapper"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">apexchart</span> 
      <span class="hljs-attr">width</span>=<span class="hljs-string">"800"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"bar"</span> 
      <span class="hljs-attr">:options</span>=<span class="hljs-string">"options"</span> <span class="hljs-attr">:series</span>=<span class="hljs-string">"series"</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">apexchart</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
&lt;/template&gt;
</code></pre><p>Next, let’s add some styling to the new <code>chart-wrapper</code> class that will place our chart in the center of the page. Here is the style I have added:</p>
<pre><code>&lt;style scoped&gt;
div.chart-wrapper {
  <span class="hljs-attr">display</span>: flex;
  align-items: center;
  justify-content: center
}
&lt;/style&gt;
</code></pre><p>While we are adding styles I want to replace the Vue logo with a title. Open up the App.vue file. Delete the <img width="600" height="400" alt="" loading="lazy"> tag and replace it with:</p>
<pre><code>&lt;h1&gt;Vue Apexcharts Demo&lt;/h1&gt;
</code></pre><p>Our charts now looks like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/firsapexchart.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-adding-a-title-and-changing-chart-color">Adding a Title and Changing Chart Color</h3>
<p>We can add a title to our bar chart to add further context and an explanation of what the chart is all about.</p>
<p>To add a title to our bar chart, go back to your HelloWorld.vue component and add a title to the <code>options</code> object.</p>
<pre><code>options: {
      <span class="hljs-attr">chart</span>: {
        <span class="hljs-attr">id</span>: <span class="hljs-string">'vuechart-example'</span>
      },
      <span class="hljs-attr">xaxis</span>: {
        <span class="hljs-attr">categories</span>: [
         <span class="hljs-string">"Jan"</span>,
         <span class="hljs-string">"Feb"</span>,
         <span class="hljs-string">"Mar"</span>,
         <span class="hljs-string">"Apr"</span>,
         <span class="hljs-string">"May"</span>,
         <span class="hljs-string">"Jun"</span>,
         <span class="hljs-string">"Jul"</span>,
         <span class="hljs-string">"Aug"</span>,
         <span class="hljs-string">"Sep"</span>,
         <span class="hljs-string">"Oct"</span>,
         <span class="hljs-string">"Nov"</span>,
         <span class="hljs-string">"Dec"</span>
        ]
      },
      <span class="hljs-attr">title</span>: {
        <span class="hljs-attr">text</span>: <span class="hljs-string">'Monthly Stock Pricing'</span>
      }
 },
 <span class="hljs-attr">series</span>: [{
   <span class="hljs-attr">name</span>: <span class="hljs-string">'series-1'</span>,
   <span class="hljs-attr">data</span>: [<span class="hljs-number">55</span>, <span class="hljs-number">62</span>, <span class="hljs-number">89</span>, <span class="hljs-number">66</span>, <span class="hljs-number">98</span>, <span class="hljs-number">72</span>, <span class="hljs-number">101</span>, <span class="hljs-number">75</span>, <span class="hljs-number">94</span>, <span class="hljs-number">120</span>, <span class="hljs-number">117</span>, <span class="hljs-number">139</span>]
 }]
</code></pre><p>By default, the title is placed on the left side of the bar chart. Here is what our chart looks like now:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/firapexchart.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Let’s change the default behaviour by adding our own custom style. I want the title to have a bigger font size and to be centered. The title object has a property called <code>align</code> which represents the horizontal plane. I want the title centered on this. To make the title have a bigger font size we need to add a <code>style</code> option.</p>
<p>Also, we can set the bar chart to have a different color. Here is what <code>options</code> look like now:</p>
<pre><code>options: {
      <span class="hljs-attr">chart</span>: {
        <span class="hljs-attr">id</span>: <span class="hljs-string">'vuechart-example'</span>
      },
      <span class="hljs-attr">xaxis</span>: {
        <span class="hljs-attr">categories</span>: [
         <span class="hljs-string">"Jan"</span>,
         <span class="hljs-string">"Feb"</span>,
         <span class="hljs-string">"Mar"</span>,
         <span class="hljs-string">"Apr"</span>,
         <span class="hljs-string">"May"</span>,
         <span class="hljs-string">"Jun"</span>,
         <span class="hljs-string">"Jul"</span>,
         <span class="hljs-string">"Aug"</span>,
         <span class="hljs-string">"Sep"</span>,
         <span class="hljs-string">"Oct"</span>,
         <span class="hljs-string">"Nov"</span>,
         <span class="hljs-string">"Dec"</span>
        ]
      },
      <span class="hljs-attr">title</span>: {
        <span class="hljs-attr">text</span>: <span class="hljs-string">'Monthly Stock Pricing'</span>,
        <span class="hljs-attr">align</span>: <span class="hljs-string">'center'</span>,
        <span class="hljs-attr">style</span>: {
          <span class="hljs-attr">fontSize</span>:  <span class="hljs-string">'20px'</span>,
        },
      },
      <span class="hljs-attr">colors</span>: [<span class="hljs-string">'#00897b'</span>]
 },
 <span class="hljs-attr">series</span>: [{
   <span class="hljs-attr">name</span>: <span class="hljs-string">'series-1'</span>,
   <span class="hljs-attr">data</span>: [<span class="hljs-number">55</span>, <span class="hljs-number">62</span>, <span class="hljs-number">89</span>, <span class="hljs-number">66</span>, <span class="hljs-number">98</span>, <span class="hljs-number">72</span>, <span class="hljs-number">101</span>, <span class="hljs-number">75</span>, <span class="hljs-number">94</span>, <span class="hljs-number">120</span>, <span class="hljs-number">117</span>, <span class="hljs-number">139</span>]
 }]
</code></pre><p>This is what the bar chart looks like now:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/fiapexchart.png" alt="Image" width="600" height="400" loading="lazy">
<em>Final version of our bar chart</em></p>
<h3 id="heading-creating-a-line-chart">Creating a Line Chart</h3>
<p>First, we need to create a new chart-wrapper div and a new apexchart element. The new apexchart element will hold props like the previous example. But the interesting thing here is all you need to do is to change the prop <code>type</code> in the template to <code>line</code>. </p>
<p>Here is what my HTML code looks like now:</p>
<pre><code>&lt;template&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"chart-wrapper"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">apexchart</span> 
        <span class="hljs-attr">width</span>=<span class="hljs-string">"800"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"bar"</span> 
        <span class="hljs-attr">:options</span>=<span class="hljs-string">"options"</span> <span class="hljs-attr">:series</span>=<span class="hljs-string">"series"</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">apexchart</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">hr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"chart-wrapper"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">apexchart</span> 
        <span class="hljs-attr">width</span>=<span class="hljs-string">"800"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"line"</span> 
        <span class="hljs-attr">:options</span>=<span class="hljs-string">"options"</span> <span class="hljs-attr">:series</span>=<span class="hljs-string">"series"</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">apexchart</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span></span>
</code></pre><p>We now have this line graph:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/06/fapexchart.png" alt="Image" width="600" height="400" loading="lazy">
<em>Line chart</em></p>
<h2 id="heading-apexcharts-documentation">Apexcharts Documentation</h2>
<p>Apexcharts have different charts type you can choose from apart from bar and line. They also have various options that you can add to your chart. You can add legends or markers, and tooltips for example.</p>
<p>If you want to find out about the other chart types and options that are available you can read their documentation <a target="_blank" href="https://apexcharts.com/docs/installation/">here</a></p>
<h3 id="heading-repo">Repo</h3>
<p>You can find the code for this article in <a target="_blank" href="https://github.com/Seybel/vue-apexcharts-demo">my github account</a>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Charts helps us visualize data and help users view and interact with it. Integrating ApexCharts to your Vue application is just as easy as you have seen above.</p>
<p>Feedback is welcome! And please share this article if you found it helpful.</p>
<p>Thanks for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create a Simple Gantt Chart Using CSS Grid ]]>
                </title>
                <description>
                    <![CDATA[ By Alfrick Opidi A Gantt chart is a handy type of bar chart that is used in project management for showcasing a schedule of tasks. This chart visualizes project activities as cascading horizontal bars, with width depicting the project’s duration.  As... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/create-gantt-chart-using-css-grid/</link>
                <guid isPermaLink="false">66d45d99768263422736e8ad</guid>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ data visualization ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 04 Feb 2020 16:32:18 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9ccd740569d1a4ca344b.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Alfrick Opidi</p>
<p>A Gantt chart is a handy type of bar chart that is used in project management for showcasing a schedule of tasks. This chart visualizes project activities as cascading horizontal bars, with width depicting the project’s duration. </p>
<p>As a front-end web designer or developer, you can make use of Gantt charts to manage projects and enhance the productivity within your team.</p>
<p>In this article, I’m going to show you how to create a simple Gantt chart using the CSS Grid Layout system—no external libraries or other fluff, just pure CSS.</p>
<p>You can reference <a target="_blank" href="https://www.freecodecamp.org/learn/responsive-web-design/css-grid/">this tutorial</a> to understand how to use the layout system for applying CSS rules.</p>
<p>The chart will show a typical software development life cycle process, from January to December. </p>
<p>Here is a screenshot of how the Gantt chart will look at the end of this tutorial:</p>
<p><img src="https://paper-attachments.dropbox.com/s_71DD472E9787F22210482D610A0DD84B11827762D701C2FF3CA4E87715003165_1567325886724_gantt-chart.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Let’s get started!</p>
<h2 id="heading-step-1-create-a-container-div">Step 1: Create a container div</h2>
<p>Let’s start by creating a container <strong>div</strong> element for the Gantt Chart:</p>
<pre><code class="lang-css">&lt;<span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">class</span>="<span class="hljs-selector-tag">container</span>"&gt;

&lt;/<span class="hljs-selector-tag">div</span>&gt;
</code></pre>
<p>Let’s add some CSS styling to it:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {  
    <span class="hljs-attribute">max-width</span>: <span class="hljs-number">1200px</span>; 
    <span class="hljs-attribute">min-width</span>: <span class="hljs-number">650px</span>;  
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto; 
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">50px</span>;
}
</code></pre>
<h2 id="heading-step-2-create-a-chart-div">Step 2: Create a chart div</h2>
<p>Let’s create a div inside the overarching container and name it chart. This is where all the remaining actions are going to take place.</p>
<pre><code class="lang-css">&lt;<span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">class</span>="<span class="hljs-selector-tag">chart</span>"&gt;

&lt;/<span class="hljs-selector-tag">div</span>&gt;
</code></pre>
<p>Let’s add some CSS styling to it:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.chart</span> { 
    <span class="hljs-attribute">display</span>: grid;  
    <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#000</span>;  
    <span class="hljs-attribute">position</span>: relative;  
    <span class="hljs-attribute">overflow</span>: hidden; 
}
</code></pre>
<p>Notice that I’ve set the <strong>display</strong> property of the class to <strong>grid.</strong> Consequently, all its direct children will automatically become <em>grid items</em>.</p>
<h2 id="heading-step-3-create-the-charts-rows">Step 3: Create the chart’s rows</h2>
<p>Let’s start by creating the first row, which will be the heading of the Gantt chart.</p>
<pre><code class="lang-css">&lt;<span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">class</span>="<span class="hljs-selector-tag">chart-row</span> <span class="hljs-selector-tag">chart-period</span>"&gt;
&lt;<span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">class</span>="<span class="hljs-selector-tag">chart-row-item</span>"&gt;
    &lt;/<span class="hljs-selector-tag">div</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">January</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">February</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">March</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;
    &lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">April</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">May</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">June</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">July</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;
    &lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">August</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">September</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">October</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;
    &lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">November</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;<span class="hljs-selector-tag">December</span>&lt;/<span class="hljs-selector-tag">span</span>&gt;
&lt;/<span class="hljs-selector-tag">div</span>&gt;
</code></pre>
<p>Notice that I’ve provided 12 <strong>span</strong> elements that will transverse the entire row, showing the months of the project’s duration—from January to December.</p>
<p>Here is its CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.chart-row</span> {  
    <span class="hljs-attribute">display</span>: grid; 
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">50px</span> <span class="hljs-number">1</span>fr; 
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#DCDCDC</span>;
}
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.chart-period</span> { 
    <span class="hljs-attribute">color</span>:  <span class="hljs-number">#fff</span>;  
    <span class="hljs-attribute">background-color</span>:  <span class="hljs-number">#708090</span> <span class="hljs-meta">!important</span>;  
    <span class="hljs-attribute">border-bottom</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#000</span>;  
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">50px</span> <span class="hljs-built_in">repeat</span>(<span class="hljs-number">12</span>, <span class="hljs-number">1</span>fr);

}

<span class="hljs-selector-class">.chart-period</span> &gt; <span class="hljs-selector-tag">span</span> {
    <span class="hljs-attribute">text-align</span>: center;  
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">13px</span>;  
    <span class="hljs-attribute">align-self</span>: center;  
    <span class="hljs-attribute">font-weight</span>: bold;  
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">15px</span> <span class="hljs-number">0</span>;   
}
</code></pre>
<p>Notice that I used the  <strong>grid-template-columns</strong> property to specify the width and the number of columns in the grid layout.</p>
<p>Let’s see how it looks in a browser, so far:</p>
<p><img src="https://paper-attachments.dropbox.com/s_71DD472E9787F22210482D610A0DD84B11827762D701C2FF3CA4E87715003165_1567023822884_gantt1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Next, let’s add lines that will run throughout the chart in a box-like style, which helps showcase the duration of each project. </p>
<p>I also used 12 <strong>span</strong> elements for creating the lines.</p>
<pre><code class="lang-css">&lt;<span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">class</span>="<span class="hljs-selector-tag">chart-row</span> <span class="hljs-selector-tag">chart-lines</span>"&gt; 
    &lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;
    &lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;
    &lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;
    &lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;&lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;    &lt;<span class="hljs-selector-tag">span</span>&gt;&lt;/<span class="hljs-selector-tag">span</span>&gt;    
&lt;/<span class="hljs-selector-tag">div</span>&gt;
</code></pre>
<p>Here is its CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.chart-lines</span> { 
    <span class="hljs-attribute">position</span>: absolute;  
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;  
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;  
    <span class="hljs-attribute">background-color</span>: transparent;  
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">50px</span> <span class="hljs-built_in">repeat</span>(<span class="hljs-number">12</span>, <span class="hljs-number">1</span>fr);}

<span class="hljs-selector-class">.chart-lines</span> &gt; <span class="hljs-selector-tag">span</span> {  
    <span class="hljs-attribute">display</span>: block;  <span class="hljs-attribute">border-right</span>: <span class="hljs-number">1px</span> solid <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.3</span>);
}
</code></pre>
<p>Let’s see the output in a browser:</p>
<p><img src="https://paper-attachments.dropbox.com/s_71DD472E9787F22210482D610A0DD84B11827762D701C2FF3CA4E87715003165_1567024250393_gantt2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-step-4-add-entry-items">Step 4: Add entry items</h2>
<p>Finally, let’s add the items that illustrate a year-long process of creating some software.</p>
<p>For example, here is how I added the first entry item:</p>
<pre><code class="lang-css">&lt;<span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">class</span>="<span class="hljs-selector-tag">chart-row</span>"&gt;  
    &lt;<span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">class</span>="<span class="hljs-selector-tag">chart-row-item</span>"&gt;1&lt;/<span class="hljs-selector-tag">div</span>&gt; 
    &lt;<span class="hljs-selector-tag">ul</span> <span class="hljs-selector-tag">class</span>="<span class="hljs-selector-tag">chart-row-bars</span>"&gt;    
        &lt;<span class="hljs-selector-tag">li</span> <span class="hljs-selector-tag">class</span>="<span class="hljs-selector-tag">chart-li-one</span>"&gt;<span class="hljs-selector-tag">Planning</span>&lt;/<span class="hljs-selector-tag">li</span>&gt;
    &lt;/<span class="hljs-selector-tag">ul</span>&gt;
&lt;/<span class="hljs-selector-tag">div</span>&gt;
</code></pre>
<p>Let me describe what is happening in the code above:</p>
<ul>
<li>First, the encompassing <strong>div</strong> element has a class of <strong>chart-row</strong>, which I illustrated earlier.</li>
<li>The <strong>div</strong> with a class of <strong>chart-row-item</strong> is used for numbering the entry items on the Gantt chart. Here is its CSS:</li>
</ul>
<pre><code class="lang-css"><span class="hljs-selector-class">.chart-row-item</span> { 
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#808080</span>;  
    <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#000</span>;  
    <span class="hljs-attribute">border-top</span>: <span class="hljs-number">0</span>;  <span class="hljs-attribute">border-left</span>: <span class="hljs-number">0</span>;  
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span> <span class="hljs-number">0</span>;  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">15px</span>;  
    <span class="hljs-attribute">font-weight</span>: bold;  
    <span class="hljs-attribute">text-align</span>: center;
}
</code></pre>
<ul>
<li>To show tasks on the Gantt chart, I created an unordered list and styled it to display a horizontal bar, with its length showing the duration of the task. </li>
</ul>
<p>Here is the CSS styling for the <strong>chart-row-bars</strong> class:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.chart-row-bars</span> { 
    <span class="hljs-attribute">list-style</span>: none; 
    <span class="hljs-attribute">display</span>: grid;  <span class="hljs-attribute">padding</span>: <span class="hljs-number">15px</span> <span class="hljs-number">0</span>;  
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;  
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">12</span>, <span class="hljs-number">1</span>fr); 
    <span class="hljs-attribute">grid-gap</span>: <span class="hljs-number">10px</span> <span class="hljs-number">0</span>;  
    <span class="hljs-attribute">border-bottom</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#000</span>;
}
</code></pre>
<ul>
<li>The entry item is defined in the <strong>li</strong> tag. Here is its CSS styling:</li>
</ul>
<pre><code class="lang-css"><span class="hljs-selector-tag">li</span> {  
    <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">450</span>;  
    <span class="hljs-attribute">text-align</span>: left;  
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">15px</span>;  <span class="hljs-attribute">min-height</span>: <span class="hljs-number">15px</span>;  
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#708090</span>;  
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">5px</span> <span class="hljs-number">15px</span>;  <span class="hljs-attribute">color</span>: <span class="hljs-number">#fff</span>;  
    <span class="hljs-attribute">overflow</span>: hidden;  
    <span class="hljs-attribute">position</span>: relative;  
    <span class="hljs-attribute">cursor</span>: pointer;  
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">15px</span>;
 } 

 <span class="hljs-selector-tag">ul</span> <span class="hljs-selector-class">.chart-li-one</span> { 
     <span class="hljs-attribute">grid-column</span>: <span class="hljs-number">1</span>/<span class="hljs-number">2</span>;  
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#588BAE</span>;
 }
</code></pre>
<p>Notice that I’ve used the <strong>grid-column</strong> property to specify the duration of the project. </p>
<p>For example, a property of  <strong>grid-column: 3/9;</strong> like the “Development” entry, spans a task across the grid from March to August.</p>
<p>Here is how the first entry item looks in a browser:</p>
<p><img src="https://paper-attachments.dropbox.com/s_71DD472E9787F22210482D610A0DD84B11827762D701C2FF3CA4E87715003165_1567024868326_gantt3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>I added the other entries on the chart following the same process as the first entry. Ultimately, it resulted into a nice-looking Gantt chart, just like the image I showed earlier. </p>
<h2 id="heading-wrapping-up">Wrapping up</h2>
<p>That’s it! You can view the entire code for this tutorial on CodePen:  </p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/jasoya/embed/GRKWXvr" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p>As you’ve seen, creating a Gantt chart using CSS Grid is not complicated. With this type of chart, you can manage your web development projects effectively and ensure that everyone is on track toward accomplishing the stipulated objectives.</p>
<p>Furthermore, Gantt charts can also be used in other industries to manage projects. For example, if you are selling <a target="_blank" href="https://www.waterless-toilet.com/top-6-best-composting-toilets-to-choose/">composting toilets</a>, you can use Gantt charts to showcase the number of sales made over a period of time. </p>
<p>Of course, I’ve just scratched the surface about the things you can achieve with Gantt charts. </p>
<p>There are several other tweaks you can make to Gantt charts to suit your specific requirements and project’s goals. For example, you can use them to show the relationship between various tasks and how the completion of one relies on another, show how resources can be allocated for the success of projects, and show clear project requirements that ensure everyone is on the same page.</p>
<p>Do you have any questions or comments?</p>
<p>Please get in touch via Twitter below and I’ll do my best to respond.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to make your first JavaScript chart with JSCharting ]]>
                </title>
                <description>
                    <![CDATA[ By Arthur Puszynski When you're starting out as a beginner JavaScript developer, I think it is important to pursue interesting projects. That way you can make sure you have fun as you learn, and you'll likely find an area of specialization that you e... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-make-your-first-javascript-chart/</link>
                <guid isPermaLink="false">66d45d99a44b8bb91150f64d</guid>
                
                    <category>
                        <![CDATA[ charts ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 08 Jan 2020 14:45:15 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/01/first-javascript-chart-using-csv-jscharting-fit.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Arthur Puszynski</p>
<p>When you're starting out as a beginner JavaScript developer, I think it is important to pursue interesting projects. That way you can make sure you have fun as you learn, and you'll likely find an area of specialization that you enjoy. </p>
<p>As they say, <em>"If you love what you do, you'll never work a day in your life"</em>. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/not-suited-for-work.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Source: giphy.com</em></p>
<p>In this article, I will introduce you to front-end data visualization, which is my personal passion. Perhaps it will become your passion as well!</p>
<p>The most rewarding moments for me as a developer are when I can see or experience the results of what I've made. It’s highly satisfying to create a chart that reveals interesting insights about its data, or an interactive experience that helps explore details of a unique data set. The more significant the result, the more rewarding it feels. </p>
<p>However, I have realized that the amount of work you put into a project does not necessarily correlate with the sense of accomplishment – sometimes it feels great even when it was relatively easy. </p>
<p>Over time, you will find tools that will help make you more efficient, and sometimes you will move mountains with little effort. There are many chart libraries and tools available in the data visualization field. With the right tools, you will create new charts with little effort, regardless of the type of chart you need. Personally, I think that datavis yields a great reward on your investment of time and effort.</p>
<p>In this tutorial you will use a number of tools to get data over the internet, process it, and draw a beautiful chart that can be viewed in any modern browser. You can click the links below to download example code for each step individually, view them all on <strong><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article">GitHub</a></strong>, or download all steps at once here: <strong><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article/raw/master/zips/all-steps.zip">all-steps.zip</a>.</strong></p>
<h2 id="heading-the-result">The Result</h2>
<p>By the end of this tutorial you will create this interactive data-driven chart. You will learn how to get data over the internet, process it, and make a chart with that data. You will also be able to make your own charts from scratch.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/javascript-line-chart.png" alt="Interactive JavaScript line chart" width="600" height="400" loading="lazy">
<em>Interactive JavaScript line chart</em></p>
<p>After processing the data and charting it, you will also learn how to make adjustments to the chart including modifying the default legend, enabling x axis crosshairs with tooltips, and applying text annotations to add context and other information to the chart.</p>
<h2 id="heading-the-tools">The Tools</h2>
<p>To get started, use an internet browser like the one you're probably using to read this article. I recommend Chrome as it offers a great experience and built in tools for developers.</p>
<p>Next you will need a text editor. Something as simple as notepad will work. But, I suggest using a more advanced code editor such as VS Code, as this is an environment you will spend a lot of time with. It will give you a more convenient and pleasant coding experience, and it makes writing HTML5, CSS, and JavaScript easier on the eyes. Most important, if you forget a quote or comma somewhere, a code editor can help you find the error.</p>
<p>This article can help you <a target="_blank" href="https://www.freecodecamp.org/news/how-to-choose-a-javascript-code-editor/">choose the best JavaScript code editor for web development</a>.</p>
<p>You will use the JSCharting chart library to automatically draw and add interactive functionality for this chart. No other JavaScript libraries such as jQuery, or front-end platforms including React and Angular (commonly used for website projects) will be required.</p>
<h3 id="heading-why-jscharting">Why JSCharting?</h3>
<p><a target="_blank" href="https://jscharting.com/">JSCharting</a> is a JavaScript charting library that can draw many different types of charts using SVG. It is easy to use and get started with, so it's a good fit for this tutorial. The API (Application Programming Interface, aka the options and settings necessary to create charts) makes difficult things simpler and it is a good option when experimenting with data visualizations. </p>
<p>You can use JSCharting for free for personal and commercial use with the included branding.</p>
<p>You can create responsive charts with JSCharting through a couple simple steps:</p>
<ul>
<li>Define a <code>&lt;div&gt;</code> tag in the HTML file with a unique id.</li>
<li>Provide this id,  data, and any other options when calling <code>JSC.Chart()</code> in the JavaScript file.</li>
</ul>
<p>That's it. JSC will draw a professional looking chart populating this div tag with SVG element visuals. The chart will be responsive and interactive without any extra effort.</p>
<h2 id="heading-the-data">The Data</h2>
<p>You will use a data file provided by the NCHS (National Center for Health Statistics) listing historical life expectancy of males and females in the US.</p>
<p>You can find it here: <a target="_blank" href="https://data.cdc.gov/resource/w9j2-ggv5.csv">https://data.cdc.gov/resource/w9j2-ggv5.csv</a>. </p>
<p>This CSV file contains data that categorizes the life expectancies by year, race, and sex. You will use some of this data to draw a simple male/female trend line over the last 100 years.</p>
<p>CSV (Comma Separated Values) is a great format for transmitting data over the internet. It is compact, human readable and you can open it directly excel, which is also nice.</p>
<p>So without further ado, let's get started.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/readycat.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Source: giphy.com</em></p>
<h2 id="heading-step-1-add-a-blank-chart">Step 1 - Add a blank chart</h2>
<p>The first zip file contains a blank starting point you can fill in as we go. If you get lost or confused, or want to skip ahead, the zip file at the end or throughout each section will bring you up to speed.  </p>
<p>If you wish to download all the files at once, see <strong><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article/raw/master/zips/all-steps.zip">all-steps.zip</a></strong> instead<em>.</em></p>
<h3 id="heading-step1-aziphttpsgithubcomarthurpuszynskifirst-chart-articlerawmasterzipsstep1-azip"><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article/raw/master/zips/step1-a.zip">step1-a.zip</a></h3>
<p>This zip file contains the following files.</p>
<ul>
<li><code>index.html</code></li>
<li><code>js/index.js</code></li>
</ul>
<p>The <code>.html</code> file is empty except for some standard code that makes it a valid file and the <code>.js</code> file is completely blank.</p>
<p>The first step is to add some scripts to the HTML web page file. Normally people suggest adding <code>&lt;script&gt;</code> tags inside the <code>&lt;head&gt;</code> tags. However, for scripts that affect the HTML content it is often better to add them after the closing <code>&lt;/body&gt;</code> tag. </p>
<p>This technique loads all the HTML into the DOM before executing any JavaScript. The chart needs the HTML loaded before it can draw in it. The DOM (Document Object Model) is a representation of your HTML code in the browser memory. Once HTML is loaded into the DOM the browser can display it and JavaScript can interact with it.</p>
<p>Start by adding the JSCharting library to the HTML file. Open the <code>index.html</code> file in your editor of choice. Then add a script tag to include JSCharting after the closing <code>&lt;/body&gt;</code> tag. The resulting code at the bottom of the file should look like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://code.jscharting.com/2.9.0/jscharting.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>This library URL points to a CDN (Content Delivery Network). It hosts the chart code and makes it convenient to quickly add the library to any HTML page for prototyping charts and experimenting. You can also <a target="_blank" href="https://jscharting.com/download/">download</a> and use the library locally or use the npm package in your project, but the CDN does not require any extra steps.</p>
<p>Next, using the same technique, add another script tag referencing your blank JavaScript file. Add this script after the <code>jscharting.js</code> script so it looks like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://code.jscharting.com/2.9.0/jscharting.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"js/index.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Great. We are almost ready to draw a blank chart. The last thing you need to do is add a <code>&lt;div&gt;</code> placeholder inside the HTML file to define where we want this chart to draw.</p>
<p>Add this HTML code inside the <code>&lt;body&gt;</code> tags.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"chartDiv"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"width:50%; height:300px; margin:0 auto;"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre>
<p>The div must have an id so you can tell the chart which div to draw in. In this case the id is <code>chartDiv</code>. </p>
<p>You may notice the style attribute of the <code>&lt;div&gt;</code> tag. It makes the div 50% of the window width, and 300 pixels tall. The margin style <code>margin:0 auto;</code> centers the div on the page. The chart will fill whatever size the div is, so changing the div size is a good way to control the chart size.</p>
<p>You're all set with the HTML file. Open the <code>index.js</code> file and add a blank chart to this page by writing the following code which includes the div id <code>chartDiv</code>:</p>
<pre><code class="lang-javascript">JSC.Chart(<span class="hljs-string">'chartDiv'</span>, {});
</code></pre>
<p>Open the <code>index.html</code> file in a browser (drag and drop the file into a web browser like chrome).</p>
<p>Not much to see yet, but you might notice a small JSC logo on this page. That indicates a chart is wired up and drawing.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/jscharting-brand.png" alt="JSCharting logo shows the chart is working" width="600" height="400" loading="lazy">
<em>JSCharting logo shows the chart is working</em></p>
<p><strong><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article/raw/master/zips/step1-b.zip">step1-b.zip</a></strong></p>
<h2 id="heading-step-2-play-with-the-chart-a-little-bit">Step 2 - Play with the chart a little bit</h2>
<p>Ok, as a test, let's add a couple values for the chart to visualize to see how it works.</p>
<p>Going back to the <code>index.js</code> file, replace the content with the following code which adds more options to the chart.</p>
<pre><code class="lang-javascript">JSC.Chart(<span class="hljs-string">'chartDiv'</span>, {
   <span class="hljs-attr">type</span>: <span class="hljs-string">'horizontal column'</span>,
   <span class="hljs-attr">series</span>: [
      {
         <span class="hljs-attr">points</span>: [
            {<span class="hljs-attr">x</span>: <span class="hljs-string">'Apples'</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">50</span>},
            {<span class="hljs-attr">x</span>: <span class="hljs-string">'Oranges'</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">42</span>}
         ]
      }
   ]
});
</code></pre>
<p>Now refresh (F5) the browser window where the <code>index.html</code> page is loaded.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/horizontal-column-chart.png" alt="Horizontal column chart with one series and two points" width="600" height="400" loading="lazy">
<em>Horizontal column chart with one series and two points</em></p>
<p>Nice! You just made your first chart using JavaScript.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/yeah-1.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Source: giphy.com</em></p>
<p>You made a bar chart by setting the chart type option to <code>'horizontal column'</code>. If you prefer a vertical column, set the value to <code>'column'</code>. You also added a series with two points to the chart for Apples and Oranges.</p>
<p>All chart data is made up of series and points. A series is simply a group of data points. Charts can contain one or more data series. Data points consist of values that map to the x and y axes. Points can also include many other descriptive variables and values.</p>
<p>The example above contains only one series. Now let's look at the options for a chart with two series. Replace the content of the JavaScript file with this code.</p>
<pre><code class="lang-javascript">JSC.Chart(<span class="hljs-string">'chartDiv'</span>, {
   <span class="hljs-attr">type</span>: <span class="hljs-string">'horizontal column'</span>,
   <span class="hljs-attr">series</span>: [
      {
         <span class="hljs-attr">name</span>:<span class="hljs-string">'Andy'</span>,
         <span class="hljs-attr">points</span>: [
            {<span class="hljs-attr">x</span>: <span class="hljs-string">'Apples'</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">50</span>},
            {<span class="hljs-attr">x</span>: <span class="hljs-string">'Oranges'</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">32</span>}
         ]
      },{
         <span class="hljs-attr">name</span>:<span class="hljs-string">'Anna'</span>,
         <span class="hljs-attr">points</span>: [
            {<span class="hljs-attr">x</span>: <span class="hljs-string">'Apples'</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">30</span>},
            {<span class="hljs-attr">x</span>: <span class="hljs-string">'Oranges'</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">22</span>}
         ]
      }
   ]
});
</code></pre>
<p>Refreshing the browser window will show this chart.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/horizontal-column-cluster.png" alt="Horizontal column chart with two series" width="600" height="400" loading="lazy">
<em>Horizontal column chart with two series</em></p>
<p>The chart options look similar. Still a bar chart, but this time there is an extra object in the series array.  We also added name properties for each series so the chart can identify them in the legend.</p>
<p>If you are interested in making different charts like radar charts, area charts, pie charts, gantt charts, or even calendar heatmap charts, take a look at the <a target="_blank" href="https://jscharting.com/examples/chart-types/">JSCharting examples gallery</a> and the source code (chart options) used to create those charts. You can quickly learn how to use other chart features by copying the available examples.</p>
<p><strong><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article/raw/master/zips/step2.zip">step2.zip</a></strong></p>
<h2 id="heading-step-3-prepare-the-data">Step 3 - Prepare the data</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/data.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Source: giphy.com</em></p>
<p>The CSV data format is exactly that – Comma Separated Values. The file contains rows (lines) and each row represents a record or entry. Normally the first row of values contains the names of each comma separated value (column). Subsequent rows contain the values themselves. </p>
<pre><code>name,age
chris,<span class="hljs-number">26</span>
mike,<span class="hljs-number">34</span>
</code></pre><p>CSV is human readable, but there are variations of this format. Sometimes if values contain commas (e.g. mailing addresses) the format doesn't work as-is so each value is also wrapped in quotes. That way the commas inside quotes are ignored and the format can still work by using only the commas outside of quotes to separate the values. </p>
<pre><code><span class="hljs-string">"name"</span>,<span class="hljs-string">"age"</span>,<span class="hljs-string">"parents"</span>
<span class="hljs-string">"Chris"</span>,<span class="hljs-string">"26"</span>,<span class="hljs-string">"Gregory, Mary"</span>
<span class="hljs-string">"Mike"</span>,<span class="hljs-string">"34"</span>,<span class="hljs-string">"David, Sarah"</span>
</code></pre><p>Values can also be separated using a different character like tabs in place of commas.</p>
<p>But let's not get bogged down in minutia. JSCharting provides a number of tools that help with this process and we will use one of them to skip worrying about the CSV file format and convert it to JSON (JavaScript Object Notation). The result will be an array of objects. Each object represents a row with named properties. The first row in the CSV file is used to define the names of those properties.</p>
<p>This is the url of the data we are interested in: <a target="_blank" href="https://data.cdc.gov/resource/w9j2-ggv5.csv">https://data.cdc.gov/resource/w9j2-ggv5.csv</a>.</p>
<p>You can click to download and open it in excel.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-28.png" alt="Image" width="600" height="400" loading="lazy">
<em>CSV file opened in Excel</em></p>
<p>However, you will download and access this CSV data in real-time using JavaScript code. The code below may be slightly confusing at first, but it's short and you can reuse it to get any CSV, text, or JSON files over the internet programmatically. It is similar to the older AJAX technology but much simpler to use.</p>
<p>Once again, replace the content of the <code>index.js</code> file with the following:</p>
<pre><code class="lang-javascript">fetch(<span class="hljs-string">'https://data.cdc.gov/resource/w9j2-ggv5.csv'</span>)
   .then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">response</span>) </span>{
      <span class="hljs-keyword">return</span> response.text();
   })
   .then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">text</span>) </span>{
    csvToSeries(text);
   })
   .catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
      <span class="hljs-comment">//Something went wrong</span>
      <span class="hljs-built_in">console</span>.log(error);
   });

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">csvToSeries</span>(<span class="hljs-params">text</span>) </span>{
   <span class="hljs-built_in">console</span>.log(text);
}
</code></pre>
<p>Why so complicated? It is because when you request a file, it does not immediately become available. There is a delay and you have to wait for the file to arrive. So first you request the file from another website using <code>fetch()</code>.</p>
<pre><code class="lang-javascript">fetch(<span class="hljs-string">'https://data.cdc.gov/resource/w9j2-ggv5.csv'</span>)
</code></pre>
<p>Then the code inside the <code>then(...)</code> argument function gets called with the response when it arrives. This function converts the response into text and returns it, which passes the result to the following <code>then()</code> argument function.</p>
<pre><code class="lang-javascript">.then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">response</span>) </span>{
    <span class="hljs-keyword">return</span> response.text();
})
</code></pre>
<p>The next <code>then(...)</code> argument function calls the <code>csvToSeries()</code> function, and passes the text as an argument.</p>
<pre><code class="lang-javascript">.then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">text</span>) </span>{
    csvToSeries(text);
})
</code></pre>
<p>In the <code>catch()</code> function, you can specify what to do if anything goes wrong. For example maybe the internet is down, or the URL is not correct.</p>
<pre><code class="lang-javascript">.catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-comment">//Something went wrong</span>
    <span class="hljs-built_in">console</span>.log(error);
});
</code></pre>
<p>In this case, the error is sent to the console.</p>
<p>In the <code>csvToSeries()</code> function we pass this text to the console for inspection.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">csvToSeries</span>(<span class="hljs-params">text</span>) </span>{
   <span class="hljs-built_in">console</span>.log(text);
}
</code></pre>
<p>? <strong>Note:</strong> The native <code>fetch()</code> function is not supported in Internet Explorer 11. If you want to support this browser as well, you can use the <code>JSC.fetch()</code> function which comes with JSCharting. It provides the same functionality, but adds additional support for IE11.</p>
<p>Drag the <code>index.html</code> file into a browser window (or refresh the page if already open) and press F12. This will open the DevTools window of the chrome browser. By default, the bottom half of the DevTools window will show the console output. This is where the text is sent when you run code like:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(text);
</code></pre>
<p><img src="https://lh4.googleusercontent.com/C9pr4DISX6SwVwUdrSUz8s54gIuNgseApHISaR-C0HXkU-8OKaup09xhIeWjn7MvzWraT4uIEYPJU63ZVopGAHSshqxE64a6m8mHQlPiTVZUV0mAh4_p_3vBvSxWnqM0B9Vt3kLP" alt="Image" width="858" height="278" loading="lazy">
<em>Console window output</em></p>
<p>You can also paste or write code into this console window to execute it. Try pasting the entire code snippet above into the console window (next to the &gt; character) and press enter. You will notice you get the same result in the console window output. This can be useful for testing a line of code and experimenting.</p>
<p><strong><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article/raw/master/zips/step3-a.zip">step3-a.zip</a></strong></p>
<p>At this point you have retrieved the text of the CSV file over the internet and sent it to the console to prove that it works. Now we can start to work with it.</p>
<p>Let's take a look at this data file to get an idea of what's inside: <a target="_blank" href="https://data.cdc.gov/resource/w9j2-ggv5.csv">https://data.cdc.gov/resource/w9j2-ggv5.csv</a></p>
<p>I used excel to sort the rows by the year column to analyze the rows of data for a single year. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-27.png" alt="Image" width="600" height="400" loading="lazy">
<em>The CSV data sorted by year.</em></p>
<p>Each year contains 9 rows with data based on race and sex. We are only interested in the highlighted male and female values of all races for each year. You will create two series based on the highlighted rows. A series for female and one for male values. </p>
<p>Now that you have an idea of what needs to happen, let's get started.</p>
<p>First, let's use the <code>JSC.csv2Json()</code> function to convert the text into JSON format and pass it to the console to see what it does. </p>
<p>Update the <code>csvToSeries()</code> function with the following code:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">csvToSeries</span>(<span class="hljs-params">text</span>) </span>{
   <span class="hljs-keyword">let</span> dataAsJson = JSC.csv2Json(text);
   <span class="hljs-built_in">console</span>.log(dataAsJson)
}
</code></pre>
<p>Refresh the browser to see the updated console output.   </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-29.png" alt="Image" width="600" height="400" loading="lazy">
<em>CSV data converted to JSON using JSC.csv2Json() utility function</em></p>
<p>The console shows an array of 1062 records. And this is one of these records:</p>
<pre><code class="lang-json">{year: <span class="hljs-number">1900</span>, race: <span class="hljs-string">"All Races"</span>, sex: <span class="hljs-string">"Both Sexes"</span>, average_life_expectancy: <span class="hljs-number">47.3</span>, mortality: <span class="hljs-number">2518</span>}
</code></pre>
<p>? <strong>Note:</strong> The console can display arrays, and objects for inspection and you can expand and collapse sections in the console to explore details.</p>
<p>The property name <code>average_life_expectancy</code> is a little long, but you will need to use it. To avoid typing it more than once, define a constant variable to store this name.  When you need to use this property, you can just write the variable name <code>lifeExp</code>. It will look like this <code>row[lifeExp]</code> instead of <code>row.average_life_expectancy</code>.</p>
<p>Add this line at the top of the <code>csvToSeries()</code> function.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">csvToSeries</span>(<span class="hljs-params">text</span>) </span>{
    <span class="hljs-keyword">const</span> lifeExp = <span class="hljs-string">'average_life_expectancy'</span>;
    ...
</code></pre>
<p>You can process this data using simple vanilla JavaScript. The end result we want is two series with data points that include a year and life expectancy for each point.</p>
<p>Update the <code>csvToSeries()</code> with the following code:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">csvToSeries</span>(<span class="hljs-params">text</span>) </span>{
    <span class="hljs-keyword">const</span> lifeExp = <span class="hljs-string">'average_life_expectancy'</span>;
    <span class="hljs-keyword">let</span> dataAsJson = JSC.csv2Json(text);
    <span class="hljs-keyword">let</span> male = [], female = [];
    dataAsJson.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">row</span>) </span>{
         <span class="hljs-comment">//add either to male, female, or discard.</span>
        <span class="hljs-built_in">console</span>.log(row);
    });
}
</code></pre>
<p>It defines arrays for male and female data points. Then it calls the array <code>dataAsJson.forEach()</code> function passing a callback <code>function(row){...}</code> function as the argument. The <code>forEach()</code> function will execute the callback function for each item in the <code>dataAsJson</code> array. For now we will just call <code>console.log(row)</code> on each row that the callback function encounters.</p>
<p>Refresh the browser and inspect the console output. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-30.png" alt="Image" width="600" height="400" loading="lazy">
<em>Each row object the callback function encountered</em></p>
<p>Let's add some logic to filter the data we want and log the result in the console window. Replace the <code>csvToSeries()</code> function with this code.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">csvToSeries</span>(<span class="hljs-params">text</span>) </span>{
    <span class="hljs-keyword">const</span> lifeExp = <span class="hljs-string">'average_life_expectancy'</span>;
    <span class="hljs-keyword">let</span> dataAsJson = JSC.csv2Json(text);
    <span class="hljs-keyword">let</span> male = [], female = [];
    dataAsJson.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">row</span>) </span>{
         <span class="hljs-comment">//add either to male, female, or discard.</span>
        <span class="hljs-keyword">if</span> (row.race === <span class="hljs-string">'All Races'</span>) {
            <span class="hljs-keyword">if</span> (row.sex === <span class="hljs-string">'Male'</span>) {
                male.push({<span class="hljs-attr">x</span>: row.year, <span class="hljs-attr">y</span>: row[lifeExp]});
            } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (row.sex === <span class="hljs-string">'Female'</span>) {
                female.push({<span class="hljs-attr">x</span>: row.year, <span class="hljs-attr">y</span>: row[lifeExp]});
            }
        }
    });
    <span class="hljs-built_in">console</span>.log([male, female]);
}
</code></pre>
<p>Inside the callback function you decide whether the row is of interest and use it or if not then discard it. </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span> (row.race === <span class="hljs-string">'All Races'</span>) {
    <span class="hljs-keyword">if</span> (row.sex === <span class="hljs-string">'Male'</span>) {
        <span class="hljs-comment">//add data to male array</span>
        male.push({<span class="hljs-attr">x</span>: row.year, <span class="hljs-attr">y</span>: row[lifeExp]});
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (row.sex === <span class="hljs-string">'Female'</span>) {
        <span class="hljs-comment">//add data to female array</span>
        female.push({<span class="hljs-attr">x</span>: row.year, <span class="hljs-attr">y</span>: row[lifeExp]});
    }
}
</code></pre>
<p>The logic checks to see if the <code>row.race</code> value equals 'All Races'. If so, then it checks to see if the <code>row.sex</code> property equals either 'Male' or 'Female'. If the row equals either, it adds the data to either the <code>male</code> or <code>female</code> arrays as a <code>{x, y}</code> point object. Notice the use of the <code>lifeExp</code> variable defined above which helps shorten this code.</p>
<p>At the end, you used <code>console.log([male, female])</code> to pass the male and female variables to the console for inspection and to make sure your code worked as expected.</p>
<p>After refreshing the browser, the console shows the result which is two arrays, each with 118 data points spanning the years 1900 to 2017.</p>
<p><img src="https://lh6.googleusercontent.com/V3yi_ZyqpoOMvn8jr1Tb31obS1WPHbgJ8p1LkPirFMLu8rjmzUs5-CgVCvtsLLnXscGO7HxR8_IM02_Q1twFPRNa1ll5JCCOoQbuK_S0hxqA7IZNoAqskksO62nXXRoSedjwUmzg" alt="Image" width="862" height="356" loading="lazy">
<em>The male and female point arrays</em></p>
<p>Lastly, instead of passing the result to the console, wrap these data points within an array of two series that the chart can use directly and return them.</p>
<p>Add this code at the end of the <code>csvToSeries()</code> function:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">return</span> [
   {<span class="hljs-attr">name</span>: <span class="hljs-string">'Male'</span>, <span class="hljs-attr">points</span>: male},
   {<span class="hljs-attr">name</span>: <span class="hljs-string">'Female'</span>, <span class="hljs-attr">points</span>: female}
];
</code></pre>
<p>If the returned value was sent to the console, it would produce this result.  </p>
<p><img src="https://lh6.googleusercontent.com/_xlnsylk8kbv1u9-Fw4K0dnmJ7J_UBzhbhrWT8j48S4xtr04gYezHIITd_cNWQ5ZvJvi4MPdqi_IIat-JSfmRiOZT7jDzco5JYSstOzec67OxAQ-LCB7zuyqm20gxV8FYEm1XL0d" alt="Image" width="852" height="356" loading="lazy">
<em>Two series objects the chart can consume directly</em></p>
<p>As you can see, the logic for filtering rows is fairly simple and you can adjust it to get other details from this data set. </p>
<p>To learn more about handling CSV files using JSCharting utilities, see this <a target="_blank" href="https://jscharting.com/tutorials/js-chart-data/client-side/fetch-csv-and-json/">tutorial</a>. When you are ready for more advanced data handling, the <a target="_blank" href="https://jscharting.com/tutorials/js-chart-data/client-side/data-nesting/">JSC.nest() utility</a> can be used to create series and points from JSON data with with very little code. </p>
<p><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article/raw/master/zips/step3-b.zip"><strong>step3-b.zip</strong></a></p>
<h2 id="heading-step-4-putting-it-all-together">Step 4 - Putting it all together</h2>
<p>The data handling section was the most difficult step, but that alone will enable you to manipulate and extract data of interest from any CSV file. This is where it all comes together and where you will feel a sense of accomplishment. </p>
<p>Start by adding a <code>renderChart()</code> function to the end of the <code>index.js</code> file. You will pass the series data to this function as an argument. </p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">renderChart</span>(<span class="hljs-params">series</span>)</span>{
   JSC.Chart(<span class="hljs-string">'chartDiv'</span>, {
      <span class="hljs-attr">series</span>: series
   });
}
</code></pre>
<p>In the <code>then()</code> argument function that calls <code>csvToSeries()</code>, pass the series result to the <code>renderChart()</code> function to see what it draws in the browser.</p>
<pre><code class="lang-javascript">.then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">text</span>) </span>{
    <span class="hljs-keyword">let</span> series = csvToSeries(text);
    renderChart(series);
})
</code></pre>
<p><strong><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article/raw/master/zips/step4-a.zip">step4-a.zip</a></strong></p>
<p>Now, refresh the browser. You should see this chart that uses the CSV data you processed in the previous section. Sweet! ?</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/csv-line-chart.png" alt="Line chart showing filtered CSV data" width="600" height="400" loading="lazy">
<em>Line chart showing filtered CSV data</em></p>
<p>Whoa, what happened in 1918? Life expectancy dropped significantly there. According to Wikipedia there was a <a target="_blank" href="https://en.wikipedia.org/wiki/Spanish_flu">flu pandemic</a> involving H1N1 virus that wiped out a portion of the world population. This unfortunate event shows how visualizing data provides insights you would not normally get from just looking at the numbers.</p>
<p>You created a chart using the default line series type and it looks good, but you can make a few adjustments and tweaks to further improve it.</p>
<p>First, add a title at the top to explain what the viewer is looking at and an annotation at the bottom of the chart to credit the data source. Update the <code>JSC.Chart()</code> constructor function to pass the following options:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">renderChart</span>(<span class="hljs-params">series</span>)</span>{
    JSC.Chart(<span class="hljs-string">'chartDiv'</span>, {
        <span class="hljs-attr">title_label_text</span>: <span class="hljs-string">'Life Expectancy in the United States'</span>,
        <span class="hljs-attr">annotations</span>: [{
            <span class="hljs-attr">label_text</span>: <span class="hljs-string">'Source: National Center for Health Statistics'</span>,
            <span class="hljs-attr">position</span>: <span class="hljs-string">'bottom left'</span>
        }],
        <span class="hljs-attr">series</span>: series
    });
}
</code></pre>
<p>When you refresh the browser you can see the updated chart.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/line-chart-annotations.png" alt="Line chart with title and annotation for attribution" width="600" height="400" loading="lazy">
<em>Line chart with title and annotation for attribution</em></p>
<p>You added an annotation with label text, and a position setting. We can use another annotation for the title as well, but it was easier to use the title label in this example. </p>
<p>It is easy to control the annotation position using values such as <code>'top right'</code> or <code>'inside bottom right'</code>. The <code>'inside'</code> value means the annotation is placed inside the chart area where data is drawn. This <a target="_blank" href="https://jscharting.com/examples/chart-features/annotation/box-positions/">box positions chart example</a> demonstrates all the position setting options.</p>
<p>The legend shows the sum of point values for each series, but the sum is not important for this data set. You can reduce the legend columns to only show the icon and series name by using this setting:</p>
<pre><code class="lang-javascript">legend_template: <span class="hljs-string">'%icon,%name'</span>
</code></pre>
<p>But you don't really need to use a legend at all. It will be cleaner to simply label the lines themselves. You can disable the legend, and tell the chart to write the series name on the last point of each line series with these chart options:</p>
<pre><code class="lang-javascript">legend_visible: <span class="hljs-literal">false</span>,
<span class="hljs-attr">defaultSeries_lastPoint_label_text</span>: <span class="hljs-string">'&lt;b&gt;%seriesName&lt;/b&gt;'</span>,
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/csv-line-chart-labels.png" alt="Line chart using point labels instead of a legend" width="600" height="400" loading="lazy">
<em>Line chart using point labels instead of a legend</em></p>
<p>The <code>'%seriesname'</code> token is one of many <a target="_blank" href="https://jscharting.com/tutorials/js-chart-labels/token-reference/#point-tokens">point related tokens</a> that can be used in any point label text to show point details and calculations. </p>
<p>Finally, let’s enable the x axis crosshair combined tooltip to show the male and female life expectancy for any given year. On mobile devices, you can tap the chart to see the crosshair tooltip. When using a PC, tooltips display when hovering over the chart with your mouse pointer.</p>
<pre><code class="lang-javascript">xAxis_crosshair_enabled: <span class="hljs-literal">true</span>,
</code></pre>
<p>You may be wondering, what's with all those underscores in property names? This is not the actual property name. It's a shorthand way to write:</p>
<pre><code class="lang-javascript">xAxis: {<span class="hljs-attr">crosshair</span>: {<span class="hljs-attr">enabled</span>: <span class="hljs-literal">true</span>}},
</code></pre>
<p>You may find it more convenient to specify a setting with underscores and JSCharting will understand what you mean. </p>
<p>The default tooltip text is clear, but let's customize it slightly to make it our own. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-60.png" alt="Image" width="600" height="400" loading="lazy">
<em>Default combined tooltip</em></p>
<p>Since the crosshair tooltip shows information about each point it crosses, the tooltip text is defined within the point options. The <code>defaultPoint</code> property defines point options that all points will inherit automatically.</p>
<pre><code class="lang-javascript">defaultPoint_tooltip: <span class="hljs-string">'%seriesName &lt;b&gt;%yValue&lt;/b&gt; years'</span>,
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/image-61.png" alt="Image" width="600" height="400" loading="lazy">
<em>Customized combined tooltip</em></p>
<p>For more information about this feature, check out the <a target="_blank" href="https://jscharting.com/tutorials/js-chart-interactivity/crosshair-combined-tooltip/">crosshair and combined tooltip tutorial</a>.</p>
<p>When you apply all these options, your code will look similar to the following snippet. Replace the entire <code>renderChart()</code> function with this code.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">renderChart</span>(<span class="hljs-params">series</span>)</span>{
    JSC.Chart(<span class="hljs-string">'chartDiv'</span>, {
        <span class="hljs-attr">title_label_text</span>: <span class="hljs-string">'Life Expectancy in the United States'</span>,
        <span class="hljs-attr">annotations</span>: [{
            <span class="hljs-attr">label_text</span>: <span class="hljs-string">'Source: National Center for Health Statistics'</span>,
            <span class="hljs-attr">position</span>: <span class="hljs-string">'bottom left'</span>
        }],
        <span class="hljs-attr">legend_visible</span>: <span class="hljs-literal">false</span>,
        <span class="hljs-attr">defaultSeries_lastPoint_label_text</span>: <span class="hljs-string">'&lt;b&gt;%seriesName&lt;/b&gt;'</span>,
        <span class="hljs-attr">defaultPoint_tooltip</span>: <span class="hljs-string">'%seriesName &lt;b&gt;%yValue&lt;/b&gt; years'</span>,
        <span class="hljs-attr">xAxis_crosshair_enabled</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-attr">series</span>: series
    });
}
</code></pre>
<p>Refresh the browser window once more.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/csv-line-chart-tooltips.png" alt="Line chart with crosshairs and customized combined tooltips" width="600" height="400" loading="lazy">
<em>Line chart with crosshairs and customized combined tooltips</em></p>
<p>You did it! </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/congratulations.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Source: giphy.com</em></p>
<p>First you fetched CSV data using native JavaScript. You then converted it into JSON format and filtered the data into two series. With those series you created a beautiful interactive line chart using JSCharting and configured it to look professional. </p>
<p>You can customize and adjusted the charts further to meet your specific needs. Visit the JSCharting <a target="_blank" href="https://jscharting.com/tutorials/">tutorials</a> section to learn more about a specific topic, or find charts similar to what you want to make in the <a target="_blank" href="https://jscharting.com/examples/chart-types/">examples gallery</a> and copy them to continue your data visualization journey.</p>
<p>If you run into problems working with JSCharting, feel free to <a target="_blank" href="https://jscharting.com/support.htm">contact</a> the support team. They will be happy to guide you or help resolve any issues you may encounter.</p>
<p><strong><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article/raw/master/zips/step4-b.zip">step4-b.zip</a></strong></p>
<h2 id="heading-bonus-challenge">Bonus Challenge</h2>
<p>We did not use all the data available in in that CSV file. Let's experiment with it for fun and practice.</p>
<p>Create this chart using what you have learned.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/bonus-trend-line-chart.png" alt="Challenge: Replicate this chart on your own" width="600" height="400" loading="lazy">
<em>Challenge: Replicate this chart on your own</em></p>
<p>This zip file contains the answer:</p>
<p><strong><a target="_blank" href="https://github.com/arthurPuszynski/first-chart-article/raw/master/zips/step5-bonus.zip">step5-bonus.zip</a></strong></p>
<p>Can you think of other charts you can make with this data? Keep experimenting and enjoy every minute of it!</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>
