简体   繁体   English

使用 Chartkick 和图表 js 的 Rails 双轴

[英]Rails dual axis using Chartkick and chart js

I am trying to use Chartkick with the Chart.js adapter and am trying to make a simple dual-axis chart but none of my settings seem to work.我正在尝试将ChartkickChart.js适配器一起使用,并尝试制作一个简单的双轴图表,但我的设置似乎都不起作用。 I know it's just a formatting issue but I feel as though I've tried everything with no luck.我知道这只是一个格式问题,但我觉得我已经尝试了一切但没有运气。

My data is this format and as you can see I've added the yAxisID to each dataset.我的数据是这种格式,正如您所看到的,我已将yAxisID添加到每个数据集。

@consultations = [{:name=>nil,
  :data=>
   [[Fri, 01 Dec 2017, 346],
    [Mon, 01 Jan 2018, 99],
    [Thu, 01 Feb 2018, 282],
    [Thu, 01 Mar 2018, 267],
    [Sun, 01 Apr 2018, 335],
  :yAxisID=>"y"},
 {:name=>"check with spouse",
  :data=>
   [[Fri, 01 Dec 2017, 0],
    [Mon, 01 Jan 2018, 1],
    [Thu, 01 Feb 2018, 9],
    [Thu, 01 Mar 2018, 13],
    [Sun, 01 Apr 2018, 19],
  :yAxisID=>"y1"},
 {:name=>"too busy",
  :data=>
   [[Fri, 01 Dec 2017, 0],
    [Mon, 01 Jan 2018, 1],
    [Thu, 01 Feb 2018, 0],
    [Thu, 01 Mar 2018, 3],
    [Sun, 01 Apr 2018, 1],
  :yAxisID=>"y1"},
 {:name=>"made post consultation",
  :data=>
   [[Fri, 01 Dec 2017, 0],
    [Mon, 01 Jan 2018, 2],
    [Thu, 01 Feb 2018, 4],
    [Thu, 01 Mar 2018, 14],
    [Sun, 01 Apr 2018, 12],
  :yAxisID=>"y1"}
  ...
]

When I plot the chart, the legend position updates, but there is only 1 y-axis and all data is plotted against it are unchanged:当我绘制图表时, legend位置会更新,但只有 1 个 y 轴,并且针对它绘制的所有数据都保持不变:

<%= line_chart @consultations, legend: true, library: {
    legend: { position: 'top' },
    scales: {
        y: {
            type: 'linear',
            display: true,
            position: 'left',
        },
        y1: {
            type: 'linear',
            display: true,
            position: 'right',
        }
    }
}, id: 'consult-chart', adapter: 'chartjs' %>

You need to make sure to pass the scales in the correct format.您需要确保以正确的格式传递scales See this example of using Axis IDs in the docs. 请参阅文档中使用轴 ID 的示例。 Ie scales needs to contain a yAxes field with an array of the axis objects and each axis object has an id field that matches one of the IDs from your datasets.scales需要包含一个带有轴对象数组的yAxes字段,并且每个轴对象都有一个id字段,该字段与数据集中的 ID 之一匹配。

So, you should change your code to something like this:因此,您应该将代码更改为如下所示:

<%= line_chart @consultations, legend: true, library: {
    legend: { position: 'top' },
    scales: {
        yAxes: [
            {
                id: 'y',
                type: 'linear',
                display: true,
                position: 'left',
            },
            {
                id: 'y1',
                type: 'linear',
                display: true,
                position: 'right',
            }
        ]
    }
}, id: 'consult-chart', adapter: 'chartjs' %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM