loony-bean / textplots-rs

Terminal plotting library for Rust
238 stars 24 forks source link

Incorrect plotting when using multiple lineplots #15

Closed joseluis closed 4 years ago

joseluis commented 4 years ago

There's a nasty bug that renders badly any chart involving multiple lineplots.

I made a simple example to illustrate the problem.

All the lines should be parallel to each other, and there should be only one x axis displayed. But an axis gets displayed per lineplot, and all the lines converge to the same point.

extern crate textplots;
use textplots::{Chart, Plot, Shape};

fn main() {
    let (mut l1, mut l2, mut l3) = (vec![], vec![], vec![]);
    for n in -2..=2 {
        l1.push((n as f32, n as f32));
        l2.push((n as f32, n as f32 - 1.));
        l3.push((n as f32, n as f32 - 2.));
    }
    println!("\nf(x)=x; f(x)=x-1; f(x)=x-2");
    Chart::new(120, 80, -2., 2.)
        .lineplot(Shape::Lines(l1.as_slice()))
        .lineplot(Shape::Lines(l2.as_slice()))
        .lineplot(Shape::Lines(l3.as_slice()))
        .nice();

    let (mut l4, mut l5, mut l6) = (vec![], vec![], vec![]);
    for n in -2..=2 {
        l4.push((n as f32, n as f32));
        l5.push((n as f32, n as f32 + 1.));
        l6.push((n as f32, n as f32 + 2.));
    }
    println!("\nf(x)=x; f(x)=x+1; f(x)=x+2");
    Chart::new(120, 80, -2., 2.)
        .lineplot(Shape::Lines(l4.as_slice()))
        .lineplot(Shape::Lines(l5.as_slice()))
        .lineplot(Shape::Lines(l6.as_slice()))
        .nice();
}

image

Related: #8

loony-bean commented 4 years ago

Fixed in #22