UPDATE: My favorite: CandleStickChart
-- easy, convenient and with a lot of cool options/features.
In particular, the last argument #6
of the color function can be used to exactly what the OP wants. All one needs is to transform the input data to a form that CandleStickChart
accepts: that is, a list {{date},{prices}}
for each data point. This is easily done using MapIndexed
as in the example below:
CandlestickChart[MapIndexed[{#2, #1} &, Riffle[dt, dt] // Transpose],
ChartElementFunction -> "ArrowCandlestick",
ColorFunction -> Function[{date, open, high, low, close, trend}, Hue[trend]]]
Some test data:
ddd = RandomInteger[100, 10];
dt = {ddd, ddd + (RandomChoice[RandomChoice[{-#, #}] & /@ Range[10, 25, 5]] & /@ Range[10])};
Post-processing ListPlot
output to replace the Line
primitive with appropriate directive/primitive combinations.
lstplt =ListPlot[dt,Filling -> {1 -> {2}},ImageSize->300,ColorFunctionScaling-> False];
{coords,lines} = {Cases[ lstplt,
GraphicsComplex[crds:{x__},___] :> crds, {0, Infinity}][[1]],
Cases[lstplt, ll : (Line[{_, _}] ..) :> ll, {0, Infinity}]};
Row[lstplt /. Line[{xx__}] :> {Thickness[.03], Opacity[.8], CapForm["Round"],
#[Rescale[coords[[{xx}]][[2, 2]] - coords[[{xx}]][[1, 2]],
{Min@#, Max@#} &@(dt[[1]] - dt[[2]]), {0.1, .9}]],
Line[coords[[{xx}]]]} & /@ {Hue, ColorData["BrightBands"]}, Spacer[10]]
Using DiscretePlot
and its options ExtentSize
, ExtentElementFunction
, and ExtentMarkers
:
diff = Rescale[dt[[1]] - dt[[2]], {Min@#, Max@#} &@(dt[[1]] - dt[[2]]), {0.1, .9}];
g[{{xmin_, xmax_}, {ymin_, ymax_}}, ___] :=
Rectangle[{xmin, ymin}, {xmax, ymax}, RoundingRadius -> Offset[10]];
sh1 = Show[DiscretePlot[{dt[[1]][[k]], dt[[2]][[k]]}, {k, Range[10]},ImageSize -> 300,
ExtentSize -> .5, Filling -> {1 -> {2}}, ColorFunctionScaling -> False,
ColorFunction -> Function[{x, y}, ColorData["BrightBands"][diff[[Round[ x]]]]],
ExtentElementFunction -> g],
DiscretePlot[{dt[[1]][[k]], dt[[2]][[k]]}, {k, Range[10]}, Joined -> {True, True},
PlotStyle -> Thick, Filling -> None, ImageSize -> 300]];
sh2 = Show[DiscretePlot[{dt[[1]][[k]], dt[[2]][[k]]}, {k, Range[10]},ImageSize -> 300,
ExtentElementFunction -> "ArrowRectangle", ExtentSize -> .5,
Filling -> {1 -> {2}}, ColorFunctionScaling -> False,
ColorFunction -> Function[{x, y}, ColorData["BrightBands"][diff[[Round[ x]]]]]],
DiscretePlot[{dt[[1]][[k]], dt[[2]][[k]]}, {k, Range[10]}, Joined -> {True, True},
PlotStyle -> Thick, Filling -> None, ImageSize -> 300]];
Row[{sh1, sh2}, Spacer[10]]