为定制表格表示创建函数 [英] Create a Function for Customized Tabular Representation

查看:193
本文介绍了为定制表格表示创建函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来获取我的数据部分的概述。


  • 最好的方法是什么一个函数不在下面的代码中?


  • 这需要dataList以及一些图形选项(如颜色)作为参数,并返回自定义表格表示如下图所示。

      overviewtheData = Text @ Grid [{Map [旋转[Text [#],
    90Degree] &,data [[1]]]}〜Join_data [[2 ;;]],
    Background-> {{{{White,Pink}},{1-> White}}} ,
    除法器 - > {全部,{1->真,2->真,0->真}},
    ItemSize-> {1-> 5,Automatic} ,
    Alignment-> Top,
    Frame-> True,
    FrameStyle-> Thickness [2],
    ItemStyle-> {Automatic,Automatic,{{1 ,1},
    {1,Length @ data [[1]]}} - >指令[FontSize-> 15,Black,Bold]}]







解决方

贝利萨留给了基本方法。我会介绍一种先进的方法,因为你看起来很想学习。

首先让我说,我看到了我认为简化代码的方法,然后我制作了它们,希望不会出错。



我将在下面的插图中使用此示例数据:

  data = Prepend [
RandomInteger [99,{5,12}],
DateString [{1,#},MonthName]& / @范围@ 12
];






目标




  1. 由于使用的主要函数是 Grid ,所以允许传递选项是有意义的。


  2. 您有一系列定义您的表格的选项。我希望能够方便地更改这些内容。






  3. 实现



    目标#1



    添加了参数模式 opts:OptionsPattern [] ,其中匹配选项 - >的任何序列。设置参数,并将其命名为 opts 。 (请参阅: for customTabular

      ClearAll [customTabular] 

    选项[customTabular] =
    {背景 - > {{{白色,粉红色}}},
    分隔线 - > {全部,{2 - > True}},
    ItemSize - > {1 - > 5},
    对齐 - > {Center,{1 - > Top}},
    Frame - >确实,
    FrameStyle - >厚度[2],
    ItemStyle - >指令[FontSize - > 15,Black,Bold]};

    现在函数正确。这里 Options @ customTabular 获取上面给出的规则。
    $ b

      customTabular [data_ ,opts:OptionsPattern []]:= 
    Grid [MapAt [Rotate [#,90 Degree]& / @#&; data,1],
    opts,
    Sequence @@ Options @ customTabular
    ] // Text

    现在您可以轻松地使用这将给默认情况下,如果没有显式给出。 http://reference.wolfram.com/mathematica/ref/FilterRules.html =nofollow noreferrer> FilterRules




    我首先将任何显式选项加入到 Options @ customTabular 列表的前面,再次覆盖默认值。

      customTabular [data_,opts:OptionsPattern []]:= 
    Grid [MapAt [旋转[#,OptionValue [旋转]]& / @#&;; data,1],
    Sequence @@ FilterRules [{opts}〜Join〜Options @ customTabular,Options @ Grid]
    ] // Text

      SetOptions [customTabular ,背景 - > {{{LightBrown,LightYellow}}}]; 

    customTabular [数据,
    分隔符 - >全部,
    旋转 - > -90度,
    FrameStyle - > {darker @ Red,Thick}
    ]


    I use the code below get an overview of parts of my data.

    • What would be the best way to make a function out of the below code ?

    • It would take a dataList as well as some graphical options (such as colors) as arguments and return a customized tabular representation as shown below.

      overviewtheData=Text@Grid[{Map[Rotate[Text[#],
      90Degree]&,data[[1]]]}~Join~data[[2;;]],
      Background->{{{{White,Pink}},{1->White}}},
      Dividers->{All,{1->True,2->True,0->True}},
      ItemSize->{1->5,Automatic},
      Alignment->Top,
      Frame->True,
      FrameStyle->Thickness[2],
      ItemStyle->{Automatic,Automatic,{{1,1},
      {1,Length@data[[1]]}}->Directive[FontSize->15,Black,Bold]}]
      


    解决方案

    Belisarius gave the basic method. I shall introduce an advanced method, because you seem eager to learn.

    First let me say that I saw what I believed were simplifications to your code, and I made them, hopefully not in error.

    I will use this sample data in illustrations below:

    data = Prepend[
             RandomInteger[99, {5, 12}], 
             DateString[{1, #}, "MonthName"] & /@ Range@12
           ];
    


    Goals

    1. Since the main function used is Grid it makes sense to allow passing options to it.

    2. You have a series of options that define your table. I want to be able to conveniently change these.

    3. I want the possibility of custom options not understood by Grid.


    Implementation

    Goal #1

    An argument pattern opts:OptionsPattern[] is added, which matches any sequence of Option -> Setting arguments, and names it opts. (See: OptionsPattern for more.) Then, opts is inserted into the basic function before the other options for Grid. This allows any explicitly given options to override the defaults, or new ones to be given.

    customTabular[data_, opts : OptionsPattern[]] :=
      Grid[MapAt[Rotate[#, 90 Degree] & /@ # &, data, 1],
       opts,
       Background -> {{{White, Pink}}},
       Dividers -> {All, {2 -> True}},
       ItemSize -> {1 -> 5},
       Alignment -> {Center, {1 -> Top}},
       Frame -> True,
       FrameStyle -> Thickness[2],
       ItemStyle -> Directive[FontSize -> 15, Black, Bold]
      ] // Text
    

    Examples:

    customTabular[data]
    

    customTabular[data, Background -> LightBlue]
    

    Goal #2

    The options that define your tabular format can be separated from the function body. This will allow them to be conveniently changed or referenced. I start by clearing the previous definition with ClearAll. Then I set default Options for customTabular:

    ClearAll[customTabular]
    
    Options[customTabular] =
      {Background -> {{{White, Pink}}},
       Dividers -> {All, {2 -> True}},
       ItemSize -> {1 -> 5},
       Alignment -> {Center, {1 -> Top}},
       Frame -> True,
       FrameStyle -> Thickness[2],
       ItemStyle -> Directive[FontSize -> 15, Black, Bold]};
    

    Now the function proper. Here Options@customTabular gets the rules given above.

    customTabular[data_, opts : OptionsPattern[]] := 
     Grid[MapAt[Rotate[#, 90 Degree] & /@ # &, data, 1],
       opts, 
       Sequence @@ Options@customTabular
     ] // Text
    

    Now you can easily change the defaults with SetOptions. Example:

    SetOptions[customTabular, 
      Background -> {{{LightMagenta, LightOrange}}}
    ];
    
    customTabular[data]
    

    Goal #3

    Now I want to add an option that is not passed to Grid. I choose "Rotation" to change the text rotation of the title row.

    Again I clear the prior definition and the default options. Notice the inclusion of "Rotation" -> 90 Degree in the list.

    ClearAll[customTabular]
    
    Options[customTabular] =
      {Background -> {{{White, Pink}}},
       Dividers -> {All, {2 -> True}},
       ItemSize -> {1 -> 5},
       Alignment -> {Center, {1 -> Top}},
       Frame -> True,
       FrameStyle -> Thickness[2],
       ItemStyle -> Directive[FontSize -> 15, Black, Bold],
       "Rotation" -> 90 Degree};
    

    Now I need a way to use this new option, and I need a way to keep this option from being sent to Grid:

    • I access the option with OptionValue which will give the default if none is explicitly given.

    • I pass only valid Grid options by using FilterRules.

    I first join any explicit options to the front of the Options@customTabular list, again to override defaults.

    customTabular[data_, opts : OptionsPattern[]] :=
     Grid[MapAt[Rotate[#, OptionValue["Rotation"]] & /@ # &, data, 1],
       Sequence @@ FilterRules[{opts} ~Join~ Options@customTabular, Options@Grid]
     ] // Text
    

    Example:

    SetOptions[customTabular, Background -> {{{LightBrown, LightYellow}}}];
    
    customTabular[data,
      Dividers -> All,
      "Rotation" -> -90 Degree,
      FrameStyle -> {Darker@Red, Thick}
    ]
    

    这篇关于为定制表格表示创建函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