Guest User

Untitled

a guest
Nov 7th, 2014
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.67 KB | None | 0 0
  1.  
  2. -- Warning, render targets require you to write into the meta.xml a min. version
  3. -- <min_mta_version client="1.3.5"/> or <min_mta_version client="1.4.0"/>
  4.  
  5. -- Get the screen size
  6. local screenWidth, screenHeight = guiGetScreenSize()
  7.  
  8. -- Resolution independet solution for width & height of the chart box
  9. local box_width = (screenWidth * 0.75)
  10. local box_height = (screenHeight * 0.50)
  11.  
  12. -- Number of rows
  13. local chart_rows = 10
  14.  
  15. -- Colors in the chart
  16. local colors = {
  17.     background = tocolor(255, 255, 255, 255),
  18.     coordline = tocolor(0, 0, 0, 255),
  19.     titlecolor = tocolor(0, 0, 0, 255),
  20.     gridline = tocolor(0, 0, 0, 5),
  21.     valueline = tocolor(255, 0, 0, 255),
  22.     rowvalue = tocolor(0, 0, 0, 255),
  23. }
  24.  
  25. -- Cache variables
  26. local lowest_value = false
  27. local highest_value = false
  28. local chart_title = false
  29. local chart_columns = false
  30. local chart_data = false
  31. local title_height = false
  32. local rendertarget = false
  33.  
  34. -- Cache a table with raw number values
  35. function cacheChartData(title, data)
  36.     -- Reset cache
  37.     lowest_value = false
  38.     highest_value = false
  39.  
  40.     -- Find highest and lowest values
  41.     for i = 1, #data do
  42.         if (not lowest_value or data[i] < lowest_value) then
  43.             lowest_value = data[i]
  44.         end
  45.         if (not highest_value or data[i] > highest_value) then
  46.             highest_value = data[i]
  47.         end
  48.     end
  49.  
  50.     -- Apply the data
  51.     chart_data = data
  52.    
  53.     -- Calculate the number of columns
  54.     chart_columns = #data - 1
  55.  
  56.     -- Apply the chart title
  57.     chart_title = title
  58.  
  59.     -- Calculate the chart title height
  60.     title_height = dxGetFontHeight(3, "default-bold")
  61.  
  62.     -- Draw the chart into a rendertarget
  63.     cacheChartDrawing()
  64. end
  65.  
  66. -- Render the chart into a render target
  67. function cacheChartDrawing(cleared)
  68.     -- Event call detection
  69.     if (type(cleared) == "boolean") then
  70.         -- Do nothing if the render target is still intact
  71.         if (not cleared) then
  72.             return
  73.         end
  74.  
  75.         -- Change the value so we know we don't have to re-create the r. t.
  76.         cleared = true
  77.     end
  78.  
  79.     -- Destroy the old version (if neccessary)
  80.     if (not cleared) then
  81.         if (isElement(rendertarget)) then
  82.             destroyElement(rendertarget)
  83.         end
  84.  
  85.         -- Create a new rendertarget
  86.         rendertarget = dxCreateRenderTarget(box_width, box_height, true)
  87.     end
  88.  
  89.     -- Abort if the render target is invalid
  90.     if (not rendertarget) then
  91.         return
  92.     -- Else switch to the render target
  93.     else
  94.         dxSetRenderTarget(rendertarget)
  95.     end
  96.  
  97.     -- Draw the background
  98.     dxDrawRectangle(0, 0, box_width, box_height, colors.background)
  99.  
  100.     -- Define a general padding to the content
  101.     local padding = 0.10 * box_height
  102.  
  103.     -- Calculate the width of the highest number
  104.     local left_width = dxGetTextWidth(tostring(highest_value), 1, "default")
  105.  
  106.     -- Calculate the height of the chart and the 0 coordinate position
  107.     local chart_height = (box_height - (padding * 2) - title_height)
  108.     local chart_y = (box_height - padding)
  109.  
  110.     -- Calculate the width of the chart and the 0 coordinate position
  111.     local chart_width = (box_width - (padding * 2) - left_width)
  112.     local chart_x = (padding + left_width)
  113.  
  114.     -- Draw the chart title
  115.     dxDrawText(chart_title, 0, (padding / 2), box_width, (padding / 2) + title_height, colors.titlecolor, 3, "default-bold", "center", "center", false, false, false, true)
  116.  
  117.     -- Draw the x and y lines
  118.     dxDrawLine(chart_x, chart_y, chart_x + chart_width, chart_y, colors.coordline, 2)
  119.     dxDrawLine(chart_x, chart_y - chart_height, chart_x, chart_y, colors.coordline, 2)
  120.  
  121.     -- Calculate the width and height for each cell
  122.     local column_width = (chart_width / chart_columns)
  123.     local column_height = (chart_height / chart_rows)
  124.  
  125.     -- Draw the grid
  126.     for i = 0, chart_columns do
  127.         for j = 0, chart_rows do
  128.             -- Draw the column line
  129.             dxDrawLine(chart_x + (i * column_width), chart_y - chart_height, chart_x + (i * column_width), chart_y, colors.gridline)
  130.  
  131.             -- Draw the row line
  132.             dxDrawLine(chart_x, chart_y - (j * column_height), chart_x + chart_width, chart_y - (j * column_height), colors.gridline)
  133.         end
  134.     end
  135.  
  136.     -- Draw the highest value
  137.     dxDrawText(tostring(highest_value), padding, chart_y - chart_height, padding / 2 + left_width, chart_y - chart_height, colors.rowvalue, 1, "default", "center", "center", false, false, false, true)
  138.  
  139.     -- Draw the values for the other rows
  140.     for i = 0, (chart_rows - 1) do
  141.         dxDrawText(("%.2f"):format((i / chart_rows) * highest_value), padding, chart_y - (column_height * i), padding / 2 + left_width, chart_y - (column_height * i), colors.rowvalue, 1, "default", "center", "center", false, false, false, true)
  142.     end
  143.  
  144.     -- Cache for the last height
  145.     local last_height = false
  146.  
  147.     -- Draw the value line
  148.     for i = 0, chart_columns do
  149.         -- Calculate the height
  150.         local height = (chart_data[i + 1] / highest_value) * chart_height
  151.  
  152.         -- Draw a line if we have a last height
  153.         if (last_height) then
  154.             dxDrawLine(chart_x + ((i - 1) * column_width), chart_y - last_height, chart_x + (i * column_width), chart_y - height, colors.valueline, 2)
  155.         end
  156.  
  157.         -- Save the height for the next value
  158.         last_height = height
  159.     end
  160.    
  161.     -- Reset the render target
  162.     dxSetRenderTarget()
  163. end
  164.  
  165. function renderChart()
  166.     -- Nothing cached
  167.     if (not rendertarget) then
  168.         return
  169.     end
  170.  
  171.     -- Calculate the position (not efficient to do here, but fuck that)
  172.     local x = (screenWidth - box_width) / 2
  173.     local y = (screenHeight - box_height) / 2
  174.  
  175.     -- Draw the render target
  176.     dxDrawImage(x, y, box_width, box_height, rendertarget)
  177. end
  178.  
  179. addEventHandler("onClientResourceStart", resourceRoot,
  180.     function ()
  181.         -- Example
  182.         cacheChartData("Fancy Chart Title", {
  183.             5, 10, 20, 6, 1, 19, 30, 15, 120, 59, 12, 22, 125, 12
  184.         })
  185.  
  186.         -- Events
  187.         addEventHandler("onClientRender", root, renderChart)
  188.         addEventHandler("onClientRestore", root, cacheChartDrawing)
  189.     end
  190. )
Advertisement
Add Comment
Please, Sign In to add comment