6
$\begingroup$

Floating Bar Chart from Google

Does anyone know how to make floating bar charts in Mathematica v11.0? I can't seem to find any similar thread or related documentation. I'd like to make a simple one like the one above.

$\endgroup$
2
  • 2
    $\begingroup$ Just because you can do this doesn't mean you should. If you're wanting to show both a "trend over time" and some measure of spread, then using vertical lines allows a more accurate comparison especially if you have many more than 4 bars. (At least you're using bars rather than elephants or bunny rabbits.) $\endgroup$
    – JimB
    Commented Jan 30, 2017 at 2:25
  • $\begingroup$ closely related Q/A: How to create a stacked BarChart with custom bar origins as function? $\endgroup$
    – kglr
    Commented Aug 18, 2017 at 14:32

2 Answers 2

8
$\begingroup$

A quick and dirty approach would be to trick BarChart into plotting two datasets, one of which is represented by transparent empty bars; this will be combined with ChartLayout -> "Stacked":

BarChart[
 {{2, 3}, {4, 6}, {2, 7}},
 ChartLayout -> "Stacked",
 ChartStyle -> {Directive[FaceForm[], EdgeForm[]], Red}
]

Mathematica graphics

The data is presented as a list of intervals; each value in the interval representing the starting value and the height of the bar, respectively.

$\endgroup$
1
  • $\begingroup$ Very sneaky and yet elegant, MarcoB. Thank you! $\endgroup$
    – drc7af
    Commented Jan 31, 2017 at 2:43
3
$\begingroup$

Update: The function Charting`RangeBarChart does not allow multiple data sets. To deal with multiple data sets, we can use a transformation of the data and define a custom ChartElementFunction to modify the rectangles:

ClearAll[ceF, trnsfrmF]
ceF[cedf_: "GlassRectangle", o : OptionsPattern[]] :=
  Module[{origin = Charting`ChartStyleInformation["BarOrigin"], box = #},
   Switch[origin, Bottom, box[[2, 1]] = #3[[1]], Top, box[[2, 2]] = -#3[[1]], 
     Left, box[[1, 1]] = #3[[1]], Right, box[[1, 2]] = -#3[[1]]]; 
   ChartElementDataFunction[cedf, o][box, ##2]] &

trnsfrmF = #2 -> # & @@@ # &;

data = {{2, 3}, {4, 7}, {2, 6}, {8, 10}};

BarChart[{trnsfrmF @ data, trnsfrmF @ (1 + data)}, 
 ChartStyle -> 1,
 ChartElementFunction -> ceF["FadingRectangle"],
 ChartLabels -> {{"Group1", "Group2"}, {"A", "B", "C", "D"}}]

Mathematica graphics

Row[BarChart[{trnsfrmF@data, trnsfrmF@(1 + data)}, ChartStyle -> 1, ImageSize -> 200,
    PlotLabel -> Style["BarOrigin -> " <> ToString[#], 12, "Panel"],
    ChartElementFunction -> ceF["FadingRectangle"],
    ChartLabels -> {{"Group1", "Group2"}, {"A", "B", "C", "D"}}, 
    BarOrigin -> #] & /@ {Bottom, Top, Left, Right}, Spacer[30]]

Mathematica graphics

Original post:

The undocumented function Charting`RangeBarChart does exactly what is required:

Charting`RangeBarChart[{{2, 3}, {4, 7}, {2, 6}, {8,10}},   
 ChartElementFunction -> "GlassRectangle", ChartStyle -> 1, 
 ChartLabels -> {"A", "B", "C", "D"}] 

Mathematica graphics

$\endgroup$

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.