Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color options for each data point #4

Merged
merged 12 commits into from
May 18, 2024
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LightweightCharts"
uuid = "d6998af1-87ca-4e7f-83d4-864c79a249fa"
version = "1.0.1"
version = "1.1.0"

[deps]
Serde = "db9b398d-9517-45f8-9a95-92af99003e0e"
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,16 @@ panel = lwc_panel(
price_scale_id = LWC_LEFT,
),
lwc_histogram(
map(x -> x.openTime, ohlc.result),
map(x -> x.volume, ohlc.result);
map(
x -> LWCSimpleChartData(
x.openTime,
x.volume,
color = x.openPrice > x.closePrice ? "rgba(222, 94, 87, 0.5)" : "rgba(82, 164, 154, 0.5)",
),
ohlc.result,
);
label_name = "lwc_histogram",
base = -100.0,
color = "rgba(47, 112, 181, 0.5)",
price_scale_id = LWC_RIGHT,
),
name = "ETHUSDT | Binance Spot",
Expand Down
Binary file modified docs/src/assets/panel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ panel = lwc_panel(
price_scale_id = LWC_LEFT,
),
lwc_histogram(
map(x -> x.openTime, ohlc.result),
map(x -> x.volume, ohlc.result);
map(
x -> LWCSimpleChartData(
x.openTime,
x.volume,
color = x.openPrice > x.closePrice ? "rgba(222, 94, 87, 0.5)" : "rgba(82, 164, 154, 0.5)",
),
ohlc.result,
);
label_name = "lwc_histogram",
base = -100.0,
color = "rgba(47, 112, 181, 0.5)",
price_scale_id = LWC_RIGHT,
),
name = "ETHUSDT | Binance Spot",
Expand Down
1 change: 1 addition & 0 deletions docs/src/pages/charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

```@docs
LWCChart
LWCSimpleChartData
```

## Line
Expand Down
4 changes: 4 additions & 0 deletions src/LightweightCharts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ function Base.propertynames(h::AbstractChartSettings)
return to_camelcase.(n)
end


artememelin marked this conversation as resolved.
Show resolved Hide resolved
function Serde.SerJson.ser_name(::Type{A}, ::Val{T}) where {A<:AbstractChartData,T}
return to_camelcase(T)
end
artememelin marked this conversation as resolved.
Show resolved Hide resolved
function Serde.SerJson.ser_name(::Type{A}, ::Val{T}) where {A<:AbstractChartSettings,T}
return to_camelcase(T)
end
Expand Down
145 changes: 138 additions & 7 deletions src/chart_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,110 @@ using NanoDates

using ..LightweightCharts

"""
LWCSimpleChartData(time::Int64, value::Real; kw...)
LWCSimpleChartData(time::TimeType, value::Real; kw...)

Representation of data for [`lwc_line`](@ref), [`lwc_area`](@ref), [`lwc_baseline`](@ref) and [`lwc_histogram`](@ref) methods.

## Fields
- `time::Int64`
- `value::Float64`

## Keyword arguments
| Name::Type | Default (Posible values) | Description |
|:-----------|:-------------------------|:------------|
| `line_color::String` | `nothing` | Line color. |
| `top_color::String` | `nothing` | Top color. |
| `bottom_color::String` | `nothing` | Bottom color. |
| `top_fill_color_1::String` | `nothing` | Top fill color 1. |
| `top_fill_color_2::String` | `nothing` | Top fill color 2. |
| `top_line_color::String` | `nothing` | Top line color. |
| `bottom_fill_color_1::String` | `nothing` | Bottom fill color 1. |
| `bottom_fill_color_2::String` | `nothing` | Bottom fill color 2. |
| `bottom_line_color::String` | `nothing` | Bottom line color. |
| `color::String` | `nothing` | Color. |
"""
mutable struct LWCSimpleChartData <: AbstractChartData
time::Int64
value::Float64
line_color::Union{String,Nothing}
top_color::Union{String,Nothing}
bottom_color::Union{String,Nothing}
top_fill_color_1::Union{String,Nothing}
top_fill_color_2::Union{String,Nothing}
top_line_color::Union{String,Nothing}
bottom_fill_color_1::Union{String,Nothing}
bottom_fill_color_2::Union{String,Nothing}
bottom_line_color::Union{String,Nothing}
color::Union{String,Nothing}

function LWCSimpleChartData(
time::Int64,
value::Real;
line_color::Union{String,Nothing} = nothing,
top_color::Union{String,Nothing} = nothing,
bottom_color::Union{String,Nothing} = nothing,
top_fill_color_1::Union{String,Nothing} = nothing,
top_fill_color_2::Union{String,Nothing} = nothing,
top_line_color::Union{String,Nothing} = nothing,
bottom_fill_color_1::Union{String,Nothing} = nothing,
bottom_fill_color_2::Union{String,Nothing} = nothing,
bottom_line_color::Union{String,Nothing} = nothing,
color::Union{String,Nothing} = nothing,
)
return new(
time,
value,
line_color,
top_color,
bottom_color,
top_fill_color_1,
top_fill_color_2,
top_line_color,
bottom_fill_color_1,
bottom_fill_color_2,
bottom_line_color,
color,
)
end

function LWCSimpleChartData(
time::TimeType,
value::Real;
kw...
)
return LWCSimpleChartData(
datetime2epochns(time),
value;
kw...
)
end
end

Serde.SerJson.ser_value(::Type{<:AbstractChartData}, ::Val{:time}, x::Int64) = string(x)

lwc_time(x::LWCSimpleChartData) = x.time
lwc_value(x::LWCSimpleChartData) = x.value
lwc_line_color(x::LWCSimpleChartData) = x.line_color
lwc_top_color(x::LWCSimpleChartData) = x.top_color
lwc_bottom_color(x::LWCSimpleChartData) = x.bottom_color
lwc_top_fill_color_1(x::LWCSimpleChartData) = x.top_fill_color_1
lwc_top_fill_color_2(x::LWCSimpleChartData) = x.top_fill_color_2
lwc_top_line_color(x::LWCSimpleChartData) = x.top_line_color
lwc_bottom_fill_color_1(x::LWCSimpleChartData) = x.bottom_fill_color_1
lwc_bottom_fill_color_2(x::LWCSimpleChartData) = x.bottom_fill_color_2
lwc_bottom_line_color(x::LWCSimpleChartData) = x.bottom_line_color
lwc_color(x::LWCSimpleChartData) = x.color

function Base.:(==)(left::LWCSimpleChartData, right::LWCSimpleChartData)
return isequal(lwc_time(left), lwc_time(right)) &&
isequal(lwc_value(left), lwc_value(right))
end

"""
LWCCandle(time::Real, open::Real, high::Real, low::Real, close::Real)
LWCCandle(time::TimeType, open::Real, high::Real, low::Real, close::Real)
LWCCandle(time::Int64, open::Real, high::Real, low::Real, close::Real; kw...)
LWCCandle(time::TimeType, open::Real, high::Real, low::Real, close::Real; kw...)

Representation of candlestick data for [`lwc_candlestick`](@ref) and [`lwc_bar`](@ref) methods.

Expand All @@ -45,20 +131,62 @@ Representation of candlestick data for [`lwc_candlestick`](@ref) and [`lwc_bar`]
- `high::Float64`
- `low::Float64`
- `close::Float64`

## Keyword arguments
| Name::Type | Default (Posible values) | Description |
|:-----------|:-------------------------|:------------|
| `color::String` | `nothing` | Candle color. |
| `border_color::String` | `nothing` | Border color. |
| `wick_color::String` | `nothing` | Wick color. |
"""
mutable struct LWCCandle <: AbstractChartData
time::Int64
open::Float64
high::Float64
low::Float64
close::Float64

function LWCCandle(time::Real, open::Real, high::Real, low::Real, close::Real)
return new(time, open, high, low, close)
color::Union{String,Nothing}
border_color::Union{String,Nothing}
wick_color::Union{String,Nothing}

function LWCCandle(
time::Int64,
open::Real,
high::Real,
low::Real,
close::Real;
color::Union{String,Nothing} = nothing,
border_color::Union{String,Nothing} = nothing,
wick_color::Union{String,Nothing} = nothing,
)
return new(
time,
open,
high,
low,
close,
color,
border_color,
wick_color,
)
end

function LWCCandle(time::TimeType, open::Real, high::Real, low::Real, close::Real)
return LWCCandle(datetime2epochns(time), open, high, low, close)
function LWCCandle(
time::TimeType,
open::Real,
high::Real,
low::Real,
close::Real;
kw...
)
return LWCCandle(
datetime2epochns(time),
open,
high,
low,
close;
kw...
)
end
end

