Skip to content

Linear value scaling #148

Open
Open

Description

@andrewgoz

The example chart on https://chartscss.org/docs/usage/ shows a drawback of how --size variables work. They are designed to scale between 0 and 1. The example markup does a very simple transformation from table data to --size:

<td style="--size: 0.46"> 46 </td>

The drawback is that the chart bars only use half the chart area, since the largest value is 0.46.

My suggestion is to add the following rule to the standard styles:

.charts-css td {
	--size:calc((var(--value) - var(--valuemin,0)) / (var(--valuemax,1) - var(--valuemin,0)));
}

This still allows the original --size markup to be used unchanged for backward compatibility, but now supports the following changes:

<tbody style="--valuemax: 50">

and

<td style="--value: 46"> 46 </td>

Which allows for a direct copy of table data to CSS --value variable, but better formats the chart to use more of the chart area since it scales values between 0 and 50 (--valuemin and --valuemax) to between 0 and 1.

Activity

  1. IDisposable commented on Jan 11, 2026

    @IDisposable

    Love this idea, want a PR?

  2. andrewgoz commented on Apr 9, 2026

    @andrewgoz
    Author

    With a bit more experience, I've realised that start and end values need autoscaling too, not just size. I also changed valuemin to value-min and valuemax to value-max because it looks better.

    .charts-css td {
      --size:calc((var(--value) - var(--value-min,0)) / (var(--value-max,1) - var(--value-min,0)));
      --start:calc((var(--value-start) - var(--value-min,0)) / (var(--value-max,1) - var(--value-min,0)));
      --end:calc((var(--value-end) - var(--value-min,0)) / (var(--value-max,1) - var(--value-min,0)));
    }
    

    I'll leave a pull request until I've been using ChartsCSS a bit more, just in case there's anything else I need to add.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions