3
$\begingroup$

I'm trying to implement a "fancy control" in my .cdf using Manipulate... My intention is to have a control in form of a table of values (I would prefer Grid), with conditional formatted background of cells, where the end-user should be able to choose and select a row. The data in the selected row should be allocated to a variable and used for further calculations. So far I was able to prepare everything (using also the great answer to one of my other questions concerning conditional formatting), but I don't seem to find a way to use the table of values as input (Control) in Manipulate. Here my sample of code:

 Manipulate[
 cG = 78500;
 len = 400;
 s[x_] := (2105 - 780*Log10[x])/Sqrt[3];
 t[x_] := 360*s[x]*len/(Pi*cG*x);
 data = Prepend[
           Transpose[{Table[d, {d, 1., 10., 0.1}], 
              Table[s[d], {d, 1., 10., 0.1}], 
              Table[t[d], {d, 1., 10., 0.1}]}], 
           {"d", "S", "T"}];
 crules = Table[n -> If[data[[n, 3]] < 90., LightRed, 
                     If[data[[n, 3]] < 120., LightYellow, LightGreen]],
          {n, 1,Length[data]}];
 maintable = Grid[data, Frame -> All, Background -> {None, crules}];
 main = Pane[maintable, ImageSize -> {All, 150}, Scrollbars -> True];
 Print[sel],
 Control[{sel, ListPicker[Dynamic[sel], main]}]]

The table to choose from should look like this:

enter image description here

I would appreciate some advice at this point. Thanks a lot in advance!

.......

Based on m_goldberg answer, I was able to further match the appearance of the table to my needs:

s[x_] := (2105 - 780*Log10[x])/Sqrt[3];
With[{cG = 78500, len = 400}, t[x_] := 360*s[x]*len/(Pi*cG*x)];
data = Transpose[{Table[d, {d, 1., 10., 0.1}], 
    Table[s[d], {d, 1., 10., 0.1}], Table[t[d], {d, 1., 10., 0.1}]}];
crules = Table[
   n -> If[data[[n, 3]] < 90., LightRed, 
     If[data[[n, 3]] < 120., LightYellow, LightGreen]], {n, 1,
        Length[data]}];

With[{cellw = 6},
  dataGrid = 
   Column[{
     Pane[
      Grid[{{"d", "S", "T"}}, Frame -> All, Background -> LightGray, 
       ItemSize -> cellw]], 
     Pane[NumberForm[
       Grid[data, Frame -> All, Background -> {None, crules}, 
        ItemSize -> cellw], {10, 3}], ImageSize -> {All, 350}, 
      AppearanceElements -> {}, Scrollbars -> {False, True}]}, 
    Dividers -> All,
    ItemSize -> {{21.2, 5}}]];

Manipulate[dataGrid, SaveDefinitions -> True]

Now it looks like this:

enter image description here

I used though Column instead of Grid, cause it seemed easier to me to align the vertical dividers. I left the other task out (ability to select data from table and assign it to a variable), as it looks indeed much more complex. I use the table just to visualize the data and I will input separately the values for further calculations. Thanks a lot @m_goldberg.

$\endgroup$
  • 1
    $\begingroup$ It's quite broad at the moment. Let's start with something working DynamicModule[{ data = RandomReal[1, {3, 10}], x, result = "" }, Grid[{{ ListPicker[Dynamic@x, data], Button["plot", result = ListPlot@x] }, {Dynamic@result, SpanFromLeft} }] ] - what do you want to change/add there? $\endgroup$ – Kuba Jun 3 '16 at 10:50
  • $\begingroup$ @Kuba looks very promising, but I'd like to have data in form of a table with Gridlines, a "freezed" header ({"d", "S","T"}), working conditional formatting of the cells for better visual feedback for user and scrolling bars (at least vertical), due to the big amount of rows. Would it be possible? If you extract the code from Manipulate (till Print) and evaluate it, you will see the targeted shape of the table. $\endgroup$ – Conrad Jun 3 '16 at 11:45
  • $\begingroup$ I added a screenshot of the table. In addition, as I said, I would like to have the headers of table always visible. $\endgroup$ – Conrad Jun 3 '16 at 11:53
  • 1
    $\begingroup$ That's a lot of work and you need to be very precise in your requirements in order to receive attention. Basically you need How can I create an advanced grid interface? + How to visualize/edit a big matrix as a table? + Handling selection of row or rows. $\endgroup$ – Kuba Jun 3 '16 at 12:09
3
$\begingroup$

I can't help you with the list picker control because I can't figure out what you are trying to do with it. However, I can help you with the scrolling data display.

The approach is two-fold.

  1. Remove all definitions of static items from the Manipulate expression to prevent unnecessary dynamic evaluation.

  2. Build the data display with two panes, the first for the titles and the second for the data. Only the second will have scroll bars, so the title will remain visible at the top to the content pane.

To get things to line up nicely, it will be necessary to fuss with the sizes of the various grid items.

s[x_] := (2105 - 780*Log10[x])/Sqrt[3];
With[{cG = 78500, len = 400}, t[x_] := 360*s[x]*len/(Pi*cG*x)];
data =
  Transpose[
    {Table[d, {d, 1., 10., 0.1}],
     Table[s[d], {d, 1., 10., 0.1}],
     Table[t[d], {d, 1., 10., 0.1}]}];
crules = 
  Table[
    n -> 
      If[data[[n, 3]] < 90., 
        LightRed, 
        If[data[[n, 3]] < 120., LightYellow, LightGreen]],
    {n, 1, Length[data]}];

With[{w = 180},
  dataGrid = 
    Grid[{
      {Pane[
         Style[
           Grid[
             {{Item["d", ItemSize -> 3], 
               Item["S", ItemSize -> 4], 
               Item["T", ItemSize -> 6]}}], 
           "SR", Bold],
         ImageSize -> {w, Automatic}]},
      {Pane[
         Grid[data, Frame -> All, Background -> {None, crules}],
         ImageSize -> {w, 400}, 
         AppearanceElements -> {},
         Scrollbars -> {False, True}]}},
      Dividers -> All]];

Manipulate[dataGrid,
  SaveDefinitions -> True]

table

The Manipulate, because it has no active controls, is only providing a display panel. I think you will have to better explain the controls you want and what their function should be before any further help can be given (at least by me).

| improve this answer | |
$\endgroup$

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.