【问题标题】:How to fix the distance between horizontal points (x-axis) in chartJS如何修复chartJS中水平点(x轴)之间的距离
【发布时间】:2017-03-29 12:12:30
【问题描述】:

问题:大家好!我正在使用 chart.js 来绘制图表。图表是动态的,有时显示 10 个点,有时可能超过一千个我已经成功应用了一个面板,这样我的点就可以显示在一定距离以便轻松阅读它们。

现在我想知道是否有任何选项可以设置 x 轴网格中点之间的距离。现在它会自动调整点。

我尝试了什么: 我试图通过搜索其他stackoverflow答案来做stepsize和scalewidth,但没有成功。任何形式的帮助将不胜感激。

P.S:我正在使用 chart.js2

这是我的 chartjs 数据集

var data = {
    labels: data.graph_date,
    datasets: [
        {
            label: 'Assay Values',
            fill: false,
            lineTension: 0,
            backgroundColor: "rgba(255, 0, 0,1)",
            borderColor: "rgba(255, 0, 0,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderWidth: 1,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(255, 0, 0,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(255, 0, 0,1)",
            pointHoverBorderColor: "rgba(255, 0, 0,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 1,
            pointHitRadius: 10,
            data: data.assay_value,
            spanGaps: false
        },var options = {
    responsive: false,
    title: {
        display: true,
        position: "top",
        text: label,
        fontSize: 18,
        fontColor: "#111"
    },
    legend: {
        display: true,
        position: "bottom",
        labels: {
            fontColor: "#333",
            fontSize: 16
        }
    },
    tooltips: {
        enabled: true,
        mode: 'single',
        callbacks: {
            label: function(tooltipItems, data) {
                var multistringText = ['Assay Value: '+tooltipItems.yLabel];
                multistringText.push('Assigned Value: '+assigned_value[tooltipItems.index]);
                multistringText.push('Sample ID: '+sample_id[tooltipItems.index]);
                return multistringText;
            }
        }
    },
    scales:{
        yAxes:[{
            ticks:{
                min:graph_min
            }
        }],
        xAxes: [{
            gridLines: {
                display:false
            }
        }]
    }
};
if(new_chart[ctx.canvas.id] != null)
    new_chart[ctx.canvas.id].destroy();

new_chart[ctx.canvas.id] =new Chart(ctx, {
    type: 'line',
    data: data,
    options: options
});

x轴有这样的数据

[19-Aug-2015,21-Aug-2015,21-Aug-2015,21-Aug-2015,21-Aug-2015,22-Aug-2015,27-Aug-2015,29-Aug-2015,1-Sep-2015,2-Sep-2015,3-Sep-2015,]

y轴数据是这样的

[0.1,0.05,0.89,0.89,0.79,0.58,0.68,0.25,0.98]

【问题讨论】:

  • stepSize 确实有效,你的意思是你没有成功?
  • kristjan 它不适用于 xaxis 它只适用于 yaxis
  • 你能发布你的整个 chart.js 配置和你的数据样本吗?这是提供具体答案所需要的。

标签: javascript html charts chart.js chart.js2


【解决方案1】:

控制点之间距离的方法是将 X 轴和 Y 轴设置为最小、最大和步长,这样无论图表中的点数如何,它们都不会改变。

这是一个设置比例的示例,使它们永远不会改变。无论图表上出现多少点,一切都将保持不变。

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    datasets: [{
      label: 'Points',
      data: [
        {x: 0, y: 2},
        {x: 1, y: 3}, 
        {x: 2, y: 2},
        {x: 1.02, y: 0.4},
        {x: 0, y: -1}
      ],
      backgroundColor: 'rgba(123, 83, 252, 0.8)',
      borderColor: 'rgba(33, 232, 234, 1)',
      borderWidth: 1,
      fill: false,
      showLine: false,
    }],
  },
  options: {
    title: {
      display: true,
      text: 'Chart.js - Fixed X and Y Axis',
    },
    scales: {
      xAxes: [{
        type: 'linear',
        position: 'bottom',
        ticks: {
          min: -1,
          max: 8,
          stepSize: 1,
          fixedStepSize: 1,
        }
      }],
      yAxes: [{
        ticks: {
          min: -2,
          max: 4,
          stepSize: 1,
          fixedStepSize: 1,
        }
      }]
    }
  }
});

这是一个codepen example,它展示了它的样子

【讨论】:

  • 我在这里看到了你的例子。问题是我没有在 y 轴上显示数字,有日期显示。所以它没有考虑像最小值和最大值这样的数值
  • 以后,请务必在您的问题中包含类似的重要细节。由于您正在处理日期,我假设您正在使用时间尺度?如果是这样,则时间刻度也具有min/max 值。像我的例子一样使用它。如果您有兴趣,我可以改用时间尺度来更新我的答案。
  • 请原谅这个错误。是的,我使用的是日期,但不是 chartjs 的时间刻度函数,实际上这些值来自数据库。让我添加代码
  • 谢谢,我去看看!
  • 我看到了。您可以发布您的完整 chart.js 配置和一小部分数据吗?这将阻止我做出任何假设。
猜你喜欢
  • 1970-01-01
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多