yuankunzhang / charming

A visualization library for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Candlestick Example

pferreira8 opened this issue · comments

if I recall correctly, I remember at one point seeing an example for how to load data for candlesticks, but the example is now gone. Adding this back would be great since there's only the svg examples. I have a Polars Dataframe as my source.

Hi, the candlestick examples are still there. Please check this section in readme - https://github.com/yuankunzhang/charming/#candlestick-charts

thanks! I will close the issue, was checking out the project on docs.rs and that might be why I didn't see it

@pferreira8
Hi,
Do you know how to use polars dataframe in charming ? Is it compatible with charming dataframe ?

@pferreira8 Hi, Do you know how to use polars dataframe in charming ? Is it compatible with charming dataframe ?

It is compatible once you convert the Dataframe into a Vec<Vec>
I have the logic split between two functions, it could be cleaned up for sure but this was my hacky method that worked.

// pass df into this function, assuming column names below or edit them to match properly
fn parse_ohlc_data(data: DataFrame) -> Result< (Vec<Vec<f64>>, Vec<String> ), Box<dyn std::error::Error> > {
       let d_open: Vec<f64> = data.column("open")?.f64()?.into_iter().map(|x| x.unwrap()).collect();
       let d_high: Vec<f64> = data.column("high")?.f64()?.into_iter().map(|x| x.unwrap()).collect();
       let d_low: Vec<f64> = data.column("low")?.f64()?.into_iter().map(|x| x.unwrap()).collect();
       let d_close: Vec<f64> = data.column("close")?.f64()?.into_iter().map(|x| x.unwrap()).collect();
       let dt: Vec<String> = data.column("date")?.iter().map(|x| x.to_string()).collect(); 
       let mut ohlc_vec: Vec<Vec<f64>> = Vec::new();
       for i in 0..d_open.len() {
            let array: [f64;4] = [d_open[i], d_high[i], d_low[i], d_close[i]]; 
            ohlc_vec.push(array.to_vec());
        }
        Ok((ohlc_vec, dt)) 
}
pub fn build_symbol_chart(symbol_name: &str, ohlc: (Vec<Vec<f64>>, Vec<String>)) -> Result<(), EchartsError> {
    let chart = Chart::new()
        .x_axis(Axis::new().show(true).data(ohlc.1))
        .y_axis(Axis::new().show(true).scale(true))
        .data_zoom(
            DataZoom::new()
                .type_(DataZoomType::Slider)
                .min_value_span(5),
        )
        .series(Candlestick::new().data(
            ohlc.0
        )).background_color("6E7468");

    let mut rend = HtmlRenderer::new("OHLCV", 1000, 800).theme(Westeros);
    let dir_file = format!("./html/charts/{}.html", symbol_name);
    HtmlRenderer::render(&rend, &chart)?;
    rend.save(&chart, std::path::Path::new(&dir_file))?;
    // println!("saving chart to html");
    
    Ok(())
}
// EXAMPLE
//converts polars df to work with charming
let ohlc = parse_ohlc_data(data).unwrap();

// creates an html file for candlestick charts
tools::chart::build_symbol_chart(YOUR_FILE_NAME_HERE, ohlc)?;