<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anil Karki on Medium]]></title>
        <description><![CDATA[Stories by Anil Karki on Medium]]></description>
        <link>https://medium.com/@anil.karki93?source=rss-636de9a1f2cd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*uj_BMR9ZcppN56fL</url>
            <title>Stories by Anil Karki on Medium</title>
            <link>https://medium.com/@anil.karki93?source=rss-636de9a1f2cd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 21:51:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anil.karki93/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Customizing react-chartjs-2 tooltip (Scrollable)]]></title>
            <link>https://medium.com/@anil.karki93/customizing-react-chartjs-2-tooltip-scrollable-20a2c1e37af9?source=rss-636de9a1f2cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/20a2c1e37af9</guid>
            <dc:creator><![CDATA[Anil Karki]]></dc:creator>
            <pubDate>Sun, 15 Jan 2023 17:01:12 GMT</pubDate>
            <atom:updated>2023-01-15T17:14:31.139Z</atom:updated>
            <content:encoded><![CDATA[<p>ReactChartJS is a powerful library for creating interactive and customizable charts and graphs. One of the features that ReactChartJS offers is the ability to create tooltips that are scrollable. In this article, we will show you how to create a scrollable tooltip in ReactChartJS using code examples.</p><figure><img alt="chartjs" src="https://cdn-images-1.medium.com/max/1024/1*XVQrIbPsqQ02mgSrkHXlGA.jpeg" /></figure><p>First, you will need to install and import the &quot;chart.js&quot;: &quot;^4.1.2&quot;, and &quot;react-chartjs-2&quot;: &quot;^5.2.0&quot;, library into your project. You can do this by adding the following command to your project terminal:</p><pre>npm install --save chart.js react-chartjs-2</pre><p>First, the code imports the necessary modules from Chart.js and ReactChartJS, such as the Scatter chart and the Tooltip. The data for the chart is then generated using the “generateData” function, which is not shown in the code provided. The chart options are set with the “options” variable, where the “plugins” option is set to enable the tooltip and make it scrollable.</p><pre>import {Scatter} from &#39;react-chartjs-2&#39;;<br>import {<br>  Chart as ChartJS,<br>  LinearScale,<br>  PointElement,<br>  LineElement,<br>  Tooltip,<br>  Legend,<br>} from &#39;chart.js&#39;;<br>ChartJS.register(LinearScale, PointElement, LineElement, Tooltip, Legend);<br>const data = {<br>  labels: [&#39;January&#39;, &#39;February&#39;, &#39;March&#39;, &#39;April&#39;, &#39;May&#39;, &#39;June&#39;, &#39;July&#39;],<br>  datasets: [{<br>    label: &#39;Scatter Dataset&#39;,<br>    data: generateData(100), // call the function to generate the data<br>    backgroundColor: &#39;rgba(255, 99, 132, 0.2)&#39;,<br>    borderColor: &#39;rgba(255, 99, 132, 1)&#39;,<br>    pointStyle: &#39;circle&#39;,<br>    pointRadius: 5,<br>  }]<br>};<br>const options = {<br>  plugins: {<br>    tooltip: {<br>      enabled: false,<br>      intersect: false,<br>      callbacks: {<br>        label: function (context) {<br>          let label = data.labels[context.datasetIndex];<br>          return (<br>            label +<br>            &quot;,  &quot; +<br>            context.parsed.x +<br>            &quot;,  &quot; +<br>            context.parsed.y<br>          );<br>        }<br>      },<br>      external: function(context) {<br>          var tooltipModel = context.tooltip;<br>          // Tooltip Element<br>          var tooltipEl = document.getElementById(&#39;chartjs-tooltip&#39;);<br>  <br>          // Create element on first render<br>          if (!tooltipEl) {<br>              tooltipEl = document.createElement(&#39;div&#39;);<br>              tooltipEl.id = &#39;chartjs-tooltip&#39;;<br>              tooltipEl.innerHTML = &#39;&lt;table&gt;&lt;/table&gt;&#39;;<br>              // tooltipEl.classList.add(&quot;scrollbar&quot;);<br>              document.body.appendChild(tooltipEl);<br>          }<br>  <br>          function getBody(bodyItem) {<br>              return bodyItem.lines;<br>          }<br>  <br>          // Set Text<br>          if (tooltipModel.body) {<br>              var titleLines = tooltipModel.title || [];<br>              var bodyLines = tooltipModel.body.map(getBody);<br>  <br>              var innerHtml = &#39;&lt;thead&gt;&#39;;<br>  <br>              titleLines.forEach(function(title) {<br>                  innerHtml += &#39;&lt;tr&gt;&lt;th&gt;&#39; + title + &#39;&lt;/th&gt;&lt;/tr&gt;&#39;;<br>              });<br>              innerHtml += &#39;&lt;/thead&gt;&lt;tbody &gt;&#39;;<br>  <br>              bodyLines.forEach(function(body, i) {<br>                  var colors = tooltipModel.labelColors[i];<br>                  var style = &#39;background:&#39; + colors.backgroundColor;<br>                  style += &#39;; border-color:&#39; + colors.borderColor;<br>                  style += &#39;; border-width: 2px !important&#39;;<br>                  style += &#39;; width: 10px !important&#39;;<br>                  style += &#39;; height: 10px !important&#39;;<br>                  style += &#39;; display: inline-block !important&#39;;<br>                  style += &#39;; margin-right: 3px !important&#39;;<br>                  var box = `&lt;span style=&quot;${style}&quot;&gt;&lt;/span&gt;`<br>                  innerHtml += `&lt;tr&gt;&lt;td&gt;${box}${body}&lt;/td&gt;&lt;/tr&gt;`;<br>  <br>              });<br>              innerHtml += &#39;&lt;/tbody&gt;&#39;;<br>  <br>              var tableRoot = tooltipEl.querySelector(&#39;table&#39;);<br>              tableRoot.innerHTML = innerHtml;<br>          }<br>  <br>          // `this` will be the overall tooltip<br>          var position = this.chart.canvas.getBoundingClientRect();<br>  <br>          // Display, position, and set styles for font<br>          tooltipEl.style.opacity = 1;<br>          tooltipEl.style.position = &#39;absolute&#39;;<br>          tooltipEl.style.left = position.left + window.pageXOffset + tooltipModel.caretX + &#39;px&#39;;<br>          tooltipEl.style.top = position.top + window.pageYOffset + tooltipModel.caretY + &#39;px&#39;;<br>          tooltipEl.style.fontFamily = tooltipModel._bodyFontFamily;<br>          tooltipEl.style.fontSize = tooltipModel.bodyFontSize + &#39;px&#39;;<br>          tooltipEl.style.fontStyle = tooltipModel._bodyFontStyle;<br>          tooltipEl.style.padding = tooltipModel.yPadding + &#39;px &#39; + tooltipModel.xPadding + &#39;px&#39;;<br>          // tooltipEl.style.pointerEvents = &#39;none&#39;; // Use when need to stop mouse events such as Onhover and Scrolling<br>          tooltipEl.style.borderColor = &#39;blue&#39;;<br>          tooltipEl.style.borderRadius = &#39;4px&#39;;<br>          tooltipEl.style.backgroundColor = &quot;white&quot;;<br>          tooltipEl.style.maxHeight = &quot;100px&quot;;<br>          tooltipEl.style.overflowY = &quot;auto&quot;;<br>          tooltipEl.style.scrollBehavior = &quot;smooth&quot;;   <br>     }<br>    },<br>  },<br>  scales: {<br>    x: {<br>      label: &#39;X-axis&#39;,<br>      min: 0,<br>      max: 20<br>    },<br>    y: {<br>      label: &#39;Y-axis&#39;,<br>      min: 0,<br>      max: 100<br>    }<br>  }<br>};<br>function generateData(n) {<br>  let data = [];<br>  for (let i = 0; i &lt; n; i++) {<br>    data.push({<br>      // x: Math.random() * 20,<br>      // y: Math.random() * 100<br>      x: 2,<br>      y: 70<br>    });<br>  }<br>  return data;<br>}<br>function Chart() {<br>  return (<br>    &lt;div&gt;<br>      &lt;h2&gt;Scatter Chart Example&lt;/h2&gt;<br>      &lt;Scatter data={data} options={options} /&gt;<br>    &lt;/div&gt;<br>  );<br>} <br><br><br>  export default Chart;</pre><p>The code then creates a custom callback function for the tooltip label, which displays the label, x, and y values of the data point. This function is then passed to the “callbacks” option within the “tooltip” option.</p><p>The code then creates an external function, which is used to create the tooltip element and add it to the DOM. This function creates a new “div” element and assigns it the id “chartjs-tooltip”. It then sets the innerHTML of the element to a table, which will be used to display the tooltip information.</p><p>The function then sets the text of the tooltip by looping through the title and body lines of the tooltip, and creating a table row for each. The function then sets the position, style, and font of the tooltip element and makes it visible by setting its opacity to 1.</p><p>Overall, this code provides a powerful and flexible way to create a scrollable tooltip in ReactChartJS. By following the code examples provided in this article, you can easily create a scrollable tooltip in ReactChartJS, making it much easier for users to view all of the data in a chart.</p><p>For a quick look at how to set up a scrollable tooltip in ReactChartJS, you can also check out the following link.</p><p><a href="https://github.com/Bleachanil/react-chart-js-custom-scrollable-tooltip">https://github.com/Bleachanil/react-chart-js-custom-scrollable-tooltip</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20a2c1e37af9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>