创建自定义表格表示的函数

发布于 2024-11-10 08:27:01 字数 557 浏览 2 评论 0 原文

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

  • 从以下代码中创建函数的最佳方法是什么?

  • 它将采用 dataList 以及一些图形选项(例如颜色)作为参数,并返回自定义的表格表示形式,如下所示。

    overviewtheData=Text@Grid[{地图[旋转[文本[#]],
    90度]&,数据[[1]]]}~加入~数据[[2;;]],
    背景->{{{{白色,粉色}},{1->白色}}},
    分隔符->{全部,{1->True,2->True,0->True}},
    ItemSize->{1->5,自动},
    对齐方式->顶部,
    框架->真实,
    框架样式->厚度[2],
    ItemStyle->{自动,自动,{{1,1},
    {1,Length@data[[1]]}}->指令[FontSize->15,Black,Bold]}]
    

在此处输入图像描述

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]}]
    

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-11-17 08:27:02

我会按照通常的方式进行

f[data_, color1_, color2_] := 
 Text@Grid[{Map[Rotate[Text[#], 90 Degree] &, data[[1]]]}~Join~
    data[[2 ;;]], Background -> {{{{color1, color2}}, {1 -> color1}}},
    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]}]

f[{{t1, t2, t3}, {1, 2, 3}, {4, 5, 6}}, LightBlue, Orange]

在此处输入图像描述

我不确定您最后想要求什么部分这样的图作为输出 ...

I'd do it the usual way

f[data_, color1_, color2_] := 
 Text@Grid[{Map[Rotate[Text[#], 90 Degree] &, data[[1]]]}~Join~
    data[[2 ;;]], Background -> {{{{color1, color2}}, {1 -> color1}}},
    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]}]

f[{{t1, t2, t3}, {1, 2, 3}, {4, 5, 6}}, LightBlue, Orange]

enter image description here

I am not sure what you are trying to ask for in the last part such a graph as an output ...

墟烟 2024-11-17 08:27:01

贝利撒留给出了基本方法。我将介绍一种高级方法,因为你看起来很渴望学习。

首先我要说的是,我看到了我认为对您的代码的简化,并且我做了它们,希望没有错误。

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

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

目标

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

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

  3. 我想要 Grid 无法理解自定义选项的可能性。


实现

目标 #1

添加参数模式 opts:OptionsPattern[],它与 Option -> 的任何序列匹配。设置参数,并将其命名为opts。 (有关更多信息,请参阅:OptionsPattern。)然后,选择 被插入到基本函数中,位于 Grid 的其他选项之前。这允许任何明确给出的选项覆盖默认值,或者给出新的选项。

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

示例:

customTabular[data]

在此处输入图像描述

customTabular[data, Background -> LightBlue]

在此处输入图像描述

目标#2

定义表格格式的选项可以与函数主体分开。这将使它们可以方便地更改或引用。我首先使用 ClearAll 清除先前的定义。然后我为 设置默认 Options 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]};

现在功能正常了。这里 Options@customTabular 获取上面给出的规则。

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

现在,您可以使用 SetOptions 轻松更改默认值。示例:

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

customTabular[data]

在此处输入图像描述

目标 #3

现在我想添加一个的选项传递到网格。我选择“Rotation”来更改标题行的文本旋转。

我再次清除了先前的定义和默认选项。请注意包含 "Rotation" ->列表中的 90 度

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};

现在我需要一种方法来使用这个新选项,并且需要一种方法来阻止此选项发送到 Grid

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

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

示例:

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

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

在此处输入图像描述

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]

enter image description here

customTabular[data, Background -> LightBlue]

enter image description here

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]

enter image description here

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}
]

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文