Code:
--###################################################
--### Lua Example by arclance 2012-09-07 ############
--### Line Graph With Outline And Color Gradient ####
--###################################################
--### CR : cairo surface from cairo_create(cs)
--### data : table of values to graph
--### max_Value : maximum value to display ie. the y-value of a point at the top of the graph box. Data containing values larger than max_Value may not work correctly.
--### table_Length : number of data points to plot from the table starting at the first table entry (data[1])
--### BLX : x-position in pixels of the bottom left corner of the graph box
--### BLY : y-position in pixels of the bottom left corner of the graph box
--### ALPHA : alpha value (transparency) to use for the graph line. Does not affect graph outline alpha
--### WIDTH : the x-axis separation of each data point in pixels. A value of 1 is reccomended
--### This controls the width of the graph window so that that width of the graph window including the outline is equal to
--### (table_Length * WIDTH) + (2 * outline_width) + 1
--### graph_height : the y-size in pixels of the graph area inside the graph outline
--### This controls the height of the graph window so that the height of the graph window including the outline is equal to
--### graph_height + (2 * outline_width) + 1
--### outline_width : the width of the line used to draw the outline around the graph area in pixels
--### shade_color : a table holding the RBGA color to use to draw the shade on the graph outline. {R,G,B,A}
--### outline_color : a table holding the RBGA color to use to draw the graph outline. {R,G,B,A}
--### grad_color_1 : a table holding the RGB color to use for left end of the color gradient. {R,G,B}
--### grad_color_2 : a table holding the RGB color to use for right end of the color gradient. {R,G,B}
--### Gradient code is not perfect it may not work correctly with all color pairs
function draw_line_graph(CR, data, max_Value, table_Length, BLX, BLY, ALPHA, WIDTH, graph_height, outline_width, shade_color, outline_color, grad_color_1, grad_color_2) --# only works with integer BLX and BLY
cairo_set_antialias (CR, CAIRO_ANTIALIAS_NONE) --# don't antialias lines are too thin and gets blurred
cairo_set_line_width (CR, outline_width)
local graphWidth = (table_Length * WIDTH) + 1
local outlineWidth = graphWidth
local outlineHeight = graph_height + 1
--# draw outline shade
cairo_set_source_rgba (CR, shade_color[1], shade_color[2], shade_color[3], shade_color[4])
cairo_rectangle (CR,(BLX + 1),(BLY + 1),outlineWidth,-outlineHeight)
cairo_stroke (CR)
--# draw outline
cairo_set_source_rgba (CR, outline_color[1], outline_color[2], outline_color[3], outline_color[4])
cairo_rectangle (CR,(BLX),(BLY),outlineWidth,-outlineHeight)
cairo_stroke (CR)
--# Start Drawing Graph Line
cairo_set_line_width (CR, 1)
--# move to start position
local bar_height = ((data[1]/max_Value)*graph_height) + 1
cairo_move_to (CR,((BLX)+(WIDTH/2)+(WIDTH)),(BLY - bar_height))
for i=1,table_Length do
--# Get gradient color
local Red,Green,Blue = linear_gradient(grad_color_1, grad_color_2, graphWidth, (graphWidth - (i*WIDTH)))
cairo_set_source_rgba (CR,Red,Green,Blue,ALPHA)
--# calculate data y positon
local bar_height = ((data[i]/max_Value)*graph_height) + 1
--# draw line from last point to current point
if data[i] <= max_Value then --# don't draw the point if it is outside of the graph area. Still need to move the point so the Line will re-enter the graph in the right place. There will be a small break in the graph were is re-enters with this simple fix.
cairo_line_to (CR, ((BLX)+(WIDTH/2)+((i-1)*WIDTH)),(BLY - bar_height))
cairo_stroke (CR)
end --# if data[i] <= max_Value then
--# move to current point
cairo_move_to (CR,((BLX)+(WIDTH/2)+((i-1)*WIDTH)),(BLY - bar_height))
end --# i=1,table_length do
cairo_set_antialias (CR, CAIRO_ANTIALIAS_DEFAULT)
return CR
end --# function draw_line_graph()
function linear_gradient(Left_Color, Right_Color, Length, Position)
local positionPercent = Position/Length
local red = Right_Color[1] + (math.abs(Right_Color[1] - Left_Color[1]) * positionPercent)
local green = Right_Color[2] + (math.abs(Right_Color[2] - Left_Color[2]) * positionPercent)
local blue = Right_Color[3] + (math.abs(Right_Color[3] - Left_Color[3]) * positionPercent)
return red,green,blue
end --# function linear_gradient()
Bookmarks