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

时间:2016-12-21 09:36:20

标签: javascript jquery angularjs charts chart.js

我想画下面的甘特图

enter image description here

没有选项在图表js中绘制甘特图。可能吗??如果不可能请建议我一些图表库来绘制这样的图表

5 个答案:

答案 0 :(得分:7)

我建议你散点图。在Scatter Charts中,您可以绘制多个独立的线条。从下图中可以看出 ClojureScript cheatsheet

[示例代码]

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
                    }
                }]
            }
        }
    });

像颜色一样休息配置,或者如果要隐藏y轴,请按照您的项目要求进行。

答案 1 :(得分:5)

编辑对于需要为单个Y值显示多个条形的更复杂情况,此方法无效。

我会使用两个数据集的堆积水平栏图表。第一个数据集将是透明的,用于偏移第二个数据集,即您的实际数据。下面的代码也可以防止工具提示出现在第一个数据集中。

enter image description here

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

[示例代码]

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>

答案 2 :(得分:1)

您可以尝试使用此库jQuery.Gantt。它非常有用,并提供了很多绘制甘特图的选项

答案 3 :(得分:1)

另一个开源选项是Frappé Gantt

答案 4 :(得分:0)

一个简单的解决方案是使用 quickchart.io

quickchart.io 的优秀支持人员非常友好地向我发送了一个示例,其中包含与上述某些答案不同的 x 轴上的日期。您可以访问示例 here

如果您想在 Gmail 电子邮件中嵌入甘特图,您首先需要将 HTML 发送到诸如 htmlcsstoimage.com 之类的服务

Example output

相关问题