on 18f736a help sign in
91 lines 3.5 kB history raw
1module ProjectM36.Relation.Show.Gnuplot where
2import ProjectM36.Base
3import ProjectM36.Relation
4import ProjectM36.Tuple
5import ProjectM36.AtomFunctions.Primitive
6import qualified ProjectM36.Attribute as A
7import qualified Data.Vector as V
9import qualified Graphics.Gnuplot.Plot.ThreeDimensional as Plot3D
10import qualified Graphics.Gnuplot.Graph.ThreeDimensional as Graph3D
12import qualified Graphics.Gnuplot.Plot.TwoDimensional as Plot2D
13import qualified Graphics.Gnuplot.Graph.TwoDimensional as Graph2D
15import qualified Graphics.Gnuplot.Advanced as GPA
17--this module support plotting relations containing integer attributes with arity 1,2, or 3 only
18--nested relations?
20data PlotError = InvalidAttributeCountError |
21 InvalidAttributeTypeError
22 deriving (Show)
24--plotRelation :: Relation -> Either PlotError
26intFromAtomIndex :: Int -> RelationTuple -> Int --warning- clips or overflows Integer -> Int
27intFromAtomIndex index tup = fromIntegral $ castInt $ tupleAtoms tup V.! index
29graph1DRelation :: Relation -> Plot2D.T Int Int
30graph1DRelation rel = Plot2D.list Graph2D.listPoints $ points1DRelation rel
32points1DRelation :: Relation -> [Int]
33points1DRelation = relFold folder []
34 where
35 folder tup acc = intFromAtomIndex 0 tup : acc
37graph2DRelation :: Relation -> Plot2D.T Int Int
38graph2DRelation rel = Plot2D.list Graph2D.points (points2DRelation rel)
40points2DRelation :: Relation -> [(Int, Int)]
41points2DRelation = relFold folder []
42 where
43 folder tup acc = (intFromAtomIndex 0 tup, intFromAtomIndex 1 tup) : acc
45graph3DRelation :: Relation -> Plot3D.T Int Int Int
46graph3DRelation rel =
47 Plot3D.cloud Graph3D.points $ points3DRelation rel
49points3DRelation :: Relation -> [(Int, Int, Int)]
50points3DRelation = relFold folder []
51 where
52 folder tup acc = (intFromAtomIndex 0 tup, intFromAtomIndex 1 tup, intFromAtomIndex 2 tup) : acc
54{-
55sample1DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType]) [[IntAtom 2], [IntAtom 3]] of
56 Right rel -> rel
57 Left _ -> undefined
59sample2DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType]) [[IntAtom 2, IntAtom 3],
60 [IntAtom 2, IntAtom 4]] of
61 Right rel -> rel
62 Left _ -> undefined
64sample3DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType, Attribute "z" IntAtomType]) [[IntAtom 2, IntAtom 3, IntAtom 3],
65 [IntAtom 2, IntAtom 4, IntAtom 4]] of
66 Right rel -> rel
67 Left _ -> undefined
69-}
71plotRelation :: Relation -> IO (Maybe PlotError)
72plotRelation rel = let attrTypes = V.replicate (arity rel) IntAtomType in
73 if attrTypes /= A.atomTypes (attributes rel) then
74 return $ Just InvalidAttributeTypeError
75 else
76 case arity rel of
77 1 -> GPA.plotDefault (graph1DRelation rel) >> return Nothing
78 2 -> GPA.plotDefault (graph2DRelation rel) >> return Nothing
79 3 -> GPA.plotDefault (graph3DRelation rel) >> return Nothing
80 _ -> return $ Just InvalidAttributeCountError
83{-
84--PNG
85savePlottedRelation :: String -> Relation -> IO ()
86savePlottedRelation path rel = case plotRelation rel of
87 Left err -> putStrLn (show err)
88 Right (Left g2d) -> plot' [] (PNG path) g2d >> return ()
89 Right (Right g3d) -> plot' [] (PNG path) g3d >> return ()
90-}