File: fig.ml

package info (click to toggle)
mlpost 0.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,844 kB
  • sloc: ml: 21,094; javascript: 4,047; makefile: 430; ansic: 34; lisp: 19; sh: 15
file content (92 lines) | stat: -rw-r--r-- 2,145 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
open Mlpost
open Num
open Path
open Command

module Edit = struct
  open Glexer

  let file = "fig.edit"

  let elements = ref []

  let table = Hashtbl.create 17

  let () =
    if Sys.file_exists file then (
      elements := Glexer.read_file file;
      let add = function
        | (Num (s, _) | Point (s, _, _)) as e -> Hashtbl.add table s e
      in
      List.iter add !elements )

  let mk_num = function
    | Pt -> Num.pt
    | Cm -> Num.cm
    | Mm -> Num.mm
    | Bp -> Num.bp
    | Inch -> Num.inch

  let num s v dimension =
    try
      match Hashtbl.find table s with
      | Num (_, (f, dim)) -> mk_num dim f
      | Point _ -> invalid_arg ("already a point of name " ^ s)
    with Not_found ->
      let e = Num (s, (v, dimension)) in
      elements := e :: !elements;
      Hashtbl.add table s e;
      mk_num dimension v

  let point s v1 dim1 v2 dim2 =
    try
      match Hashtbl.find table s with
      | Num _ -> invalid_arg ("already a num of name " ^ s)
      | Point (_, (n1, d1), (n2, d2)) -> Point.pt (mk_num d1 n1, mk_num d2 n2)
    with Not_found ->
      let e = Point (s, (v1, dim1), (v2, dim2)) in
      elements := e :: !elements;
      Hashtbl.add table s e;
      Point.pt (mk_num dim1 v1, mk_num dim2 v2)

  let () = at_exit (fun () -> Glexer.write_file file !elements)
end

(* Exemple d'utilisation de Gmlpost *)

open Color

(* un histogramme *)

let fill = [ lightblue; lightgreen; lightyellow; lightred ]

(* le padding est une valeur éditable *)

let padding = Edit.num "padding" 5. Glexer.Bp

let hist =
  Hist.simple ~width:(bp 50.) ~height:(bp 100.) ~fill ~padding
    [ 1.; 2.; 3.; 4. ]

(* et sa légende *)

let leg =
  Legend.legend
    [
      (lightblue, "2006");
      (lightgreen, "2007");
      (lightyellow, "2008");
      (lightred, "2009");
    ]

(* la légende est positionnée au point p1 qui est éditable *)

let p1 = Edit.point "legend" 100. Glexer.Bp 100. Glexer.Bp

let pic = Command.draw_pic (Picture.shift p1 (Picture.scale (bp 0.5) leg))

(* la figure est l'ensemble de l'histogramme et de sa légende *)

let example = hist ++ pic

let () = Metapost.emit "example" example