Skip to content

Commit 48d10bb

Browse files
Offer a proper way to set the colour of plot objects.
1 parent 72b2d7e commit 48d10bb

File tree

1 file changed

+41
-27
lines changed
  • Graphics/Dynamic/Plot

1 file changed

+41
-27
lines changed

Graphics/Dynamic/Plot/R2.hs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,22 @@ module Graphics.Dynamic.Plot.R2 (
4343
, PlainGraphicsR2
4444
, shapePlot
4545
, diagramPlot
46+
-- * Plot-object attributes
47+
-- ** Colour
48+
, tint, autoTint
4649
-- ** Legend captions
4750
, legendName
51+
-- * Viewport
4852
-- ** View selection
4953
, xInterval, yInterval, forceXRange, forceYRange
50-
-- ** View dependance
54+
-- ** View dependence
5155
, ViewXCenter(..), ViewYCenter(..), ViewWidth(..), ViewHeight(..)
5256
, ViewXResolution(..), ViewYResolution(..)
53-
-- ** Auxiliary plot objects
57+
-- * Auxiliary plot objects
5458
, dynamicAxes, noDynamicAxes
55-
-- ** Plot type
59+
-- * The plot type
5660
, DynamicPlottable
5761
, tweakPrerendered
58-
-- ** Legacy
59-
, PlainGraphics(..)
6062
) where
6163

6264
import Graphics.Dynamic.Plot.Colour
@@ -151,7 +153,7 @@ type GraphWindowSpec = GraphWindowSpecR2
151153

