【问题标题】:How to make a chart scroll horizontally (when using Chart.js)如何使图表水平滚动(使用 Chart.js 时)
【发布时间】:2020-03-21 17:49:38
【问题描述】:

我检查了this questionthis question 并让图表可以水平滚动,但它的高度也增加了,现在它也可以垂直滚动了。如何使图表能够水平滚动而不垂直扩展?

HTML
<div class="chartWrapper">
 <div class="chartAreaWrapper">
    <div class="chartAreaWrapper2">
        <canvas id="canvas"></canvas>
    </div>
</div>
<canvas id="myChartAxis" height="300" width="0"></canvas>
</div>

JS
var ctx = document.getElementById('canvas').getContext('2d');
        window.myLine = new Chart(ctx, config);
        var sourceCanvas = myLiveChart.chart.canvas;
                    var copyWidth = myLiveChart.scales['y-axis-0'].width - 10;
                    var copyHeight = myLiveChart.scales['y-axis-0'].height + myLiveChart.scales['y-axis-0'].top + 10;
                    var targetCtx = document.getElementById("myChartAxis").getContext("2d");
                    targetCtx.canvas.width = copyWidth;
            targetCtx.drawImage(sourceCanvas, 0, 0, copyWidth, copyHeight, 0, 0, copyWidth, copyHeight);

CSS
.chartWrapper {
        position: relative;

    }
    .chartWrapper > canvas {
        position: absolute;
        left: 0;
        top: 0;
        pointer-events:none;
    }
.chartAreaWrapper {
      overflow-x: scroll;
        position: relative;
        width: 100%;
    }
    .chartAreaWrapper2 {
        position: relative;
        height: 300px;
}

【问题讨论】:

    标签: javascript html css chart.js


    【解决方案1】:

    一个

    chart.js docs: "以下示例无效:"

    = 任何像 (&lt;canvas style="width: 100px;..") + 绝对位置和其他技巧这样的画布尺寸都不适合她。

    b

    Chart.js 使用其父容器来更新画布渲染和 显示尺寸。 但是,此方法要求容器是 相对定位和专用于图表画布。 然后可以通过为 容器大小https://www.chartjs.org/docs/latest/general/responsive.html#important-note

    <div class="chart-container" style="position: relative; height:40vh; width:80vw">
        <canvas id="chart"></canvas>
    </div>
    

    = 为chart-container 设置任意大小。

    c

    • maintainAspectRatio - 默认为 true(调整大小时保持原来的画布纵横比(宽度/高度)。

    • responive - 默认为 true(在其容器发生时调整图表画布的大小)

    a+b+c

    我不知道为什么其他 stackoverflow 答案会给出如此“复杂”的解决方案。 overflow-x 她的工作方式与任何其他块级元素一样(将 800px w 图像放入 600px W div = 200px overflow-x)。

    “你好世界”示例

    特定的溢出添加额外的包装到chart-container:

    <div style="overflow-x: scroll">
      <div class="chart-container" style="position: relative;  width:900px;">
        <canvas id="chart"></canvas>
      </div>
    </div>
    

    当屏幕宽度小于 900 像素时,您会获得水平滚动(例如在移动设备上):

    ** 如果需要,可以使用 CSS 断点 在移动设备上调整容器的大小。

    /* data */
    var data = {
      labels: ["Africa", "Asia", "Europe", "America"],
      datasets: [{
        /* data */
        label: "Data label",
        backgroundColor: ["red", "#8e5ea2","#3cba9f", '#1d49b8'],
        data: [5.0,6.7,7.5, 8.6, 3.3, 4.4, 4.5]
      }]
    };
    
    var options = {
      responsive: true,
      maintainAspectRatio: true,
      title: {
        text: 'Hello',
        display: true
      },
      scales: {
        xAxes: [{
          stacked: false,
          ticks: {
    
          },
        }],
        yAxes: [{
          stacked: true,
          ticks: {
    
          }
        }]
      }
    };
    
    var myChart = new Chart(document.getElementById("chart"), {
      type: 'bar',
      data: data,
      options: options
    });
    
    document.getElementById('submitChange').addEventListener('click', function() {
      console.log("before color: " + myChart.data.datasets[0].backgroundColor[0]);
      myChart.data.datasets[0].backgroundColor[0] = "green";
      myChart.update();
    });
    div{
      border: 1px solid red;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
    <div style="overflow-x: scroll">
      <div class="chart-container" style="position: relative;  width:900px;">
        <canvas id="chart"></canvas>
      </div>
    </div>

    【讨论】:

    • 我尝试了您的解决方案,它在您描述的场景中运行良好(当屏幕小于 900 像素时图表可以水平滚动),但这不是我的问题的解决方案。实际上是在逐日显示全年的数据,目前数据点之间的距离太近了。我想水平扩展图表,以便数据点之间有一些空间,并且图表也可以滚动,因此用户可以通过滚动查看所有数据点
    • 所以如果你想为图表容器添加 8000px 的宽度(你想要的任何宽度)。您的问题与您的数据不溢出有关(溢出工作很好)。在您的情况下 365 条 - 这是更多 ui/x 问题(可能添加月份按钮来过滤数据(点击用户从 jan 更改为 feb 等等)。例如查看 google Analytics 图表。请记住无休止的滚动 =不好的 ui 体验。也许将此问题添加到 ui/x 或数据可视化论坛。
    • 增加图表容器宽度的问题是它也会垂直扩展而不是水平扩展。
    • 所以也要设置高度。对于可变宽度取决于内容(例如通过点击添加),您应该使用 update() 函数。另外,使用响应式 true/false。
    【解决方案2】:

    使用“溢出-x:scroll;”而不是“溢出:滚动;”

    【讨论】:

    • 能分享一下你的html和javascript代码吗?
    • 更新了问题。有了这段代码,它现在可以垂直滚动了,现在如果我增加宽度,它会垂直扩展但不能水平扩展。
    猜你喜欢
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多