-- -- CSVファイルを読み出し、テーブルを表示する -- --グローバル変数宣言---------------------- Path = ".\\" --luaファイルを保存しているPath(相対 or 絶対) Fname = "luacsv.csv" -- HTML部定義 Html1 = "\n".. "\n".. "\n".. "\n".. "\n".. "Graph Disp Test\n".. "\n".. "\n".. "

Graph Disp Test" Html2 = "\n".. "
\n".. "\n".. "\n" SvgBgColor = "\n".. "\n".. "\n" ------------------------------------------ --文字の分解 ------------------------------------------ function split(str, d) local s = str local t = {} local p = "%s*(.-)%s*"..d.."%s*" local f = function(v) table.insert(t, v) end if s ~= nil then string.gsub(s, p, f) -- 最後の要素より前の要素を処理する f(string.gsub(s, p, "")) -- 最後の要素はカンマで区切られていないときにマッチしないので、その前の値を空白で置き換えてからマッチさせる end return t end ------------------------------------------ --CSVファイルを読み込みます ------------------------------------------ function readdata( filename ) local fp local msg local str local t={} local i = 1 local x = 0 local y = 0 local width = 0 local height = 0 local graph_height = 0 local graph_width = 0 local fill_color = "fill=\"#008d46\"" local draw_width = 640 local draw_height = 480 local draw_offset_x = 16 local draw_offset_y = 16 local scale_x = 1.0 local scale_y = 1.0 local font_size = 16 local graph_title = "XXX Task" local x_axis_title = "time" local y_axis_title = "size" --ファイルを開きます fp,msg = io.open( filename, "r") if( not(fp) )then print( "Can't open "..Path..Fname) return end --CSVデータを読み込みます -- 1回目:x,yの最大値を検索し、表示サイズを確定する while(true)do str = fp:read("*l") --1行読み込み if( str==nil )then break end --読込むデータが無ければ終了 str = string.gsub( str,"\r","" ) --改行コードを外す t = split( str, "," ) if( t[1] ~= "x" )then x = tonumber(t[1]) y = tonumber(t[2]) if (x > graph_width) then graph_width = x end if (y > graph_height) then graph_height = y end end end scale_x = (graph_width / draw_width) scale_y = (graph_height / draw_height) -- HTML前半部表示 print(Html1) print("") -- 背景色指定 print(SvgBgColor) -- 棒グラフを画像サイズに合わせて表示する (縮尺) print("") -- ファイルの先頭に戻す fp:seek("set") --CSVデータを読み込みます -- 2回目:x,y,width,heightをsvg形式で出力します i = 1 while(true)do str = fp:read("*l") --1行読み込み if( str==nil )then break end --読込むデータが無ければ終了 str = string.gsub( str,"\r","" ) --改行コードを外す t = split( str, "," ) if( t[1]~="x" )then if (i % 2 == 1) then x = tonumber(t[1]) y = tonumber(t[2]) else width = tonumber(t[1]) - x height = y y = graph_height - height print("") end i = i + 1 end end print("") -- 罫線を引く print("") print("") -- テキスト描画 print(""..graph_title.."") print(""..x_axis_title.." max = "..(graph_width).."") print(""..y_axis_title.." max = "..(graph_height).."") print("") -- HTML後半部表示 print(Html2) io.close(fp) end ------------------------------------------ --メインプログラム ------------------------------------------ function main() --CSVファイルを読み込みます readdata( Path..Fname ) end main()