* [Caml-list] Polymorphic graph widget problem
@ 2003-08-29 11:54 Richard Jones
2003-08-29 13:24 ` Remi Vanicat
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Richard Jones @ 2003-08-29 11:54 UTC (permalink / raw)
To: caml-list
As part of a project I'm doing at the moment, I've written a Gtk graph
widget using lablgtk. The graph currently plots ints, so the type
looks something like this:
class chart :
?width:int ->
?height:int ->
?packing:(GObj.widget -> unit) ->
?show:bool ->
int array -> (* the data being plotted *)
object
method repaint : Gdk.Rectangle.t option -> unit
end
All fine, but now I'd like to generalise this so it can plot floating
point values as well as int, thus:
class ['a] chart :
?width:int ->
?height:int ->
?packing:(GObj.widget -> unit) ->
?show:bool ->
'a array -> (* the data being plotted *)
object
method repaint : Gdk.Rectangle.t option -> unit
end
The problem with this is that at various places in the implementation
we need to break the polymorphism. eg. To plot Y labels we call
'string_of_int', and to work out the height of the Y axis we do some
sums on the values using the (+) operator.
What I need is some way to isolate the parts which break the
polymorphism. How do I do that?
Rich.
--
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
All new technology is irrelevant until it is taken up by the public.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Polymorphic graph widget problem
2003-08-29 11:54 [Caml-list] Polymorphic graph widget problem Richard Jones
@ 2003-08-29 13:24 ` Remi Vanicat
2003-08-29 13:27 ` Michal Moskal
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Remi Vanicat @ 2003-08-29 13:24 UTC (permalink / raw)
To: caml-list
Richard Jones <rich@annexia.org> writes:
> As part of a project I'm doing at the moment, I've written a Gtk graph
> widget using lablgtk. The graph currently plots ints, so the type
> looks something like this:
>
> class chart :
> ?width:int ->
> ?height:int ->
> ?packing:(GObj.widget -> unit) ->
> ?show:bool ->
> int array -> (* the data being plotted *)
> object
> method repaint : Gdk.Rectangle.t option -> unit
> end
>
> All fine, but now I'd like to generalise this so it can plot floating
> point values as well as int, thus:
>
> class ['a] chart :
> ?width:int ->
> ?height:int ->
> ?packing:(GObj.widget -> unit) ->
> ?show:bool ->
> 'a array -> (* the data being plotted *)
> object
> method repaint : Gdk.Rectangle.t option -> unit
> end
>
> The problem with this is that at various places in the implementation
> we need to break the polymorphism. eg. To plot Y labels we call
> 'string_of_int', and to work out the height of the Y axis we do some
> sums on the values using the (+) operator.
>
> What I need is some way to isolate the parts which break the
> polymorphism. How do I do that?
Well, as defined, your class should work not only on int and real but
also on string, Array of file descriptor, or whatever. It seem hard.
In fact, you need to give to your class not only the data, but also
the function for manipulating those data :
class ['a] chart :
?width:int ->
?height:int ->
?packing:(GObj.widget -> unit) ->
?show:bool ->
'a array -> (* the data being plotted *)
('a -> string) -> (* a function to have the string representation
of the data *)
('a -> int) -> (* a function to have an aproximation of the data *)
object
method repaint : Gdk.Rectangle.t option -> unit
end
(well, you might want to choose some other type but you have the idea)
--
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Polymorphic graph widget problem
2003-08-29 11:54 [Caml-list] Polymorphic graph widget problem Richard Jones
2003-08-29 13:24 ` Remi Vanicat
@ 2003-08-29 13:27 ` Michal Moskal
2003-08-29 13:40 ` Olivier Andrieu, Olivier Andrieu
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Michal Moskal @ 2003-08-29 13:27 UTC (permalink / raw)
To: Richard Jones; +Cc: caml-list
On Fri, Aug 29, 2003 at 12:54:46PM +0100, Richard Jones wrote:
>
> As part of a project I'm doing at the moment, I've written a Gtk graph
> widget using lablgtk. The graph currently plots ints, so the type
> looks something like this:
>
> class chart :
> ?width:int ->
> ?height:int ->
> ?packing:(GObj.widget -> unit) ->
> ?show:bool ->
> int array -> (* the data being plotted *)
> object
> method repaint : Gdk.Rectangle.t option -> unit
> end
>
> All fine, but now I'd like to generalise this so it can plot floating
> point values as well as int, thus:
>
> class ['a] chart :
> ?width:int ->
> ?height:int ->
> ?packing:(GObj.widget -> unit) ->
> ?show:bool ->
> 'a array -> (* the data being plotted *)
> object
> method repaint : Gdk.Rectangle.t option -> unit
> end
>
> The problem with this is that at various places in the implementation
> we need to break the polymorphism. eg. To plot Y labels we call
> 'string_of_int', and to work out the height of the Y axis we do some
> sums on the values using the (+) operator.
>
> What I need is some way to isolate the parts which break the
> polymorphism. How do I do that?
Put this 'a array inside an object with string_of_int and (+) functions.
Then parametrize chart with 'a, where 'a :> that object. Finally provide
functions to embed int array (or float array) in this kind of object.
You can also try to wait for generics to fix this problem.
--
: Michal Moskal :: http://www.kernel.pl/~malekith : GCS {C,UL}++++$ a? !tv
: When in doubt, use brute force. -- Ken Thompson : {E-,w}-- {b++,e}>+++ h
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Polymorphic graph widget problem
2003-08-29 11:54 [Caml-list] Polymorphic graph widget problem Richard Jones
2003-08-29 13:24 ` Remi Vanicat
2003-08-29 13:27 ` Michal Moskal
@ 2003-08-29 13:40 ` Olivier Andrieu, Olivier Andrieu
2003-08-29 17:01 ` Issac Trotts
2003-08-29 18:30 ` Matt Gushee
4 siblings, 0 replies; 6+ messages in thread
From: Olivier Andrieu, Olivier Andrieu @ 2003-08-29 13:40 UTC (permalink / raw)
To: Richard Jones; +Cc: caml-list
Hi, Richard
Richard Jones [Friday 29 August 2003] :
> As part of a project I'm doing at the moment, I've written a Gtk graph
> widget using lablgtk. The graph currently plots ints, so the type
> looks something like this:
>
> class chart :
> ?width:int ->
> ?height:int ->
> ?packing:(GObj.widget -> unit) ->
> ?show:bool ->
> int array -> (* the data being plotted *)
> object
> method repaint : Gdk.Rectangle.t option -> unit
> end
>
> All fine, but now I'd like to generalise this so it can plot floating
> point values as well as int, thus:
>
> class ['a] chart :
> ?width:int ->
> ?height:int ->
> ?packing:(GObj.widget -> unit) ->
> ?show:bool ->
> 'a array -> (* the data being plotted *)
> object
> method repaint : Gdk.Rectangle.t option -> unit
> end
first, you do not need to parametrize your `chart' class if the type
variable doesn't appear in a method signature. I.e. you can do :
class chart : 'a array -> object method repaint : ... end
or even :
class chart : 'a array ->
object val data : 'a array method repaint : ... end
> The problem with this is that at various places in the implementation
> we need to break the polymorphism. eg. To plot Y labels we call
> 'string_of_int', and to work out the height of the Y axis we do some
> sums on the values using the (+) operator.
Here's one solution :
class virtual chart ?width ?height ?packing ?show (data : 'a array) =
object (self)
method private virtual sum : 'a array -> 'a
method private virtual to_string : 'a -> string
method repaint =
(* code using self#to_string and self#sum *)
end
class type chart_t = object method repaint : Gdk.Rectangle.t option -> unit end
class int_chart ?w ?h ?p ?s data : chart_t =
object
inherit chart ?w ?h ?p ?s data
method private to_string = string_of_int
method private sum a = Array.fold_left (+) 0 a
end
class float_chart ?w ?h ?p ?s data : chart_t =
object
inherit chart ?w ?h ?p ?s data
method private to_string = string_of_float
method private sum a = Array.fold_left (+.) 0. a
end
--
Olivier
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Polymorphic graph widget problem
2003-08-29 11:54 [Caml-list] Polymorphic graph widget problem Richard Jones
` (2 preceding siblings ...)
2003-08-29 13:40 ` Olivier Andrieu, Olivier Andrieu
@ 2003-08-29 17:01 ` Issac Trotts
2003-08-29 18:30 ` Matt Gushee
4 siblings, 0 replies; 6+ messages in thread
From: Issac Trotts @ 2003-08-29 17:01 UTC (permalink / raw)
To: caml-list
If you'll only be graphing ints and floats, why not just use floats and
cast to them? To support both, you could try this:
type number = Int of int | Float of float
class chart :
...
number array -> (* the data being plotted *)
object
method repaint : Gdk.Rectangle.t option -> unit
end
It might also help to think of the array as a map from ints to floats.
Then you could write this instead:
class chart :
...
(int * int * (int -> float)) -> (* x0, x1, the data being plotted *)
object
method repaint : Gdk.Rectangle.t option -> unit
end
- ijt
Richard Jones wrote:
>As part of a project I'm doing at the moment, I've written a Gtk graph
>widget using lablgtk. The graph currently plots ints, so the type
>looks something like this:
>
>class chart :
> ?width:int ->
> ?height:int ->
> ?packing:(GObj.widget -> unit) ->
> ?show:bool ->
> int array -> (* the data being plotted *)
> object
> method repaint : Gdk.Rectangle.t option -> unit
> end
>
>All fine, but now I'd like to generalise this so it can plot floating
>point values as well as int, thus:
>
>class ['a] chart :
> ?width:int ->
> ?height:int ->
> ?packing:(GObj.widget -> unit) ->
> ?show:bool ->
> 'a array -> (* the data being plotted *)
> object
> method repaint : Gdk.Rectangle.t option -> unit
> end
>
>The problem with this is that at various places in the implementation
>we need to break the polymorphism. eg. To plot Y labels we call
>'string_of_int', and to work out the height of the Y axis we do some
>sums on the values using the (+) operator.
>
>What I need is some way to isolate the parts which break the
>polymorphism. How do I do that?
>
>Rich.
>
>
>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Polymorphic graph widget problem
2003-08-29 11:54 [Caml-list] Polymorphic graph widget problem Richard Jones
` (3 preceding siblings ...)
2003-08-29 17:01 ` Issac Trotts
@ 2003-08-29 18:30 ` Matt Gushee
4 siblings, 0 replies; 6+ messages in thread
From: Matt Gushee @ 2003-08-29 18:30 UTC (permalink / raw)
To: caml-list
On Fri, Aug 29, 2003 at 12:54:46PM +0100, Richard Jones wrote:
>
> As part of a project I'm doing at the moment, I've written a Gtk graph
> widget using lablgtk. The graph currently plots ints, so the type
> looks something like this:
Hi, Richard--
I am working on a vaguely similar application. I say vaguely, because
the problem domain is very similar, but the practical constraints are
very different: my application also creates charts based on numeric (and
textual) data, but the input will usually be represented as strings (SQL
results and maybe XML data sets) and creates bitmap graphics for the
web. So my approach is necessarily quite different from yours, and I
don't know if any of my techniques will be useful to you. But just in
case it will help, let me briefly describe what I am doing.
* The two main active components in the system are the module types
CHART_TYPE and RENDERER, defined as follows:
module type CHART_TYPE =
sig
type t
val create : string list list -> t
val drawing : t -> cp_drawing
val raw_drawing : t -> cp_drawing (* W/O labels, axes, etc. -
useful for composite
charts *)
end
module type RENDERER =
sig
type t
val create : output_spec -> style -> t
val reset : t -> output_spec -> t
val render : cp_drawing -> unit
end
* All modules conforming to CHART_TYPE will produce an instance of
the 'cp_drawing' type, which is then passed to a RENDERER to
produce an actual image. The 'cp_drawing' type represents an
abstract drawing--by which I mean that it includes shapes such as
Rectangle, Ellipse, and so on, but all dimensions are expressed in
an abstract way, and graphical properties such as line width, fill,
etc. are unspecified (see example below).
* All numbers are handled internally as floats. Whether they are
*displayed* as floats or integers will be based on a style specified
by the user, who presumably knows which is most appropriate to the
data being charted.
EXAMPLE (this is an actual toplevel session, edited for clarity)
(* Create a chart, then a drawing based on it. *)
let data = [["A";"121.8"];["B";"84.3"];["C";"98"];["D";"108.5"]];;
let ch = SimpleColumnChart.create data;;
let dr = SimpleColumnChart.drawing ch;;
val dr : Chartpak.cp_drawing =
{drawing_id = "simple_column_chart"; components = <abstr>}
(* Here are the components--first the four columns *)
{id = "rect-0"; tags = []; label = SingleLabel "A";
figure = Rectangle (Slot 1, Start, AutoSizeCentered, Length 121.8)}
{id = "rect-1"; tags = []; label = SingleLabel "B";
figure = Rectangle (Slot 2, Start, AutoSizeCentered, Length 84.3)}
{id = "rect-2"; tags = []; label = SingleLabel "C";
figure = Rectangle (Slot 3, Start, AutoSizeCentered, Length 98.)}
{id = "rect-3"; tags = []; label = SingleLabel "D";
figure = Rectangle (Slot 4, Start, AutoSizeCentered, Length 108.5)}
(* And here are the vertical and horizontal axes. Note that the
length of the vertical axis is wrong -- it should be at least as
long as the longest column. Thanks for helping me discover this
bug! *)
{id = "line-0"; tags = []; label = FloatSeries (0., 100., 10.);
figure = Line (Start, Start, AutoSizeCentered, Length 100.)}
{id = "line-1"; tags = []; label = NoLabel;
figure = Line (Start, Start, SpanTo (Slot 4), AutoSizeCentered)}
--
Matt Gushee When a nation follows the Way,
Englewood, Colorado, USA Horses bear manure through
mgushee@havenrock.com its fields;
http://www.havenrock.com/ When a nation ignores the Way,
Horses bear soldiers through
its streets.
--Lao Tzu (Peter Merel, trans.)
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2003-08-29 18:30 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-08-29 11:54 [Caml-list] Polymorphic graph widget problem Richard Jones
2003-08-29 13:24 ` Remi Vanicat
2003-08-29 13:27 ` Michal Moskal
2003-08-29 13:40 ` Olivier Andrieu, Olivier Andrieu
2003-08-29 17:01 ` Issac Trotts
2003-08-29 18:30 ` Matt Gushee
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox