How to make the bar be a button?

8

1

This is a BarChart:

BarChart[Tooltip[#, "I want tip"] & /@ {1, 2, 3}, 
 ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel"]

I hope when I left-right the bar, then I copy the value into the clipboard. Is it possible?

yode

Posted 2019-01-24T04:08:42.133

Reputation: 19 940

1how about clicking the bar: Deploy@BarChart[Button[#, CopyToClipboard[#]] & /@ {1, 2, 3}, ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel"]? – kglr – 2019-01-24T04:23:11.823

@kglr I'm sorry I make a little tuning in the origin question. Actually I want the Button and Tooltip coexsit... – yode – 2019-01-24T04:29:10.467

Tooltip is not removed. If the tooltips are removed in your version/os, you can use Button[Tooltip@#, CopyToClipboard[#]] & as wrapper (You might want to add the option PerformanceGoal -> "Quality"if there are many bars in your chart). – kglr – 2019-01-24T04:32:46.423

@kglr Do you mean BarChart[Button[Tooltip[#, "I want tip"], #] & /@ {1, 2, 3}, ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel"] work for you? My version is 11.2.. – yode – 2019-01-24T04:36:09.707

@kglr The code can draw the bar graphics here, but it cannot copy the value into the clipboard. Please check....

– yode – 2019-01-24T04:42:35.287

yode you need to use BarChart[Button[Tooltip[#, "I want tip"], CopyToClipboard@#] & /@ {1, 2, 3}, ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel"] – kglr – 2019-01-24T04:46:22.937

@kglr I'm sorry, your comment is the answer indeed.. Thanks..If you submit it I will accept it..

– yode – 2019-01-24T05:15:26.683

ref / BarChart / Scope / Wrappers. – Kuba – 2019-01-24T07:10:35.837

Answers

9

You can wrap input data with Button with action CopyToClipboard:

data = {1, 2, 3};
BarChart[Button[Tooltip[#, "I want tip"], CopyToClipboard@#] & /@ data, 
  ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel"]

Update: You can add tool tips in several ways:

tooltips = {"tooltip1", "tooltip2", "tooltip3"};
  1. Use Tooltip as a wrapper on input data:

BarChart[Button[Tooltip@##, CopyToClipboard[#]] & @@@ 
  Transpose[{{1, 2, 3}, tooltips}],
 ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel"]
  1. Use Placed[tooltips, Tooltip] as the setting for the option ChartLabels:

BarChart[Button[#, CopyToClipboard[#]] & /@ data,
 ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel",
 ChartLabels -> Placed[tooltips, Tooltip]]
  1. Use a custom ChartElementFunction and pass the tooltips as metadata:

ceF[cedf_: "GlassRectangle"] := Button[Tooltip[ChartElementData[cedf][##], #3[[1]]], 
BarChart[Thread[data -> tooltips], ChartElementFunction -> ceF[], 
 ChartStyle -> "Pastel", PlotLabel -> (Paste[])]

enter image description here

kglr

Posted 2019-01-24T04:08:42.133

Reputation: 302 076

3

This is to get a slightly modified version of kglr's comments recorded as an answer. The modification is provide individual tooltips for each bar.

BarChart[
  MapThread[
    Button[Tooltip[#1, #2], CopyToClipboard@#1] &,
    {{1, 2, 3}, {"left", "mid", "right"}}], 
 ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel"]

chart.1

Also, be aware that if only want the tooltip to show the value of the bar, you don't need to specify Tooltip; it will be supplied automatically.

BarChart[Button[#, CopyToClipboard@#] & /@ {1, 2, 3}, 
  ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel"]

chart.2

m_goldberg

Posted 2019-01-24T04:08:42.133

Reputation: 104 223

Thanks very much..:) – yode – 2019-01-25T03:56:40.477