Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,49 @@
DataGrid[
rowHeights:{__Integer},
colWidths:{__Integer},
spacings:{_Integer,_Integer},
borderWidths:{{_Integer,_Integer},{_Integer,_Integer}},
options_Association,
data:{__List?MatrixQ}]:=
With[
(*Need to make sure we have sensible defaults for the decoration options.*)
{alignment=Lookup[options,"alignment",{0,0}],
background=Lookup[options,"background"," "],
dividers=Lookup[options,"dividers",{" "," "," "}],
border=Lookup[options,"border"," "],
dims={Length[rowHeights],Length[colWidths]}},
(*Pad the data so that it will fit into the specified rectangle (list of lists).*)
With[{augmentedData=PadRight[data,Times@@dims,{{{background}}}]},
(*Create a matrix of dimensions based on desired rectangle. Once we have a matrix of cells we can "thread" these two matrices and use that data to coerce each cell into its final dimensions.*)
With[{cellDims=ArrayReshape[Outer[List,rowHeights,colWidths],{Times@@dims,2}]},
(*MatrixAlign, defined below, rescales and aligns each cell's data.*)
With[{undecoratedGrid=Partition[MapThread[MatrixAlign[alignment,#1,background][#2]&, {cellDims,augmentedData}],dims[[2]]]},
(*Add the spacing to each row.*)
With[{dividedRows=MapThread[Transpose[Riffle[#2,{ConstantArray[dividers[[2]],{#1,spacings[[2]]}]},{2,-2,2}]]&, {rowHeights,undecoratedGrid}]},
(*Add the spacing between rows.*)
With[{dividedColumn=Riffle[dividedRows,{Transpose[Riffle[ConstantArray[dividers[[1]],{spacings[[1]],#}]&/@colWidths,{ConstantArray[dividers[[3]],spacings]},{2,-2,2}]]},{2,-2,2}]},
(*Assemble all cell rows into actual character rows. We now have one large matrix.*)
With[{dividedGrid=Catenate[Map[Flatten,dividedColumn,{2}]]},
(*Add borders.*)
ArrayPad[dividedGrid,borderWidths,border]]]]]]]];
DataGrid[dims:{_Integer,_Integer},spacings_,borderWidths_,options_,data:{__List?MatrixQ}]:=
(*Calculate the max height for each row and max width for each column, and then just call the previous DataGrid function above.*)
With[
{rowHeights=Flatten@BlockMap[Max[Part[#,All,All,1]]&,ArrayReshape[Dimensions/@data,Append[dims,2],1],{1,dims[[2]]}],
colWidths=Flatten@BlockMap[Max[Part[#,All,All,2]]&,ArrayReshape[Dimensions/@data,Append[dims,2],1],{dims[[1]],1}]},
DataGrid[rowHeights,colWidths,spacings,borderWidths,options,data]];
(*This could probably be simplified, but I like having all of the aligment options explicit and separate for testability.*)
MatrixAlign[{-1,-1},dims_,pad_]:=PadRight[#,dims,pad]&;
MatrixAlign[{-1,0},dims_,pad_]:=PadRight[CenterArray[#,{Dimensions[#][[1]],dims[[2]]},pad],dims,pad]&;
MatrixAlign[{-1,1},dims_,pad_]:=PadRight[PadLeft[#,{Dimensions[#][[1]],dims[[2]]},pad],dims,pad]&;
MatrixAlign[{0,-1},dims_,pad_]:=CenterArray[PadRight[#,{Dimensions[#][[1]],dims[[2]]},pad],dims,pad]&;
MatrixAlign[{0,0},dims_,pad_]:=CenterArray[#,dims,pad]&;
MatrixAlign[{0,1},dims_,pad_]:=CenterArray[PadLeft[#,{Dimensions[#][[1]],dims[[2]]},pad],dims,pad]&;
MatrixAlign[{1,-1},dims_,pad_]:=PadLeft[PadRight[#,{Dimensions[#][[1]],dims[[2]]},pad],dims,pad]&;
MatrixAlign[{1,0},dims_,pad_]:=PadLeft[CenterArray[#,{Dimensions[#][[1]],dims[[2]]},pad],dims,pad]&;
MatrixAlign[{1,1},dims_,pad_]:=PadLeft[#,dims,pad]&;
(*While the grid functions make no assumptions about the format of the data, we will be using them with string/character data, and we will eventually want to output a calendar as a single large string. AsString gives us a standard method for transforming a matrix of characters into a string with rows delimited by newlines.*)
AsString[matrix_List?MatrixQ]:=StringRiffle[matrix,"\n",""];

View file

@ -0,0 +1,2 @@
(*Choose a best fit for our 1969 calendar data on 80 character wide display.*)
WidestFitDimensions[80,4,{0,0},MonthGrids1969]

View file

@ -0,0 +1,2 @@
(*Choose a best fit for our 1969 calendar data on 132 character wide display.*)
WidestFitDimensions[132,4,{0,0},MonthGrids1969]

View file

@ -0,0 +1,2 @@
(*Can we fit into a 20-character wide display?*)
WidestFitDimensions[20,4,{0,0},MonthGrids1969]

View file

@ -0,0 +1,9 @@
DataA={{"A"}};
GridA = DataGrid[{2},{5},{0,0},{{1,1},{1,1}},<|"border"->"*","alignment"->{1,-1}|>,{DataA}];
DataB = {{"B"},{"B"}};
GridB =
DataGrid[{4,3},{4,8},{2,3},{{0,0},{0,0}},<|"background"->".","alignment"->{0,1},"dividers"->{"-","|","+"}|>,{DataB,DataB,DataB}];
GridC =
DataGrid[{2,3},{2,3},{{0,1},{2,3}},<|"border"->"@","alignment"->{0,0},"dividers"->{"/","/","/"}|>,{GridA,GridB,GridA,GridB,GridA}];
DataGrid[{2,3},{2,12},{{0,0},{0,0}},<||>,{{{"A"}},{{"B"}},{{"C"}},GridA,GridB,GridC}]//AsString

View file

@ -0,0 +1,23 @@
HeadedGrid[
finalSpacings:{_Integer,_Integer},
finalBorderWidths:{{_Integer,_Integer},{_Integer,_Integer}},
finalOptions_Association,
headerCellGridder_Function,
rawHeaders:{__String},
dataCellGridder_Function,
rawData:{__String}]:=
With[
{finalDims={Ceiling[(Length@rawData+Length@rawHeaders)/Length@rawHeaders],Length@rawHeaders},
headerCells=headerCellGridder/@List/@List/@Characters[rawHeaders],
dataCells=dataCellGridder/@List/@List/@Characters[rawData]},
DataGrid[finalDims,finalSpacings,finalBorderWidths,finalOptions,Join[headerCells,dataCells]]];
(*An example with a few decorations:*)
HeadedGrid[
{1,1},
{{1,1},{1,1}},
<|"dividers"->{"-","|","+"},"border"->"*"|>,
DataGrid[{3},{6},{0,0},{{0,0},{0,0}},<|"background"->"-"|>,#]&,
{"Su","Mo","Tu","We","Th","Fr","Sa"},
DataGrid[{3},{4},{0,0},{{0,0},{0,0}},<|"alignment"->{-1,1}|>,#]&,
ToString/@Range[31]]//AsString

View file

@ -0,0 +1,26 @@
MonthGrid[
finalSpacings:{_Integer,_Integer},
finalBorderWidths:{{_Integer,_Integer},{_Integer,_Integer}},
finalOptions_Association,
titleGridder_Function,
monthName_String,
monthSpacings:{_Integer,_Integer},
monthBorderWidths:{{_Integer,_Integer},{_Integer,_Integer}},
monthOptions_Association,
headerCellGridder_Function,
weekdayNames:{__String},
dayCellGridder_Function,
dayOffset_Integer,
days:{__String}]:=
DataGrid[{2,1},finalSpacings,finalBorderWidths,finalOptions,
{titleGridder[List/@List/@Characters[monthName]],
HeadedGrid[monthSpacings,monthBorderWidths,monthOptions,headerCellGridder,weekdayNames,dayCellGridder,ArrayPad[days,{dayOffset,0},""]]}];
(*A compact example:*)
MonthGrid[{0, 0}, {{0, 0}, {0, 0}}, <||>,
DataGrid[{1, Length@#}, {0, 1}, {{1, 1}, {0, 0}}, <||>,ToUpperCase@#] &,
"September", {0, 1}, {{0, 0}, {0, 0}}, <||>,
DataGrid[{1}, {2}, {0, 0}, {{0, 0}, {0, 0}}, <||>, #] &,
{"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"},
DataGrid[{1}, {2}, {0, 0}, {{0, 0}, {0, 0}}, <|"alignment" -> {-1, 1}|>, #] &,
2, ToString /@ Range[31]]//AsString

View file

@ -0,0 +1,12 @@
FractalChars[rasterSize_,font_,char_String/;1==StringLength[char]]:=
ReplaceAll[ImageData[ImageCrop[Binarize[Rasterize[Style[char,FontFamily->font],RasterSize->rasterSize]]]],{1->" ",0->char}];
FractalChars[rasterSize_,font_,word_String]:=FractalChars[rasterSize,font,#]&/@Characters[word];
(*And here's the convenience function that ultimately calls DataGrid.*)
BannerGrid[finalSpacings_,finalBorderWidths_,finalOptions_,charGridder_Function,rasterSize_,text_String]:=
With[
{charData=FractalChars[rasterSize,"Courier",text]},
DataGrid[{1,StringLength@text},finalSpacings,finalBorderWidths,finalOptions,charGridder/@List/@charData]];
(*An example banner.*)
BannerGrid[{0,5},{{1,1},{0,0}},<|"border"->"X"|>,DataGrid[{1,1},{0,0},{{1,1},{0,0}},<||>,#]&,20,"1969"]//AsString

View file

@ -0,0 +1,9 @@
(*Mathematica makes it easy to get month names and day names for several standard calendars.*)
MonthNames[]:=MonthNames["Gregorian"];
MonthNames[calType_]:=Lookup[CalendarData[calType,"PropertyAssociation"],"MonthNames"];
WeekdayNames[]:=WeekdayNames["Gregorian"];
WeekdayNames[calType_]:=Lookup[CalendarData[calType,"PropertyAssociation"],"DayNames"];
(*Since month boundaries don't align with week boundaries on most calendars, we need to pad month data with empty cells. I was tempted to create a function that would generate offsets for a given year on a given calendar, but even that required too many decisions on the part of the calendar maker to be feasible. So, I removed all of the calendar semantics. If you provide the fixed small group length (week length), the initial offset, and the list of the large group lengths (month lengths), this function will give you the offsets you need for each large group (month).*)
Offsets[groupLength_,firstOffset_,lengths_List]:=FoldPairList[{#1,Mod[#1+#2,groupLength]}&,firstOffset,lengths];

View file

@ -0,0 +1,23 @@
Data1969=GroupBy[Most[DateRange[DateObject[{1969,1,1}],DateObject[{1+1969,1,1}]]],DateValue[#,"MonthName"]&];
InitialOffset1969=QuantityMagnitude[DateDifference[PreviousDate[DateObject[{1969,1,1}],Sunday],DateObject[{1969,1,1}]]];
MonthLengths1969=Length/@Values[Data1969];
Offsets1969=Offsets[7,InitialOffset1969,MonthLengths1969];
MonthNames1969=Keys@Data1969;
YearBanner1969=BannerGrid[{0,0},{{1,1},{0,0}},<|"border"->"X"|>,DataGrid[{1,1},{1,1},{{1,1},{5,5}},<||>,#]&,13,"1969"];
MonthGrids1969=
With[
{monthNameGridder=DataGrid[{1,Length@#},{0,1},{{1,1},{0,0}},<||>,ToUpperCase@#]&,
dayNameGridder=DataGrid[{1},{2},{0,0},{{0,0},{0,0}},<|"alignment"->{0,-1}|>,#]&,
dayGridder=DataGrid[{1},{2},{0,0},{{0,0},{0,0}},<|"alignment"->{-1,1}|>,#]&},
With[
{monthGridder=
MonthGrid[{0,0},{{0,0},{0,0}},<||>,monthNameGridder,#1,
{0,1},{{0,0},{0,0}},<||>,dayNameGridder,WeekdayNames[],
dayGridder,#2,#3]&},
MapThread[monthGridder,{MonthNames1969,Offsets1969,Map[ToString,Range/@MonthLengths1969,{2}]}]]];
MonthsGrid1969=DataGrid[{3,4},{1,4},{{0,0},{0,0}},<||>,MonthGrids1969];
DataGrid[{2,1},{2,0},{{0,0},{0,0}},<||>,{YearBanner1969,MonthsGrid1969}]//AsString

View file

@ -0,0 +1,30 @@
Dates1582France=
Join[
DateRange[DateObject[{1582,1,1},CalendarType->"Julian"],DateObject[{1582,10,4},CalendarType->"Julian"],CalendarType->"Julian"],
DateRange[DateObject[{1582,10,15}],DateObject[{1582,12,31}]]];
Data1582France=GroupBy[Dates1582France,DateValue[#,"MonthName"]&];
InitialOffset1582France=
QuantityMagnitude[DateDifference[
PreviousDate[CalendarConvert[DateObject[{1582,1,1},CalendarType->"Julian"],"Gregorian"],Sunday],
CalendarConvert[DateObject[{1582,1,1},CalendarType->"Julian"],"Gregorian"]]];
MonthLengths1582France=Length/@Values[Data1582France];
DatesByMonth1582France=Map[DateString[#,"DayShort"]&,Values[Data1582France],{2}];
Offsets1582France=Offsets[7,InitialOffset1582France,MonthLengths1582France];
MonthNames1582France=Keys@Data1582France;
With[
{yearBanner=BannerGrid[{0,0},{{1,1},{0,0}},<|"border"->"X"|>,DataGrid[{1,1},{1,1},{{1,1},{5,5}},<||>,#]&,13,"1582"],
monthNameGridder=DataGrid[{1,Length@#},{0,1},{{1,1},{0,0}},<||>,ToUpperCase@#]&,
dayNameGridder=DataGrid[{1},{2},{0,0},{{0,0},{0,0}},<|"alignment"->{0,-1}|>,#]&,
dayGridder=DataGrid[{1},{2},{0,0},{{0,0},{0,0}},<|"alignment"->{-1,1}|>,#]&},
With[
{monthGridder=MonthGrid[{0,0},{{0,0},{0,0}},<||>,monthNameGridder,#1,{0,1},{{0,0},{0,0}},<||>,dayNameGridder,WeekdayNames[],dayGridder,#2,#3]&},
With[
{monthCells=MapThread[monthGridder,{MonthNames1582France,Offsets1582France,Map[ToString,Range/@MonthLengths1582France,{2}]}],
octoberCells=DataGrid[{1},{2},{0,0},Switch[#,"4"|"15",{{1,1},{1,1}},_,{{0,0},{0,0}}],<|"alignment"->{-1,1},"border"->"|","background"->Switch[#,"4"|"15","|",_," "]|>,{{Characters[#]}}]&/@ArrayPad[DatesByMonth1582France[[10]],{Offsets1582France[[10]],0},""]},
With[
{octoberGrid=
DataGrid[{2,1},{0,0},{{1,1},{1,1}},<|"border"->"*"|>,
{monthNameGridder[{{Characters[MonthNames1582France[[10]]]}}],
DataGrid[{1+Ceiling[Length@octoberCells/7],7},{0,1},{{0,0},{0,0}},<|"alignment"->{0,0}|>,octoberCells]}]},
DataGrid[{2,1},{2,0},{{0,0},{0,0}},<||>,{yearBanner,DataGrid[{3,4},{2,4},{{0,0},{0,0}},<|"alignment"->{-1,0}|>,ReplacePart[monthCells,10->octoberGrid]]}]]]]]//AsString

View file

@ -0,0 +1,17 @@
WidestFitDimensions[
targetWidth_Integer,
columnSpacings_Integer,
leftRightBorderWidths:{_Integer,_Integer},
data:{__List?MatrixQ}]:=
With[
{widths=Last/@Dimensions/@data,
fullWidthOfRow=Total[ArrayPad[Riffle[#,columnSpacings],leftRightBorderWidths,1]]&},
With[
{fullWidthOfGrid=Max[fullWidthOfRow/@#]&},
With[
{isTooLarge=(targetWidth<fullWidthOfGrid[#])&},
With[
{bestFitGrid=
NestWhile[Partition[widths,-1+Last@Dimensions@#,-1+Last@Dimensions@#,{1,1},0]&,
{widths},isTooLarge[#]&,1,-1+Length@widths]},
<|"dimensions"->Dimensions@bestFitGrid,"width"->fullWidthOfGrid@bestFitGrid|>]]]];