ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen via ntg-context <ntg-context@ntg.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Cc: Hans Hagen <j.hagen@xs4all.nl>
Subject: Re: Axis macro in MetaFun
Date: Sat, 11 Sep 2021 19:00:25 +0200	[thread overview]
Message-ID: <2b05aa4a-edc3-3af6-0383-1328ce327b9b@xs4all.nl> (raw)
In-Reply-To: <2945364.l7x7BXaAel@sreeramtplt>

On 9/11/2021 4:27 PM, kauśika cittūr via ntg-context wrote:
> Dear All,
> 
> I am truly grateful for the new additions to MetaFun. It has greatly helped me
> in producing neat drawings for my academic work. In this connection, I have a
> few queries.
> 
> Currently I am using PGFPlots (with ConTeXt) to draw simple plots from CSV
> data. I was wondering if there is any way to do the same in MetaFuns. That is,
> is it possible to do something like the following in order to obtain a line
> plot of the values in the 'data.csv' file? Also is it possible to have the
> 'labels' be the identical for all points in a given list?
> 
> \startMPcode
>    draw lmt_axis [
>        sx = 2mm, sy = 2mm,
>        nx = 80, ny = 40,
>        tx = 15, ty = 15,
>        
>        list = {
>              [
>                    connect = true,
>                    color = "darkblue",
>                    close = true,
>                    points = {data.csv},
>                    labels = {"•"},
>              ]
>        },
>    ] withpen pencircle scaled 0.5mm;
> \stopMPcode
> 

It depends on how one has data in the csv, You can patch mlib-scn.lmt 
(line 659)

     if type(v) == "table" then
         return injectpath(v,connector,close)
     elseif type(v) == "string" then
         local code = load("return " .. v)
         if code then
             return code()
         end
     else
         return injectpair(0,0)
     end

then a string represents a function:

\starttext

\startluacode
     io.savedata("temp.csv", "x,y\n10,10\n20,40\n30,30\n")

     local mycsvsplitter = utilities.parsers.rfc4180splitter()

     function MP.MyPoints(name)
         local data = io.loaddata(name)
         local list = mycsvsplitter(data)
         table.remove(list,1)
         for i=1,#list do
             list[i][1] = tonumber(list[i][1])
             list[i][2] = tonumber(list[i][2])
         end
         mp.inject.path(list)
     end

\stopluacode

\startMPcode
   draw lmt_axis [
       sx = 2mm, sy = 2mm,
       nx = 80, ny = 40,
       tx = 15, ty = 15,

       list = {
             [
                   connect = true,
                   color = "darkblue",
                   close = true,
                   points = "MP.MyPoints('temp.csv')",
                   labels = {"•"},
             ]
       },
   ] withpen pencircle scaled 0.5mm;
\stopMPcode

\stoptext

we can of course have some predefined and when we can agree on how these 
files loo, adding a "csv" is no big deal ... it's all about consistent 
interfacing


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

  parent reply	other threads:[~2021-09-11 17:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-11 14:27 kauśika cittūr via ntg-context
2021-09-11 16:19 ` Hans Hagen via ntg-context
2021-09-11 17:00 ` Hans Hagen via ntg-context [this message]
2021-09-12  2:07   ` kauśika cittūr via ntg-context

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2b05aa4a-edc3-3af6-0383-1328ce327b9b@xs4all.nl \
    --to=ntg-context@ntg.nl \
    --cc=j.hagen@xs4all.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).