March 9, 2021
Hot Topics:

Charting Amazon Sales Ranks with the Grafico

  • By Jason Gilmore
  • Send Email »
  • More Articles »

Writing a book is a long and difficult process -- one which is so laborious that most authors wonder where in the world they will find the energy to actually promote it after the writing is over! But these days it's more important than ever not only to promote new books actively, but also to gauge the success of promotional activities. Never before has the old saying "If you are not measuring it, you are not managing it" rung more true.

For many authors, one useful measuring stick is a book's Amazon sales rank (see Figure 1). A sales rank is associated with every book sold on Amazon.com; the lower the sales rank, the more popular (in terms of unit sales) the book at that given point in time. I specify "given point in time" because sales ranks are volatile, often changing daily and even hourly in accordance with sales volume fluctuations. While Amazon doesn't publish specifics regarding any information regarding the correlation between sales ranks and unit sales, publishers and authors alike have put a great deal of work into deciphering the algorithm. Many analyses are available online; however, one of my favorites is published here.

Chart Amazon Sales Ranks with the Grafico Library
Click here for larger image

Figure 1. Amazon.com Sales Rank Associated with Every Book

Being a lazy programmer, I've never been interested in navigating to my books' various Amazon pages on a daily basis to examine the ranks. Further, charting the rank fluctuations over a long period of time can be useful for determining the efficacy of book reviews, advertising and other promotional activities. In this article, I'll focus on how I implemented the latter feature, showing you how I used the Grafico charting library to produce visual representations of sales ranks.

Incidentally, before jumping into the Grafico introduction, I just wanted to explain quickly how I'm automating the retrieval of sales ranks. I use the Amazon-ECS-PHP-Library in conjunction with Cron to execute a script every six hours (12am, 6am, 12pm, 6pm). The line in my crontab file looks like this:

 0 0,6,12,18 * * * wget -O /dev/null

The sales ranks are saved to a simple MySQL table containing each book's ASIN, sales rank, and a timestamp.

Introducing Grafico

Grafico is a JavaScript-based charting library based on Raphaël and Prototype.js, providing the ability to create a wide variety of charts, including bar, dot, line, pie, sparklines and more. Supporting all mainstream browsers, all Grafico requires you to do is identify the type of chart you'd like to create, pass along the chart values and other options, and Grafico will do the rest.

To install Grafico, head over to the Grafico website and download the grafico.base.js file, in addition to the files representing the type of chart you'd like to create. For instance if you want to create a line chart, download the grafico.line.js script. Once downloaded, add them to your HTML file like this (notice how I have also referenced Prototype.js via Google's CDN and Raphaël):


  <title>Book Sales Ranks</title>

  <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript">
    google.load("prototype", "1.7");
  <script src="raphael-min.js" type="text/javascript" charset="utf-8"></script>
  <script src="grafico.base.js" type="text/javascript"></script>
  <script src="grafico.line.js" type="text/javascript"></script>

  <div id="chart" style="width:500px;height:300px"></div>


The DIV identified by chart defines the location where the graph will be rendered. I'll show you exactly how in the next example.

Creating a Graph

Creating a simple graph with Grafico is incredibly easy, requiring you only to identify the type of graph you'd like to create, the DIV identifying the graph location and the graph values. The following example creates the graph found in Figure 2.

<script type="text/javascript" charset="utf-8">
window.onload = function () {
  var linegraph = new Grafico.LineGraph($('chart'), { ranks: [32136, 24865, 19498] });

Chart Amazon Sales Ranks with the Grafico Library
Click here for larger image

Figure 2.Creating a Simple Graph with Grafico

As you can see in Figure 2, Grafico will, by default, associate each chart value with its array index offset. Presuming the chart values were inserted into the array in accordance with the day in which they were recorded, this default feature conveniently serves nicely as a chronological representation of recorded sales ranks. But there is much more we can do to improve this simple graph, starting with adding labels and adjusting the colors.

Originally published on https://www.developer.com.

Page 1 of 2

This article was originally published on June 27, 2011

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date