chart-svg-0.2.0: Charting library targetting SVGs.
Safe HaskellNone
LanguageHaskell2010

Chart.Examples

Description

Examples of chart construction.

Synopsis

Documentation

unitExample :: ChartSvg Source #

unit example

rectExample :: ChartSvg Source #

rect example

textExample :: ChartSvg Source #

text example

glyphsExample :: ChartSvg Source #

glyphs example

lineExample :: ChartSvg Source #

line example

barDataExample :: BarData Source #

Example data for Bar chart

barExample :: ChartSvg Source #

Bar chart example.

waveExample :: ChartSvg Source #

wave example

lglyphExample :: ChartSvg Source #

Labelled Glyphs

glinesExample :: ChartSvg Source #

Glyph + Lines

compoundExample :: ChartSvg Source #

mappend of lglyph and glines examples

boundTextBugExample :: ChartSvg Source #

An example of how bounding box calculations for text is broken.

labelExample :: ChartSvg Source #

label example.

surfaceExample Source #

Arguments

:: Text

Legend title

-> Point Int

Surface grid

-> Rect Double

Surface range

-> (Point Double -> Double)

Surface function

-> ChartSvg 

Create a chart across a surface using a function.

writeChartSvg "other/surface.svg" $ surfaceExample "rosenbrock" (Point 100 100) one (fst . first (-1.0 *) . second (-1.0 .*) . rosenbrock 1 10)

arrowgExample Source #

Arguments

:: Point Int

Surface grid

-> Rect Double

Surface range

-> (Point Double -> Point Double)

Surface gradient function

-> ChartSvg 

Create an arrow chart across a surface using a function.

Typically used to represent a gradient.

writeChartSvg "other/arrowg.svg" $ arrowgExample (Point 20 20) one (fst . first (-1.0 *) . second (-1.0 .*) . rosenbrock 1 10)

surfacegExample Source #

Arguments

:: Text

Title

-> Point Int

Surface grid

-> Point Int

Gradient grid

-> Rect Double

Surface range

-> (Point Double -> (Double, Point Double))

Surface + gradient function

-> ChartSvg 

A surface chart with gradient arrows.

writeChartSvg "other/surfaceg.svg" $ surfacegExample "rosenbrock" (Point 100 100) (Point 20 20) one (first (-1.0 *) . second (-1.0 .*) . rosenbrock 1 10)

rosenbrock :: Double -> Double -> Point Double -> (Double, Point Double) Source #

function for testing

f(x,y) = (a-x)^2 + b * (y - x^2)^2
       = a^2 - 2ax + x^2 + b * y^2 - b * 2 * y * x^2 + b * x ^ 4
f'x = -2a + 2 * x - b * 4 * y * x + 4 * b * x ^ 3
f'y = 2 * b * y - 2 * b * x^2
f a b (Point x y) = (a^2 - 2ax + x^2 + b * y^2 - b * 2 * y * x^2 + b * x^4, Point (-2a + 2 * x - b * 4 * y * x + 4 * b * x ^ 3), 2 * b * y - 2 * b * x^2)

arcExample :: ArcPosition Double -> ChartSvg Source #

arc example

arcExample (ArcPosition (Point 1.0 0.0) (Point 0.0 1.0) (ArcInfo (Point 1.0 0.5) 0 False True))

See also problematic2 for scaling issue when phi is non-zero.

ellipseExample :: ArcPosition Double -> ChartSvg Source #

ellipse example

(ArcPosition (Point 1 0) (Point 0 1) (ArcInfo (Point 1.5 1) (pi/3) True True))

quadExample :: QuadPosition Double -> ChartSvg Source #

quad example

quadExample (QuadPosition (Point 0 0) (Point 1 1) (Point 2 -1))

cubicExample :: CubicPosition Double -> ChartSvg Source #

cubic example

cubicExample (CubicPosition (Point 0 0) (Point 1 1) (Point 1 -1) (Point 0 2))

pathExample :: ChartSvg Source #

Compound path example.

vennExample :: ChartSvg Source #

venn diagram

problematic1 :: ChartAspect -> ArcPosition Double -> ChartSvg Source #

This was a problem chart that helped me get the aspect scaling of curves right.

The answer was that radii of an Arc needs to be transformed by scaling changes but not translation ones (radii are relative to existing points which are already being translated).

The problem can be isolated to RunHud ...

>>> let cs = [toPathChart defaultPathStyle $ singletonArc $ ArcPosition (Point 0 1) (Point 1 0) (ArcInfo (Point 1 1) 0 False False)]
>>> runHud (aspect 3) [canvas $ blob (Colour 0.2 0.1 0.7 0.1)] cs
[Chart {annotation = RectA (RectStyle {borderSize = 0.0, borderColor = RGBA 0.00 0.00 0.00 0.00, color = RGBA 0.20 0.10 0.70 0.10}), xys = [R -1.5 1.5000000000000002 -0.5 0.5]},Chart {annotation = PathA (PathStyle {borderSize = 1.0e-2, borderColor = RGBA 0.12 0.47 0.71 0.80, color = RGBA 0.12 0.47 0.71 0.30}) [StartI,ArcI (ArcInfo {radii = Point 3.0 1.0, phi = 0.0, large = False, clockwise = False})], xys = [P -1.5 0.5,P 1.5 -0.5]}]

