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

Add data point labels #161

Merged
merged 3 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ chart-simple/dist/
chart-tests/dist/
chart-tests/output/
dist-newstyle/

test*.png
test*.svg
misc*.png
misc*.svg
parametric.png
parametric.svg
sparklines.png
sparklines.svg
17 changes: 13 additions & 4 deletions chart-tests/tests/Test4.hs
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
module Test4 where

import Graphics.Rendering.Chart
import Control.Lens
import Data.Colour
import Data.Colour.Names
import Control.Lens
import Data.Default.Class
import Graphics.Rendering.Chart

import Utils

chart :: Bool -> Bool -> Renderable (LayoutPick Double Double Double)
chart xrev yrev = layoutToRenderable layout
where

pointValues = [ (x, 10**x) | x <- [0.5,1,1.5,2,2.5 :: Double] ]

points = plot_points_style .~ filledCircles 3 (opaque red)
$ plot_points_values .~ [ (x, 10**x) | x <- [0.5,1,1.5,2,2.5 :: Double] ]
$ plot_points_values .~ pointValues
$ plot_points_title .~ "values"
$ def

labels = plot_annotation_hanchor .~ HTA_Left
$ plot_annotation_vanchor .~ VTA_Top
$ plot_annotation_offset .~ Vector 10 10
$ plot_annotation_values .~ [ (x, y, show (x, y)) | (x, y) <- pointValues ]
$ def


lines = plot_lines_values .~ [ [(x, 10**x) | x <- [0,3]] ]
$ plot_lines_title .~ "values"
$ def
Expand All @@ -27,7 +36,7 @@ chart xrev yrev = layoutToRenderable layout
$ layout_y_axis . laxis_generate .~ autoScaledLogAxis def
$ layout_y_axis . laxis_title .~ "vertical"
$ layout_y_axis . laxis_reverse .~ yrev
$ layout_plots .~ [ toPlot points, toPlot lines ]
$ layout_plots .~ [ toPlot points, toPlot lines, toPlot labels ]
$ def

-- main = main' "test4" (chart False False)
Expand Down
12 changes: 7 additions & 5 deletions chart-tests/tests/Test9.hs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
module Test9 where
module Test9 where

import Graphics.Rendering.Chart
import Control.Arrow
import Control.Lens
import Data.Colour
import Data.Colour.Names
import Control.Lens
import Data.Default.Class
import Graphics.Rendering.Chart

import Utils

chart :: Bool -> Renderable (LayoutPick PlotIndex Double Double)
chart borders = layoutToRenderable layout
where
layout =
layout =
layout_title .~ "Sample Bars" ++ btitle
$ layout_title_style . font_size .~ 10
$ layout_x_axis . laxis_generate .~ autoIndexAxis alabels
Expand All @@ -21,12 +22,13 @@ chart borders = layoutToRenderable layout
$ def :: Layout PlotIndex Double

bars2 = plot_bars_titles .~ ["Cash","Equity"]
$ plot_bars_values .~ addIndexes [[20,45],[45,30],[30,20],[70,25]]
$ plot_bars_values_with_labels .~ addLabels (addIndexes vals)
$ plot_bars_style .~ BarsClustered
$ plot_bars_spacing .~ BarsFixGap 30 5
$ plot_bars_item_styles .~ map mkstyle (cycle defaultColorSeq)
$ def

vals = [[20,45],[45,30],[30,20],[70,25]]
alabels = [ "Jun", "Jul", "Aug", "Sep", "Oct" ]

btitle = if borders then "" else " (no borders)"
Expand Down
32 changes: 20 additions & 12 deletions chart-tests/tests/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ import Graphics.Rendering.Chart
import Graphics.Rendering.Chart.Drawing
import Graphics.Rendering.Chart.Grid

