2
$\begingroup$

I have tried a method to highlight certain rows in various tableform. An example:

product = {"Apple", "Banana", "Watermelone", "Strawberry"};
value = {5, 10, 40, 20};
percent = N[value/Total[value]];
rank = value /. Thread[# -> Ordering[#, All, Greater]] &@Union@value;
TableForm[Map[{#, product[[#]], value[[#]], 100*percent[[#]], rank[[#]]} &, Range[4]], TableHeadings -> {None, {"Serial Nr.", "Product", "Value", "%", 
"Ranking"}}, TableSpacing -> {2, 2}]

It consists this table:

enter image description here

I’d like to automatically highlight data which meets bigger then the 50% and this table arrange in according to rank.

Finally a table would look like:

enter image description here

Thanks

$\endgroup$
  • 1
    $\begingroup$ Go with Grid. There is an option Background, there is also a function Sort/SortBy. That's all you need to check. TableHeadings are nice feature but lack of others makes Grid better suited for your problem. $\endgroup$ – Kuba Sep 3 '15 at 10:38
  • $\begingroup$ @Kuba can you maybe futher help with this grid...Grid[Map[{#, product[[#]], value[[#]], 100*percent[[#]], rank[[#]]} &, Range[4]], Background -> Green].. thats not looking good :).. how can I use this sort or Sortby functions? $\endgroup$ – usernew1 Sep 3 '15 at 10:51
  • 2
    $\begingroup$ Type Grid then press F1, then go to Options section. The same with Sort. $\endgroup$ – Kuba Sep 3 '15 at 11:59
  • $\begingroup$ Or you can use Item. $\endgroup$ – Silvia Sep 3 '15 at 12:56
  • $\begingroup$ @Kuba Grid[Map[{#, product[[#]], value[[#]], 100*percent[[#]], rank[[#]]} &, Range[4]], Frame -> All, Spacings -> {4, 0.5}, Background -> {None, {3 -> Green}}].. i dont want to do it manuel like this.. is the no options that I do it automatically? if the percent more than 50% that can make it automatically green or what else..? $\endgroup$ – usernew1 Sep 3 '15 at 13:05
6
$\begingroup$

Not necessarily elegant or optimal, but this seems to work:

hdr = {"Serial Nr.", "Product", "Value", "%", "Ranking"};
serial = Range[Length[product]];
value = {5, 10, 40, 20};
percent = 100 N[value/Total[value]];
rank = value /. Thread[# -> Ordering[#, All, Greater]] &@Union@value;

data = Transpose[{serial, product, value, percent, rank}];
sorted = Sort[data, #1[[4]] > #2[[4]] &];

over50 = 1 + Pick[sorted[[All, 5]], Thread[sorted[[All, 4]] >= 50]];
coloring = Rule[#, Green] & /@ over50;

Grid[Prepend[sorted, hdr], Frame -> All, Spacings -> {4, 0.5},
    Alignment -> {{Left, Left, Right, ".", Left}, None}, 
    Background -> {None, coloring}]

enter image description here

|improve this answer|||||
$\endgroup$
  • $\begingroup$ thanks for ur answer..not mather its work :) $\endgroup$ – usernew1 Sep 4 '15 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.