Skip to content

fix: gradient for color thresholds - #1361

Open
fran68 wants to merge 2 commits into
kalkih:devfrom
fran68:dev-fix/gradient_for_color_thresholds
Open

fix: gradient for color thresholds#1361
fran68 wants to merge 2 commits into
kalkih:devfrom
fran68:dev-fix/gradient_for_color_thresholds

Conversation

@fran68

@fran68 fran68 commented Apr 21, 2026

Copy link
Copy Markdown

IMO this should fix the problem with the color thresholds. In function computeGradient in src/graph.js the thresholds are evaluated as real values. When displaying the graph the gradient course is distorted since the margins at top and bottom affect the gradient offset. So the offset of the gradient needs to be adapted by considering the margin.

This might fix
#1080
#1108
#1137
#723

Before:
fix_fill01bfix_fill01a

After applying the patch:
fix_fill01d
fix_fill01c

line_width: 6
color_thresholds_transition: hard
color_thresholds:
  - value: 14
    color: red
  - value: 15
    color: cyan
  - value: 16
    color: green
  - value: 17
    color: red

@fran68
fran68 force-pushed the dev-fix/gradient_for_color_thresholds branch from da5a819 to 16b2e31 Compare April 21, 2026 21:14
@fran68 fran68 changed the title fix: gradient for color thresholds fix: gradient for color thresholds Apr 22, 2026
@fran68
fran68 force-pushed the dev-fix/gradient_for_color_thresholds branch from 16b2e31 to 47aabd5 Compare May 4, 2026 19:47
@fran68
fran68 force-pushed the dev-fix/gradient_for_color_thresholds branch from 47aabd5 to fc1a471 Compare May 11, 2026 20:59
@ildar170975

ildar170975 commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

@fran68
I would suggest using a _calcY() function to convert a stop value to pixels.
It accounts max/min & logarithmic.

      ...
      let offset;
      if (scale <= 0) {
        offset = 0;
      } else {
        // limit stopValue within max/min
        const stopValue = (stop.value >= this._max)
          ? this._max
          : (stop.value <= this._min)
            ? this._min
            : stop.value;
        // get Y coord for stopValue
        const [stopCoord] = this._calcY([[0, 0, stopValue]]);
        const [, coordY] = stopCoord;
        // calculate absolute offset
        offset = coordY * 100 / (this.height + this.margin[Y] * 4);
      }
      return {
        color: color || stop.color,
        offset,
      };
      ...

Here:

  1. Limit a stop value within max/min.
  2. Convert the stop value to pixels.
  3. Calculate an offset.

What do you think?
In my opinion, this is a more proper & transparent way.
I can issue my own PR (with credits for you of course) if you have no time for testing this.

Also, I am a bit unsure about

      if (scale <= 0) {
        offset = 0;
      }

may be an offset should be 100?

@fran68
fran68 force-pushed the dev-fix/gradient_for_color_thresholds branch from fc1a471 to 52934d7 Compare July 24, 2026 15:05
@fran68

fran68 commented Jul 24, 2026

Copy link
Copy Markdown
Author

@ildar170975
I've tested your code and it's fine. It's better to handle it with the coordinates.

But I dont know how to deal with that logarithmic thing. Is there ever a scale value which is negative?

@fran68
fran68 force-pushed the dev-fix/gradient_for_color_thresholds branch from 7535bd5 to b4c7d3b Compare July 24, 2026 16:57
@ildar170975

Copy link
Copy Markdown
Collaborator

We can discuss it in our chat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants