Skip to content

Instantly share code, notes, and snippets.

@fredbogg fredbogg/DataSeries Secret forked from devilstower/DataSeries
Created May 19, 2012

Embed
What would you like to do?
TrendGraphics -- Codea Acceleration and Gravity by Mark
DataSeries = class()
function DataSeries:init(name, length, min, max, symbol, symbolsize, thick, clr)
    self.name = name
    self.symbol = symbol
    self.symbolsize = symbolsize
    self.nextpt = 0
    self.length = length
    self.lineclr = clr
    self.linethick = thick
    self.points = {}
    self.min = min
    self.max = max
    print(name, thick)
end
function DataSeries:addValue(y)
    self.nextpt = self.nextpt + 1
    if self.nextpt > self.length then
        self.nextpt = 1
    end
    self.points[self.nextpt] = vec2(i, y)
end
function DataSeries:draw(frame)
    local ox, oy, w, h, x, y, p
    stroke(self.lineclr)
    strokeWidth(self.linethick)
    w = frame.x2 - frame.x1
    h = frame.y2 - frame.y1
    dx = w / (self.length + 1)
    dy = h / (self.max - self.min)
    pushMatrix()
    translate(frame.x1, frame.y1)
    clip(frame.x1, frame.y1, w, h)
    ox = 0
    x = 0
   -- print(dx,dy)
    for i = self.nextpt + 1, self.length do
        x = x + dx
        p = self.points[i]
        if p ~= nil then
            y = (p.y - self.min) * dy
            if ox > 0 then
                line(ox,oy,x,y)
            end
            ox = x
            oy = y
            if self.symbol == 1 then
                noFill()
                ellipse(x, y, self.symbolsize)
            end
        end
    end
    for i = 1, self.nextpt do
        x = x + dx
        p = self.points[i]
        if p ~= nil then
            y = (p.y - self.min) * dy
            if ox > 0 then
                line(ox,oy,x,y)
            end
            ox = x
            oy = y
            if self.symbol == 1 then
                noFill()
                ellipse(x, y, self.symbolsize)
            end
        end
    end
    popMatrix()
    noClip()
end
-- ====================
-- Frame 
-- ver. 0.1
-- a simple rectangle to act as a base for controls
-- ====================
Frame = class()
function Frame:init(x1, y1, x2, y2)
    self.x1 = x1
    self.x2 = x2
    self.y1 = y1
    self.y2 = y2
end
function Frame:draw()
    pushStyle()
    rectMode(CORNERS)
    rect(self.x1, self.y1, self.x2, self.y2)
    popStyle()
end
function Frame:gloss()
    local i, t, r, y
    pushStyle()
    fill(255, 255, 255, 255)
    rectMode(CORNERS)
    rect(self.x1, self.y1, self.x2, self.y2)
    r = (self.y2 - self.y1) / 2
    for i = 1 , r do
        t = 255 - i 
        stroke(t, t, t, 255)
        y = (self.y1 + self.y2) / 2
        line(self.x1, y + i, self.x2, y + i)
        line(self.x1, y - i, self.x2, y - i)
    end
    popStyle()
end
function Frame:touched(touch)
    if touch.x >= self.x1 and touch.x <= self.x2 then
        if touch.y >= self.y1 and touch.y <= self.y2 then
            return true
        end
    end
    return false
end
function Frame:midx()
    return (self.x1 + self.x2) / 2
end
    
function Frame:midy()
    return (self.y1 + self.y2) / 2
