API.txt 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. Flot Reference
  2. --------------
  3. Consider a call to the plot function:
  4. var plot = $.plot(placeholder, data, options)
  5. The placeholder is a jQuery object or DOM element or jQuery expression
  6. that the plot will be put into. This placeholder needs to have its
  7. width and height set as explained in the README (go read that now if
  8. you haven't, it's short). The plot will modify some properties of the
  9. placeholder so it's recommended you simply pass in a div that you
  10. don't use for anything else. Make sure you check any fancy styling
  11. you apply to the div, e.g. background images have been reported to be a
  12. problem on IE 7.
  13. The format of the data is documented below, as is the available
  14. options. The plot object returned from the call has some methods you
  15. can call. These are documented separately below.
  16. Note that in general Flot gives no guarantees if you change any of the
  17. objects you pass in to the plot function or get out of it since
  18. they're not necessarily deep-copied.
  19. Data Format
  20. -----------
  21. The data is an array of data series:
  22. [ series1, series2, ... ]
  23. A series can either be raw data or an object with properties. The raw
  24. data format is an array of points:
  25. [ [x1, y1], [x2, y2], ... ]
  26. E.g.
  27. [ [1, 3], [2, 14.01], [3.5, 3.14] ]
  28. Note that to simplify the internal logic in Flot both the x and y
  29. values must be numbers (even if specifying time series, see below for
  30. how to do this). This is a common problem because you might retrieve
  31. data from the database and serialize them directly to JSON without
  32. noticing the wrong type. If you're getting mysterious errors, double
  33. check that you're inputting numbers and not strings.
  34. If a null is specified as a point or if one of the coordinates is null
  35. or couldn't be converted to a number, the point is ignored when
  36. drawing. As a special case, a null value for lines is interpreted as a
  37. line segment end, i.e. the points before and after the null value are
  38. not connected.
  39. Lines and points take two coordinates. For filled lines and bars, you
  40. can specify a third coordinate which is the bottom of the filled
  41. area/bar (defaults to 0).
  42. The format of a single series object is as follows:
  43. {
  44. color: color or number
  45. data: rawdata
  46. label: string
  47. lines: specific lines options
  48. bars: specific bars options
  49. points: specific points options
  50. xaxis: number
  51. yaxis: number
  52. clickable: boolean
  53. hoverable: boolean
  54. shadowSize: number
  55. }
  56. You don't have to specify any of them except the data, the rest are
  57. options that will get default values. Typically you'd only specify
  58. label and data, like this:
  59. {
  60. label: "y = 3",
  61. data: [[0, 3], [10, 3]]
  62. }
  63. The label is used for the legend, if you don't specify one, the series
  64. will not show up in the legend.
  65. If you don't specify color, the series will get a color from the
  66. auto-generated colors. The color is either a CSS color specification
  67. (like "rgb(255, 100, 123)") or an integer that specifies which of
  68. auto-generated colors to select, e.g. 0 will get color no. 0, etc.
  69. The latter is mostly useful if you let the user add and remove series,
  70. in which case you can hard-code the color index to prevent the colors
  71. from jumping around between the series.
  72. The "xaxis" and "yaxis" options specify which axis to use. The axes
  73. are numbered from 1 (default), so { yaxis: 2} means that the series
  74. should be plotted against the second y axis.
  75. "clickable" and "hoverable" can be set to false to disable
  76. interactivity for specific series if interactivity is turned on in
  77. the plot, see below.
  78. The rest of the options are all documented below as they are the same
  79. as the default options passed in via the options parameter in the plot
  80. commmand. When you specify them for a specific data series, they will
  81. override the default options for the plot for that data series.
  82. Here's a complete example of a simple data specification:
  83. [ { label: "Foo", data: [ [10, 1], [17, -14], [30, 5] ] },
  84. { label: "Bar", data: [ [11, 13], [19, 11], [30, -7] ] } ]
  85. Plot Options
  86. ------------
  87. All options are completely optional. They are documented individually
  88. below, to change them you just specify them in an object, e.g.
  89. var options = {
  90. series: {
  91. lines: { show: true },
  92. points: { show: true }
  93. }
  94. };
  95. $.plot(placeholder, data, options);
  96. Customizing the legend
  97. ======================
  98. legend: {
  99. show: boolean
  100. labelFormatter: null or (fn: string, series object -> string)
  101. labelBoxBorderColor: color
  102. noColumns: number
  103. position: "ne" or "nw" or "se" or "sw"
  104. margin: number of pixels or [x margin, y margin]
  105. backgroundColor: null or color
  106. backgroundOpacity: number between 0 and 1
  107. container: null or jQuery object/DOM element/jQuery expression
  108. }
  109. The legend is generated as a table with the data series labels and
  110. small label boxes with the color of the series. If you want to format
  111. the labels in some way, e.g. make them to links, you can pass in a
  112. function for "labelFormatter". Here's an example that makes them
  113. clickable:
  114. labelFormatter: function(label, series) {
  115. // series is the series object for the label
  116. return '<a href="#' + label + '">' + label + '</a>';
  117. }
  118. "noColumns" is the number of columns to divide the legend table into.
  119. "position" specifies the overall placement of the legend within the
  120. plot (top-right, top-left, etc.) and margin the distance to the plot
  121. edge (this can be either a number or an array of two numbers like [x,
  122. y]). "backgroundColor" and "backgroundOpacity" specifies the
  123. background. The default is a partly transparent auto-detected
  124. background.
  125. If you want the legend to appear somewhere else in the DOM, you can
  126. specify "container" as a jQuery object/expression to put the legend
  127. table into. The "position" and "margin" etc. options will then be
  128. ignored. Note that Flot will overwrite the contents of the container.
  129. Customizing the axes
  130. ====================
  131. xaxis, yaxis: {
  132. show: null or true/false
  133. position: "bottom" or "top" or "left" or "right"
  134. mode: null or "time"
  135. color: null or color spec
  136. tickColor: null or color spec
  137. min: null or number
  138. max: null or number
  139. autoscaleMargin: null or number
  140. transform: null or fn: number -> number
  141. inverseTransform: null or fn: number -> number
  142. ticks: null or number or ticks array or (fn: range -> ticks array)
  143. tickSize: number or array
  144. minTickSize: number or array
  145. tickFormatter: (fn: number, object -> string) or string
  146. tickDecimals: null or number
  147. labelWidth: null or number
  148. labelHeight: null or number
  149. reserveSpace: null or true
  150. tickLength: null or number
  151. alignTicksWithAxis: null or number
  152. }
  153. All axes have the same kind of options. The following describes how to
  154. configure one axis, see below for what to do if you've got more than
  155. one x axis or y axis.
  156. If you don't set the "show" option (i.e. it is null), visibility is
  157. auto-detected, i.e. the axis will show up if there's data associated
  158. with it. You can override this by setting the "show" option to true or
  159. false.
  160. The "position" option specifies where the axis is placed, bottom or
  161. top for x axes, left or right for y axes. The "mode" option determines
  162. how the data is interpreted, the default of null means as decimal
  163. numbers. Use "time" for time series data, see the time series data
  164. section.
  165. The "color" option determines the color of the labels and ticks for
  166. the axis (default is the grid color). For more fine-grained control
  167. you can also set the color of the ticks separately with "tickColor"
  168. (otherwise it's autogenerated as the base color with some
  169. transparency).
  170. The options "min"/"max" are the precise minimum/maximum value on the
  171. scale. If you don't specify either of them, a value will automatically
  172. be chosen based on the minimum/maximum data values. Note that Flot
  173. always examines all the data values you feed to it, even if a
  174. restriction on another axis may make some of them invisible (this
  175. makes interactive use more stable).
  176. The "autoscaleMargin" is a bit esoteric: it's the fraction of margin
  177. that the scaling algorithm will add to avoid that the outermost points
  178. ends up on the grid border. Note that this margin is only applied when
  179. a min or max value is not explicitly set. If a margin is specified,
  180. the plot will furthermore extend the axis end-point to the nearest
  181. whole tick. The default value is "null" for the x axes and 0.02 for y
  182. axes which seems appropriate for most cases.
  183. "transform" and "inverseTransform" are callbacks you can put in to
  184. change the way the data is drawn. You can design a function to
  185. compress or expand certain parts of the axis non-linearly, e.g.
  186. suppress weekends or compress far away points with a logarithm or some
  187. other means. When Flot draws the plot, each value is first put through
  188. the transform function. Here's an example, the x axis can be turned
  189. into a natural logarithm axis with the following code:
  190. xaxis: {
  191. transform: function (v) { return Math.log(v); },
  192. inverseTransform: function (v) { return Math.exp(v); }
  193. }
  194. Similarly, for reversing the y axis so the values appear in inverse
  195. order:
  196. yaxis: {
  197. transform: function (v) { return -v; },
  198. inverseTransform: function (v) { return -v; }
  199. }
  200. Note that for finding extrema, Flot assumes that the transform
  201. function does not reorder values (it should be monotone).
  202. The inverseTransform is simply the inverse of the transform function
  203. (so v == inverseTransform(transform(v)) for all relevant v). It is
  204. required for converting from canvas coordinates to data coordinates,
  205. e.g. for a mouse interaction where a certain pixel is clicked. If you
  206. don't use any interactive features of Flot, you may not need it.
  207. The rest of the options deal with the ticks.
  208. If you don't specify any ticks, a tick generator algorithm will make
  209. some for you. The algorithm has two passes. It first estimates how
  210. many ticks would be reasonable and uses this number to compute a nice
  211. round tick interval size. Then it generates the ticks.
  212. You can specify how many ticks the algorithm aims for by setting
  213. "ticks" to a number. The algorithm always tries to generate reasonably
  214. round tick values so even if you ask for three ticks, you might get
  215. five if that fits better with the rounding. If you don't want any
  216. ticks at all, set "ticks" to 0 or an empty array.
  217. Another option is to skip the rounding part and directly set the tick
  218. interval size with "tickSize". If you set it to 2, you'll get ticks at
  219. 2, 4, 6, etc. Alternatively, you can specify that you just don't want
  220. ticks at a size less than a specific tick size with "minTickSize".
  221. Note that for time series, the format is an array like [2, "month"],
  222. see the next section.
  223. If you want to completely override the tick algorithm, you can specify
  224. an array for "ticks", either like this:
  225. ticks: [0, 1.2, 2.4]
  226. Or like this where the labels are also customized:
  227. ticks: [[0, "zero"], [1.2, "one mark"], [2.4, "two marks"]]
  228. You can mix the two if you like.
  229. For extra flexibility you can specify a function as the "ticks"
  230. parameter. The function will be called with an object with the axis
  231. min and max and should return a ticks array. Here's a simplistic tick
  232. generator that spits out intervals of pi, suitable for use on the x
  233. axis for trigonometric functions:
  234. function piTickGenerator(axis) {
  235. var res = [], i = Math.floor(axis.min / Math.PI);
  236. do {
  237. var v = i * Math.PI;
  238. res.push([v, i + "\u03c0"]);
  239. ++i;
  240. } while (v < axis.max);
  241. return res;
  242. }
  243. You can control how the ticks look like with "tickDecimals", the
  244. number of decimals to display (default is auto-detected).
  245. Alternatively, for ultimate control over how ticks are formatted you can
  246. provide a function to "tickFormatter". The function is passed two
  247. parameters, the tick value and an axis object with information, and
  248. should return a string. The default formatter looks like this:
  249. function formatter(val, axis) {
  250. return val.toFixed(axis.tickDecimals);
  251. }
  252. The axis object has "min" and "max" with the range of the axis,
  253. "tickDecimals" with the number of decimals to round the value to and
  254. "tickSize" with the size of the interval between ticks as calculated
  255. by the automatic axis scaling algorithm (or specified by you). Here's
  256. an example of a custom formatter:
  257. function suffixFormatter(val, axis) {
  258. if (val > 1000000)
  259. return (val / 1000000).toFixed(axis.tickDecimals) + " MB";
  260. else if (val > 1000)
  261. return (val / 1000).toFixed(axis.tickDecimals) + " kB";
  262. else
  263. return val.toFixed(axis.tickDecimals) + " B";
  264. }
  265. "labelWidth" and "labelHeight" specifies a fixed size of the tick
  266. labels in pixels. They're useful in case you need to align several
  267. plots. "reserveSpace" means that even if an axis isn't shown, Flot
  268. should reserve space for it - it is useful in combination with
  269. labelWidth and labelHeight for aligning multi-axis charts.
  270. "tickLength" is the length of the tick lines in pixels. By default, the
  271. innermost axes will have ticks that extend all across the plot, while
  272. any extra axes use small ticks. A value of null means use the default,
  273. while a number means small ticks of that length - set it to 0 to hide
  274. the lines completely.
  275. If you set "alignTicksWithAxis" to the number of another axis, e.g.
  276. alignTicksWithAxis: 1, Flot will ensure that the autogenerated ticks
  277. of this axis are aligned with the ticks of the other axis. This may
  278. improve the looks, e.g. if you have one y axis to the left and one to
  279. the right, because the grid lines will then match the ticks in both
  280. ends. The trade-off is that the forced ticks won't necessarily be at
  281. natural places.
  282. Multiple axes
  283. =============
  284. If you need more than one x axis or y axis, you need to specify for
  285. each data series which axis they are to use, as described under the
  286. format of the data series, e.g. { data: [...], yaxis: 2 } specifies
  287. that a series should be plotted against the second y axis.
  288. To actually configure that axis, you can't use the xaxis/yaxis options
  289. directly - instead there are two arrays in the options:
  290. xaxes: []
  291. yaxes: []
  292. Here's an example of configuring a single x axis and two y axes (we
  293. can leave options of the first y axis empty as the defaults are fine):
  294. {
  295. xaxes: [ { position: "top" } ],
  296. yaxes: [ { }, { position: "right", min: 20 } ]
  297. }
  298. The arrays get their default values from the xaxis/yaxis settings, so
  299. say you want to have all y axes start at zero, you can simply specify
  300. yaxis: { min: 0 } instead of adding a min parameter to all the axes.
  301. Generally, the various interfaces in Flot dealing with data points
  302. either accept an xaxis/yaxis parameter to specify which axis number to
  303. use (starting from 1), or lets you specify the coordinate directly as
  304. x2/x3/... or x2axis/x3axis/... instead of "x" or "xaxis".
  305. Time series data
  306. ================
  307. Time series are a bit more difficult than scalar data because
  308. calendars don't follow a simple base 10 system. For many cases, Flot
  309. abstracts most of this away, but it can still be a bit difficult to
  310. get the data into Flot. So we'll first discuss the data format.
  311. The time series support in Flot is based on Javascript timestamps,
  312. i.e. everywhere a time value is expected or handed over, a Javascript
  313. timestamp number is used. This is a number, not a Date object. A
  314. Javascript timestamp is the number of milliseconds since January 1,
  315. 1970 00:00:00 UTC. This is almost the same as Unix timestamps, except it's
  316. in milliseconds, so remember to multiply by 1000!
  317. You can see a timestamp like this
  318. alert((new Date()).getTime())
  319. Normally you want the timestamps to be displayed according to a
  320. certain time zone, usually the time zone in which the data has been
  321. produced. However, Flot always displays timestamps according to UTC.
  322. It has to as the only alternative with core Javascript is to interpret
  323. the timestamps according to the time zone that the visitor is in,
  324. which means that the ticks will shift unpredictably with the time zone
  325. and daylight savings of each visitor.
  326. So given that there's no good support for custom time zones in
  327. Javascript, you'll have to take care of this server-side.
  328. The easiest way to think about it is to pretend that the data
  329. production time zone is UTC, even if it isn't. So if you have a
  330. datapoint at 2002-02-20 08:00, you can generate a timestamp for eight
  331. o'clock UTC even if it really happened eight o'clock UTC+0200.
  332. In PHP you can get an appropriate timestamp with
  333. 'strtotime("2002-02-20 UTC") * 1000', in Python with
  334. 'calendar.timegm(datetime_object.timetuple()) * 1000', in .NET with
  335. something like:
  336. public static int GetJavascriptTimestamp(System.DateTime input)
  337. {
  338. System.TimeSpan span = new System.TimeSpan(System.DateTime.Parse("1/1/1970").Ticks);
  339. System.DateTime time = input.Subtract(span);
  340. return (long)(time.Ticks / 10000);
  341. }
  342. Javascript also has some support for parsing date strings, so it is
  343. possible to generate the timestamps manually client-side.
  344. If you've already got the real UTC timestamp, it's too late to use the
  345. pretend trick described above. But you can fix up the timestamps by
  346. adding the time zone offset, e.g. for UTC+0200 you would add 2 hours
  347. to the UTC timestamp you got. Then it'll look right on the plot. Most
  348. programming environments have some means of getting the timezone
  349. offset for a specific date (note that you need to get the offset for
  350. each individual timestamp to account for daylight savings).
  351. Once you've gotten the timestamps into the data and specified "time"
  352. as the axis mode, Flot will automatically generate relevant ticks and
  353. format them. As always, you can tweak the ticks via the "ticks" option
  354. - just remember that the values should be timestamps (numbers), not
  355. Date objects.
  356. Tick generation and formatting can also be controlled separately
  357. through the following axis options:
  358. minTickSize: array
  359. timeformat: null or format string
  360. monthNames: null or array of size 12 of strings
  361. twelveHourClock: boolean
  362. Here "timeformat" is a format string to use. You might use it like
  363. this:
  364. xaxis: {
  365. mode: "time"
  366. timeformat: "%y/%m/%d"
  367. }
  368. This will result in tick labels like "2000/12/24". The following
  369. specifiers are supported
  370. %h: hours
  371. %H: hours (left-padded with a zero)
  372. %M: minutes (left-padded with a zero)
  373. %S: seconds (left-padded with a zero)
  374. %d: day of month (1-31), use %0d for zero-padding
  375. %m: month (1-12), use %0m for zero-padding
  376. %y: year (four digits)
  377. %b: month name (customizable)
  378. %p: am/pm, additionally switches %h/%H to 12 hour instead of 24
  379. %P: AM/PM (uppercase version of %p)
  380. Inserting a zero like %0m or %0d means that the specifier will be
  381. left-padded with a zero if it's only single-digit. So %y-%0m-%0d
  382. results in unambigious ISO timestamps like 2007-05-10 (for May 10th).
  383. You can customize the month names with the "monthNames" option. For
  384. instance, for Danish you might specify:
  385. monthNames: ["jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"]
  386. If you set "twelveHourClock" to true, the autogenerated timestamps
  387. will use 12 hour AM/PM timestamps instead of 24 hour.
  388. The format string and month names are used by a very simple built-in
  389. format function that takes a date object, a format string (and
  390. optionally an array of month names) and returns the formatted string.
  391. If needed, you can access it as $.plot.formatDate(date, formatstring,
  392. monthNames) or even replace it with another more advanced function
  393. from a date library if you're feeling adventurous.
  394. If everything else fails, you can control the formatting by specifying
  395. a custom tick formatter function as usual. Here's a simple example
  396. which will format December 24 as 24/12:
  397. tickFormatter: function (val, axis) {
  398. var d = new Date(val);
  399. return d.getUTCDate() + "/" + (d.getUTCMonth() + 1);
  400. }
  401. Note that for the time mode "tickSize" and "minTickSize" are a bit
  402. special in that they are arrays on the form "[value, unit]" where unit
  403. is one of "second", "minute", "hour", "day", "month" and "year". So
  404. you can specify
  405. minTickSize: [1, "month"]
  406. to get a tick interval size of at least 1 month and correspondingly,
  407. if axis.tickSize is [2, "day"] in the tick formatter, the ticks have
  408. been produced with two days in-between.
  409. Customizing the data series
  410. ===========================
  411. series: {
  412. lines, points, bars: {
  413. show: boolean
  414. lineWidth: number
  415. fill: boolean or number
  416. fillColor: null or color/gradient
  417. }
  418. points: {
  419. radius: number
  420. symbol: "circle" or function
  421. }
  422. bars: {
  423. barWidth: number
  424. align: "left" or "center"
  425. horizontal: boolean
  426. }
  427. lines: {
  428. steps: boolean
  429. }
  430. shadowSize: number
  431. }
  432. colors: [ color1, color2, ... ]
  433. The options inside "series: {}" are copied to each of the series. So
  434. you can specify that all series should have bars by putting it in the
  435. global options, or override it for individual series by specifying
  436. bars in a particular the series object in the array of data.
  437. The most important options are "lines", "points" and "bars" that
  438. specify whether and how lines, points and bars should be shown for
  439. each data series. In case you don't specify anything at all, Flot will
  440. default to showing lines (you can turn this off with
  441. lines: { show: false }). You can specify the various types
  442. independently of each other, and Flot will happily draw each of them
  443. in turn (this is probably only useful for lines and points), e.g.
  444. var options = {
  445. series: {
  446. lines: { show: true, fill: true, fillColor: "rgba(255, 255, 255, 0.8)" },
  447. points: { show: true, fill: false }
  448. }
  449. };
  450. "lineWidth" is the thickness of the line or outline in pixels. You can
  451. set it to 0 to prevent a line or outline from being drawn; this will
  452. also hide the shadow.
  453. "fill" is whether the shape should be filled. For lines, this produces
  454. area graphs. You can use "fillColor" to specify the color of the fill.
  455. If "fillColor" evaluates to false (default for everything except
  456. points which are filled with white), the fill color is auto-set to the
  457. color of the data series. You can adjust the opacity of the fill by
  458. setting fill to a number between 0 (fully transparent) and 1 (fully
  459. opaque).
  460. For bars, fillColor can be a gradient, see the gradient documentation
  461. below. "barWidth" is the width of the bars in units of the x axis (or
  462. the y axis if "horizontal" is true), contrary to most other measures
  463. that are specified in pixels. For instance, for time series the unit
  464. is milliseconds so 24 * 60 * 60 * 1000 produces bars with the width of
  465. a day. "align" specifies whether a bar should be left-aligned
  466. (default) or centered on top of the value it represents. When
  467. "horizontal" is on, the bars are drawn horizontally, i.e. from the y
  468. axis instead of the x axis; note that the bar end points are still
  469. defined in the same way so you'll probably want to swap the
  470. coordinates if you've been plotting vertical bars first.
  471. For lines, "steps" specifies whether two adjacent data points are
  472. connected with a straight (possibly diagonal) line or with first a
  473. horizontal and then a vertical line. Note that this transforms the
  474. data by adding extra points.
  475. For points, you can specify the radius and the symbol. The only
  476. built-in symbol type is circles, for other types you can use a plugin
  477. or define them yourself by specifying a callback:
  478. function cross(ctx, x, y, radius, shadow) {
  479. var size = radius * Math.sqrt(Math.PI) / 2;
  480. ctx.moveTo(x - size, y - size);
  481. ctx.lineTo(x + size, y + size);
  482. ctx.moveTo(x - size, y + size);
  483. ctx.lineTo(x + size, y - size);
  484. }
  485. The parameters are the drawing context, x and y coordinates of the
  486. center of the point, a radius which corresponds to what the circle
  487. would have used and whether the call is to draw a shadow (due to
  488. limited canvas support, shadows are currently faked through extra
  489. draws). It's good practice to ensure that the area covered by the
  490. symbol is the same as for the circle with the given radius, this
  491. ensures that all symbols have approximately the same visual weight.
  492. "shadowSize" is the default size of shadows in pixels. Set it to 0 to
  493. remove shadows.
  494. The "colors" array specifies a default color theme to get colors for
  495. the data series from. You can specify as many colors as you like, like
  496. this:
  497. colors: ["#d18b2c", "#dba255", "#919733"]
  498. If there are more data series than colors, Flot will try to generate
  499. extra colors by lightening and darkening colors in the theme.
  500. Customizing the grid
  501. ====================
  502. grid: {
  503. show: boolean
  504. aboveData: boolean
  505. color: color
  506. backgroundColor: color/gradient or null
  507. labelMargin: number
  508. axisMargin: number
  509. markings: array of markings or (fn: axes -> array of markings)
  510. borderWidth: number
  511. borderColor: color or null
  512. minBorderMargin: number or null
  513. clickable: boolean
  514. hoverable: boolean
  515. autoHighlight: boolean
  516. mouseActiveRadius: number
  517. }
  518. The grid is the thing with the axes and a number of ticks. Many of the
  519. things in the grid are configured under the individual axes, but not
  520. all. "color" is the color of the grid itself whereas "backgroundColor"
  521. specifies the background color inside the grid area, here null means
  522. that the background is transparent. You can also set a gradient, see
  523. the gradient documentation below.
  524. You can turn off the whole grid including tick labels by setting
  525. "show" to false. "aboveData" determines whether the grid is drawn
  526. above the data or below (below is default).
  527. "labelMargin" is the space in pixels between tick labels and axis
  528. line, and "axisMargin" is the space in pixels between axes when there
  529. are two next to each other. Note that you can style the tick labels
  530. with CSS, e.g. to change the color. They have class "tickLabel".
  531. "borderWidth" is the width of the border around the plot. Set it to 0
  532. to disable the border. You can also set "borderColor" if you want the
  533. border to have a different color than the grid lines.
  534. "minBorderMargin" controls the default minimum margin around the
  535. border - it's used to make sure that points aren't accidentally
  536. clipped by the canvas edge so by default the value is computed from
  537. the point radius.
  538. "markings" is used to draw simple lines and rectangular areas in the
  539. background of the plot. You can either specify an array of ranges on
  540. the form { xaxis: { from, to }, yaxis: { from, to } } (with multiple
  541. axes, you can specify coordinates for other axes instead, e.g. as
  542. x2axis/x3axis/...) or with a function that returns such an array given
  543. the axes for the plot in an object as the first parameter.
  544. You can set the color of markings by specifying "color" in the ranges
  545. object. Here's an example array:
  546. markings: [ { xaxis: { from: 0, to: 2 }, yaxis: { from: 10, to: 10 }, color: "#bb0000" }, ... ]
  547. If you leave out one of the values, that value is assumed to go to the
  548. border of the plot. So for example if you only specify { xaxis: {
  549. from: 0, to: 2 } } it means an area that extends from the top to the
  550. bottom of the plot in the x range 0-2.
  551. A line is drawn if from and to are the same, e.g.
  552. markings: [ { yaxis: { from: 1, to: 1 } }, ... ]
  553. would draw a line parallel to the x axis at y = 1. You can control the
  554. line width with "lineWidth" in the range object.
  555. An example function that makes vertical stripes might look like this:
  556. markings: function (axes) {
  557. var markings = [];
  558. for (var x = Math.floor(axes.xaxis.min); x < axes.xaxis.max; x += 2)
  559. markings.push({ xaxis: { from: x, to: x + 1 } });
  560. return markings;
  561. }
  562. If you set "clickable" to true, the plot will listen for click events
  563. on the plot area and fire a "plotclick" event on the placeholder with
  564. a position and a nearby data item object as parameters. The coordinates
  565. are available both in the unit of the axes (not in pixels) and in
  566. global screen coordinates.
  567. Likewise, if you set "hoverable" to true, the plot will listen for
  568. mouse move events on the plot area and fire a "plothover" event with
  569. the same parameters as the "plotclick" event. If "autoHighlight" is
  570. true (the default), nearby data items are highlighted automatically.
  571. If needed, you can disable highlighting and control it yourself with
  572. the highlight/unhighlight plot methods described elsewhere.
  573. You can use "plotclick" and "plothover" events like this:
  574. $.plot($("#placeholder"), [ d ], { grid: { clickable: true } });
  575. $("#placeholder").bind("plotclick", function (event, pos, item) {
  576. alert("You clicked at " + pos.x + ", " + pos.y);
  577. // axis coordinates for other axes, if present, are in pos.x2, pos.x3, ...
  578. // if you need global screen coordinates, they are pos.pageX, pos.pageY
  579. if (item) {
  580. highlight(item.series, item.datapoint);
  581. alert("You clicked a point!");
  582. }
  583. });
  584. The item object in this example is either null or a nearby object on the form:
  585. item: {
  586. datapoint: the point, e.g. [0, 2]
  587. dataIndex: the index of the point in the data array
  588. series: the series object
  589. seriesIndex: the index of the series
  590. pageX, pageY: the global screen coordinates of the point
  591. }
  592. For instance, if you have specified the data like this
  593. $.plot($("#placeholder"), [ { label: "Foo", data: [[0, 10], [7, 3]] } ], ...);
  594. and the mouse is near the point (7, 3), "datapoint" is [7, 3],
  595. "dataIndex" will be 1, "series" is a normalized series object with
  596. among other things the "Foo" label in series.label and the color in
  597. series.color, and "seriesIndex" is 0. Note that plugins and options
  598. that transform the data can shift the indexes from what you specified
  599. in the original data array.
  600. If you use the above events to update some other information and want
  601. to clear out that info in case the mouse goes away, you'll probably
  602. also need to listen to "mouseout" events on the placeholder div.
  603. "mouseActiveRadius" specifies how far the mouse can be from an item
  604. and still activate it. If there are two or more points within this
  605. radius, Flot chooses the closest item. For bars, the top-most bar
  606. (from the latest specified data series) is chosen.
  607. If you want to disable interactivity for a specific data series, you
  608. can set "hoverable" and "clickable" to false in the options for that
  609. series, like this { data: [...], label: "Foo", clickable: false }.
  610. Specifying gradients
  611. ====================
  612. A gradient is specified like this:
  613. { colors: [ color1, color2, ... ] }
  614. For instance, you might specify a background on the grid going from
  615. black to gray like this:
  616. grid: {
  617. backgroundColor: { colors: ["#000", "#999"] }
  618. }
  619. For the series you can specify the gradient as an object that
  620. specifies the scaling of the brightness and the opacity of the series
  621. color, e.g.
  622. { colors: [{ opacity: 0.8 }, { brightness: 0.6, opacity: 0.8 } ] }
  623. where the first color simply has its alpha scaled, whereas the second
  624. is also darkened. For instance, for bars the following makes the bars
  625. gradually disappear, without outline:
  626. bars: {
  627. show: true,
  628. lineWidth: 0,
  629. fill: true,
  630. fillColor: { colors: [ { opacity: 0.8 }, { opacity: 0.1 } ] }
  631. }
  632. Flot currently only supports vertical gradients drawn from top to
  633. bottom because that's what works with IE.
  634. Plot Methods
  635. ------------
  636. The Plot object returned from the plot function has some methods you
  637. can call:
  638. - highlight(series, datapoint)
  639. Highlight a specific datapoint in the data series. You can either
  640. specify the actual objects, e.g. if you got them from a
  641. "plotclick" event, or you can specify the indices, e.g.
  642. highlight(1, 3) to highlight the fourth point in the second series
  643. (remember, zero-based indexing).
  644. - unhighlight(series, datapoint) or unhighlight()
  645. Remove the highlighting of the point, same parameters as
  646. highlight.
  647. If you call unhighlight with no parameters, e.g. as
  648. plot.unhighlight(), all current highlights are removed.
  649. - setData(data)
  650. You can use this to reset the data used. Note that axis scaling,
  651. ticks, legend etc. will not be recomputed (use setupGrid() to do
  652. that). You'll probably want to call draw() afterwards.
  653. You can use this function to speed up redrawing a small plot if
  654. you know that the axes won't change. Put in the new data with
  655. setData(newdata), call draw(), and you're good to go. Note that
  656. for large datasets, almost all the time is consumed in draw()
  657. plotting the data so in this case don't bother.
  658. - setupGrid()
  659. Recalculate and set axis scaling, ticks, legend etc.
  660. Note that because of the drawing model of the canvas, this
  661. function will immediately redraw (actually reinsert in the DOM)
  662. the labels and the legend, but not the actual tick lines because
  663. they're drawn on the canvas. You need to call draw() to get the
  664. canvas redrawn.
  665. - draw()
  666. Redraws the plot canvas.
  667. - triggerRedrawOverlay()
  668. Schedules an update of an overlay canvas used for drawing
  669. interactive things like a selection and point highlights. This
  670. is mostly useful for writing plugins. The redraw doesn't happen
  671. immediately, instead a timer is set to catch multiple successive
  672. redraws (e.g. from a mousemove). You can get to the overlay by
  673. setting up a drawOverlay hook.
  674. - width()/height()
  675. Gets the width and height of the plotting area inside the grid.
  676. This is smaller than the canvas or placeholder dimensions as some
  677. extra space is needed (e.g. for labels).
  678. - offset()
  679. Returns the offset of the plotting area inside the grid relative
  680. to the document, useful for instance for calculating mouse
  681. positions (event.pageX/Y minus this offset is the pixel position
  682. inside the plot).
  683. - pointOffset({ x: xpos, y: ypos })
  684. Returns the calculated offset of the data point at (x, y) in data
  685. space within the placeholder div. If you are working with multiple axes, you
  686. can specify the x and y axis references, e.g.
  687. o = pointOffset({ x: xpos, y: ypos, xaxis: 2, yaxis: 3 })
  688. // o.left and o.top now contains the offset within the div
  689. - resize()
  690. Tells Flot to resize the drawing canvas to the size of the
  691. placeholder. You need to run setupGrid() and draw() afterwards as
  692. canvas resizing is a destructive operation. This is used
  693. internally by the resize plugin.
  694. - shutdown()
  695. Cleans up any event handlers Flot has currently registered. This
  696. is used internally.
  697. There are also some members that let you peek inside the internal
  698. workings of Flot which is useful in some cases. Note that if you change
  699. something in the objects returned, you're changing the objects used by
  700. Flot to keep track of its state, so be careful.
  701. - getData()
  702. Returns an array of the data series currently used in normalized
  703. form with missing settings filled in according to the global
  704. options. So for instance to find out what color Flot has assigned
  705. to the data series, you could do this:
  706. var series = plot.getData();
  707. for (var i = 0; i < series.length; ++i)
  708. alert(series[i].color);
  709. A notable other interesting field besides color is datapoints
  710. which has a field "points" with the normalized data points in a
  711. flat array (the field "pointsize" is the increment in the flat
  712. array to get to the next point so for a dataset consisting only of
  713. (x,y) pairs it would be 2).
  714. - getAxes()
  715. Gets an object with the axes. The axes are returned as the
  716. attributes of the object, so for instance getAxes().xaxis is the
  717. x axis.
  718. Various things are stuffed inside an axis object, e.g. you could
  719. use getAxes().xaxis.ticks to find out what the ticks are for the
  720. xaxis. Two other useful attributes are p2c and c2p, functions for
  721. transforming from data point space to the canvas plot space and
  722. back. Both returns values that are offset with the plot offset.
  723. Check the Flot source code for the complete set of attributes (or
  724. output an axis with console.log() and inspect it).
  725. With multiple axes, the extra axes are returned as x2axis, x3axis,
  726. etc., e.g. getAxes().y2axis is the second y axis. You can check
  727. y2axis.used to see whether the axis is associated with any data
  728. points and y2axis.show to see if it is currently shown.
  729. - getPlaceholder()
  730. Returns placeholder that the plot was put into. This can be useful
  731. for plugins for adding DOM elements or firing events.
  732. - getCanvas()
  733. Returns the canvas used for drawing in case you need to hack on it
  734. yourself. You'll probably need to get the plot offset too.
  735. - getPlotOffset()
  736. Gets the offset that the grid has within the canvas as an object
  737. with distances from the canvas edges as "left", "right", "top",
  738. "bottom". I.e., if you draw a circle on the canvas with the center
  739. placed at (left, top), its center will be at the top-most, left
  740. corner of the grid.
  741. - getOptions()
  742. Gets the options for the plot, normalized, with default values
  743. filled in. You get a reference to actual values used by Flot, so
  744. if you modify the values in here, Flot will use the new values.
  745. If you change something, you probably have to call draw() or
  746. setupGrid() or triggerRedrawOverlay() to see the change.
  747. Hooks
  748. =====
  749. In addition to the public methods, the Plot object also has some hooks
  750. that can be used to modify the plotting process. You can install a
  751. callback function at various points in the process, the function then
  752. gets access to the internal data structures in Flot.
  753. Here's an overview of the phases Flot goes through:
  754. 1. Plugin initialization, parsing options
  755. 2. Constructing the canvases used for drawing
  756. 3. Set data: parsing data specification, calculating colors,
  757. copying raw data points into internal format,
  758. normalizing them, finding max/min for axis auto-scaling
  759. 4. Grid setup: calculating axis spacing, ticks, inserting tick
  760. labels, the legend
  761. 5. Draw: drawing the grid, drawing each of the series in turn
  762. 6. Setting up event handling for interactive features
  763. 7. Responding to events, if any
  764. 8. Shutdown: this mostly happens in case a plot is overwritten
  765. Each hook is simply a function which is put in the appropriate array.
  766. You can add them through the "hooks" option, and they are also available
  767. after the plot is constructed as the "hooks" attribute on the returned
  768. plot object, e.g.
  769. // define a simple draw hook
  770. function hellohook(plot, canvascontext) { alert("hello!"); };
  771. // pass it in, in an array since we might want to specify several
  772. var plot = $.plot(placeholder, data, { hooks: { draw: [hellohook] } });
  773. // we can now find it again in plot.hooks.draw[0] unless a plugin
  774. // has added other hooks
  775. The available hooks are described below. All hook callbacks get the
  776. plot object as first parameter. You can find some examples of defined
  777. hooks in the plugins bundled with Flot.
  778. - processOptions [phase 1]
  779. function(plot, options)
  780. Called after Flot has parsed and merged options. Useful in the
  781. instance where customizations beyond simple merging of default
  782. values is needed. A plugin might use it to detect that it has been
  783. enabled and then turn on or off other options.
  784. - processRawData [phase 3]
  785. function(plot, series, data, datapoints)
  786. Called before Flot copies and normalizes the raw data for the given
  787. series. If the function fills in datapoints.points with normalized
  788. points and sets datapoints.pointsize to the size of the points,
  789. Flot will skip the copying/normalization step for this series.
  790. In any case, you might be interested in setting datapoints.format,
  791. an array of objects for specifying how a point is normalized and
  792. how it interferes with axis scaling.
  793. The default format array for points is something along the lines of:
  794. [
  795. { x: true, number: true, required: true },
  796. { y: true, number: true, required: true }
  797. ]
  798. The first object means that for the first coordinate it should be
  799. taken into account when scaling the x axis, that it must be a
  800. number, and that it is required - so if it is null or cannot be
  801. converted to a number, the whole point will be zeroed out with
  802. nulls. Beyond these you can also specify "defaultValue", a value to
  803. use if the coordinate is null. This is for instance handy for bars
  804. where one can omit the third coordinate (the bottom of the bar)
  805. which then defaults to 0.
  806. - processDatapoints [phase 3]
  807. function(plot, series, datapoints)
  808. Called after normalization of the given series but before finding
  809. min/max of the data points. This hook is useful for implementing data
  810. transformations. "datapoints" contains the normalized data points in
  811. a flat array as datapoints.points with the size of a single point
  812. given in datapoints.pointsize. Here's a simple transform that
  813. multiplies all y coordinates by 2:
  814. function multiply(plot, series, datapoints) {
  815. var points = datapoints.points, ps = datapoints.pointsize;
  816. for (var i = 0; i < points.length; i += ps)
  817. points[i + 1] *= 2;
  818. }
  819. Note that you must leave datapoints in a good condition as Flot
  820. doesn't check it or do any normalization on it afterwards.
  821. - drawSeries [phase 5]
  822. function(plot, canvascontext, series)
  823. Hook for custom drawing of a single series. Called just before the
  824. standard drawing routine has been called in the loop that draws
  825. each series.
  826. - draw [phase 5]
  827. function(plot, canvascontext)
  828. Hook for drawing on the canvas. Called after the grid is drawn
  829. (unless it's disabled or grid.aboveData is set) and the series have
  830. been plotted (in case any points, lines or bars have been turned
  831. on). For examples of how to draw things, look at the source code.
  832. - bindEvents [phase 6]
  833. function(plot, eventHolder)
  834. Called after Flot has setup its event handlers. Should set any
  835. necessary event handlers on eventHolder, a jQuery object with the
  836. canvas, e.g.
  837. function (plot, eventHolder) {
  838. eventHolder.mousedown(function (e) {
  839. alert("You pressed the mouse at " + e.pageX + " " + e.pageY);
  840. });
  841. }
  842. Interesting events include click, mousemove, mouseup/down. You can
  843. use all jQuery events. Usually, the event handlers will update the
  844. state by drawing something (add a drawOverlay hook and call
  845. triggerRedrawOverlay) or firing an externally visible event for
  846. user code. See the crosshair plugin for an example.
  847. Currently, eventHolder actually contains both the static canvas
  848. used for the plot itself and the overlay canvas used for
  849. interactive features because some versions of IE get the stacking
  850. order wrong. The hook only gets one event, though (either for the
  851. overlay or for the static canvas).
  852. Note that custom plot events generated by Flot are not generated on
  853. eventHolder, but on the div placeholder supplied as the first
  854. argument to the plot call. You can get that with
  855. plot.getPlaceholder() - that's probably also the one you should use
  856. if you need to fire a custom event.
  857. - drawOverlay [phase 7]
  858. function (plot, canvascontext)
  859. The drawOverlay hook is used for interactive things that need a
  860. canvas to draw on. The model currently used by Flot works the way
  861. that an extra overlay canvas is positioned on top of the static
  862. canvas. This overlay is cleared and then completely redrawn
  863. whenever something interesting happens. This hook is called when
  864. the overlay canvas is to be redrawn.
  865. "canvascontext" is the 2D context of the overlay canvas. You can
  866. use this to draw things. You'll most likely need some of the
  867. metrics computed by Flot, e.g. plot.width()/plot.height(). See the
  868. crosshair plugin for an example.
  869. - shutdown [phase 8]
  870. function (plot, eventHolder)
  871. Run when plot.shutdown() is called, which usually only happens in
  872. case a plot is overwritten by a new plot. If you're writing a
  873. plugin that adds extra DOM elements or event handlers, you should
  874. add a callback to clean up after you. Take a look at the section in
  875. PLUGINS.txt for more info.
  876. Plugins
  877. -------
  878. Plugins extend the functionality of Flot. To use a plugin, simply
  879. include its Javascript file after Flot in the HTML page.
  880. If you're worried about download size/latency, you can concatenate all
  881. the plugins you use, and Flot itself for that matter, into one big file
  882. (make sure you get the order right), then optionally run it through a
  883. Javascript minifier such as YUI Compressor.
  884. Here's a brief explanation of how the plugin plumbings work:
  885. Each plugin registers itself in the global array $.plot.plugins. When
  886. you make a new plot object with $.plot, Flot goes through this array
  887. calling the "init" function of each plugin and merging default options
  888. from the "option" attribute of the plugin. The init function gets a
  889. reference to the plot object created and uses this to register hooks
  890. and add new public methods if needed.
  891. See the PLUGINS.txt file for details on how to write a plugin. As the
  892. above description hints, it's actually pretty easy.
  893. Version number
  894. --------------
  895. The version number of Flot is available in $.plot.version.