Creating a graph

I’m trying to create a graph, with dots and lines. Got the dots done, but can’t get the lines connecting them figured out.

local function CreateGraph()
	local Size = Graph.Dates.UIListLayout.Padding.Offset
	
	-- Create dots
	for i = 1, 7 do
		local NewDot = DotTemplate:Clone()
		NewDot.Name = "Dot"
		NewDot.Position = UDim2.new(0.5, 0, Rates[i] / MAX, 0)
		
		NewDot.Parent = Graph.Dates[i]
	end
	
	-- Create lines
	for i = 1, 7 do
		local Line1 = Graph.Dates:FindFirstChild(i)
		local Line2 = Graph.Dates:FindFirstChild(i + 1)
		if not Line1 or not Line2 then continue end
		
		local NewLine = LineTemplate:Clone()
		
		local XLength = Line1.Dot.AbsolutePosition.X - Line2.Dot.AbsolutePosition.X
		local YLength = Line1.Dot.AbsolutePosition.Y - Line2.Dot.AbsolutePosition.Y
		
		local Mag = (
			Vector2.new(Line1.Dot.AbsolutePosition.X, Line1.Dot.AbsolutePosition.Y) 
			- Vector2.new(Line2.Dot.AbsolutePosition.X, Line2.Dot.AbsolutePosition.Y)
		)
		local CenterPosition = (Line2.AbsolutePosition + Line1.AbsolutePosition) / 2
		NewLine.Rotation = math.atan2(YLength, XLength) * 180 / math.pi
		
		NewLine.Position = UDim2.new(
			0,
			CenterPosition.X,
			0,
			CenterPosition.Y
		)
		NewLine.Size = UDim2.fromOffset(Mag.Magnitude, 4)
		
		NewLine.Parent = Graph.Lines
	end
end

image
It ends up creating the lines on the bottom left of the screen
image

3 Likes

I’m no genius in Vector2 Frame Size and position but Here I’ve have given you a idea on how it’s going to work

local table = {}
local firstval = 0
local secondval = 1

for index = 1,7 do
firstval = firstval + 1
table.insert(table,firstposition)
secondval = secondval + 1
table.insert(table,secondposition)
drawgraph.Size = Vector2.new(1,(table[firstval] - table[secondval]).Magnitude)
end

You can align the CFrame just how you did for the one now

1 Like

In the CenterPosition line, did you mean to use Line2.Dot or Line1.Dot? Because that’s the only pattern I’m seeing that is broken, you’ve used Line.dot everywhere besides there.

1 Like