8
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ how about clicking the bar: Deploy@BarChart[Button[#, CopyToClipboard[#]] & /@ {1, 2, 3}, ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel"]? $\endgroup$ – kglr Jan 24 at 4:23
  • $\begingroup$ @kglr I'm sorry I make a little tuning in the origin question. Actually I want the Button and Tooltip coexsit... $\endgroup$ – yode Jan 24 at 4:29
  • $\begingroup$ 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). $\endgroup$ – kglr Jan 24 at 4:32
  • $\begingroup$ @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.. $\endgroup$ – yode Jan 24 at 4:36
  • $\begingroup$ @kglr The code can draw the bar graphics here, but it cannot copy the value into the clipboard. Please check.... $\endgroup$ – yode Jan 24 at 4:42
9
$\begingroup$

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

$\endgroup$
3
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Thanks very much..:) $\endgroup$ – yode Jan 25 at 3:56

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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