Created
November 10, 2020 05:55
-
-
Save sguzman/b757892ccd111d5a664aca551570f068 to your computer and use it in GitHub Desktop.
Pie chart of diff industries by specified prop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
limit = 100; | |
s = "TotalRevenue"; | |
prop[p_String] := prop[p] = EntityProperty["Company", p]; | |
c[e_, p_String] := c[e, p] = CompanyData[e, p]; | |
ind[e_] := ind[e] = c[e, "Industry"]; | |
m[e_, p_String] := m[e, p] = Not@MissingQ@c[e, p]; | |
es[n_Integer, p_String] := | |
es[n, p] = | |
EntityList@EntityClass["Company", {prop[p] -> TakeLargest[n]}]; | |
sel[n_Integer, p_String] := sel[n, p] = Select[es[n, p], m[#, p] &]; | |
ga[n_Integer, p_String] := ga[n, p] = GatherBy[sel[n, p], ind]; | |
quant[es_, p_] := | |
quant[es, p] = QuantityMagnitude@Total@Map[c[#, p] &, es]; | |
sum[es_] := sum[es] = Total@Map[Last, es]; | |
seg[n_Integer, p_String] := | |
seg[n, p] = Table[{ind@First@g, quant[g, p]}, {g, ga[n, p]}]; | |
sort[n_Integer, p_String] := | |
sort[n, p] = ReverseSortBy[seg[n, p], Last] | |
pie[n_Integer, p_String] := | |
pie[n, p] = | |
PieChart[Last /@ sort[n, p], | |
ChartLegends -> First /@ sort[n, p], | |
ChartLabels -> Callout[First /@ sort[n, p]], | |
ColorFunction -> "DarkRainbow", | |
ImageSize -> Full | |
] | |
pie[100, "TotalRevenue"] | |
Clear[limit, s, prop, ind , c, m, es, sel, ga, quant, seg, sum, pie, | |
sort]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment