【问题标题】:Chart.js Mixed Bar and Line chart with different scalesChart.js 不同比例的混合条形图和折线图
【发布时间】:2017-07-22 03:16:21
【问题描述】:

我正在使用 chart.js 构建一个图表,它是一个散点类型的堆积条形图。我的问题是条形图 x 轴上的比例不能正确表示散点的 x 轴比例。在过去的几个小时里,我查看了文档并浏览了 SOF,并提出了 This Answer,这对于条形图是有意义的,而不是由 x-y 坐标绘制的散点图。

var chartDefault = {
  type: 'bar',
  data: {
    labels: ['30', '45', '60', '90', '120', '120+'],
    datasets: [{
      type: 'bar',
      label: 'Receivable',
      data: [730, 492.5, 120, 4732.5, 2760.85, 0],
      backgroundColor: 'rgba(75, 192, 192, 0.2)',
      borderColor: 'rgba(75, 192, 192, 1)',
      borderWidth: 1
    }, {
      type: 'bar',
      label: 'Past Due',
      data: [2760.85, 0, 0, 0, 0, 0],
      backgroundColor: 'rgba(255, 99, 132, 0.2)',
      borderColor: 'rgba(255,99,132,1)',
      borderWidth: 1
    }, {
      type: 'scatter',
      label: 'Invoice',
      data: [{"x":106,"y":177.7},{"x":101,"y":1},{"x":92,"y":1},{"x":88,"y":120},{"x":65,"y":4},{"x":66,"y":120},{"x":59,"y":120},{"x":36,"y":372.5},{"x":35,"y":120},{"x":29,"y":120},{"x":4,"y":185},{"x":4,"y":120},{"x":1,"y":240},{"x":1,"y":65}],
      xAxisID: 'invoice-time',
      yAxisID: 'invoice-amount',
      backgroundColor: 'rgba(75, 00, 150, 0.2)',
      borderColor: 'rgba(75, 00, 150,1)',
      borderWidth: 2
    }]
  },
  options: {
    scales: {
      xAxes: [{
        display: true,
        stacked: true,
        scaleLabel: {
          display: true,
          labelString: 'Days'
        },
      }, {
        id: 'invoice-time',
        display: false,
        stacked: false,
        scaleLabel: {
          display: false,
          labelString: 'Days'
        },
        ticks: {
          beginAtZero: true,
          stepSize: 1,
          suggestedMax: 125
        }
      }],
      yAxes: [{
        display: true,
        stacked: true,
        scaleLabel: {
          display: true,
          labelString: 'Dollar Amount'
        },
        ticks: {
          beginAtZero: true,
        }
      }, {
        id: 'invoice-amount',
        display: false,
        stacked: false,
        scaleLabel: {
          display: false,
          labelString: 'Dollar Amount'
        },
        ticks: {
          beginAtZero: true,
        }
      }]
    },
  }
};
var chart = new Chart($('#creditSat'), chartDefault);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<canvas id="creditSat"></canvas>

因此,如果您查看 scatter 数据集的 sn-p,则有 11 个数据点,但只有 6 个被映射。我希望所有 11 个都被映射,我知道有一些重叠,这是有道理的。本质上这是发票的表示,您有应收账款,由条形表示的逾期,然后线上的点代表发票本身。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript jquery chart.js


    【解决方案1】:

    找到答案了,折线图的轴需要指定类型,像这样:

    {
        id: 'invoice-time',
        type: 'linear',
        display: false,
        stacked: false,
        scaleLabel: {
          display: false,
          labelString: 'Days'
        },
        ticks: {
          beginAtZero: true,
          stepSize: 1,
          suggestedMax: 125
        }
      }
    

    var chartDefault = {
      type: 'bar',
      data: {
        labels: ['30', '45', '60', '90', '120', '120+'],
        datasets: [{
          type: 'bar',
          label: 'Receivable',
          data: [730, 492.5, 120, 4732.5, 2760.85, 0],
          backgroundColor: 'rgba(75, 192, 192, 0.2)',
          borderColor: 'rgba(75, 192, 192, 1)',
          borderWidth: 1
        }, {
          type: 'bar',
          label: 'Past Due',
          data: [2760.85, 0, 0, 0, 0, 0],
          backgroundColor: 'rgba(255, 99, 132, 0.2)',
          borderColor: 'rgba(255,99,132,1)',
          borderWidth: 1
        }, {
          type: 'scatter',
          label: 'Invoice',
          data: [{"x":106,"y":177.7},{"x":101,"y":1},{"x":92,"y":1},{"x":88,"y":120},{"x":65,"y":4},{"x":66,"y":120},{"x":59,"y":120},{"x":36,"y":372.5},{"x":35,"y":120},{"x":29,"y":120},{"x":4,"y":185},{"x":4,"y":120},{"x":1,"y":240},{"x":1,"y":65}],
          xAxisID: 'invoice-time',
          yAxisID: 'invoice-amount',
          backgroundColor: 'rgba(75, 00, 150, 0.2)',
          borderColor: 'rgba(75, 00, 150,1)',
          borderWidth: 2
        }]
      },
      options: {
        scales: {
          xAxes: [{
            display: true,
            stacked: true,
            scaleLabel: {
              display: true,
              labelString: 'Days'
            },
          }, {
            id: 'invoice-time',
            type: 'linear',
            display: false,
            stacked: false,
            scaleLabel: {
              display: false,
              labelString: 'Days'
            },
            ticks: {
              beginAtZero: true,
              stepSize: 1,
              suggestedMax: 125
            }
          }],
          yAxes: [{
            display: true,
            stacked: true,
            scaleLabel: {
              display: true,
              labelString: 'Dollar Amount'
            },
            ticks: {
              beginAtZero: true,
            }
          }, {
            id: 'invoice-amount',
            display: false,
            stacked: false,
            scaleLabel: {
              display: false,
              labelString: 'Dollar Amount'
            },
            ticks: {
              beginAtZero: true,
            }
          }]
        },
      }
    };
    var chart = new Chart($('#creditSat'), chartDefault);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <canvas id="creditSat"></canvas>

    【讨论】:

    • 使用散点图时,如何将数据点转换为 x,y 值? x,y 值是像素吗?
    • X 为水平值,Y 为垂直值。所以在上面的示例图表中,Days 是 X 值,Amount 是 Y。
    猜你喜欢
    • 1970-01-01
    • 2018-06-04
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    相关资源
    最近更新 更多