3

What I would like to do is explore a data set by clicking on elements in a BarChart. I have some existing code which uses a manipulate which I was hoping to modify.

classification = {"class1", "class2", "class3"};
cause = {"cause1", "cause2", "cause3", "cause4", "cause5"};
dataSet = Table[{classification[[RandomInteger[{1, 3}]]],
cause[[RandomInteger[{1, 5}]]], RandomReal[{0, 10}]}, {i, 1, 50}];

(*generate the classification BarChart*)
byClass = GatherBy[dataSet, #[[1]] &];
durationByClass = {First[#][[1]], Total[#[[All, 3]]]} & /@ byClass;
classChart = BarChart[durationByClass[[All, 2]], 
ChartLabels -> durationByClass[[All, 1]], 
ChartStyle -> {LightGray, LightGray, LightGray}, 
PlotLabel -> "Data by Classification", ImageSize -> 400];

(*a function to get the cause chart*)
getCauseChartExample[classificationIndex_] := Module[{},
durationByCause = GatherBy[
Select[dataSet, #[[1]] == 
   classification[[classificationIndex]] &], #[[2]] &];
plotData = {First[#][[2]], Total[#[[All, 3]]]} & /@ durationByCause;
BarChart[plotData[[All, 2]], ChartLabels -> plotData[[All, 1]], 
ChartStyle -> LightGray,
PlotLabel -> classification[[classificationIndex]] <> " by Cause", 
ImageSize -> 400]
]

Manipulate[Row[{classChart, getCauseChartExample[classIndex]}], {classIndex, {1,2, 3}}]

The output looks like this;

enter image description here

I would like to replace the Manipulate with the ability to mouse click the bars on the chart on the left and have the corresponding chart display on the right.

Cam
  • 1,596
  • 12
  • 20

1 Answers1

5

I'll work with a minimal example instead of your code.

Start by setting a variable status:

status=1;

Now, create the clickable BarChart that'll change the value of status depending on what bar is getting clicked:

BarChart[{Button[1, status = 1], Button[2, status = 2], 
  Button[3, status = 3]}]

Define a data set:

data = Partition[RandomInteger[10, 9], 3];

Now, build a BarChart that will choose a data set depending on the value of status:

Dynamic@BarChart[data[[status]]]

All code in one, easier to copy:

status=1;
BarChart[{Button[1, status = 1], Button[2, status = 2], 
  Button[3, status = 3]}]
data = Partition[RandomInteger[10, 9], 3];
Dynamic@BarChart[data[[status]]]

Specifically in your case you need to change the first argument in your cause bar chart to this:

MapIndexed[Button[#, classindex = #2] &, durationByClass[[All, 2]]]

And you need to change the last row to:

Row[{classChart, Dynamic@getCauseChartExample[First@classindex]}]
C. E.
  • 70,533
  • 6
  • 140
  • 264