【问题标题】:Chart.js get array of currently visible points on graphChart.js 获取图表上当前可见点的数组
【发布时间】:2020-02-24 20:04:03
【问题描述】:

使用启用了缩放/平移的 Chart.js,我正在尝试编写一个函数来仅将画布上可见的内容导出到 CSV。

但我不知道如何在用户缩放/平移后仅获取可见数据点。

我知道我可以只查看myChart.data.datasets,但这会获取图表中的所有点,而不仅仅是当前可见的点。

var config = {
    type: 'line',
    data: {
        // data is from API call
    }
        pan: {
            enabled: true,
            mode: 'xy'
        },
        zoom: {
            enabled: true,
            mode: 'xy'
        },
        bezierCurve : false
    }
};

var ctx = document.getElementById('myChart').getContext('2d');
window.myChart = new Chart(ctx, config);

$('#exportTheseRunsToCSV').on('click', function() {
    // something like myChart.getVisibleDataPoints() ???
    console.log(chart)
    console.log(chart.data.datasets)
});

必须有一些内部 API 调用来获取可见点,因为我还有一个函数可以将当前可见点导出到 PNG,并且只获取当前在画布上绘制的内容:

<a id="exportTheseRuns" class="text-white" href="#" download="image.png" download><i class="fas fa-download"></i> Export to PNG</a>

$('#exportTheseRuns').on('click', function() {
    $('#exportTheseRuns').attr('href', myChart.toBase64Image());
})

.toBase64Image() 函数似乎在内部完成了我正在寻找的工作,因为它只会捕获可见点,但随后它返回一个 Base64 URI,我无法将其用于我的 arrayToCSV 函数。

【问题讨论】:

    标签: javascript arrays chart.js export-to-csv


    【解决方案1】:

    图表对象包含minIndex 和maxIndex 属性,可用于slice 数据集的可见值。

    这是一个 sn-p,展示了如何使用plugin-provided functions onPanComplete and onZoomComplete 在平移或缩放图表后输出可见值:

    function getVisibleValues({
      chart
    }) {
      let x = chart.scales["x-axis-0"];
    
      document.getElementById("vv").innerText = JSON.stringify(chart.data.datasets[0].data.slice(x.minIndex, x.maxIndex + 1));
    }
    
    new Chart(document.getElementById("chart"), {
      type: "line",
      data: {
        labels: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
        datasets: [{
          data: [0, 1, 2, 4, 8, 16, 32, 64, 128, 256]
        }]
      },
      options: {
        maintainAspectRatio: false,
        scales: {
          xAxes: [{
            ticks: {
              min: 'c',
              max: 'h'
            }
          }]
        },
        plugins: {
          zoom: {
            pan: {
              enabled: true,
              mode: 'x',
              onPanComplete: getVisibleValues
            },
            zoom: {
              enabled: true,
              mode: 'x',
              onZoomComplete: getVisibleValues
            }
          }
        }
      }
    });
    <script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.1"></script>
    <script src="https://cdn.jsdelivr.net/npm/hammerjs@2.0.8"></script>
    <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@0.7.4"></script>
    <canvas id="chart" style="max-height:125px"></canvas>
    <p>
      Visible values: <span id="vv"></span>
    </p>

    【讨论】:

    • 非常感谢,这正是我想要的。我认为会有一个烘焙函数,但这也同样有效。
    【解决方案2】:

    toBase64Image 函数不是你想要的,它只是从画布中获取数据 URL。

    toBase64Image on github.com/chartjs

    toBase64Image: function() {
      return this.canvas.toDataURL.apply(this.canvas, arguments);
    },
    

    您可以使用onZoomComplete 和onPanComplete 获取更新的图表数据,它们都可以获取包含chart 的对象,您可能会从中获得有关当前缩放的更多信息,但据我所知,您会需要做一些计算/工作以获得可见点...

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多