Alex,

Thank you! I did not know about plot-pict.

Best regards,

Dmitry

On 11/11/18 2:45 AM, Alex Harsanyi wrote:

If you are willing to do some programming, you can write your own function to 
create the legend, as below:

    #lang racket
    (require plot pict racket/draw)

    ;; I could not find how to draw styled lines in pict, so I wrote my own
    ;; function....
    (define (hline1 w h color style)
      (define (draw dc x y)
        (define pen (send the-pen-list find-or-create-pen color h style))
        (define old-pen (send dc get-pen))
        (send dc set-pen pen)
        (send dc draw-line x (+ y (/ h 2)) (+ x w) (+ y (/ h 2)))
        (send dc set-pen old-pen))
      (dc draw w h))

    (define (legend)
      (define picts
        (list
         (hline1 30 2 "red" 'long-dash) (text "sin")
         (hline1 30 4 "blue" 'short-dash) (text "cos")))
      (frame
       (inset
        (vc-append
         15
         (text "Legend" 'default 14)
         (table 2 picts cc-superimpose cc-superimpose 10 10))
        5)))

    (define (the-plot)
      (plot-pict (list
           (function sin #:color "red" #:width 2 #:style 'long-dash)
           (function cos #:color "blue" #:width 4 #:style 'short-dash))
          #:x-min -5 #:x-max 5))

    (ht-append (legend) (the-plot))

Which produces:

loutside.png

You can than write the pict to a bitmap using `pict->bitmap`.  The code above 
will not produce interactive plot snips -- it is possible to do that as well, in 
my own application I have a floating legend that I can drag around the plot so I 
can see what is underneath it, but implementing that is a bit more complex.

The obvious disadvantage of this code is that you have to manually manage the 
legend entries or write higher level wrappers for the plot functions to 
generate both legend entries and plot renderers.  The big advantage is that you 
can style the legend however you want.

Best Regards,
Alex.

On Friday, November 9, 2018 at 9:16:48 PM UTC+8, Alex Harsanyi wrote:


    It is not currently possible to draw the legend outside the plot area.  The 
code that draws the legend is at the link below, however, this code assumes 
that the position will be inside the plot area and will not reserve extra space 
outside this area:

    
https://github.com/racket/plot/blob/8dcfd7745e2595b8d517fd8cd7c59510efab84a9/plot-lib/plot/private/common/plot-device.rkt#L587
 
<https://github.com/racket/plot/blob/8dcfd7745e2595b8d517fd8cd7c59510efab84a9/plot-lib/plot/private/common/plot-device.rkt#L587>

    Alex.

    On Thursday, November 8, 2018 at 11:18:14 PM UTC+8, Dmitry Pavlov wrote:

        Hello,

        Is it possible to render a plot with the legend outside the plotting
        area, like gnuplot does with "set key outside" option?

        I see only (plot-legend-anchor) parameter for placement of the legend in
        different places inside the plot area.

        Best regards,

        Dmitry


--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 
racket-users+unsubscr...@googlegroups.com 
<mailto:racket-users+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to