How can I create a horizontal scrolling Chart.js line chart with a locked y axis?

后端 未结 7 1825
生来不讨喜
生来不讨喜 2020-11-29 00:39

I\'d like to create a line chart with Chart.Js but have the Y-Axis not move when I scroll.

I\'m assuming I can use a fixed width, and put i

7条回答
  •  星月不相逢
    2020-11-29 00:50

    Chart.js 2.x:

    CSS:

        .graph {
            padding: 10px;
            position: relative;
            overflow-x: scroll;
            width: 100%;
            .graph-container {
                height: 500px;
                width: 100%;
                min-width: 100%
            }
        }
    

    HTML : Ignore the angular ngFor

      

    JS

    Set these options:

            options: {
                responsive: true,
                maintainAspectRatio: false
            }
    

    Set the width of the "graph-container"

    I'm setting the width based on the number of elements I have X a zoom that the user chooses

    graphCanvasElement.nativeElement.parentElement
        .setAttribute('style', `width: ${chartData.data.labels.length*graph.zoom}px;min-width: 100%`);
    
    graph.chartObj = new Chart(graphCanvasElement.nativeElement.getContext('2d'), chartData);
    

提交回复
热议问题