November 26, 2014
Hot Topics:

Charting Amazon Sales Ranks with the Grafico

  • June 27, 2011
  • 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 http://127.0.0.1/developer/2011/amazonpas/update.php -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):

<html>
<head>

  <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>
  <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>

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

</html>

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] });
};
</script>


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.


Tags: javascript code, Graph API

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

Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

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

Sitemap | Contact Us

Rocket Fuel