import System.Time
import System.Random
import Data.Time.LocalTime
import Control.Lens
import Control.Monad
import Data.Char (chr)
import Data.Colour
import Data.Colour.Names
import Data.Colour.SRGB
import Data.List(sort,nub,scanl1)
import Data.Default.Class
import Data.List(sort,nub,scanl1)
import qualified Data.Map as Map
import Control.Monad
import Data.Time.LocalTime
import Prices
import System.Random
import System.Time
import qualified Test1
import qualified Test14
import qualified Test14a
import qualified Test15
import qualified Test17
import qualified Test19
import qualified Test2
import qualified Test3
import qualified Test4
import qualified Test5
import qualified Test7
import qualified Test8
import qualified Test9
import qualified Test14
import qualified Test14a
import qualified Test15
import qualified Test17
import qualified Test19
import qualified TestParametric
import qualified TestSparkLines

Expand Down Expand Up @@ -166,14 +166,22 @@ test9 alignment lw = fillBackground fwhite $ (gridToRenderable t)
$ layout_plots .~ [ plotBars bars ]
$ def :: Layout PlotIndex Double

vals1 = [[20],[45],[30],[70]]
bars1 = plot_bars_titles .~ ["Cash"]
$ plot_bars_values .~ addIndexes [[20],[45],[30],[70]]
$ plot_bars_values_with_labels .~ addLabels (addIndexes vals1)
$ plot_bars_alignment .~ alignment
$ plot_bars_label_bar_hanchor .~ BHA_Centre
$ plot_bars_label_bar_vanchor .~ BVA_Centre
$ plot_bars_label_text_hanchor .~ HTA_Centre
$ def

vals2 = [[20,45],[45,30],[30,20],[70,25]]
bars2 = plot_bars_titles .~ ["Cash","Equity"]
$ plot_bars_values .~ addIndexes [[20,45],[45,30],[30,20],[70,25]]
$ plot_bars_values_with_labels .~ addLabels (addIndexes vals2)
$ plot_bars_alignment .~ alignment
$ plot_bars_label_bar_hanchor .~ BHA_Centre
$ plot_bars_label_bar_vanchor .~ BVA_Centre
$ plot_bars_label_text_hanchor .~ HTA_Centre
$ def

-------------------------------------------------------------------------------
Expand Down
14 changes: 9 additions & 5 deletions chart/Graphics/Rendering/Chart/Plot/Annotation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ module Graphics.Rendering.Chart.Plot.Annotation(
plot_annotation_vanchor,
plot_annotation_angle,
plot_annotation_style,
plot_annotation_values
plot_annotation_values,
plot_annotation_offset
) where

import Control.Lens
import Graphics.Rendering.Chart.Geometry
import Data.Default.Class
import Graphics.Rendering.Chart.Drawing
import Graphics.Rendering.Chart.Geometry
import Graphics.Rendering.Chart.Plot.Types
import Data.Default.Class

-- | Value for describing a series of text annotations
-- to be placed at arbitrary points on the graph. Annotations
Expand All @@ -34,7 +35,8 @@ data PlotAnnotation x y = PlotAnnotation {
_plot_annotation_angle :: Double,
-- ^ Angle, in degrees, to rotate the annotation about the anchor point.
_plot_annotation_style :: FontStyle,
_plot_annotation_values :: [(x,y,String)]
_plot_annotation_values :: [(x,y,String)],
_plot_annotation_offset :: Vector
}


Expand All @@ -56,8 +58,9 @@ renderAnnotation p pMap = withFontStyle style $
values = _plot_annotation_values p
angle = _plot_annotation_angle p
style = _plot_annotation_style p
offset = _plot_annotation_offset p
drawOne (x,y,s) = drawTextsR hta vta angle point s
where point = pMap (LValue x, LValue y)
where point = pMap (LValue x, LValue y) `pvadd` offset

instance Default (PlotAnnotation x y) where
def = PlotAnnotation
Expand All @@ -66,6 +69,7 @@ instance Default (PlotAnnotation x y) where
, _plot_annotation_angle = 0
, _plot_annotation_style = def
, _plot_annotation_values = []
, _plot_annotation_offset = Vector 0 0
}

$( makeLenses ''PlotAnnotation )
Loading