152154
data DynamicPlottable = DynamicPlottable {
153155
_relevantRange_x, _relevantRange_y :: RangeRequest R
154-
, _isTintableMonochromic :: Bool
156+
, _inherentColours :: [DCol.Colour ]
155157
, _occlusiveness :: Double
156158
-- ^ How surface-occupying the plot is.
157159
-- Use positive values for opaque 2D plots that would tend to obscure
@@ -211,7 +213,7 @@ instance Plottable PlainGraphics where
211213
-- Use 'diagramPlot' instead, if you want to view the diagram as-is.
212214
shapePlot :: PlainGraphicsR2 -> DynamicPlottable
213215
shapePlot d = diagramPlot d
214-
& isTintableMonochromic .~ True
216+
& inherentColours .~ []
215217
& axesNecessity .~ 0
216218

217219
-- | Plot a generic 'Dia.Diagram'.
@@ -222,7 +224,7 @@ diagramPlot d = plot $ PlainGraphics d
222224

223225
instance Plottable (R-->R) where
224226
plot f = def & relevantRange_y .~ OtherDimDependantRange yRangef
225-
& isTintableMonochromic .~ True
227+
& autoTint
226228
& axesNecessity .~ 1
227229
& dynamicPlot .~ plot
228230
where yRangef (Option Nothing) = Option Nothing
@@ -255,7 +257,7 @@ instance Plottable (R-->R) where
255257

256258
instance Plottable (R-->(R,R)) where
257259
plot f = def & relevantRange_y .~ mempty
258-
& isTintableMonochromic .~ True
260+
& autoTint
259261
& axesNecessity .~ 1
260262
& dynamicPlot .~ plot
261263
where plot gs@(GraphWindowSpecR2{..}) = curves `deepseq`
@@ -297,7 +299,7 @@ instance Plottable (R-.^>R) where
297299
= def
298300
& relevantRange_x .~ atLeastInterval (Interval x₀ xr)
299301
& relevantRange_y .~ otherDimDependence (rPCMLinFitRange rPCM)
300-
& isTintableMonochromic .~ True
302+
& autoTint
301303
& axesNecessity .~ 1
302304
& dynamicPlot .~ plot
303305
where
@@ -336,7 +338,7 @@ instance Plottable (RecursiveSamples Int P2 (DevBoxes P2)) where
336338
= def
337339
& relevantRange_x .~ atLeastInterval xRange
338340
& relevantRange_y .~ atLeastInterval yRange
339-
& isTintableMonochromic .~ True
341+
& autoTint
340342
& axesNecessity .~ 1
341343
& dynamicPlot .~ plot
342344
where plot (GraphWindowSpecR2{..}) = mkPlot
@@ -394,13 +396,13 @@ tracePlot = plot . recursiveSamples . map ((,()) . Dia.p2)
394396
-- there is no &#201c;statistic optimisation&#201d; as in 'tracePlot'.
395397
lineSegPlot :: [(Double, Double)] -> DynamicPlottable
396398
lineSegPlot ps'
397-
| null ps = mempty & isTintableMonochromic .~ True
399+
| null ps = mempty & autoTint
398400
| otherwise = def
399401
& relevantRange_x .~ atLeastInterval'
400402
( foldMap (pure . spInterval . fst) (concat ps) )
401403
& relevantRange_y .~ atLeastInterval'
402404
( foldMap (pure . spInterval . snd) (concat ps) )
403-
& isTintableMonochromic .~ True
405+
& autoTint
404406
& axesNecessity .~ 1
405407
& dynamicPlot .~ plot
406408
where plot (GraphWindowSpecR2{..}) = mkPlot (foldMap trace ps)
@@ -472,7 +474,7 @@ instance Plottable (Shade P2) where
472474
plot shade = def
473475
& relevantRange_x .~ atLeastInterval xRange
474476
& relevantRange_y .~ atLeastInterval yRange
475-
& isTintableMonochromic .~ True
477+
& autoTint
476478
& axesNecessity .~ 1
477479
& dynamicPlot .~ plot
478480
where plot _ = mkPlot $ foldMap axLine eigVs
@@ -486,7 +488,7 @@ instance Plottable (Shade (R,R)) where
486488

487489
instance Plottable (Shade' (R,R)) where
488490
plot shade = def
489-
& isTintableMonochromic .~ True
491+
& autoTint
490492
& axesNecessity .~ 1
491493
& dynamicPlot .~ plot
492494
where plot _ = mkPlot $ Dia.circle 1
@@ -518,7 +520,7 @@ instance Plottable (Shaded ℝ ℝ) where
518520
plot tr | length trivs' >= 2
519521
= def & relevantRange_x .~ atLeastInterval (Interval xmin xmax)
520522
& relevantRange_y .~ atLeastInterval (Interval ymin ymax)
521-
& isTintableMonochromic .~ True
523+
& autoTint
522524
& axesNecessity .~ 1
523525
& dynamicPlot .~ plot
524526
where plot grWS@(GraphWindowSpecR2{..}) = mkPlot $
@@ -554,7 +556,7 @@ instance Plottable (PointsWeb ℝ (Shade' ℝ)) where
554556
plot web | length locals >= 2
555557
= def & relevantRange_x .~ atLeastInterval (Interval xmin xmax)
556558
& relevantRange_y .~ atLeastInterval (Interval ymin ymax)
557-
& isTintableMonochromic .~ True
559+
& autoTint
558560
& axesNecessity .~ 1
559561
& dynamicPlot .~ plot
560562
where plot grWS@(GraphWindowSpecR2{..}) = mkPlot $
@@ -615,7 +617,7 @@ instance Plottable (SimpleTree P2) where
615617
= def
616618
& relevantRange_x .~ atLeastInterval xRange
617619
& relevantRange_y .~ atLeastInterval yRange
618-
& isTintableMonochromic .~ True
620+
& autoTint
619621
& axesNecessity .~ 1
620622
& dynamicPlot .~ plot
621623
where plot _ = mkPlot $ go 4 ctr (treeBranches root)
@@ -689,9 +691,9 @@ instance Semigroup DynamicPlottable where
689691
DynamicPlottable rx₁ ry₁ tm₁ oc₁ ax₁ le₁ dp₁
690692
<> DynamicPlottable rx₂ ry₂ tm₂ oc₂ ax₂ le₂ dp₂
691693
= DynamicPlottable
692-
(rx₁<>rx₂) (ry₁<>ry₂) (tm₁||tm₂) (oc₁+oc₂) (ax₁+ax₂) (le₁++le₂) (dp₁<>dp₂)
694+
(rx₁<>rx₂) (ry₁<>ry₂) (tm₁++tm₂) (oc₁+oc₂) (ax₁+ax₂) (le₁++le₂) (dp₁<>dp₂)
693695
instance Monoid DynamicPlottable where
694-
mempty = DynamicPlottable mempty mempty False 0 0 [] mempty
696+
mempty = DynamicPlottable mempty mempty [] 0 0 [] mempty
695697
mappend = (<>)
696698
instance Default DynamicPlottable where def = mempty
697699

@@ -703,9 +705,21 @@ data GraphViewState = GraphViewState {
703705

704706

705707

708+
-- | Set the caption for this plot object that should appear in the
709+
-- plot legend.
706710
legendName :: String -> DynamicPlottable -> DynamicPlottable
707711
legendName n = legendEntries %~ (LegendEntry (PlainText n) mempty :)
708712

713+
-- | Colour this plot object in a fixed shade.
714+
tint :: DCol.Colour -> DynamicPlottable -> DynamicPlottable
715+
tint col = inherentColours .~ [col]
716+
>>> dynamicPlot %~ fmap (getPlot %~ Dia.lc col . Dia.fc col)
717+
718+
-- | Allow the object to be automatically assigned a colour that's otherwise
719+
-- unused in the plot. (This is the default for most plot objects.)
720+
autoTint :: DynamicPlottable -> DynamicPlottable
721+
autoTint = inherentColours .~ []
722+
709723

710724
instance (Ord r) => Semigroup (RangeRequest r) where
711725
MustBeThisRange r <> _ = MustBeThisRange r
@@ -782,7 +796,7 @@ plotWindow graphs' = do
782796
, graphColor = cl }
783797
) : ) $ assignGrViews gs cs' (axn + _axesNecessity)
784798
where (cl, cs')
785-
| _isTintableMonochromic = (Just $ defColourScheme c, cs)
799+
| null _inherentColours = (Just $ defColourScheme c, cs)
786800
| otherwise = (Nothing, c:cs)
787801
assignGrViews [] _ axesNeed
788802
| axesNeed > 0 = assignGrViews [dynamicAxes] [grey] (-1)
@@ -1053,7 +1067,7 @@ scrollZoomStrength = 1/20
10531067
continFnPlot :: (Double -> Double) -> DynamicPlottable
10541068
continFnPlot f = def
10551069
& relevantRange_y .~ otherDimDependence yRangef
1056-
& isTintableMonochromic .~ True
1070+
& autoTint
10571071
& axesNecessity .~ 1
10581072
& dynamicPlot .~ plot
10591073
where yRangef = onInterval $ \(l, r) -> ((!%0.1) &&& (!%0.9)) . sort . pruneOutlyers
@@ -1114,7 +1128,7 @@ scrutiniseDiffability f = plot [{-plot fd, -}dframe 0.2, dframe 0.02]
11141128
fd = alg f
11151129
fscrut = analyseLocalBehaviour fd
11161130
dframe rfh = def
1117-
& isTintableMonochromic .~ True
1131+
& autoTint
11181132
& dynamicPlot .~ mkFrame
11191133
where mkFrame (GraphWindowSpecR2{..}) = case fscrut xm of
11201134
Option (Just ((ym,y'm), δOδx²))
@@ -1275,7 +1289,7 @@ newtype ViewXCenter = ViewXCenter { getViewXCenter :: Double }
12751289
instance (Plottable p) => Plottable (ViewXCenter -> p) where
12761290
plot f = def & relevantRange_y .~ OtherDimDependantRange
12771291
(\g -> deescalate relevantRange_y g . plot . f . cxI =<< g)
1278-
& isTintableMonochromic .~ fcxVoid^.isTintableMonochromic
1292+
& inherentColours .~ fcxVoid^.inherentColours
12791293
& axesNecessity .~ fcxVoid^.axesNecessity
12801294
& dynamicPlot .~ \g -> _dynamicPlot (plot . f $ cx g) g
12811295
where cx (GraphWindowSpecR2{..}) = ViewXCenter $ (lBound+rBound)/2
@@ -1288,7 +1302,7 @@ newtype ViewYCenter = ViewYCenter { getViewYCenter :: Double }
12881302
instance (Plottable p) => Plottable (ViewYCenter -> p) where
12891303
plot f = def & relevantRange_x .~ OtherDimDependantRange
12901304
(\g -> deescalate relevantRange_x g . plot . f . cyI =<< g)
1291-
& isTintableMonochromic .~ fcyVoid^.isTintableMonochromic
1305+
& inherentColours .~ fcyVoid^.inherentColours
12921306
& axesNecessity .~ fcyVoid^.axesNecessity
12931307
& dynamicPlot .~ \g -> _dynamicPlot (plot . f $ cy g) g
12941308
where cy (GraphWindowSpecR2{..}) = ViewYCenter $ (bBound+tBound)/2
@@ -1301,7 +1315,7 @@ newtype ViewWidth = ViewWidth { getViewWidth :: Double }
13011315
instance (Plottable p) => Plottable (ViewWidth -> p) where
13021316
plot f = def & relevantRange_y .~ OtherDimDependantRange
13031317
(\g -> deescalate relevantRange_y g . plot . f . wI =<< g)
1304-
& isTintableMonochromic .~ fwVoid^.isTintableMonochromic
1318+
& inherentColours .~ fwVoid^.inherentColours
13051319
& axesNecessity .~ fwVoid^.axesNecessity
13061320
& dynamicPlot .~ \g -> _dynamicPlot (plot . f $ w g) g
13071321
where w (GraphWindowSpecR2{..}) = ViewWidth $ rBound - lBound
@@ -1314,7 +1328,7 @@ newtype ViewHeight = ViewHeight { getViewHeight :: Double }
13141328
instance (Plottable p) => Plottable (ViewHeight -> p) where
13151329
plot f = def & relevantRange_x .~ OtherDimDependantRange
13161330
(\g -> deescalate relevantRange_x g . plot . f . hI =<< g)
1317-
& isTintableMonochromic .~ fhVoid^.isTintableMonochromic
1331+
& inherentColours .~ fhVoid^.inherentColours
13181332
& axesNecessity .~ fhVoid^.axesNecessity
13191333
& dynamicPlot .~ \g -> _dynamicPlot (plot . f $ h g) g
13201334
where h (GraphWindowSpecR2{..}) = ViewHeight $ tBound - bBound

0 commit comments

Comments
 (0)