An Unnamed Connection !

All our life cerebral activities keep on forcing us to name every relation we encounter through our journey of life. Questions like who he or she is, what they belong to me, why don’t I get a clear…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Building a javascript library with zero dependencies

Building tools for the web today is greatly simplified thanks to technologies like npm and webpack that make it easy to drop in dependencies and bundle them together as a package. Need a url parser?..npm has just what you need. These tools have improved the velocity of development and enriched the open source ecosystem with a variety of libraries being published for various use cases.

Around a year ago I decided I wanted to build something from scratch. I had spent the past five years blindly adding dependencies to my packages and focusing purely on my own use cases. This lead to a lot of code bloat but also left a lasting dissatisfaction within me because what I was building wasn’t really something I could truly call mine. This coupled with my love for visualisations made me consider creating my own charting library.

Based on my past experience with building data visualization dashboards I had a few pain points that I wanted to address with bubblesjs. The developer API for charting libraries is unnecessarily complex. I rarely used moe than a handful of the provided configuration properties. I also spent a lot of time writing glue code to make one visualization react to changes in another. This is something I felt the library should take care of on its own. Finally, the size of the library should be small. Creating a library that is over an MB in size is a great way of ensuring it never gets used in fast loading pages.
The challenge was whether it was possible to do all of this.

Creating a basic line and bar charts was easy enough but I quickly ran into issues I had never imagined. The initial iterations of bubbles used fixed percentage space allocations for things like axis containers. This way the y axis container would always be allotted 10% of the total width. Things looked fine for a while until I tried visualizing a dataset that had abnormally long labels along the y axis.

Chart with fixed percentage space allocation

Notice how the y axis labels are clipped and overlap with the axis name. This made me realize that fixed space allocation would never work since there would always be datasets like this one. Also on mobile screens this effect would be more pronounced since there would be far lesser space available.
To alleviate this I went ahead with implemeneting a space allocation scheme that first precomputed the width of all y axis labels based on the font size of the text and then adjusted positioning of all chart elements appropriately.

Chart with dynamic space allocation

The next hard problem I faced was showing tooltips. Tooltips show the details about the point being hovered on in the chart. Based on the cursor position on the chart identifying the data to show is an interesting problem. Based on the hover coordinates we can estimate the target x axis value by applying an inverse scale. The data can then be searched to find the target value that most closely matches the supplied value. I leveraged binary search to find the largest value less than or equal to the target value.

Tooltip showing data closest to users hover position.

Somewhere in the midst of wrangling all the this complexity I forgot to keep track of the size of the library. Once I had ironed out most of the bugs and had a basic function version of bubblesjs I created a new build using rollup.
The minified library came out to be around 40KB.
I was momentarily shocked to see such a small number so I tried creating some visualizations with the built library and loaded the page with chrome devtools open. Chrome network requests inspector showed bubbles was only 10KB when loaded gzipped.

Add a comment

Related posts:

The Most Dreaded Message I Ever Heard from the Dead

True story about a medium's reading, afterlife contact, mediumship, shocking message heard from a dead teenager,

Top Claims Processing and Management Solution Companies

Insurance firms are taking all possible measures to utilize ground-breaking practices and the latest technology for improving their business processes and streamlining legacy applications. An…

Clean Swift Coding Style

Have you ever seen thousand lines of code on one view? If so, we are on the same page. At first, it was easy to find and easy to read. (repeat: at the beginning). However, it would be hard to add…