end
-- By Mark
-- Tweaked by Fred for inclusion in wiki, upgraded to text()
supportedOrientations(LANDSCAPE_ANY)
-- 
function setup()
    displayMode(FULLSCREEN)
    w = WIDTH / 3
    g = TrendGraph(10, 170, WIDTH-10, 320, "Gravity")
    g:addSeries("X", 100, -1, 1, 0, 0, 2, color(255, 0, 0,255))
    g:addSeries("Y", 100, -1, 1, 0, 0, 2, color(0,128,0,255))
    g:addSeries("Z", 100, -1, 1, 0, 0, 2, color(0,0,255,255))
    gx = TrendGraph(10, 10, w-20, 160, "Gravity-X")
    gx:addSeries("X", 100, -1, 1, 0, 0, 2, color(255, 0, 0,255))
    gy = TrendGraph(w, 10, w*2-20, 160, "Gravity-Y")
    gy:addSeries("Y", 100, -1, 1, 0, 0, 2, color(0,128,0,255))
    gz = TrendGraph(w*2, 10, w*3-10, 160, "Gravity-Z")
    gz:addSeries("Z", 100, -1, 1, 0, 0, 2, color(0,0,255,255))
    
    a = TrendGraph(10, HEIGHT-160, WIDTH-10, HEIGHT-10, "Acceleration")
    a:addSeries("X", 100, -1, 1, 0, 0, 2, color(255, 0, 0,255))
    a:addSeries("Y", 100, -1, 1, 0, 0, 2, color(0,128,0,255))
    a:addSeries("Z", 100, -1, 1, 0, 0, 2, color(0,0,255,255))
    ax = TrendGraph(10, HEIGHT-320, w-20, HEIGHT-170, "Acc-X")
    ax:addSeries("X", 100, -1, 1, 0, 0, 2, color(255, 0, 0,255))
    ay = TrendGraph(w, HEIGHT-320, w*2-20, HEIGHT-170, "Acc-Y")
    ay:addSeries("Y", 100, -1, 1, 0, 0, 2, color(0,128,0,255))
    az = TrendGraph(w*2, HEIGHT-320, w*3-10, HEIGHT-170, "Acc-Z")
    az:addSeries("Z", 100, -1, 1, 0, 0, 2, color(0,0,255,255))
end
function draw()
    noSmooth()
    background(0, 0, 0)
    g:draw()
    gx:draw()
    gy:draw()
    gz:draw()
    a:draw()
    ax:draw()
    ay:draw()
    az:draw()
    g.series[1]:addValue(Gravity.x)
    g.series[2]:addValue(Gravity.y)
    g.series[3]:addValue(Gravity.z)
    gx.series[1]:addValue(Gravity.x)
    gy.series[1]:addValue(Gravity.y)
    gz.series[1]:addValue(Gravity.z)
    stroke(255, 192, 0, 255)
    line(10, HEIGHT/2, WIDTH-10, HEIGHT/2)
    a.series[1]:addValue(UserAcceleration.x)
    a.series[2]:addValue(UserAcceleration.y)
    a.series[3]:addValue(UserAcceleration.z)
    ax.series[1]:addValue(UserAcceleration.x)
    ay.series[1]:addValue(UserAcceleration.y)
    az.series[1]:addValue(UserAcceleration.z)
end
TrendGraph = class()
function TrendGraph:init(x1, y1, x2, y2, title)
    self.outer = Frame(x1, y1, x2, y2)
    self.inner = Frame(x1 + 20, y1 + 20, x2 - 10, y2 - 40)
    self.title = title
    self.series = {}
    self.xlabel = ""
    self.ylabel = ""
    
end
function TrendGraph:addSeries(name, len, min, max, sym, size, thick, clr)
    local i
    i = table.getn(self.series)
    self.series[i + 1] = DataSeries(name, len, min, max, sym, size, thick, clr)
end
function TrendGraph:draw(pts)
    local i, dx, dy, x, y, ox, oy
    pushMatrix()
    pushStyle()
    fill(215, 215, 215, 255)
    stroke(253, 3, 3, 255)
    self.outer:draw()
    --tint(0, 0, 0, 255)
    pushStyle()
    fill(24, 34, 222, 255)
    text(self.title,self.outer:midx() - string.len(self.title)*6, self.outer.y2 - 20)
    popStyle()
    for s, series in ipairs(self.series) do
        series:draw(self.inner, self.min, self.max)
    end
    popMatrix()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.