I’m trying to making a widget for time and have a view of my favorite stock on the stock market. So after some searching I’m using ECharts that seem the best since now.
However the charts in the examples on ECharts’s site should be completed because according to the definition they should have full and empty candles as well as colored ones.
- Solid candles show that the current close price is less than the current open price.
- Hollow candles show that the current close price is greater than the current open price.
- Red candles show that the current close price is less than the previous close price.
- Green candles show that the current close price is greater than the previous close price.
Source: Wikipedia – Candlestick chart (see the link if you want to know more)
Solid and hollow candles
So how to make a chart like this with candles solid and hollow.
How I would like it to be (but with colors):
(Source: Wikipedia – Candlestick chart)
Colored like this https://ibb.co/1fhsPrf
(Source: tradingview.com – Title: Tesla)
Uniform volumn color
Also I want to know how to have the volumns all of a color (example: azure) (together on the same grid, candles and volumns, not 2 grids).
P.S.
I would also like to point out (if any of the creators of ECharts reads) that the volumns are sometimes a color that does not correspond to the rise or fall (e.g. it may happen that it is red if the stock goes up).
2
Answers
Solution
Candlestick
solid
andhollow
displayYou can handle this by configuring the
series-candlestick.itemStyle
for the candlestick series.Uniform volume color
You can control this in the
series-bar.itemStyle
.Example
More information
series-candlestick.itemStyle
– ECharts Docscolor
property – ECharts Docscolor0
property – ECharts DocsborderColor
property – ECharts DocsborderColor0
property – ECharts Docsseries-bar.itemStyle
– ECharts Docscolor
property – ECharts DocsTo customize individual elements in a candlestick chart, I did some research. Initially, I intended to use the traditional approach where
itemStyle.[parameter]
is assigned afunction
instead of a fixedstring
value, but I encountered the same issue as described in apache/echarts #12840 issue.Following that, instead of using
dataset.source
for data transfer, I split the data set into two parts:values
(candlestickData
andvolumeData
for two series) andlabels
(dateData
for X Axis). I extracted the dates asdateData
and collected the remaining values ascandlestickData
andvolumeData
.By doing this,
candlestickData
andvolumeData
is passed to theseries.data
parameter, which allows for injecting customitemStyle
. When injecting a customitemStyle
, the item is an object rather than an array, where the data is added through the value parameter. See the example for details.In my code, the sole purpose was to illustrate how you can assign custom colors to a specific element. Which element and the logic you want to apply is up to you; it’s an additional detail that’s not essential to the solution.