简体   繁体   English

如何使用chart js或其他库绘制甘特图

[英]How to Draw Gantt chart using chart js or other libraries

I want to draw Gantt chart like below我想绘制如下甘特图

在此处输入图片说明

There is no option to draw Gantt chart in chart js.在图表js中没有绘制甘特图的选项。 is it possible??是否可以?? if not possible please suggest me some charting libraries to draw graph like this如果不可能,请建议我一些图表库来绘制这样的图形

I suggest you Scatter Chart .我建议你散点图 In Scatter Charts, you can draw multiple independent lines.在散点图中,您可以绘制多条独立的线。 As you can see from the below image.如下图所示。
在此处输入图片说明

[ Sample Code ] [示例代码]

var scatterChart = new Chart(ctx1, {
        type: 'line',
        data: {
            datasets: [
            {

                label: 'Scatter Dataset',
                backgroundColor: "rgba(246,156,85,1)",
                borderColor: "rgba(246,156,85,1)",
                fill: false,
                borderWidth : 15,
                pointRadius : 0,
                data: [
                    {
                        x: 0,
                        y: 9
                    }, {
                        x: 3,
                        y: 9
                    }
                ]
            },
            {
                backgroundColor: "rgba(208,255,154,1)",
                borderColor: "rgba(208,255,154,1)",
                fill: false,
                borderWidth : 15,
                pointRadius : 0,
                data: [
                    {
                        x: 3,
                        y: 7
                    }, {
                        x: 5,
                        y: 7
                    }
                ]
            },
            {

                label: 'Scatter Dataset',
                backgroundColor: "rgba(246,156,85,1)",
                borderColor: "rgba(246,156,85,1)",
                fill: false,
                borderWidth : 15,
                pointRadius : 0,
                data: [
                    {
                        x: 5,
                        y: 5
                    }, {
                        x: 10,
                        y: 5
                    }
                ]
            },
            {
                backgroundColor: "rgba(208,255,154,1)",
                borderColor: "rgba(208,255,154,1)",
                fill: false,
                borderWidth : 15,
                pointRadius : 0,
                data: [
                    {
                        x: 10,
                        y: 3
                    }, {
                        x: 13,
                        y: 3
                    }
                ]
            }
            ]
        },
        options: {
            legend : {
                display : false
            },
            scales: {
                xAxes: [{
                    type: 'linear',
                    position: 'bottom',
                    ticks : {
                        beginAtzero :true,
                        stepSize : 1
                    }
                }],
                yAxes : [{
                    scaleLabel : {
                        display : false
                    },
                    ticks : {
                        beginAtZero :true,
                        max : 10
                    }
                }]
            }
        }
    });

Rest the configuration like colors or if you want to hide the y axes do it as your project required.保留颜色等配置,或者如果您想隐藏 y 轴,请根据您的项目需要进行设置。

EDIT this method would not work efficiently for more complicated cases where multiple bars need to be shown for a single Y value.编辑此方法对于需要为单个 Y 值显示多个条形的更复杂情况无效。

I would go with a stacked horizontalbar chart of two datasets.我会使用两个数据集的堆叠水平条形图。 The first dataset would be transparent and used to offset the second dataset which is your actual data.第一个数据集将是透明的,用于抵消作为您的实际数据的第二个数据集。 The code below prevents tooltip from appearing for the first dataset as well.下面的代码也防止出现第一个数据集的工具提示。

在此处输入图片说明

http://codepen.io/pursianKatze/pen/OmbWvZ?editors=1111 http://codepen.io/pursianKatze/pen/OmbWvZ?editors=1111

[SAMPLE CODE] [示例代码]

 var barOptions_stacked = { hover :{ animationDuration:10 }, scales: { xAxes: [{ label:"Duration", ticks: { beginAtZero:true, fontFamily: "'Open Sans Bold', sans-serif", fontSize:11 }, scaleLabel:{ display:false }, gridLines: { }, stacked: true }], yAxes: [{ gridLines: { display:false, color: "#fff", zeroLineColor: "#fff", zeroLineWidth: 0 }, ticks: { fontFamily: "'Open Sans Bold', sans-serif", fontSize:11 }, stacked: true }] }, legend:{ display:false }, }; var ctx = document.getElementById("myChart"); var myChart = new Chart(ctx, { type: 'horizontalBar', data: { labels: ["1", "2", "3", "4"], datasets: [{ data: [50,150, 300, 400, 500], backgroundColor: "rgba(63,103,126,0)", hoverBackgroundColor: "rgba(50,90,100,0)" },{ data: [100, 100, 200, 200, 100], backgroundColor: ['red', 'green', 'blue', 'yellow'], }] }, options: barOptions_stacked, }); // this part to make the tooltip only active on your real dataset var originalGetElementAtEvent = myChart.getElementAtEvent; myChart.getElementAtEvent = function (e) { return originalGetElementAtEvent.apply(this, arguments).filter(function (e) { return e._datasetIndex === 1; }); }
 .graph_container{ display:block; width:600px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.js"></script> <html> <body> <div class="graph_container"> <canvas id="myChart"></canvas> </div> </body> </html>

另一个开源选项是Frappé Gantt

You can try this library jQuery.Gantt .你可以试试这个库jQuery.Gantt It is very useful and provide lots of options to draw Gantt Chart它非常有用,并提供了许多绘制甘特图的选项

An easy solution to this is to use quickchart.io一个简单的解决方案是使用 quickchart.io

The good support people at quickchart.io were kind enough to send me an example that includes dates on the x-axis unlike some of the answers above. quickchart.io 上的优秀支持人员非常友好地向我发送了一个示例,其中包含 x 轴上的日期,这与上面的一些答案不同。 You can access the example here .您可以在此处访问示例。

If you would then like to embed a Gantt chart into Gmail email you would first need to send the HTML to a service such as htmlcsstoimage.com如果您想在 Gmail 电子邮件中嵌入甘特图,您首先需要将 HTML 发送到诸如 htmlcsstoimage.com 之类的服务

示例输出

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

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