Incorrect scaling of an Arc was occuring on x-axis gaps, but not with gapless x-axis elements, titles or any y axis variations.

problematic1 (FixedAspect 1) (ArcPosition (Point 1 0) (Point 0 1) (ArcInfo (Point 1.0 0.5) (0) False True)) & #hudOptions .~ (mempty & #hudAxes .~ [defaultAxisOptions & #place .~ PlaceTop & #adjust .~ Nothing & #axisBar .~ Nothing & #axisTick . #ltick .~ Nothing & #axisTick . #tstyle .~ TickRound (FormatComma (Just 2)) 2 NoTickExtend & #axisTick . #ttick .~ Nothing & #axisTick . #gtick .~ Just (defaultGlyphTick, 0.2), defaultAxisOptions & #place .~ PlaceRight & #adjust .~ Nothing & #axisBar .~ Nothing & #axisTick . #ltick .~ Nothing & #axisTick . #tstyle .~ TickRound (FormatComma (Just 2)) 2 NoTickExtend & #axisTick . #ttick .~ Nothing & #axisTick . #gtick .~ Just (defaultGlyphTick, 0.2)])

Again isolating to runHud ...

let cs = [toPathChart defaultPathStyle $ singletonArc $ (ArcPosition (Point 1 0) (Point 0 1) (ArcInfo (Point 1.0 0.5) (0) False True))]
let dbox = padBox $ dataBoxes cs
dbox

Rect -0.6180339784260676 1.0 -5.901699179399067e-2 1.0

let ho = (mempty & #hudAxes .~ [defaultAxisOptions & #place .~ PlaceBottom & #adjust .~ Nothing & #axisBar .~ Nothing & #axisTick . #ltick .~ Nothing & #axisTick . #tstyle .~ TickRound (FormatComma (Just 2)) 2 NoTickExtend & #axisTick . #ttick .~ Nothing & #axisTick . #gtick .~ Just (defaultGlyphTick, 0.2), defaultAxisOptions & #place .~ PlaceRight & #adjust .~ Nothing & #axisBar .~ Nothing & #axisTick . #ltick .~ Nothing & #axisTick . #tstyle .~ TickRound (FormatComma (Just 2)) 2 NoTickExtend & #axisTick . #ttick .~ Nothing & #axisTick . #gtick .~ Just (defaultGlyphTick, 0.2)])
let (hs',cs') = makeHud dbox ho
let cs1 = runHud dbox hs' cs
cs1
[Chart {annotation = GlyphA (GlyphStyle {size = 3.0e-2, color = RGBA 0.50 0.50 0.50 1.00, borderColor = RGBA 0.50 0.50 0.50 1.00, borderSize = 5.0e-3, shape = VLineGlyph 5.0e-3, rotation = Just 1.5707963267948966, translate = Nothing}), xys = [P 1.2049999999999998 0.0,P 1.2049999999999998 0.5,P 1.2049999999999998 1.0]},Chart {annotation = GlyphA (GlyphStyle {size = 3.0e-2, color = RGBA 0.50 0.50 0.50 1.00, borderColor = RGBA 0.50 0.50 0.50 1.00, borderSize = 5.0e-3, shape = VLineGlyph 5.0e-3, rotation = Nothing, translate = Nothing}), xys = [P 0.0 -0.2640169917939907,P 1.0 -0.2640169917939907]},Chart {annotation = PathA (PathStyle {borderSize = 1.0e-2, borderColor = RGBA 0.12 0.47 0.71 0.80, color = RGBA 0.12 0.47 0.71 0.30}) [StartI,ArcI (ArcInfo {radii = Point 1.0 0.5, phi = 0.0, large = False, clockwise = True})], xys = [P 1.0 0.0,P 0.0 1.0]}]

The output from runHud looks ok, so the problem was isolated to projectXYsWith ...

phi was then causing aspect scaling problems. Unadjusted was good:

writeChartSvg "other/t1.svg" $ problematic1 (UnadjustedAspect) (ArcPosition (Point 1 0) (Point 0 1) (ArcInfo (Point 1.0 0.5) (-pi/4) False True)) & #hudOptions .~ defaultHudOptions

And then I realised that the x and y radii of the ellipse was firstly rotated in the XY-space and only then should be subject to scaling...

problematic2 :: ArcPosition Double -> ArcCentroid Double -> ChartSvg Source #

FIXME: A guesstimate for arc scaling

In a chart-svg projection (See projectOnP say), points on the ellipse scale, but radii and angles do not, and not sure about the centroid.

This causes distortion for chart-svg re-scaling.

This is a chart of a guess for values for (aspect 2)

let p = ArcPosition (Point 0 0) (Point 1 0) (ArcInfo (Point 1 2) (pi/6) True True)
let guess = ArcCentroid (Point -0.2864867185179476 1.6092991486979669) (Point 1.3266801807145205 3.0142082605509395) 1.0962340928888052 -2.8 -5.5

writeAllExamples :: IO () Source #

Run this to refresh haddock example SVGs.