@@ -4,8 +4,11 @@ import ProjectM36.Base
44import ProjectM36.Relation
55import ProjectM36.Tuple
66import qualified ProjectM36.Attribute as A
7−import Graphics.EasyPlot
87import qualified Data.Vector as V
8+import qualified Graphics.Gnuplot.Simple as GP
9+import qualified Graphics.Gnuplot.Plot.ThreeDimensional as Plot3D
10+import qualified Graphics.Gnuplot.Graph.ThreeDimensional as Graph3D
11+import qualified Graphics.Gnuplot.Advanced as GPA
912
1013--this module support plotting relations containing integer attributes with arity 1,2, or 3 only
1114--nested relations?
@@ -19,80 +22,71 @@ data PlotError = InvalidAttributeCountError |
1922intFromAtomIndex :: Int -> RelationTuple -> Int
2023intFromAtomIndex index tup = (\(IntAtom i) -> i) $ (tupleAtoms tup) V.! index
2124
22−graph1DRelation :: Relation -> Graph2D Double Double
23−graph1DRelation rel = Data2D [Title "rel"] [] $ map (\i -> (fromIntegral i, 0.0)) (points1DRelation rel)
25+pointStyle :: GP.PlotStyle
26+pointStyle = GP.PlotStyle { GP.plotType = GP.Points,
27+ GP.lineSpec = GP.CustomStyle [GP.PointSize 2.0]}
28+
29+graph1DRelation :: Relation -> IO ()
30+graph1DRelation rel = GP.plotPathStyle [] pointStyle $ map (\i -> (i, 0)) (points1DRelation rel)
2431
2532points1DRelation :: Relation -> [Int]
2633points1DRelation rel = relFold folder [] rel
2734 where
2835 folder tup acc = intFromAtomIndex 0 tup : acc
2936
30−graph2DRelation :: Relation -> Graph2D Double Double
31−graph2DRelation rel = Data2D [Title "rel"] [Step 1.0] $ map (\(x,y) -> (fromIntegral x, fromIntegral y)) $ points2DRelation rel
37+graph2DRelation :: Relation -> IO ()
38+graph2DRelation rel = GP.plotPathStyle [] pointStyle $ points2DRelation rel
3239
3340points2DRelation :: Relation -> [(Int, Int)]
3441points2DRelation rel = relFold folder [] rel
3542 where
3643 folder tup acc = (intFromAtomIndex 0 tup, intFromAtomIndex 1 tup) : acc
3744
38−graph3DRelation :: Relation -> [Graph3D Double Double Double]
39−graph3DRelation rel = map lineGraph $ points3DRelation rel
40− where
41− lineGraph (x,y,z) = Data3D [Title "rel", Style Lines] [StepX 1.0, StepY 1.0] $ [(fromIntegral x, fromIntegral y, fromIntegral z),
42− (fromIntegral x, 0, 0)]
45+graph3DRelation :: Relation -> IO ()
46+graph3DRelation rel = do
47+ _ <- GPA.plotDefault $ Plot3D.cloud Graph3D.points $ points3DRelation rel
48+ return ()
4349
4450points3DRelation :: Relation -> [(Int, Int, Int)]
4551points3DRelation rel = relFold folder [] rel
4652 where
4753 folder tup acc = (intFromAtomIndex 0 tup, intFromAtomIndex 1 tup, intFromAtomIndex 2 tup) : acc
4854
55+{-
4956sample1DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType]) [[IntAtom 2], [IntAtom 3]] of
5057 Right rel -> rel
5158 Left _ -> undefined
5259
53−sample2dRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType]) [[IntAtom 2, IntAtom 3],
54− [IntAtom 2, IntAtom 4]] of
60+sample2DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType]) [[IntAtom 2, IntAtom 3],
61+ [IntAtom 2, IntAtom 4]] of
5562 Right rel -> rel
5663 Left _ -> undefined
5764
58−sample3dRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType, Attribute "z" IntAtomType]) [[IntAtom 2, IntAtom 3, IntAtom 3],
65+sample3DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType, Attribute "z" IntAtomType]) [[IntAtom 2, IntAtom 3, IntAtom 3],
5966 [IntAtom 2, IntAtom 4, IntAtom 4]] of
6067 Right rel -> rel
6168 Left _ -> undefined
62−
63−type G2D = Graph2D Double Double
64−type G3D = Graph3D Double Double Double
6569
66−plotRelation :: Relation -> Either PlotError (Either G2D [G3D])
70+-}
71+
72+plotRelation :: Relation -> IO (Maybe PlotError)
6773plotRelation rel = let attrTypes = V.replicate (arity rel) IntAtomType in
6874 if attrTypes /= A.atomTypes (attributes rel) then
69− Left InvalidAttributeTypeError
75+ return $ Just InvalidAttributeTypeError
7076 else
7177 case arity rel of
72− 1 -> Right $ Left $ graph1DRelation rel
73− 2 -> Right $ Left $ graph2DRelation rel
74− 3 -> Right $ Right $ graph3DRelation rel
75− _ -> Left InvalidAttributeCountError
78+ 1 -> graph1DRelation rel >> return Nothing
79+ 2 -> graph2DRelation rel >> return Nothing
80+ 3 -> graph3DRelation rel >> return Nothing
81+ _ -> return $ Just InvalidAttributeCountError
82+
7683
84+{-
7785--PNG
7886savePlottedRelation :: String -> Relation -> IO ()
7987savePlottedRelation path rel = case plotRelation rel of
8088 Left err -> putStrLn (show err)
8189 Right (Left g2d) -> plot' [] (PNG path) g2d >> return ()
8290 Right (Right g3d) -> plot' [] (PNG path) g3d >> return ()
83−
84−--X11
85−showPlottedRelation :: Relation -> IO ()
86−showPlottedRelation rel = case plotRelation rel of
87− Left err -> putStrLn (show err)
88− Right (Left g2d) -> plot' [] X11 g2d >> return ()
89− Right (Right g3d) -> plot' [] X11 g3d >> return ()
90−
91−
92−ezplot :: Relation -> IO ()
93−ezplot rel = do
94− case plotRelation rel of
95− Left err -> putStrLn $ show err
96− Right (Left g2d) -> plot' [Interactive] X11 g2d >> return ()
97− Right (Right g3d) -> plot' [Interactive] X11 g3d >> return ()
91+-}
9892