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

Bar charts enhancements #253

Merged
merged 15 commits into from
Jun 3, 2023
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ 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
.ghc.environment.*
cabal.project.local
1 change: 1 addition & 0 deletions chart-tests/Chart-tests.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Executable chart-harness
Test15
Test17
Test19
Test20
TestParametric
Tests
TestSparkLines
Expand Down
6 changes: 3 additions & 3 deletions chart-tests/tests/Test15.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Test15 where
module Test15 where

import Graphics.Rendering.Chart
import Data.Colour
Expand All @@ -10,14 +10,14 @@ import Utils

chart lo lp = layoutToRenderable layout
where
layout =
layout =
layout_title .~ "Legend Test"
$ layout_title_style . font_size .~ 10
$ layout_x_axis . laxis_generate .~ autoIndexAxis alabels
$ layout_y_axis . laxis_override .~ axisGridHide
$ layout_left_axis_visibility . axis_show_ticks .~ False
$ layout_plots .~ [ plotBars bars2 ]
$ layout_legend .~ Just lstyle
$ layout_legend ?~ lstyle
$ def :: Layout PlotIndex Double

bars2 = plot_bars_titles .~ ["A","B","C","D","E","F","G","H","I","J"]
Expand Down
97 changes: 97 additions & 0 deletions chart-tests/tests/Test20.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module Test20 where

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

import Utils
import Test.QuickCheck (Result(Failure))

dat :: [[Double]]
dat = [ [66.22192174833207, 50.85424119528999]
, [18.507408294149144, 29.94826136042779]
, [271.34564215397256, 482.0060747629345]
, [0.33308595521927825, 0.25399999403605966]
, [8.418936013584233, 5.144029932796894]
, [67.19053209933879, 62.55976275368557]
, [0.20500418021076805, 0.7397264674905577]
, [93.52395426151023, 96.01214737959424]
, [486.5332691543843, 333.4124444949074]
, [151.27192832718126, 317.4545157262858]
, [42.246424931587924, 56.89305428360467]
, [8.812241283978576, 3.0449891300138225]
, [41.763424901388305, 23.924663084356638]
, [50.77174917622324, 91.54897286917759]
, [0.743806669182276, 0.14540395376496337]
, [3.152519452338129, 3.76835741734118]
, [55.75637240640731, 66.5350935501769]
, [0.9546959351374888, 0.6673023316342984]
, [58.81299411301322, 41.6766677808916]
, [7.496126744615885, 1.4640493059283133]
, [1.21316661759154927, 2.547769800092524232]
, [0.97655290356243897, 0.90002335523825158]
, [0.74607620980626329, 0.618295299567391062]
, [33.3228047562302481, 58.28982568453356767]
, [4.73981774972079753, 5.092283613261595367]
]

chart :: Renderable (LayoutPick LogValue PlotIndex PlotIndex)
chart = layoutToRenderable layout
where
layout =
-- title + legend
layout_title .~ "Sample Horizontal Log Bars"
$ layout_title_style . font_size .~ 10
$ layout_legend . _Just . legend_position .~ LegendAbove
$ layout_legend . _Just . legend_margin .~ 10

-- X
$ layout_x_axis . laxis_style . axis_grid_style .~ solidLine 0.15 (opaque lightgrey)
$ layout_x_axis . laxis_style . axis_label_gap .~ 3
$ layout_x_axis . laxis_override .~ axisGridAtBigTicks
$ layout_top_axis_visibility . axis_show_line .~ True
$ layout_top_axis_visibility . axis_show_ticks .~ True
$ layout_top_axis_visibility . axis_show_labels .~ True
$ layout_bottom_axis_visibility . axis_show_ticks .~ True

-- Y
$ layout_y_axis . laxis_generate .~ autoIndexAxis' True alabels
$ layout_y_axis . laxis_override .~ axisGridAtTicks
$ layout_y_axis . laxis_reverse .~ True
$ layout_y_axis . laxis_style . axis_grid_style .~ solidLine 0.3 (opaque lightgrey)
$ layout_left_axis_visibility . axis_show_ticks .~ False

-- data
$ layout_plots .~ [ plotHBars bars2 ]

$ def :: Layout LogValue PlotIndex

bars2 = plot_bars_values_with_labels .~ addIndexes dat'
$ plot_bars_titles .~ ["","after","before"]
$ plot_bars_style .~ BarsStacked
$ plot_bars_spacing .~ BarsFixGap 12 5
$ plot_bars_item_styles .~ map (\c -> (solidFillStyle $ withOpacity c 0.7, Nothing)) [grey, red, green]
$ plot_bars_label_bar_hanchor .~ BHA_Right
$ plot_bars_label_bar_vanchor .~ BVA_Centre
$ plot_bars_label_text_hanchor .~ HTA_Left
$ plot_bars_label_text_vanchor .~ VTA_Centre
$ plot_bars_label_offset .~ Vector 3 0
$ plot_bars_label_style . font_slant .~ FontSlantItalic
$ def

dat' = map (\[a,b] -> [ (LogValue (min a b), "")
, if a < b then
let v = b - a in
(LogValue v, printf "%0.2f" v)
else (LogValue 0, "")
, if b < a then
let v = a - b in
(LogValue v, printf "%0.2f" (-v))
else (LogValue 0, "")
]) dat

alabels = map (\n -> "longDataPointName" ++ show n) $ take (length dat) [1..]

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