How to Draw Gantt chart using chart js or other libraries

前端 未结 4 1158
-上瘾入骨i
-上瘾入骨i 2020-12-31 14:09

I want to draw Gantt chart like below

There is no option to draw Gantt chart in chart js. is it possible?? if not possible please suggest me some charting l

4条回答
  •  别那么骄傲
    2020-12-31 14:51

    EDIT this method would not work efficiently for more complicated cases where multiple bars need to be shown for a single Y value.

    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

    [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;
    }
    
    
    
    

提交回复
热议问题