src/js/libs/jquery.sparkline.js
author hamidouk
Tue, 03 Jan 2012 12:25:57 +0100
branchpopcorn-port
changeset 567 ada550479aaf
parent 523 8dfeaec4724b
permissions -rw-r--r--
fixed search highlighting bug.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     1
/**
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     2
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     3
* jquery.sparkline.js
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     4
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     5
* v1.6
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     6
* (c) Splunk, Inc 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     7
* Contact: Gareth Watts (gareth@splunk.com)
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     8
* http://omnipotent.net/jquery.sparkline/
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     9
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    10
* Generates inline sparkline charts from data supplied either to the method
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    11
* or inline in HTML
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    12
* 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    13
* Compatible with Internet Explorer 6.0+ and modern browsers equipped with the canvas tag
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    14
* (Firefox 2.0+, Safari, Opera, etc)
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    15
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    16
* License: New BSD License
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    17
* 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    18
* Copyright (c) 2010, Splunk Inc.
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    19
* All rights reserved.
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    20
* 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    21
* Redistribution and use in source and binary forms, with or without modification, 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    22
* are permitted provided that the following conditions are met:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    23
* 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    24
*     * Redistributions of source code must retain the above copyright notice, 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    25
*       this list of conditions and the following disclaimer.
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    26
*     * Redistributions in binary form must reproduce the above copyright notice, 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    27
*       this list of conditions and the following disclaimer in the documentation 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    28
*       and/or other materials provided with the distribution.
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    29
*     * Neither the name of Splunk Inc nor the names of its contributors may 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    30
*       be used to endorse or promote products derived from this software without 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    31
*       specific prior written permission.
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    32
* 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    33
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    34
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    35
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    36
* SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    37
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    38
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    39
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    40
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    41
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    42
* 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    43
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    44
* Usage: 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    45
*  $(selector).sparkline(values, options)
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    46
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    47
* If values is undefined or set to 'html' then the data values are read from the specified tag:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    48
*   <p>Sparkline: <span class="sparkline">1,4,6,6,8,5,3,5</span></p>
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    49
*   $('.sparkline').sparkline();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    50
* There must be no spaces in the enclosed data set
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    51
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    52
* Otherwise values must be an array of numbers or null values
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    53
*    <p>Sparkline: <span id="sparkline1">This text replaced if the browser is compatible</span></p>
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    54
*    $('#sparkline1').sparkline([1,4,6,6,8,5,3,5])
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    55
*    $('#sparkline2').sparkline([1,4,6,null,null,5,3,5])
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    56
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    57
* Values can also be specified in an HTML comment, or as a values attribute:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    58
*    <p>Sparkline: <span class="sparkline"><!--1,4,6,6,8,5,3,5 --></span></p>
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    59
*    <p>Sparkline: <span class="sparkline" values="1,4,6,6,8,5,3,5"></span></p>
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    60
*    $('.sparkline').sparkline();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    61
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    62
* For line charts, x values can also be specified:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    63
*   <p>Sparkline: <span class="sparkline">1:1,2.7:4,3.4:6,5:6,6:8,8.7:5,9:3,10:5</span></p>
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    64
*    $('#sparkline1').sparkline([ [1,1], [2.7,4], [3.4,6], [5,6], [6,8], [8.7,5], [9,3], [10,5] ])
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    65
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    66
* By default, options should be passed in as teh second argument to the sparkline function:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    67
*   $('.sparkline').sparkline([1,2,3,4], {type: 'bar'})
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    68
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    69
* Options can also be set by passing them on the tag itself.  This feature is disabled by default though
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    70
* as there's a slight performance overhead:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    71
*   $('.sparkline').sparkline([1,2,3,4], {enableTagOptions: true})
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    72
*   <p>Sparkline: <span class="sparkline" sparkType="bar" sparkBarColor="red">loading</span></p>
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    73
* Prefix all options supplied as tag attribute with "spark" (configurable by setting tagOptionPrefix)
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    74
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    75
* Supported options:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    76
*   lineColor - Color of the line used for the chart
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    77
*   fillColor - Color used to fill in the chart - Set to '' or false for a transparent chart
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    78
*   width - Width of the chart - Defaults to 3 times the number of values in pixels
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    79
*   height - Height of the chart - Defaults to the height of the containing element
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    80
*   chartRangeMin - Specify the minimum value to use for the Y range of the chart - Defaults to the minimum value supplied
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    81
*   chartRangeMax - Specify the maximum value to use for the Y range of the chart - Defaults to the maximum value supplied
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    82
*   chartRangeClip - Clip out of range values to the max/min specified by chartRangeMin and chartRangeMax
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    83
*   chartRangeMinX - Specify the minimum value to use for the X range of the chart - Defaults to the minimum value supplied
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    84
*   chartRangeMaxX - Specify the maximum value to use for the X range of the chart - Defaults to the maximum value supplied
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    85
*   composite - If true then don't erase any existing chart attached to the tag, but draw
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    86
*           another chart over the top - Note that width and height are ignored if an
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    87
*           existing chart is detected.
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    88
*   tagValuesAttribute - Name of tag attribute to check for data values - Defaults to 'values'
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    89
*   enableTagOptions - Whether to check tags for sparkline options 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    90
*   tagOptionPrefix - Prefix used for options supplied as tag attributes - Defaults to 'spark'
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    91
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    92
* There are 7 types of sparkline, selected by supplying a "type" option of 'line' (default),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    93
* 'bar', 'tristate', 'bullet', 'discrete', 'pie' or 'box'
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    94
*    line - Line chart.  Options:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    95
*       spotColor - Set to '' to not end each line in a circular spot
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    96
*       minSpotColor - If set, color of spot at minimum value
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    97
*       maxSpotColor - If set, color of spot at maximum value
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    98
*       spotRadius - Radius in pixels
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    99
*       lineWidth - Width of line in pixels
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   100
*       normalRangeMin 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   101
*       normalRangeMax - If set draws a filled horizontal bar between these two values marking the "normal"
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   102
*                      or expected range of values
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   103
*       normalRangeColor - Color to use for the above bar
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   104
*       drawNormalOnTop - Draw the normal range above the chart fill color if true
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   105
*       defaultPixelsPerValue - Defaults to 3 pixels of width for each value in the chart
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   106
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   107
*   bar - Bar chart.  Options:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   108
*       barColor - Color of bars for postive values
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   109
*       negBarColor - Color of bars for negative values
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   110
*       zeroColor - Color of bars with zero values
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   111
*       nullColor - Color of bars with null values - Defaults to omitting the bar entirely
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   112
*       barWidth - Width of bars in pixels
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   113
*       colorMap - Optional mappnig of values to colors to override the *BarColor values above
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   114
*                  can be an Array of values to control the color of individual bars
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   115
*       barSpacing - Gap between bars in pixels
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   116
*       zeroAxis - Centers the y-axis around zero if true
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   117
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   118
*   tristate - Charts values of win (>0), lose (<0) or draw (=0)
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   119
*       posBarColor - Color of win values
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   120
*       negBarColor - Color of lose values
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   121
*       zeroBarColor - Color of draw values
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   122
*       barWidth - Width of bars in pixels
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   123
*       barSpacing - Gap between bars in pixels
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   124
*       colorMap - Optional mappnig of values to colors to override the *BarColor values above
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   125
*                  can be an Array of values to control the color of individual bars
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   126
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   127
*   discrete - Options:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   128
*       lineHeight - Height of each line in pixels - Defaults to 30% of the graph height
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   129
*       thesholdValue - Values less than this value will be drawn using thresholdColor instead of lineColor
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   130
*       thresholdColor
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   131
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   132
*   bullet - Values for bullet graphs msut be in the order: target, performance, range1, range2, range3, ...
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   133
*       options:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   134
*       targetColor - The color of the vertical target marker
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   135
*       targetWidth - The width of the target marker in pixels
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   136
*       performanceColor - The color of the performance measure horizontal bar
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   137
*       rangeColors - Colors to use for each qualitative range background color
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   138
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   139
*   pie - Pie chart. Options:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   140
*       sliceColors - An array of colors to use for pie slices
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   141
*       offset - Angle in degrees to offset the first slice - Try -90 or +90
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   142
*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   143
*   box - Box plot. Options:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   144
*       raw - Set to true to supply pre-computed plot points as values
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   145
*             values should be: low_outlier, low_whisker, q1, median, q3, high_whisker, high_outlier
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   146
*             When set to false you can supply any number of values and the box plot will
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   147
*             be computed for you.  Default is false.
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   148
*       showOutliers - Set to true (default) to display outliers as circles
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   149
*       outlierIRQ - Interquartile range used to determine outliers.  Default 1.5
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   150
*       boxLineColor - Outline color of the box
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   151
*       boxFillColor - Fill color for the box
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   152
*       whiskerColor - Line color used for whiskers
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   153
*       outlierLineColor - Outline color of outlier circles
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   154
*       outlierFillColor - Fill color of the outlier circles
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   155
*       spotRadius - Radius of outlier circles
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   156
*       medianColor - Line color of the median line
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   157
*       target - Draw a target cross hair at the supplied value (default undefined)
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   158
*      
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   159
*   
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   160
*       
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   161
*   Examples:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   162
*   $('#sparkline1').sparkline(myvalues, { lineColor: '#f00', fillColor: false });
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   163
*   $('.barsparks').sparkline('html', { type:'bar', height:'40px', barWidth:5 });
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   164
*   $('#tristate').sparkline([1,1,-1,1,0,0,-1], { type:'tristate' }):
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   165
*   $('#discrete').sparkline([1,3,4,5,5,3,4,5], { type:'discrete' });
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   166
*   $('#bullet').sparkline([10,12,12,9,7], { type:'bullet' });
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   167
*   $('#pie').sparkline([1,1,2], { type:'pie' });
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   168
*/
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   169
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   170
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   171
(function($) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   172
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   173
    /*
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   174
     * Default configuration settings
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   175
     */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   176
    var defaults = {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   177
        // Settings common to most/all chart types
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   178
        common: {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   179
            type : 'line',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   180
            lineColor : '#00f',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   181
            fillColor : '#cdf',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   182
            defaultPixelsPerValue : 3,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   183
            width : 'auto', 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   184
            height : 'auto',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   185
            composite : false,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   186
            tagValuesAttribute: 'values',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   187
            tagOptionsPrefix: 'spark',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   188
            enableTagOptions: false
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   189
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   190
        // Defaults for line charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   191
        line: {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   192
            spotColor : '#f80',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   193
            spotRadius : 1.5,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   194
            minSpotColor : '#f80',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   195
            maxSpotColor : '#f80',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   196
            lineWidth: 1, 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   197
            normalRangeMin : undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   198
            normalRangeMax : undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   199
            normalRangeColor : '#ccc',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   200
            drawNormalOnTop: false,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   201
            chartRangeMin : undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   202
            chartRangeMax : undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   203
            chartRangeMinX : undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   204
            chartRangeMaxX : undefined
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   205
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   206
        // Defaults for bar charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   207
        bar: {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   208
            barColor : '#00f',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   209
            negBarColor : '#f44',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   210
            zeroColor: undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   211
            nullColor: undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   212
            zeroAxis : undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   213
            barWidth : 4,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   214
            barSpacing : 1,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   215
            chartRangeMax: undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   216
            chartRangeMin: undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   217
            chartRangeClip: false,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   218
            colorMap : undefined
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   219
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   220
        // Defaults for tristate charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   221
        tristate: {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   222
            barWidth : 4,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   223
            barSpacing : 1,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   224
            posBarColor: '#6f6',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   225
            negBarColor : '#f44',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   226
            zeroBarColor : '#999',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   227
            colorMap : {}
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   228
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   229
        // Defaults for discrete charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   230
        discrete: {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   231
            lineHeight: 'auto',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   232
            thresholdColor: undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   233
            thresholdValue : 0,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   234
            chartRangeMax: undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   235
            chartRangeMin: undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   236
            chartRangeClip: false
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   237
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   238
        // Defaults for bullet charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   239
        bullet: {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   240
            targetColor : 'red',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   241
            targetWidth : 3, // width of the target bar in pixels
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   242
            performanceColor : 'blue',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   243
            rangeColors : ['#D3DAFE', '#A8B6FF', '#7F94FF' ],
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   244
            base : undefined // set this to a number to change the base start number
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   245
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   246
        // Defaults for pie charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   247
        pie: {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   248
            sliceColors : ['#f00', '#0f0', '#00f']
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   249
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   250
        // Defaults for box plots
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   251
        box: {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   252
            raw: false,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   253
            boxLineColor: 'black',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   254
            boxFillColor: '#cdf',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   255
            whiskerColor: 'black',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   256
            outlierLineColor: '#333',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   257
            outlierFillColor: 'white',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   258
            medianColor: 'red',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   259
            showOutliers: true,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   260
            outlierIQR: 1.5,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   261
            spotRadius: 1.5,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   262
            target: undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   263
            targetColor: '#4a2',
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   264
            chartRangeMax: undefined,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   265
            chartRangeMin: undefined
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   266
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   267
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   268
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   269
    // Provide a cross-browser interface to a few simple drawing primitives
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   270
    var VCanvas_base, VCanvas_canvas, VCanvas_vml;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   271
    $.fn.simpledraw = function(width, height, use_existing) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   272
        if (use_existing && this[0].VCanvas) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   273
            return this[0].VCanvas;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   274
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   275
        if (width === undefined) { 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   276
            width=$(this).innerWidth();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   277
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   278
        if (height === undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   279
            height=$(this).innerHeight();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   280
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   281
        if ($.browser.hasCanvas) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   282
            return new VCanvas_canvas(width, height, this);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   283
        } else if ($.browser.msie) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   284
            return new VCanvas_vml(width, height, this);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   285
        } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   286
            return false;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   287
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   288
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   289
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   290
    var pending = [];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   291
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   292
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   293
    $.fn.sparkline = function(uservalues, userOptions) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   294
        return this.each(function() {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   295
            var options = new $.fn.sparkline.options(this, userOptions);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   296
            var render = function() {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   297
                var values, width, height;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   298
                if (uservalues==='html' || uservalues===undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   299
                    var vals = this.getAttribute(options.get('tagValuesAttribute'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   300
                    if (vals===undefined || vals===null) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   301
                        vals = $(this).html();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   302
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   303
                    values = vals.replace(/(^\s*<!--)|(-->\s*$)|\s+/g, '').split(',');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   304
                } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   305
                    values = uservalues;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   306
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   307
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   308
                width = options.get('width')=='auto' ? values.length*options.get('defaultPixelsPerValue') : options.get('width');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   309
                if (options.get('height') == 'auto') {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   310
                    if (!options.get('composite') || !this.VCanvas) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   311
                        // must be a better way to get the line height
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   312
                        var tmp = document.createElement('span');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   313
                        tmp.innerHTML = 'a';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   314
                        $(this).html(tmp);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   315
                        height = $(tmp).innerHeight();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   316
                        $(tmp).remove();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   317
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   318
                } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   319
                    height = options.get('height');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   320
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   321
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   322
                $.fn.sparkline[options.get('type')].call(this, values, options, width, height);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   323
            };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   324
            // jQuery 1.3.0 completely changed the meaning of :hidden :-/
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   325
            if (($(this).html() && $(this).is(':hidden')) || ($.fn.jquery < "1.3.0" && $(this).parents().is(':hidden')) || !$(this).parents('body').length) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   326
                pending.push([this, render]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   327
            } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   328
                render.call(this);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   329
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   330
        });
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   331
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   332
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   333
    $.fn.sparkline.defaults = defaults;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   334
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   335
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   336
    $.sparkline_display_visible = function() {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   337
        for (var i=pending.length-1; i>=0; i--) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   338
            var el = pending[i][0];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   339
            if ($(el).is(':visible') && !$(el).parents().is(':hidden')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   340
                pending[i][1].call(el);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   341
                pending.splice(i, 1);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   342
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   343
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   344
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   345
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   346
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   347
    /**
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   348
     * User option handler
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   349
     */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   350
    var UNSET_OPTION = {};
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   351
    var normalizeValue = function(val) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   352
        switch(val) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   353
            case 'undefined':
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   354
                val = undefined;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   355
                break;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   356
            case 'null':
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   357
                val = null;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   358
                break;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   359
            case 'true':
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   360
                val = true;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   361
                break;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   362
            case 'false':
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   363
                val = false;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   364
                break;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   365
            default:
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   366
                var nf = parseFloat(val);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   367
                if (val == nf) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   368
                    val = nf;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   369
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   370
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   371
        return val;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   372
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   373
    $.fn.sparkline.options = function(tag, userOptions) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   374
        var extendedOptions;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   375
        this.userOptions = userOptions = userOptions || {};
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   376
        this.tag = tag;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   377
        this.tagValCache = {};
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   378
        var defaults = $.fn.sparkline.defaults;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   379
        var base = defaults.common;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   380
        this.tagOptionsPrefix = userOptions.enableTagOptions && (userOptions.tagOptionsPrefix || base.tagOptionsPrefix);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   381
            
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   382
        var tagOptionType = this.getTagSetting('type');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   383
        if (tagOptionType === UNSET_OPTION) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   384
            extendedOptions = defaults[userOptions.type || base.type];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   385
        } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   386
            extendedOptions = defaults[tagOptionType];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   387
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   388
        this.mergedOptions = $.extend({}, base, extendedOptions, userOptions);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   389
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   390
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   391
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   392
    $.fn.sparkline.options.prototype.getTagSetting = function(key) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   393
        var val, i, prefix = this.tagOptionsPrefix;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   394
        if (prefix === false || prefix === undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   395
            return UNSET_OPTION;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   396
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   397
        if (this.tagValCache.hasOwnProperty(key)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   398
            val = this.tagValCache.key;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   399
        } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   400
            val = this.tag.getAttribute(prefix + key);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   401
            if (val === undefined || val === null) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   402
                val = UNSET_OPTION;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   403
            } else if (val.substr(0, 1) == '[') {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   404
                val = val.substr(1, val.length-2).split(',');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   405
                for(i=val.length; i--;) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   406
                    val[i] = normalizeValue(val[i].replace(/(^\s*)|(\s*$)/g, ''));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   407
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   408
            } else if (val.substr(0, 1) == '{') {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   409
                var pairs= val.substr(1, val.length-2).split(',');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   410
                val = {};
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   411
                for(i=pairs.length; i--;) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   412
                    var keyval = pairs[i].split(':', 2);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   413
                    val[keyval[0].replace(/(^\s*)|(\s*$)/g, '')] = normalizeValue(keyval[1].replace(/(^\s*)|(\s*$)/g, ''));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   414
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   415
            } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   416
                val = normalizeValue(val);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   417
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   418
            this.tagValCache.key = val;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   419
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   420
        return val; 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   421
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   422
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   423
    $.fn.sparkline.options.prototype.get = function(key) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   424
        var tagOption = this.getTagSetting(key);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   425
        if (tagOption !== UNSET_OPTION) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   426
            return tagOption;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   427
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   428
        return this.mergedOptions[key];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   429
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   430
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   431
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   432
    /**
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   433
     * Line charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   434
     */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   435
    $.fn.sparkline.line = function(values, options, width, height) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   436
        var xvalues = [], yvalues = [], yminmax = [];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   437
        for (var i=0; i<values.length; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   438
            var val = values[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   439
            var isstr = typeof(values[i])=='string';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   440
            var isarray = typeof(values[i])=='object' && values[i] instanceof Array;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   441
            var sp = isstr && values[i].split(':');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   442
            if (isstr && sp.length == 2) { // x:y
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   443
                xvalues.push(Number(sp[0]));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   444
                yvalues.push(Number(sp[1]));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   445
                yminmax.push(Number(sp[1]));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   446
            } else if (isarray) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   447
                xvalues.push(val[0]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   448
                yvalues.push(val[1]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   449
                yminmax.push(val[1]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   450
            } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   451
                xvalues.push(i);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   452
                if (values[i]===null || values[i]=='null') {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   453
                    yvalues.push(null);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   454
                } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   455
                    yvalues.push(Number(val));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   456
                    yminmax.push(Number(val));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   457
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   458
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   459
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   460
        if (options.get('xvalues')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   461
            xvalues = options.get('xvalues');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   462
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   463
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   464
        var maxy = Math.max.apply(Math, yminmax);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   465
        var maxyval = maxy;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   466
        var miny = Math.min.apply(Math, yminmax);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   467
        var minyval = miny;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   468
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   469
        var maxx = Math.max.apply(Math, xvalues);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   470
        var minx = Math.min.apply(Math, xvalues);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   471
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   472
        var normalRangeMin = options.get('normalRangeMin');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   473
        var normalRangeMax = options.get('normalRangeMax');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   474
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   475
        if (normalRangeMin!==undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   476
            if (normalRangeMin<miny) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   477
                miny = normalRangeMin;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   478
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   479
            if (normalRangeMax>maxy) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   480
                maxy = normalRangeMax;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   481
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   482
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   483
        if (options.get('chartRangeMin')!==undefined && (options.get('chartRangeClip') ||  options.get('chartRangeMin')<miny)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   484
            miny = options.get('chartRangeMin');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   485
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   486
        if (options.get('chartRangeMax')!==undefined && (options.get('chartRangeClip') || options.get('chartRangeMax')>maxy)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   487
            maxy = options.get('chartRangeMax');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   488
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   489
        if (options.get('chartRangeMinX')!==undefined && (options.get('chartRangeClipX') || options.get('chartRangeMinX')<minx)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   490
            minx = options.get('chartRangeMinX');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   491
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   492
        if (options.get('chartRangeMaxX')!==undefined && (options.get('chartRangeClipX') || options.get('chartRangeMaxX')>maxx)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   493
            maxx = options.get('chartRangeMaxX');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   494
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   495
        var rangex = maxx-minx === 0 ? 1 : maxx-minx;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   496
        var rangey = maxy-miny === 0 ? 1 : maxy-miny;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   497
        var vl = yvalues.length-1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   498
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   499
        if (vl<1) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   500
            this.innerHTML = '';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   501
            return;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   502
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   503
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   504
        var target = $(this).simpledraw(width, height, options.get('composite'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   505
        if (target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   506
            var canvas_width = target.pixel_width;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   507
            var canvas_height = target.pixel_height;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   508
            var canvas_top = 0;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   509
            var canvas_left = 0;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   510
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   511
            var spotRadius = options.get('spotRadius');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   512
            if (spotRadius && (canvas_width < (spotRadius*4) || canvas_height < (spotRadius*4))) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   513
                spotRadius = 0;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   514
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   515
            if (spotRadius) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   516
                // adjust the canvas size as required so that spots will fit
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   517
                if (options.get('minSpotColor') || (options.get('spotColor') && yvalues[vl]==miny)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   518
                    canvas_height -= Math.ceil(spotRadius);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   519
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   520
                if (options.get('maxSpotColor') || (options.get('spotColor') && yvalues[vl]==maxy)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   521
                    canvas_height -= Math.ceil(spotRadius);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   522
                    canvas_top += Math.ceil(spotRadius);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   523
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   524
                if (options.get('minSpotColor') || options.get('maxSpotColor') && (yvalues[0]==miny || yvalues[0]==maxy)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   525
                    canvas_left += Math.ceil(spotRadius);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   526
                    canvas_width -= Math.ceil(spotRadius);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   527
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   528
                if (options.get('spotColor') || (options.get('minSpotColor') || options.get('maxSpotColor') && (yvalues[vl]==miny||yvalues[vl]==maxy))) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   529
                    canvas_width -= Math.ceil(spotRadius);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   530
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   531
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   532
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   533
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   534
            canvas_height--;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   535
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   536
            var drawNormalRange = function() {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   537
                if (normalRangeMin!==undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   538
                    var ytop = canvas_top+Math.round(canvas_height-(canvas_height*((normalRangeMax-miny)/rangey)));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   539
                    var height = Math.round((canvas_height*(normalRangeMax-normalRangeMin))/rangey);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   540
                    target.drawRect(canvas_left, ytop, canvas_width, height, undefined, options.get('normalRangeColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   541
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   542
            };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   543
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   544
            if (!options.get('drawNormalOnTop')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   545
                drawNormalRange();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   546
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   547
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   548
            var path = [];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   549
            var paths = [path];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   550
            var x, y, vlen=yvalues.length;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   551
            for(i=0; i<vlen; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   552
                x=xvalues[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   553
                y=yvalues[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   554
                if (y===null) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   555
                    if (i) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   556
                        if (yvalues[i-1]!==null) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   557
                            path = [];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   558
                            paths.push(path);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   559
                        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   560
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   561
                } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   562
                    if (y < miny) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   563
                        y=miny;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   564
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   565
                    if (y > maxy) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   566
                        y=maxy;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   567
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   568
                    if (!path.length) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   569
                        // previous value was null
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   570
                        path.push([canvas_left+Math.round((x-minx)*(canvas_width/rangex)), canvas_top+canvas_height]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   571
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   572
                    path.push([canvas_left+Math.round((x-minx)*(canvas_width/rangex)), canvas_top+Math.round(canvas_height-(canvas_height*((y-miny)/rangey)))]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   573
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   574
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   575
            var lineshapes = [];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   576
            var fillshapes = [];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   577
            var plen=paths.length;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   578
            for(i=0; i<plen; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   579
                path = paths[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   580
                if (!path.length) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   581
                    continue; // last value was null
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   582
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   583
                if (options.get('fillColor')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   584
                    path.push([path[path.length-1][0], canvas_top+canvas_height-1]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   585
                    fillshapes.push(path.slice(0));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   586
                    path.pop();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   587
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   588
                // if there's only a single point in this path, then we want to display it as a vertical line
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   589
                // which means we keep path[0]  as is
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   590
                if (path.length>2) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   591
                    // else we want the first value 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   592
                    path[0] = [ path[0][0], path[1][1] ];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   593
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   594
                lineshapes.push(path);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   595
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   596
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   597
            // draw the fill first, then optionally the normal range, then the line on top of that
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   598
            plen = fillshapes.length;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   599
            for(i=0; i<plen; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   600
                target.drawShape(fillshapes[i], undefined, options.get('fillColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   601
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   602
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   603
            if (options.get('drawNormalOnTop')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   604
                drawNormalRange();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   605
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   606
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   607
            plen = lineshapes.length;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   608
            for(i=0; i<plen; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   609
                target.drawShape(lineshapes[i], options.get('lineColor'), undefined, options.get('lineWidth'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   610
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   611
                
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   612
            if (spotRadius && options.get('spotColor')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   613
                target.drawCircle(canvas_left+Math.round(xvalues[xvalues.length-1]*(canvas_width/rangex)),  canvas_top+Math.round(canvas_height-(canvas_height*((yvalues[vl]-miny)/rangey))), spotRadius, undefined, options.get('spotColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   614
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   615
            if (maxy!=minyval) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   616
                if (spotRadius && options.get('minSpotColor')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   617
                    x = xvalues[$.inArray(minyval, yvalues)];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   618
                    target.drawCircle(canvas_left+Math.round((x-minx)*(canvas_width/rangex)),  canvas_top+Math.round(canvas_height-(canvas_height*((minyval-miny)/rangey))), spotRadius, undefined, options.get('minSpotColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   619
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   620
                if (spotRadius && options.get('maxSpotColor')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   621
                    x = xvalues[$.inArray(maxyval, yvalues)];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   622
                    target.drawCircle(canvas_left+Math.round((x-minx)*(canvas_width/rangex)),  canvas_top+Math.round(canvas_height-(canvas_height*((maxyval-miny)/rangey))), spotRadius, undefined, options.get('maxSpotColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   623
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   624
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   625
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   626
        } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   627
            // Remove the tag contents if sparklines aren't supported
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   628
            this.innerHTML = '';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   629
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   630
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   631
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   632
    
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   633
    /** 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   634
     * Bar charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   635
     */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   636
    $.fn.sparkline.bar = function(values, options, width, height) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   637
        width = (values.length * options.get('barWidth')) + ((values.length-1) * options.get('barSpacing'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   638
        var num_values = [];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   639
        for(var i=0, vlen=values.length; i<vlen; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   640
            if (values[i]=='null' || values[i]===null) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   641
                values[i] = null;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   642
            } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   643
                values[i] = Number(values[i]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   644
                num_values.push(Number(values[i]));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   645
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   646
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   647
        var max = Math.max.apply(Math, num_values),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   648
            min = Math.min.apply(Math, num_values);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   649
        if (options.get('chartRangeMin')!==undefined && (options.get('chartRangeClip') || options.get('chartRangeMin')<min)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   650
            min = options.get('chartRangeMin');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   651
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   652
        if (options.get('chartRangeMax')!==undefined && (options.get('chartRangeClip') || options.get('chartRangeMax')>max)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   653
            max = options.get('chartRangeMax');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   654
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   655
        var zeroAxis = options.get('zeroAxis');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   656
        if (zeroAxis === undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   657
            zeroAxis = min<0;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   658
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   659
        var range = max-min === 0 ? 1 : max-min;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   660
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   661
        var colorMapByIndex, colorMapByValue;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   662
        if ($.isArray(options.get('colorMap'))) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   663
            colorMapByIndex = options.get('colorMap');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   664
            colorMapByValue = null;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   665
        } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   666
            colorMapByIndex = null;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   667
            colorMapByValue = options.get('colorMap');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   668
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   669
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   670
        var target = $(this).simpledraw(width, height, options.get('composite'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   671
        if (target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   672
            var color,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   673
                canvas_height = target.pixel_height,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   674
                yzero = min<0 && zeroAxis ? canvas_height-Math.round(canvas_height * (Math.abs(min)/range))-1 : canvas_height-1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   675
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   676
            for(i=values.length; i--;) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   677
                var x = i*(options.get('barWidth')+options.get('barSpacing')),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   678
                    y, 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   679
                    val = values[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   680
                if (val===null) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   681
                    if (options.get('nullColor')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   682
                        color = options.get('nullColor');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   683
                        val = (zeroAxis && min<0) ? 0 : min;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   684
                        height = 1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   685
                        y = (zeroAxis && min<0) ? yzero : canvas_height - height;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   686
                    } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   687
                        continue;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   688
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   689
                } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   690
                    if (val < min) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   691
                        val=min;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   692
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   693
                    if (val > max) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   694
                        val=max;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   695
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   696
                    color = (val < 0) ? options.get('negBarColor') : options.get('barColor');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   697
                    if (zeroAxis && min<0) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   698
                        height = Math.round(canvas_height*((Math.abs(val)/range)))+1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   699
                        y = (val < 0) ? yzero : yzero-height;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   700
                    } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   701
                        height = Math.round(canvas_height*((val-min)/range))+1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   702
                        y = canvas_height-height;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   703
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   704
                    if (val===0 && options.get('zeroColor')!==undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   705
                        color = options.get('zeroColor');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   706
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   707
                    if (colorMapByValue && colorMapByValue[val]) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   708
                        color = colorMapByValue[val];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   709
                    } else if (colorMapByIndex && colorMapByIndex.length>i) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   710
                        color = colorMapByIndex[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   711
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   712
                    if (color===null) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   713
                        continue;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   714
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   715
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   716
                target.drawRect(x, y, options.get('barWidth')-1, height-1, color, color);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   717
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   718
        } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   719
            // Remove the tag contents if sparklines aren't supported
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   720
            this.innerHTML = '';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   721
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   722
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   723
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   724
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   725
    /**
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   726
     * Tristate charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   727
     */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   728
    $.fn.sparkline.tristate = function(values, options, width, height) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   729
        values = $.map(values, Number);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   730
        width = (values.length * options.get('barWidth')) + ((values.length-1) * options.get('barSpacing'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   731
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   732
        var colorMapByIndex, colorMapByValue;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   733
        if ($.isArray(options.get('colorMap'))) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   734
            colorMapByIndex = options.get('colorMap');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   735
            colorMapByValue = null;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   736
        } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   737
            colorMapByIndex = null;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   738
            colorMapByValue = options.get('colorMap');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   739
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   740
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   741
        var target = $(this).simpledraw(width, height, options.get('composite'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   742
        if (target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   743
            var canvas_height = target.pixel_height,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   744
                half_height = Math.round(canvas_height/2);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   745
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   746
            for(var i=values.length; i--;) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   747
                var x = i*(options.get('barWidth')+options.get('barSpacing')),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   748
                    y, color;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   749
                if (values[i] < 0) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   750
                    y = half_height;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   751
                    height = half_height-1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   752
                    color = options.get('negBarColor');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   753
                } else if (values[i] > 0) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   754
                    y = 0;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   755
                    height = half_height-1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   756
                    color = options.get('posBarColor');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   757
                } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   758
                    y = half_height-1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   759
                    height = 2;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   760
                    color = options.get('zeroBarColor');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   761
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   762
                if (colorMapByValue && colorMapByValue[values[i]]) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   763
                    color = colorMapByValue[values[i]];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   764
                } else if (colorMapByIndex && colorMapByIndex.length>i) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   765
                    color = colorMapByIndex[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   766
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   767
                if (color===null) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   768
                    continue;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   769
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   770
                target.drawRect(x, y, options.get('barWidth')-1, height-1, color, color);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   771
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   772
        } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   773
            // Remove the tag contents if sparklines aren't supported
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   774
            this.innerHTML = '';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   775
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   776
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   777
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   778
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   779
    /** 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   780
     * Discrete charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   781
     */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   782
    $.fn.sparkline.discrete = function(values, options, width, height) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   783
        values = $.map(values, Number);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   784
        width = options.get('width')=='auto' ? values.length*2 : width;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   785
        var interval = Math.floor(width / values.length);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   786
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   787
        var target = $(this).simpledraw(width, height, options.get('composite'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   788
        if (target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   789
            var canvas_height = target.pixel_height,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   790
                line_height = options.get('lineHeight') == 'auto' ? Math.round(canvas_height * 0.3) : options.get('lineHeight'),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   791
                pheight = canvas_height - line_height,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   792
                min = Math.min.apply(Math, values),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   793
                max = Math.max.apply(Math, values);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   794
            if (options.get('chartRangeMin')!==undefined && (options.get('chartRangeClip') || options.get('chartRangeMin')<min)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   795
                min = options.get('chartRangeMin');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   796
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   797
            if (options.get('chartRangeMax')!==undefined && (options.get('chartRangeClip')  || options.get('chartRangeMax')>max)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   798
                max = options.get('chartRangeMax');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   799
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   800
            var range = max-min;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   801
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   802
            for(var i=values.length; i--;) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   803
                var val = values[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   804
                if (val < min) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   805
                    val=min;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   806
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   807
                if (val > max) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   808
                    val=max;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   809
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   810
                var x = (i*interval),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   811
                    ytop = Math.round(pheight-pheight*((val-min)/range));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   812
                target.drawLine(x, ytop, x, ytop+line_height, (options.get('thresholdColor') && val < options.get('thresholdValue')) ? options.get('thresholdColor') : options.get('lineColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   813
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   814
        }  else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   815
            // Remove the tag contents if sparklines aren't supported
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   816
            this.innerHTML = '';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   817
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   818
                
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   819
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   820
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   821
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   822
    /**
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   823
     * Bullet charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   824
     */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   825
    $.fn.sparkline.bullet = function(values, options, width, height) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   826
        values = $.map(values, Number);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   827
        // target, performance, range1, range2, range3
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   828
        
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   829
        width = options.get('width')=='auto' ? '4.0em' : width;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   830
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   831
        var target = $(this).simpledraw(width, height, options.get('composite'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   832
        if (target && values.length>1) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   833
            var canvas_width = target.pixel_width-Math.ceil(options.get('targetWidth')/2),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   834
                canvas_height = target.pixel_height,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   835
                min = Math.min.apply(Math, values),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   836
                max = Math.max.apply(Math, values);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   837
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   838
            if (options.get('base') === undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   839
                min = min < 0 ? min : 0;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   840
            } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   841
                min = options.get('base');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   842
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   843
            var range = max-min;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   844
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   845
            // draw range values
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   846
            for(var i=2, vlen=values.length; i<vlen; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   847
                var rangeval = values[i],
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   848
                    rangewidth = Math.round(canvas_width*((rangeval-min)/range));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   849
                target.drawRect(0, 0, rangewidth-1, canvas_height-1, options.get('rangeColors')[i-2], options.get('rangeColors')[i-2]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   850
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   851
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   852
            // draw the performance bar
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   853
            var perfval = values[1],
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   854
                perfwidth = Math.round(canvas_width*((perfval-min)/range));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   855
            target.drawRect(0, Math.round(canvas_height*0.3), perfwidth-1, Math.round(canvas_height*0.4)-1, options.get('performanceColor'), options.get('performanceColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   856
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   857
            // draw the target line
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   858
            var targetval = values[0],
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   859
                x = Math.round(canvas_width*((targetval-min)/range)-(options.get('targetWidth')/2)),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   860
                targettop = Math.round(canvas_height*0.10),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   861
                targetheight = canvas_height-(targettop*2);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   862
            target.drawRect(x, targettop, options.get('targetWidth')-1, targetheight-1, options.get('targetColor'), options.get('targetColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   863
        }  else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   864
            // Remove the tag contents if sparklines aren't supported
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   865
            this.innerHTML = '';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   866
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   867
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   868
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   869
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   870
    /**
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   871
     * Pie charts
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   872
     */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   873
    $.fn.sparkline.pie = function(values, options, width, height) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   874
        values = $.map(values, Number);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   875
        width = options.get('width')=='auto' ? height : width;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   876
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   877
        var target = $(this).simpledraw(width, height, options.get('composite'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   878
        if (target && values.length>1) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   879
            var canvas_width = target.pixel_width,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   880
                canvas_height = target.pixel_height,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   881
                radius = Math.floor(Math.min(canvas_width, canvas_height)/2),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   882
                total = 0,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   883
                next = 0,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   884
                circle = 2*Math.PI;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   885
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   886
            for(var i=values.length; i--;) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   887
                total += values[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   888
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   889
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   890
            if (options.get('offset')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   891
                next += (2*Math.PI)*(options.get('offset')/360);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   892
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   893
            var vlen = values.length;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   894
            for(i=0; i<vlen; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   895
                var start = next;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   896
                var end = next;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   897
                if (total > 0) {  // avoid divide by zero
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   898
                    end = next + (circle*(values[i]/total));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   899
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   900
                target.drawPieSlice(radius, radius, radius, start, end, undefined, options.get('sliceColors')[i % options.get('sliceColors').length]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   901
                next = end;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   902
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   903
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   904
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   905
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   906
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   907
    /**
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   908
     * Box plots
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   909
     */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   910
    var quartile = function(values, q) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   911
        if (q==2) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   912
            var vl2 = Math.floor(values.length/2);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   913
            return values.length % 2 ? values[vl2] : (values[vl2]+values[vl2+1])/2;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   914
        } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   915
            var vl4 = Math.floor(values.length/4);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   916
            return values.length % 2 ? (values[vl4*q]+values[vl4*q+1])/2 : values[vl4*q];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   917
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   918
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   919
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   920
    $.fn.sparkline.box = function(values, options, width, height) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   921
        values = $.map(values, Number);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   922
        width = options.get('width')=='auto' ? '4.0em' : width;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   923
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   924
        var minvalue = options.get('chartRangeMin')===undefined ? Math.min.apply(Math, values) : options.get('chartRangeMin'),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   925
            maxvalue = options.get('chartRangeMax')===undefined ? Math.max.apply(Math, values) : options.get('chartRangeMax'),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   926
            target = $(this).simpledraw(width, height, options.get('composite')),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   927
            vlen = values.length,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   928
            lwhisker, loutlier, q1, q2, q3, rwhisker, routlier;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   929
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   930
        if (target && values.length>1) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   931
            var canvas_width = target.pixel_width,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   932
                canvas_height = target.pixel_height;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   933
            if (options.get('raw')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   934
                if (options.get('showOutliers') && values.length>5) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   935
                    loutlier=values[0]; lwhisker=values[1]; q1=values[2]; q2=values[3]; q3=values[4]; rwhisker=values[5]; routlier=values[6];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   936
                } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   937
                    lwhisker=values[0]; q1=values[1]; q2=values[2]; q3=values[3]; rwhisker=values[4];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   938
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   939
            } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   940
                values.sort(function(a, b) { return a-b; });
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   941
                q1 = quartile(values, 1);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   942
                q2 = quartile(values, 2);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   943
                q3 = quartile(values, 3);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   944
                var iqr = q3-q1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   945
                if (options.get('showOutliers')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   946
                    lwhisker=undefined; rwhisker=undefined;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   947
                    for(var i=0; i<vlen; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   948
                        if (lwhisker===undefined && values[i] > q1-(iqr*options.get('outlierIQR'))) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   949
                            lwhisker = values[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   950
                        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   951
                        if (values[i] < q3+(iqr*options.get('outlierIQR'))) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   952
                            rwhisker = values[i];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   953
                        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   954
                    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   955
                    loutlier = values[0];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   956
                    routlier = values[vlen-1];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   957
                } else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   958
                    lwhisker = values[0];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   959
                    rwhisker = values[vlen-1];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   960
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   961
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   962
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   963
            var unitsize = canvas_width / (maxvalue-minvalue+1),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   964
                canvas_left = 0;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   965
            if (options.get('showOutliers')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   966
                canvas_left = Math.ceil(options.get('spotRadius'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   967
                canvas_width -= 2*Math.ceil(options.get('spotRadius'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   968
                unitsize = canvas_width / (maxvalue-minvalue+1);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   969
                if (loutlier < lwhisker) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   970
                    target.drawCircle((loutlier-minvalue)*unitsize+canvas_left, canvas_height/2, options.get('spotRadius'), options.get('outlierLineColor'), options.get('outlierFillColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   971
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   972
                if (routlier > rwhisker) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   973
                    target.drawCircle((routlier-minvalue)*unitsize+canvas_left, canvas_height/2, options.get('spotRadius'), options.get('outlierLineColor'), options.get('outlierFillColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   974
                }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   975
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   976
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   977
            // box
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   978
            target.drawRect(
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   979
                Math.round((q1-minvalue)*unitsize+canvas_left),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   980
                Math.round(canvas_height*0.1),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   981
                Math.round((q3-q1)*unitsize), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   982
                Math.round(canvas_height*0.8), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   983
                options.get('boxLineColor'), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   984
                options.get('boxFillColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   985
            // left whisker
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   986
            target.drawLine(
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   987
                Math.round((lwhisker-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   988
                Math.round(canvas_height/2), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   989
                Math.round((q1-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   990
                Math.round(canvas_height/2), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   991
                options.get('lineColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   992
            target.drawLine(
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   993
                Math.round((lwhisker-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   994
                Math.round(canvas_height/4), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   995
                Math.round((lwhisker-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   996
                Math.round(canvas_height-canvas_height/4), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   997
                options.get('whiskerColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   998
            // right whisker
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
   999
            target.drawLine(Math.round((rwhisker-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1000
                Math.round(canvas_height/2), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1001
                Math.round((q3-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1002
                Math.round(canvas_height/2), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1003
                options.get('lineColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1004
            target.drawLine(
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1005
                Math.round((rwhisker-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1006
                Math.round(canvas_height/4), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1007
                Math.round((rwhisker-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1008
                Math.round(canvas_height-canvas_height/4), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1009
                options.get('whiskerColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1010
            // median line
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1011
            target.drawLine(
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1012
                Math.round((q2-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1013
                Math.round(canvas_height*0.1),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1014
                Math.round((q2-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1015
                Math.round(canvas_height*0.9),
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1016
                options.get('medianColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1017
            if (options.get('target')) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1018
                var size = Math.ceil(options.get('spotRadius'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1019
                target.drawLine(
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1020
                    Math.round((options.get('target')-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1021
                    Math.round((canvas_height/2)-size), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1022
                    Math.round((options.get('target')-minvalue)*unitsize+canvas_left), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1023
                    Math.round((canvas_height/2)+size), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1024
                    options.get('targetColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1025
                target.drawLine(
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1026
                    Math.round((options.get('target')-minvalue)*unitsize+canvas_left-size), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1027
                    Math.round(canvas_height/2), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1028
                    Math.round((options.get('target')-minvalue)*unitsize+canvas_left+size), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1029
                    Math.round(canvas_height/2), 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1030
                    options.get('targetColor'));
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1031
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1032
        }  else {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1033
            // Remove the tag contents if sparklines aren't supported
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1034
            this.innerHTML = '';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1035
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1036
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1037
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1038
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1039
    // Setup a very simple "virtual canvas" to make drawing the few shapes we need easier
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1040
    // This is accessible as $(foo).simpledraw()
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1041
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1042
    if ($.browser.msie && !document.namespaces.v) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1043
        document.namespaces.add('v', 'urn:schemas-microsoft-com:vml', '#default#VML');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1044
    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1045
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1046
    if ($.browser.hasCanvas === undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1047
        var t = document.createElement('canvas');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1048
        $.browser.hasCanvas = t.getContext!==undefined;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1049
    }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1050
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1051
    VCanvas_base = function(width, height, target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1052
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1053
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1054
    VCanvas_base.prototype = {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1055
        init : function(width, height, target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1056
            this.width = width;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1057
            this.height = height;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1058
            this.target = target;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1059
            if (target[0]) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1060
                target=target[0];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1061
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1062
            target.VCanvas = this;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1063
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1064
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1065
        drawShape : function(path, lineColor, fillColor, lineWidth) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1066
            alert('drawShape not implemented');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1067
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1068
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1069
        drawLine : function(x1, y1, x2, y2, lineColor, lineWidth) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1070
            return this.drawShape([ [x1,y1], [x2,y2] ], lineColor, lineWidth);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1071
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1072
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1073
        drawCircle : function(x, y, radius, lineColor, fillColor) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1074
            alert('drawCircle not implemented');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1075
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1076
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1077
        drawPieSlice : function(x, y, radius, startAngle, endAngle, lineColor, fillColor) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1078
            alert('drawPieSlice not implemented');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1079
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1080
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1081
        drawRect : function(x, y, width, height, lineColor, fillColor) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1082
            alert('drawRect not implemented');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1083
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1084
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1085
        getElement : function() {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1086
            return this.canvas;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1087
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1088
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1089
        _insert : function(el, target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1090
            $(target).html(el);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1091
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1092
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1093
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1094
    VCanvas_canvas = function(width, height, target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1095
        return this.init(width, height, target);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1096
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1097
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1098
    VCanvas_canvas.prototype = $.extend(new VCanvas_base(), {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1099
        _super : VCanvas_base.prototype,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1100
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1101
        init : function(width, height, target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1102
            this._super.init(width, height, target);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1103
            this.canvas = document.createElement('canvas');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1104
            if (target[0]) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1105
                target=target[0];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1106
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1107
            target.VCanvas = this;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1108
            $(this.canvas).css({ display:'inline-block', width:width, height:height, verticalAlign:'top' });
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1109
            this._insert(this.canvas, target);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1110
            this.pixel_height = $(this.canvas).height();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1111
            this.pixel_width = $(this.canvas).width();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1112
            this.canvas.width = this.pixel_width;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1113
            this.canvas.height = this.pixel_height;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1114
            $(this.canvas).css({width: this.pixel_width, height: this.pixel_height});
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1115
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1116
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1117
        _getContext : function(lineColor, fillColor, lineWidth) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1118
            var context = this.canvas.getContext('2d');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1119
            if (lineColor !== undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1120
                context.strokeStyle = lineColor;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1121
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1122
            context.lineWidth = lineWidth===undefined ? 1 : lineWidth;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1123
            if (fillColor !== undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1124
                context.fillStyle = fillColor;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1125
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1126
            return context;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1127
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1128
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1129
        drawShape : function(path, lineColor, fillColor, lineWidth) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1130
            var context = this._getContext(lineColor, fillColor, lineWidth);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1131
            context.beginPath();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1132
            context.moveTo(path[0][0]+0.5, path[0][1]+0.5);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1133
            for(var i=1, plen=path.length; i<plen; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1134
                context.lineTo(path[i][0]+0.5, path[i][1]+0.5); // the 0.5 offset gives us crisp pixel-width lines
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1135
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1136
            if (lineColor !== undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1137
                context.stroke();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1138
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1139
            if (fillColor !== undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1140
                context.fill();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1141
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1142
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1143
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1144
        drawCircle : function(x, y, radius, lineColor, fillColor) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1145
            var context = this._getContext(lineColor, fillColor);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1146
            context.beginPath();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1147
            context.arc(x, y, radius, 0, 2*Math.PI, false);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1148
            if (lineColor !== undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1149
                context.stroke();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1150
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1151
            if (fillColor !== undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1152
                context.fill();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1153
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1154
        }, 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1155
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1156
        drawPieSlice : function(x, y, radius, startAngle, endAngle, lineColor, fillColor) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1157
            var context = this._getContext(lineColor, fillColor);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1158
            context.beginPath();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1159
            context.moveTo(x, y);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1160
            context.arc(x, y, radius, startAngle, endAngle, false);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1161
            context.lineTo(x, y);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1162
            context.closePath();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1163
            if (lineColor !== undefined) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1164
                context.stroke();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1165
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1166
            if (fillColor) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1167
                context.fill();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1168
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1169
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1170
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1171
        drawRect : function(x, y, width, height, lineColor, fillColor) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1172
            return this.drawShape([ [x,y], [x+width, y], [x+width, y+height], [x, y+height], [x, y] ], lineColor, fillColor);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1173
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1174
        
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1175
    });
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1176
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1177
    VCanvas_vml = function(width, height, target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1178
        return this.init(width, height, target);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1179
    };
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1180
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1181
    VCanvas_vml.prototype = $.extend(new VCanvas_base(), {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1182
        _super : VCanvas_base.prototype,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1183
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1184
        init : function(width, height, target) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1185
            this._super.init(width, height, target);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1186
            if (target[0]) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1187
                target=target[0];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1188
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1189
            target.VCanvas = this;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1190
            this.canvas = document.createElement('span');
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1191
            $(this.canvas).css({ display:'inline-block', position: 'relative', overflow:'hidden', width:width, height:height, margin:'0px', padding:'0px', verticalAlign: 'top'});
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1192
            this._insert(this.canvas, target);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1193
            this.pixel_height = $(this.canvas).height();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1194
            this.pixel_width = $(this.canvas).width();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1195
            this.canvas.width = this.pixel_width;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1196
            this.canvas.height = this.pixel_height;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1197
            var groupel = '<v:group coordorigin="0 0" coordsize="'+this.pixel_width+' '+this.pixel_height+'"' +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1198
                    ' style="position:absolute;top:0;left:0;width:'+this.pixel_width+'px;height='+this.pixel_height+'px;"></v:group>';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1199
            this.canvas.insertAdjacentHTML('beforeEnd', groupel);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1200
            this.group = $(this.canvas).children()[0];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1201
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1202
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1203
        drawShape : function(path, lineColor, fillColor, lineWidth) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1204
            var vpath = [];
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1205
            for(var i=0, plen=path.length; i<plen; i++) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1206
                vpath[i] = ''+(path[i][0])+','+(path[i][1]);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1207
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1208
            var initial = vpath.splice(0,1);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1209
            lineWidth = lineWidth === undefined ? 1 : lineWidth;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1210
            var stroke = lineColor === undefined ? ' stroked="false" ' : ' strokeWeight="'+lineWidth+'" strokeColor="'+lineColor+'" ';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1211
            var fill = fillColor === undefined ? ' filled="false"' : ' fillColor="'+fillColor+'" filled="true" ';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1212
            var closed = vpath[0] == vpath[vpath.length-1] ? 'x ' : '';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1213
            var vel = '<v:shape coordorigin="0 0" coordsize="'+this.pixel_width+' '+this.pixel_height+'" ' +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1214
                 stroke +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1215
                 fill +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1216
                ' style="position:absolute;left:0px;top:0px;height:'+this.pixel_height+'px;width:'+this.pixel_width+'px;padding:0px;margin:0px;" ' +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1217
                ' path="m '+initial+' l '+vpath.join(', ')+' '+closed+'e">' +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1218
                ' </v:shape>';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1219
             this.group.insertAdjacentHTML('beforeEnd', vel);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1220
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1221
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1222
        drawCircle : function(x, y, radius, lineColor, fillColor) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1223
            x -= radius+1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1224
            y -= radius+1;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1225
            var stroke = lineColor === undefined ? ' stroked="false" ' : ' strokeWeight="1" strokeColor="'+lineColor+'" ';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1226
            var fill = fillColor === undefined ? ' filled="false"' : ' fillColor="'+fillColor+'" filled="true" ';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1227
            var vel = '<v:oval ' +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1228
                stroke +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1229
                fill +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1230
                ' style="position:absolute;top:'+y+'px; left:'+x+'px; width:'+(radius*2)+'px; height:'+(radius*2)+'px"></v:oval>';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1231
            this.group.insertAdjacentHTML('beforeEnd', vel);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1232
            
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1233
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1234
        
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1235
        drawPieSlice : function(x, y, radius, startAngle, endAngle, lineColor, fillColor) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1236
            if (startAngle == endAngle) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1237
                return;  // VML seems to have problem when start angle equals end angle.
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1238
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1239
            if ((endAngle - startAngle) == (2*Math.PI)) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1240
                startAngle = 0.0;  // VML seems to have a problem when drawing a full circle that doesn't start 0
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1241
                endAngle = (2*Math.PI);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1242
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1243
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1244
            var startx = x + Math.round(Math.cos(startAngle) * radius);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1245
            var starty = y + Math.round(Math.sin(startAngle) * radius);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1246
            var endx = x + Math.round(Math.cos(endAngle) * radius);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1247
            var endy = y + Math.round(Math.sin(endAngle) * radius);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1248
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1249
            // Prevent very small slices from being mistaken as a whole pie
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1250
            if (startx==endx && starty==endy && (endAngle-startAngle) < Math.PI) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1251
                return;
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1252
            }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1253
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1254
            var vpath = [  x-radius, y-radius, x+radius, y+radius, startx, starty, endx, endy ]; 
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1255
            var stroke = lineColor === undefined ? ' stroked="false" ' : ' strokeWeight="1" strokeColor="'+lineColor+'" ';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1256
            var fill = fillColor === undefined ? ' filled="false"' : ' fillColor="'+fillColor+'" filled="true" ';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1257
            var vel = '<v:shape coordorigin="0 0" coordsize="'+this.pixel_width+' '+this.pixel_height+'" ' +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1258
                 stroke +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1259
                 fill +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1260
                ' style="position:absolute;left:0px;top:0px;height:'+this.pixel_height+'px;width:'+this.pixel_width+'px;padding:0px;margin:0px;" ' +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1261
                ' path="m '+x+','+y+' wa '+vpath.join(', ')+' x e">' +
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1262
                ' </v:shape>';
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1263
             this.group.insertAdjacentHTML('beforeEnd', vel);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1264
        },
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1265
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1266
        drawRect : function(x, y, width, height, lineColor, fillColor) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1267
            return this.drawShape( [ [x, y], [x, y+height], [x+width, y+height], [x+width, y], [x, y] ], lineColor, fillColor);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1268
        }
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1269
    });
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1270
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
  1271
})(jQuery);