首页 > 解决方案 > Chartjs 持续时间水平条形图

问题描述

我试图弄清楚如何绘制这张图表。它看起来像一个堆叠的水平条形图,但我在为持续时间间隔定义数据集格式时遇到了麻烦。我仍然没有找到构建数据源以实现此结果的正确方法。

另一种选择可能是折线图/散点图。最后一个是编写自定义插件并手动将其绘制在画布上,逐个形状。我想避免这种情况:)

任何想法都会非常有帮助。谢谢!

在此处输入图像描述

标签: chart.jschartjs-2.6.0vue-chartjs

解决方案


在@John Go-Soco 的帮助下,我找到了解决这个问题的方法。图配置的一些非常关键的部分如下所示。简而言之,每一行都是一个单独的数据集,有两个数据点定义开始日期和结束日期。

const yMap = [ 'amlodipine', 'simvastatin', 'lisinopril' ];

const mapDataPoint = function(xValue, yValue) {
                       return {
                          x: xValue,
                          y: yMap.indexOf(yValue)
                       };
                     };

const config = {
                type: 'line',
                data: {
                    datasets: [
                        {
                            //other dataset config options here
                            data: [
                                mapDataPoint('1/1/2007', 'simvastatin'),
                                mapDataPoint('6/1/2010', 'simvastatin'),
                            ]
                        },
                        {
                            //other dataset config options here
                            data: [
                                mapDataPoint('9/1/2010', 'simvastatin'),
                                mapDataPoint('11/1/2018', 'simvastatin'),
                            ]
                        },
                        {
                            //other dataset config options here
                            data: [
                                mapDataPoint('1/1/2007', 'lisinopril'),
                                mapDataPoint('9/1/2015', 'lisinopril'),
                            ]
                        },
                        {
                            //other dataset config options here
                            data: [
                                mapDataPoint('1/1/2014', 'amlodipine'),
                                mapDataPoint('11/1/2022', 'amlodipine'),
                            ]
                        },
                    ]
                },
                options: {
                    //other chart config options here
                    scales: {
                        xAxes: [
                            {
                                type: 'time',
                                time: {
                                    unit: 'year',
                                    round: 'day',
                                    displayFormats: {
                                        year: 'YYYY'
                                    },
                                    min: moment('2006', 'YYYY'),
                                    max: moment('2019', 'YYYY')
                                },

                            }
                        ],
                        yAxes: [
                            {
                                 ticks: {
                                    min: -1,
                                    max: yMap.length,
                                    //setting custom labels on the Y-axes
                                    callback: function(value) {
                                        return yMap[ value ];
                                    }
                                }
                            }
                        ]
                    }
                },

            };

const ctx = 'reference to your ctx here';

const chart = new Chart(ctx, config)

推荐阅读