500*_*500 6 grid wolfram-mathematica function tabular
我使用下面的代码概述了我的部分数据.
从下面的代码中创建函数的最佳方法是什么?
它将使用dataList以及一些图形选项(例如颜色)作为参数,并返回自定义表格表示,如下所示.
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]}]
Run Code Online (Sandbox Code Playgroud)
Belisarius给出了基本方法.我将介绍一种先进的方法,因为你似乎渴望学习.
首先让我说我看到了我认为对你的代码进行了简化,我制作了它们,希望不会出错.
我将在下面的插图中使用此示例数据:
data = Prepend[
RandomInteger[99, {5, 12}],
DateString[{1, #}, "MonthName"] & /@ Range@12
];
Run Code Online (Sandbox Code Playgroud)
由于使用的主要功能是Grid允许传递选项是有意义的.
您有一系列定义表格的选项.我希望能够方便地改变这些.
我想要不了解自定义选项的可能性Grid.
opts:OptionsPattern[]添加一个参数模式,它匹配任何Option -> Setting参数序列,并命名它opts.(参见:OptionsPattern了解更多.)然后,opts在其他选项之前插入基本功能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
Run Code Online (Sandbox Code Playgroud)
例子:
customTabular[data]
Run Code Online (Sandbox Code Playgroud)

customTabular[data, Background -> LightBlue]
Run Code Online (Sandbox Code Playgroud)

定义表格格式的选项可以与函数体分开.这将允许方便地更改或引用它们.我首先清除以前的定义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]};
Run Code Online (Sandbox Code Playgroud)
现在功能正常.这里Options@customTabular得到上面给出的规则.
customTabular[data_, opts : OptionsPattern[]] :=
Grid[MapAt[Rotate[#, 90 Degree] & /@ # &, data, 1],
opts,
Sequence @@ Options@customTabular
] // Text
Run Code Online (Sandbox Code Playgroud)
现在您可以轻松更改默认值SetOptions.例:
SetOptions[customTabular,
Background -> {{{LightMagenta, LightOrange}}}
];
customTabular[data]
Run Code Online (Sandbox Code Playgroud)

现在我想添加一个未传递给的选项Grid.我选择"Rotation"更改标题行的文本旋转.
我再次清除了先前的定义和默认选项.请注意列入"Rotation" -> 90 Degree清单.
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};
Run Code Online (Sandbox Code Playgroud)
现在我需要一种方法来使用这个新选项,我需要一种方法来保持此选项不被发送到Grid:
OptionValue如果没有明确给出,我将访问带有默认值的选项.
我通过使用只传递有效Grid选项FilterRules.
我首先将任何显式选项加入到Options@customTabular列表的前面,再次覆盖默认值.
customTabular[data_, opts : OptionsPattern[]] :=
Grid[MapAt[Rotate[#, OptionValue["Rotation"]] & /@ # &, data, 1],
Sequence @@ FilterRules[{opts} ~Join~ Options@customTabular, Options@Grid]
] // Text
Run Code Online (Sandbox Code Playgroud)
例:
SetOptions[customTabular, Background -> {{{LightBrown, LightYellow}}}];
customTabular[data,
Dividers -> All,
"Rotation" -> -90 Degree,
FrameStyle -> {Darker@Red, Thick}
]
Run Code Online (Sandbox Code Playgroud)
