Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Commit

Permalink
Add bar chart for performance stats. (#527)
Browse files Browse the repository at this point in the history
* Toggle for Performance Stats.

* Performance stats window comes & goes as setting is toggled.

* tweaks.

* Stopping point on Performance Stats work: piping to poll and store stats, put up buttons and viewer.

* removing some files

* Got label side of things working.

* More proper includes.  Rendering values.

* respond to review

* responded to review

* reparenting gui to core gui instead of player.

* pause

* pausing

* got parenting right so things lay on top of each other properly, parent is core gui.

* spelling mistakes

* spelling

* graph is working

* whassup

* removed waits on requires.

* responded to review from SolarCrane

* respond to review

* removing files.

* cleaning up stupid Panel3d.lua file.

* Fixmes on number formatting

* respond to review
  • Loading branch information
DougBanksPersonal authored and hockeychaos committed Sep 9, 2016
1 parent 0687a82 commit 74f2217
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
*~

*.gem
*.rbc
.bundle
Expand Down
Expand Up @@ -26,7 +26,7 @@ local showPerformanceStatsInGui = getShowPerformanceStatsInGuiSuccess and showPe
--[[ Script Variables ]]--
local masterFrame = Instance.new("Frame")
masterFrame.Name = "PS_MasterFrame"
masterFrame.Name = "PerformanceStats"
local statsAggregatorManager = StatsAggregatorManagerClass.new()
local statsViewer = StatsViewerClass.new()
Expand Down
57 changes: 57 additions & 0 deletions CoreScriptsRoot/Modules/Stats/BarGraph.lua
@@ -0,0 +1,57 @@
--[[
Filename: BarGraph.lua
Written by: dbanks
Description: A simple bar graph.
--]]
--[[ Services ]]--
local CoreGuiService = game:GetService('CoreGui')
--[[ Globals ]]--
local BarColor = Color3.new(0.1, 0.7, 0.1)
--[[ Modules ]]--
local StatsUtils = require(CoreGuiService.RobloxGui.Modules.Stats.StatsUtils)
--[[ Classes ]]--
local BarGraphClass = {}
BarGraphClass.__index = BarGraphClass
function BarGraphClass.new()
local self = {}
setmetatable(self, BarGraphClass)
self._frame = Instance.new("Frame")
self._frame.Name = "PS_BarGraph"
self._frame.BackgroundTransparency = 1.0
return self
end
function BarGraphClass:PlaceInParent(parent, size, position)
self._frame.Position = position
self._frame.Size = size
self._frame.Parent = parent
end
function BarGraphClass:UpdateGraph(values, axisMax)
self._frame:ClearAllChildren()
local numValues = table.getn(values)
for i, value in ipairs(values) do
self:_addBar(i, value, numValues, axisMax)
end
end
function BarGraphClass:_addBar(i, value, numValues, axisMax)
local realIndex = i-1
local bar = Instance.new("Frame")
bar.Name = string.format("Bar_%d", realIndex)
bar.Position = UDim2.new(realIndex/numValues, 0, (axisMax - value)/axisMax, 0)
bar.Size = UDim2.new(1/numValues, 0, value/axisMax, 0)
bar.Parent = self._frame
bar.BackgroundColor3 = BarColor
bar.BorderSizePixel = 0
end
return BarGraphClass
142 changes: 132 additions & 10 deletions CoreScriptsRoot/Modules/Stats/StatsAnnotatedGraph.lua
Expand Up @@ -9,42 +9,164 @@
local CoreGuiService = game:GetService('CoreGui')
--[[ Globals ]]--
local TextPanelXFraction = 0.5
local GraphXFraction = 1 - TextPanelXFraction
local TextPanelPosition = UDim2.new(0, 0, 0, 0)
local TextPanelSize = UDim2.new(TextPanelXFraction, 0, 1, 0)
local GraphPosition = UDim2.new(TextPanelXFraction, 0, 0, 0)
local GraphSize = UDim2.new(GraphXFraction, 0, 1, 0)
local Margin = 10
local LabelXWidth = 30
--[[ Modules ]]--
local StatsUtils = require(CoreGuiService.RobloxGui.Modules.Stats.StatsUtils)
local BarGraphClass = require(CoreGuiService.RobloxGui.Modules.Stats.BarGraph)
--[[ Classes ]]--
local StatsAnnotatedGraphClass = {}
StatsAnnotatedGraphClass.__index = StatsAnnotatedGraphClass
function StatsAnnotatedGraphClass.new()
function StatsAnnotatedGraphClass.new(isMaximized)
local self = {}
setmetatable(self, StatsAnnotatedGraphClass)
self._isMaximized = isMaximized
self._frame = Instance.new("Frame")
self._frame.Name = "PS_AnnotatedGraph"
self._frame.BackgroundTransparency = 1.0
self._topLabel = Instance.new("TextLabel")
self._topLabel.Name = "PS_TopAxisLabel"
StatsUtils.StyleTextWidget(self._topLabel)
self._topLabel.Parent = self._frame
self._topLabel.TextXAlignment = Enum.TextXAlignment.Left
self._topLabel.TextYAlignment = Enum.TextYAlignment.Top
self._midLabel = Instance.new("TextLabel")
self._midLabel.Name = "PS_MidAxisLabel"
StatsUtils.StyleTextWidget(self._midLabel)
self._midLabel.Parent = self._frame
self._midLabel.TextXAlignment = Enum.TextXAlignment.Left
self._midLabel.TextYAlignment = Enum.TextYAlignment.Center
self._bottomLabel = Instance.new("TextLabel")
self._bottomLabel.Name = "PS_BottomAxisLabel"
StatsUtils.StyleTextWidget(self._bottomLabel)
self._bottomLabel.Parent = self._frame
self._bottomLabel.TextXAlignment = Enum.TextXAlignment.Left
self._bottomLabel.TextYAlignment = Enum.TextYAlignment.Bottom
StatsUtils.StyleFrame(self._frame)
self._graph = BarGraphClass.new()
self:_layoutElements()
return self
end
function StatsAnnotatedGraphClass:_layoutElements()
local labelWidth
if (self._isMaximized) then
labelWidth = LabelXWidth
self._topLabel.Visible = true
self._midLabel.Visible = true
self._bottomLabel.Visible = true
else
labelWidth = 0
self._topLabel.Visible = false
self._midLabel.Visible = false
self._bottomLabel.Visible = false
end
local GraphFramePosition = UDim2.new(0, Margin, 0, Margin)
local GraphFrameSize = UDim2.new(1, -(2 * Margin + labelWidth), 1, -2 * Margin)
local TopLabelFramePosition = UDim2.new(1, -(Margin + labelWidth), 0, Margin)
local TopLabelFrameSize = UDim2.new(0, labelWidth, 0.333, -2 * Margin)
local MidLabelFramePosition = UDim2.new(1, -(Margin + labelWidth), 0.333, Margin)
local MidLabelFrameSize = UDim2.new(0, labelWidth, 0.333, -2 * Margin)
local BottomLabelFramePosition = UDim2.new(1, -(Margin + labelWidth), 0.666, Margin)
local BottomLabelFrameSize = UDim2.new(0, labelWidth, 0.333, -2 * Margin)
self._topLabel.Size = TopLabelFrameSize
self._topLabel.Position = TopLabelFramePosition
self._midLabel.Size = MidLabelFrameSize
self._midLabel.Position = MidLabelFramePosition
self._bottomLabel.Size = BottomLabelFrameSize
self._bottomLabel.Position = BottomLabelFramePosition
self._graph:PlaceInParent(self._frame, GraphFrameSize, GraphFramePosition)
end
function StatsAnnotatedGraphClass:PlaceInParent(parent, size, position)
self._frame.Position = position
self._frame.Size = size
self._frame.Parent = parent
end
function StatsAnnotatedGraphClass:SetValues(values)
local axisMax = self:_calculateAxisMax(values)
self._graph:UpdateGraph(values, axisMax)
-- FIXME(dbanks)
-- Fill this in.
-- Format based on stat type.
self._topLabel.Text = string.format("%.4f", axisMax)
self._midLabel.Text = string.format("%.4f", axisMax/2)
self._bottomLabel.Text = string.format("%.4f", 0,.0)
end
function StatsAnnotatedGraphClass:_calculateAxisMax(values)
-- Calculate an optimal axis label for this set of values.
-- We want a final value 'axisMax' s.t. the largest value 'max' in 'values' is
-- such that:
-- 0.1 * axisMax <= max < axisMax
local max = 0.0
for i, value in ipairs(values) do
if value > max then
max = value
end
end
return self:_recursiveGetAxisMax(1, max)
end
function StatsAnnotatedGraphClass:SetStatsAggregator(aggregator)
if (self._aggregator) then
self._aggregator:RemoveListener(self._listenerId)
self._listenerId = nil
self._aggregator = nil
end
self._aggregator = aggregator
if (self._aggregator ~= nil) then
self._listenerId = aggregator:AddListener(function()
self:_updateValue()
end)
end
self:_updateValue()
end
function StatsAnnotatedGraphClass:_recursiveGetAxisMax(axisMax, max)
local axisMin = 0.1 * axisMax
if (max < axisMin) then
return self:_recursiveGetAxisMax(axisMin, max)
elseif (max >= axisMax) then
return self:_recursiveGetAxisMax(10 * axisMax, max)
else
return axisMax
end
end
function StatsAnnotatedGraphClass:_updateValue()
local values = {}
if self._aggregator ~= nil then
values = self._aggregator:GetValues()
else
values = {}
end
self:SetValues(values)
end
return StatsAnnotatedGraphClass
32 changes: 7 additions & 25 deletions CoreScriptsRoot/Modules/Stats/StatsButton.lua
Expand Up @@ -42,6 +42,11 @@ function StatsButtonClass.new(statsDisplayType)
TextPanelSize,
TextPanelPosition)
self._graph = StatsAnnotatedGraphClass.new(false)
self._graph:PlaceInParent(self._button,
GraphSize,
GraphPosition)
self._isSelected = false
self:_updateColor();
Expand Down Expand Up @@ -74,31 +79,8 @@ function StatsButtonClass:SetParent(parent)
end
function StatsButtonClass:SetStatsAggregator(aggregator)
if (self._aggregator) then
self._aggregator:RemoveListener(self._listenerId)
self._listenerId = nil
self._aggregator = nil
end
self._aggregator = aggregator
if (self._aggregator ~= nil) then
self._listenerId = aggregator:AddListener(function()
self:_updateValue()
end)
end
self:_updateValue()
self._textPanel:SetStatsAggregator(aggregator)
self._graph:SetStatsAggregator(aggregator)
end
function StatsButtonClass:_updateValue()
local value
if self._aggregator ~= nil then
value = self._aggregator:GetLatestValue()
else
value = 0
end
self._textPanel:SetValue(value)
end
return StatsButtonClass
31 changes: 30 additions & 1 deletion CoreScriptsRoot/Modules/Stats/StatsTextPanel.lua
Expand Up @@ -108,8 +108,37 @@ end
function StatsTextPanelClass:SetValue(value)
-- FIXME(dbanks)
-- Transform to appropriate units and format
-- Format based on stat type.
self._valueLabel.Text = string.format("%.4f", value)
end
function StatsTextPanelClass:SetStatsAggregator(aggregator)
if (self._aggregator) then
self._aggregator:RemoveListener(self._listenerId)
self._listenerId = nil
self._aggregator = nil
end
self._aggregator = aggregator
if (self._aggregator ~= nil) then
self._listenerId = aggregator:AddListener(function()
self:_updateValue()
end)
end
self:_updateValue()
end
function StatsTextPanelClass:_updateValue()
local value
if self._aggregator ~= nil then
value = self._aggregator:GetLatestValue()
else
value = 0
end
self:SetValue(value)
end
return StatsTextPanelClass

0 comments on commit 74f2217

Please sign in to comment.