Expand All @@ -67,6 +195,9 @@ lwc_open(x::LWCCandle) = x.open
lwc_high(x::LWCCandle) = x.high
lwc_low(x::LWCCandle) = x.low
lwc_close(x::LWCCandle) = x.close
lwc_color(x::LWCCandle) = x.color
lwc_border_color(x::LWCCandle) = x.border_color
lwc_wick_color(x::LWCCandle) = x.wick_color

function Base.:(==)(left::LWCCandle, right::LWCCandle)
return (
Expand Down
2 changes: 2 additions & 0 deletions src/charts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Serde.SerJson.ser_type(::Type{A}, x::LWC_LINE_STYLES) where {A<:AbstractChartSet

Serde.SerJson.ser_type(::Type{A}, x::LWC_PRICE_SCALE_ID) where {A<:AbstractChartSettings} = x == LWC_RIGHT ? "right" : "left"

(Serde.SerJson.ser_ignore_null(::Type{A})::Bool) where {A<:AbstractChartData} = true

function prepare_data(
timestamps::Vector{D},
values::Vector{T};
Expand Down
14 changes: 10 additions & 4 deletions src/charts/area.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Wrapper function for [`Area`](https://tradingview.github.io/lightweight-charts/d
function lwc_area end

function lwc_area(
timearray::AbstractVector{Tuple{D,T}};
data::AbstractVector{<:AbstractChartData};
price_scale_id::LWC_PRICE_SCALE_ID = LWC_LEFT,
label_name::String = "",
visible::Bool = true,
Expand All @@ -76,9 +76,7 @@ function lwc_area(
crosshair_marker_background_color::String = "",
crosshair_marker_border_width::Float64 = 2.0,
plugins::Vector{LWCPlugin} = Vector{LWCPlugin}(),
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
data = lwc_convert_data(timearray)

)::LWCChart
settings = AreaChartSettings(
price_scale_id,
label_name,
Expand Down Expand Up @@ -111,6 +109,14 @@ function lwc_area(
)
end

function lwc_area(
timearray::AbstractVector{Tuple{D,T}};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
data = lwc_convert_data(timearray)
return lwc_area(data; kw...)
end

function lwc_area(
timestamps::Vector{D},
values::Vector{T};
Expand Down
14 changes: 10 additions & 4 deletions src/charts/baseline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Wrapper function for [`Baseline`](https://tradingview.github.io/lightweight-char
function lwc_baseline end

function lwc_baseline(
timearray::AbstractVector{Tuple{D,T}};
data::AbstractVector{<:AbstractChartData};
price_scale_id::LWC_PRICE_SCALE_ID = LWC_LEFT,
label_name::String = "",
visible::Bool = true,
Expand All @@ -93,9 +93,7 @@ function lwc_baseline(
crosshair_marker_background_color::String = "",
crosshair_marker_border_width::Float64 = 2.0,
plugins::Vector{LWCPlugin} = Vector{LWCPlugin}(),
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
data = lwc_convert_data(timearray)

)::LWCChart
settings = BaseLineChartSettings(
price_scale_id,
label_name,
Expand Down Expand Up @@ -132,6 +130,14 @@ function lwc_baseline(
)
end

function lwc_baseline(
timearray::AbstractVector{Tuple{D,T}};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
data = lwc_convert_data(timearray)
return lwc_baseline(data; kw...)
end

function lwc_baseline(
timestamps::Vector{D},
values::Vector{T};
Expand Down
14 changes: 10 additions & 4 deletions src/charts/histogram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ Wrapper function for [`Histogram`](https://tradingview.github.io/lightweight-cha
function lwc_histogram end

function lwc_histogram(
timearray::AbstractVector{Tuple{D,T}};
data::AbstractVector{<:AbstractChartData};
price_scale_id::LWC_PRICE_SCALE_ID = LWC_LEFT,
label_name::String = "",
visible::Bool = true,
precision::Int64 = 2,
color::String = randcolor(),
base::Real = 0.0,
plugins::Vector{LWCPlugin} = Vector{LWCPlugin}(),
)::LWCChart where {T<:Real,D<:Union{Real,TimeType}}
data = lwc_convert_data(timearray)

)::LWCChart
settings = HistogramChartSettings(
price_scale_id,
label_name,
Expand All @@ -63,6 +61,14 @@ function lwc_histogram(
)
end

function lwc_histogram(
timearray::AbstractVector{Tuple{D,T}};
kw...
)::LWCChart where {D<:Union{Real,TimeType},T<:Real}
data = lwc_convert_data(timearray)
return lwc_histogram(data; kw...)
end

function lwc_histogram(
timestamps::Vector{D},
values::Vector{T};
Expand Down
Loading