leeoniya / leeoniya/uPlot

this.chart.setScale does not work correctly

Open
#1,063 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
JavaScript
Stars
10.5k
Forks
463
PR merge metrics
No merged PRs in 30d

Description

Hello,
I have the following chart:

let chart_options =
       {
           title: "Schuetz Graph",
           width: this.width,
           height: this.heigth,
           cursor: {
               points: {
                   one: true
               },
               focus: {
                   prox: 30
               }
           },
           plugins: [drawAlarmLines(() => this.alarmThreshold, () => this.dynamicThreshold)],
           scales: {
               x: {
                   time: false,
                   auto: false,
                   min: 0,
                   max: this.scrollWindowSize - 1,
               },
               y: {
                   auto: false,
                   min: this.Y_MIN,
                   max: this.alarmThreshold,
                   range: (): [number, number] => {
                       return [this.Y_MIN, this.alarmThreshold];
                   }
               }
           },
           drag: {
               scale: false
           },
           series: [
               {},
               {
                   show: true,
                   stroke: (u, seriesIdx) => {
                       return this.firstSeriesColor;
                   },
                   scale: "y",
                   label: "ppm",
                   width: 2,
                   points: {
                       show: false,
                   },
               },
               {
                   show: true,
                   stroke: (u, seriesIdx) => {
                       return this.secondSeriesColor;
                   },
                   scale: "y",
                   label: "ppm",
                   width: 2,
                   points: {
                       show: false,
                   },
               },
               {
                   show: true,
                   stroke: (u, seriesIdx) => {
                       return this.thirdSeriesColor;
                   },
                   scale: "y",
                   label: "ppm",
                   width: 2,
                   points: {
                       show: false,
                   },
               }
           ],
           axes: [
               {
                   grid: {
                       show: false,
                   },
                   show: false,
               },
               {
                   grid: { show: false },
               },
           ],
           legend: {
               show: false,
           }
       }
       let chartDiv = document.getElementById("chart")!;
       this.chart = new uPlot(chart_options, [[0], [0], [0], [0]], chartDiv);

since uPlot doesn´t support X-Axis scrolling. I try to implement my own scrollbar.

private initializeScrollbar() {
        if (!this.scrollBar) {
            this.scrollBar = document.getElementById("xScrollbar") as HTMLInputElement;
            if (!this.scrollBar) return;
            this.scrollBar.addEventListener("input", () => {
                this.updateVisibleWindow(parseInt(this.scrollBar!.value));
            });
        }
        if (!this.routeData?.xTicks) return;
        const totalLength = this.routeData.xTicks.length;
        const maxScrollStart = Math.max(0, totalLength - this.scrollWindowSize);
        console.log("init", totalLength, maxScrollStart);
        this.scrollBar.min = "0";
        this.scrollBar.max = maxScrollStart.toString();
        this.scrollBar.value = "0";
        this.updateVisibleWindow(0);
    }

the scrollbar should then update the scale

 private updateVisibleWindow(startIndex: number) {
        if (!this.routeData?.xTicks) return;
        const xTicks = this.routeData.xTicks;
        const totalLength = xTicks.length;
        const endIndex = Math.min(startIndex + this.scrollWindowSize - 1, totalLength - 1);
        const minX = xTicks[startIndex];
        const maxX = xTicks[endIndex];
        this.chart.setScale("x", { min: minX, max: maxX });
        console.log("scale", minX, maxX);
        console.log("x scale AFTER setScale:", this.chart.scales.x);
        this.chart.redraw();
    }

my minX and maxX are correct but this.chart.scales.x is always 0 - 999 (i set my scrollWindowSize to 1000) and the scale of the chart does not change

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with updateVisibleWindow, especially the chart.setScale("x", { min, max }) call, and compare it with uPlot's scale API and the configured x scale. Reproduce the scrollbar update using the supplied chart options, then verify that chart.scales.x and the visible chart range change from 0–999 to the selected minX and maxX.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.