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

fix: use d3 to interpolate colors #1118

Merged
merged 5 commits into from
Aug 7, 2024
Merged

Conversation

akloeckner
Copy link
Collaborator

@akloeckner akloeckner commented Jul 26, 2024

This uses d3-interpolate to interpolate between two colors in the thresholds. The library can handle all sorts of color inputs, most importantly named colors. Our old implementation could only interpolate hexadecimal color values.

The following features now use the d3 interpolation (and should thus be fixed):

  • plot indicator relating a plot to the entity name
  • filled plots with an area under the line

This also extends usage of color interpolation (to elements not interpolating at all before):

  • the state
  • points on the graph
  • bars

Old (wrong though explicit) behaviour of bars can be restored by setting color_thresholds_transition: hard.

fixes #1116

where computeColor was used. Which does not interpolate.but uses the same logic otherwise.

rename intCOlor to computeColor for consitency
and delete our own nterpolation, as this was the last usage of it
@akloeckner
Copy link
Collaborator Author

akloeckner commented Jul 28, 2024

Example:

  - type: custom:mini-graph-card
    entities:
      - entity: sensor.aussentemperatur
        name: Außentemperatur
        unit: °C
        state_adaptive_color: true
      - entity: sensor.aussentemperatur
        name: Außentemperatur
        unit: °C
    hours_to_show: 72
    points_per_hour: 1
    group_by: hour
    aggregate_func: avg
    hour24: true
    decimals: 1
    show:
      labels: true
      points: true
    color_thresholds:
      - value: 10
        color: blue
      - value: 20
        color: green
      - value: 23
        color: red
    color_thresholds_transition: smooth
Before: After:
image image
Black indicator, no blue in Graph, points transition hard, state is plain blue. Correct card.

@akloeckner
Copy link
Collaborator Author

I noticed, that bars do not inteprolate color. And there is some explicit code to prevent that. I wonder, if we should change that. See this example:

Currently: Could be:
image image

The original behavior could still be achieved by using hard color transition...

@akloeckner akloeckner marked this pull request as ready for review July 28, 2024 01:33
@akloeckner
Copy link
Collaborator Author

I went for interpolating bar colors, too. It just doesn't make sense to not do it. This changes appearance of colored bars. But I consider it a fix. The old behaviour can be restored by setting color_thresholds_transition: hard.

@akloeckner
Copy link
Collaborator Author

@ildar170975, could you please double-check that this behavior is as expected?

@ildar170975
Copy link
Collaborator

@akloeckner
Thanks a lot!
Just arrived to a normal PC, can test.
Please explain me once again (please....) where to find a js file to download...

@akloeckner
Copy link
Collaborator Author

akloeckner commented Aug 7, 2024

Don't hurry. I just noticed it was next week. ;-)

  • Click on "Checks" for the PR (top of the PR page)
  • Select "CI"
  • Scroll down to "Artifacts"
  • Download one of the zips
  • It should contain the compiled .js

@ildar170975
Copy link
Collaborator

  • It should contain the compiled .js

This one?
image

@akloeckner
Copy link
Collaborator Author

Yes, any of the two dist* files should be ok. They are just compiled with different Node.js versions.

@ildar170975
Copy link
Collaborator

@akloeckner

Test 1:

type: custom:mini-graph-card
entities:
  - entity: sensor.xiaomi_cg_1_co2
    show_state: true
    state_adaptive_color: true
  - entity: sensor.xiaomi_cg_2_co2
    show_state: true
    state_adaptive_color: true
name: CO2
hours_to_show: 24
points_per_hour: 60
lower_bound: ~400
color_thresholds:
  - value: 450
    color: orange
  - value: 550
    color: green
  - value: 600
    color: magenta
color_thresholds_transition: hard
height: 400
show:
  labels: true

image
Colors of legend's indicators are same as colors of state labels - as expected.

With attributes - colors are also OK (do not bother about absence of graphs since these "master" sensors are excluded from Recorder):
image

Imho we can merge this & allow other people to test.
I am having an access to PC only 3-4 days, then leaving for 8-10 days, and again 3-4 days, and so on - on a routine basis ))).

@ildar170975
Copy link
Collaborator

Test 2 - other color formats:

image

image

@akloeckner
Copy link
Collaborator Author

Imho we can merge this & allow other people to test.

Perfect. Let's do this then! Thanks for checking.

I am having an access to PC only 3-4 days, then leaving for 8-10 days, and again 3-4 days, and so on - on a routine basis ))).

This sounds like an exciting life. Or an exhausting one. :-)

@akloeckner akloeckner merged commit b8676f5 into kalkih:dev Aug 7, 2024
4 checks passed
github-actions bot pushed a commit that referenced this pull request Aug 7, 2024
## [0.12.2-dev.2](v0.12.2-dev.1...v0.12.2-dev.2) (2024-08-07)

### Bug Fixes

* use d3 to interpolate colors ([#1118](#1118)) ([b8676f5](b8676f5))
Copy link

github-actions bot commented Aug 7, 2024

🎉 This PR is included in version 0.12.2-dev.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants