Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export pieToGrid #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions chart/Graphics/Rendering/Chart/Plot/Pie.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module Graphics.Rendering.Chart.Plot.Pie(
PieLayout(..),
PieChart(..),
PieItem(..),


pieToGrid,
pieToRenderable,
pieChartToRenderable,

Expand Down Expand Up @@ -72,7 +73,7 @@ data PieChart = PieChart {
_pie_data :: [PieItem],
_pie_colors :: [AlphaColour Double],
_pie_label_style :: FontStyle,
_pie_label_line_style :: LineStyle,
_pie_label_line_style :: LineStyle,
_pie_start_angle :: Double

}
Expand All @@ -84,7 +85,7 @@ data PieItem = PieItem {
}

instance Default PieChart where
def = PieChart
def = PieChart
{ _pie_data = []
, _pie_colors = defaultColorSeq
, _pie_label_style = def
Expand All @@ -96,7 +97,7 @@ instance Default PieItem where
def = PieItem "" 0 0

instance Default PieLayout where
def = PieLayout
def = PieLayout
{ _pie_background = solidFillStyle $ opaque white
, _pie_title = ""
, _pie_title_style = def { _font_size = 15
Expand All @@ -118,14 +119,17 @@ instance ToRenderable PieChart where

pieToRenderable :: PieLayout -> Renderable (PickFn a)
pieToRenderable p = fillBackground (_pie_background p) (
gridToRenderable $ aboveN
[ tval $ addMargins (lm/2,0,0,0) (setPickFn nullPickFn title)
, weights (1,1) $ tval $ addMargins (lm,lm,lm,lm)
(pieChartToRenderable $ _pie_plot p)
] )
where
title = label (_pie_title_style p) HTA_Centre VTA_Top (_pie_title p)
lm = _pie_margin p
gridToRenderable $ pieToGrid p )

pieToGrid :: PieLayout -> Grid (Renderable (PickFn a))
pieToGrid p = aboveN
[ tval $ addMargins (lm/2,0,0,0) (setPickFn nullPickFn title)
, weights (1,1) $ tval $ addMargins (lm,lm,lm,lm)
(pieChartToRenderable $ _pie_plot p)
]
where
title = label (_pie_title_style p) HTA_Centre VTA_Top (_pie_title p)
lm = _pie_margin p

extraSpace :: PieChart -> BackendProgram (Double, Double)
extraSpace p = do
Expand Down Expand Up @@ -153,10 +157,10 @@ renderPie p (w,h) = do
foldM_ (paint center radius) (_pie_start_angle p)
(zip (_pie_colors p) content)
return nullPickFn

where
-- p1 = Point 0 0
-- p2 = Point w h
-- p1 = Point 0 0
-- p2 = Point w h
content = let total = sum (map _pitem_value (_pie_data p))
in [ pitem{_pitem_value=_pitem_value pitem/total}
| pitem <- _pie_data p ]
Expand All @@ -176,13 +180,13 @@ renderPie p (w,h) = do

where
pieLabel :: String -> Double -> Double -> BackendProgram ()
pieLabel name angle offset =
withFontStyle (_pie_label_style p) $
pieLabel name angle offset =
withFontStyle (_pie_label_style p) $
withLineStyle (_pie_label_line_style p) $ do
let p1 = ray angle (radius+label_rgap+label_rlength+offset)
p1a <- alignStrokePoint p1
(tw,_) <- textDimension name
let (offset',anchor) = if angle < 90 || angle > 270
let (offset',anchor) = if angle < 90 || angle > 270
then ((0+),HTA_Left)
else ((0-),HTA_Right)
p0 <- alignStrokePoint $ ray angle (radius + label_rgap+offset)
Expand All @@ -200,9 +204,9 @@ renderPie p (w,h) = do
<> lineTo' x y
<> close

withFillStyle (FillStyleSolid pColor) $
withFillStyle (FillStyleSolid pColor) $
fillPath path
withLineStyle (def { _line_color = withOpacity white 0.1 }) $
withLineStyle (def { _line_color = withOpacity white 0.1 }) $
strokePath path

ray :: Double -> Double -> Point
Expand Down