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$
6
  • 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
    Commented Sep 3, 2015 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
    Commented Sep 3, 2015 at 10:51
  • 2
    $\begingroup$ Type Grid then press F1, then go to Options section. The same with Sort. $\endgroup$
    – Kuba
    Commented Sep 3, 2015 at 11:59
  • $\begingroup$ Or you can use Item. $\endgroup$
    – Silvia
    Commented Sep 3, 2015 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
    Commented Sep 3, 2015 at 13:05

1 Answer 1

7
$\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

$\endgroup$
1
  • $\begingroup$ thanks for ur answer..not mather its work :) $\endgroup$
    – usernew1
    Commented Sep 4, 2015 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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