Skip to content

Fixed #4292 - Plotter scaling by adapting margin computation based on sign #4365

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

Closed
wants to merge 1 commit into from

Conversation

vicnevicne
Copy link

This is my proposed fix for #4292.
To reproduce the issue and check the fix, please use the following sketch and open Serial plotter. Cases 2 and 4 are correct only with the fix applied - see comparison in video: http://screencast.com/t/Wj0vxqjU

#define PAUSE_MS 5

void setup() {
  Serial.begin(9600);
}

void loop() {

  // 20 positive sawtooth : OK
  for (int n = 0; n < 20; n++) {
    for(int value = 0; value < 100; value++) {
      Serial.println(value);
      delay(PAUSE_MS);
    }
  }

  // 20 negative sawtooth : NOK
  for (int n = 0; n < 20; n++) {
    for(int value = 0; value < 100; value++) {
      Serial.println(-value);
      delay(PAUSE_MS);
    }
  }

  // 20 sawtooth high in positive range: OK
  for (int n = 0; n < 20; n++) {
    for(int value = 100; value < 200; value++) {
      Serial.println(value);
      delay(PAUSE_MS);
    }
  }

  // 20 sawtooth low in negative range: NOK
  for (int n = 0; n < 20; n++) {
    for(int value = 100; value < 200; value++) {
      Serial.println(-value);
      delay(PAUSE_MS);
    }
  }
}

@cmaglie
Copy link
Member

cmaglie commented Jan 5, 2016

Thank you, your proposal looks much better, BTW I see that there is always a lot of "unused" space in the graph.

IMHO a way to maximize the use of the whole graph area is to calulate the delta between max and min and "expand" max and min of a small percent of this delta, something like that:

double delta = maxY - minY; // This is the graph height

minY = minY - (delta / 10); // add a few more space under the graph
maxY = maxY + (delta / 10); // add a few more space over the graph

may you try with this? What I want to accomplish is to resize the graph always to the optimal size.

@vicnevicne
Copy link
Author

Hi Cristian, and thanks for having a look.
The question of the "wasted space" did also worry me. In fact, the original code did not have any margins. Frederico added them to make it "look better" I guess.
I also thought the "factor 2" was huge but after a few tests, I think I understand the reason : the "Ticks" computation in the block that follows determines the Y-axis labels based on the minY and maxY. So by adding 10% margins, the labels do not match the actual min and max values, which looks weird.
For example, here is a screenshot of the sawtooth with your code :

2016-01-05_2011

The graph ranges from 100 to 200, which is not easy to figure out by looking at the Y labels 80, 160 and 240.
Probably that cosmetic issue would need to be adressed by passing "hints" to the Ticks computation logic or something, but I thought best to just fix here what clearly is a bug, while letting the percentage as a separate issue. But if the general feeling is that the 10% code is better, that's fine by me of course...

@vicnevicne
Copy link
Author

Err...
In fact, I just wondered how it did look like before the addition of margins, and to my surprise, it behaves extremely well without any adjustment of minY and maxY, because the Ticks take care of leaving margins on top and bottom.
I thought maybe it was just my round numbers (0, 100, 200, ...) that made it look OK, but I quickly coded the generator below and the result looks really good to me:
http://www.screencast.com/t/ADbkB8ysvLm2

#define PAUSE_MS 2

void setup() {
  Serial.begin(9600);
}

void loop() {

  int a = random(-300, 300);
  int b = random(-300, 300);
  // 10  sawtooth
  for (int n = 0; n < 10; n++) {
    for(int step=0; step < 100; step++) {
      Serial.println(a + ((step * (b-a))/100.0));
      delay(PAUSE_MS);
    }
  }
}

Again, this is by basically undoing the computation change ffissore included in its commit 4cb72ce
but I see no drawback, so I would go for it.
All comments are welcome of course...

@cmaglie
Copy link
Member

cmaglie commented Jan 6, 2016

Looking at the commit message, it was added to solve this issue #3767 when the graph is a constant value that leads to minY equals maxY (probably there is a problem drawing the Y axis in this case). At this point, IMHO, the general solution is to adjust minY and maxY only if the delta (maxY - minY) is 0 or is less than a threshold, say 10, in that case we can just extend the range to reach 10, something like:

if ((maxY - minY) < 10) {
  double middle = (maxY + minY) / 2;
  maxY = middle + 5;
  minY = middle - 5;
}

may I ask you to check this last version?

@vicnevicne
Copy link
Author

You are perfectly right. I thought the fix to #3767 he referred to was in the Tick class (part of the same commit) but the adjustment of minY and maxY is indeed required for the test case listed in that issue (plotting a constant value of 2).
Here are screenshots of that test case with no adjustment (top, not ok) and with your proposed solution (bottom, ok):
2016-01-07_0035_001
2016-01-07_0037
So that looks good to me :-)

@cmaglie cmaglie added this to the Release 1.6.8 milestone Jan 8, 2016
@cmaglie cmaglie closed this in 870171a Jan 8, 2016
@cmaglie
Copy link
Member

cmaglie commented Jan 8, 2016

Thanks @vicnevicne !

@per1234 per1234 added the SerialPlotter Tools > Serial Plotter label Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SerialPlotter Tools > Serial Plotter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants