当只有1 bar时,Chart.js无法正常绘制线条

时间:2016-12-19 05:56:22

标签: html css canvas charts chart.js

我想做这个图表:

正如您所看到的那样( Meta Rangodeaceptación)这些线条被绘制成一个点。我相信这是因为我只有一个酒吧而在另一个图表中,我用2个条形图制作,我得到的线条就像一条线条。

这是代码,我做错了什么?

<canvas id="myChart2016_5857751099b04" width="500" height="500" style="display: block; width: 500px; height: 500px;"></canvas>
<script style="text/javascript">
                var ctx2016_5857751099b04 = document.getElementById("myChart2016_5857751099b04");
                var myChart2016_5857751099b04 = new Chart(ctx2016_5857751099b04, {
                    type: 'bar',
                    data: {

            labels: ["Año 1"],
            datasets: [
                            {backgroundColor:'rgba(36, 143, 36,0)',borderColor:'rgba(75, 172, 198,1)',
                        type: 'line',
                        label: 'Meta',
                        data: [100],
                        options:  { tension:0.0, bezierCurve:false },borderWidth: 1,tension:0.25 },                         {backgroundColor:'rgba(51, 51, 26,0)',borderColor:'rgba(182, 87, 8,1)',
                        type: 'line',
                        label: 'Rango de aceptación ',
                        data: [90],
                        options:  { tension:0.0, bezierCurve:false },borderWidth: 1,tension:0.25 },                         {backgroundColor:['rgba(99, 33, 36 ,0.1)'],borderColor:['rgba(99, 33, 36, 1)'],
                        type: 'bar',
                        label: 'Porcentaje de proveedores evaluados satisfactoriamente ',
                        data: [100],
                        options:  { tension:0.0, bezierCurve:false },borderWidth: 1,tension:0.25 }          ]},
                options: {
                        tension:1,
                        scaleBeginAtZero: true,
                        scaleStartValue: 0,
                        scales: {
                            xAxes: [{
                                categorySpacing: 20,
                                gridLines: {
                                      color: "rgba(0, 0, 0, 0)",
                                  },
                            }],
                            yAxes: [{
                                categorySpacing: 20,
                                ticks: {
                                    beginAtZero:true
                                }
                            }]
                        }

                    }
                });
                </script>

我尝试了 Meta Rangodeaceptación数据:

data: [100,100,100]data: [90,90,90]

但结果出人意料:

[1]

1 个答案:

答案 0 :(得分:0)

将您的标签分配给它将开始工作的值。

labels: ["Año 1","",""]

以下是工作示例代码

<script style="text/javascript">
                var ctx2016_5857751099b04 = document.getElementById("myChart2016_5857751099b04");
                var myChart2016_5857751099b04 = new Chart(ctx2016_5857751099b04, {
                    type: 'bar',
                    data: {

            labels: ["Año 1","",""],
            datasets: [
                            {backgroundColor:'rgba(36, 143, 36,0)',borderColor:'rgba(75, 172, 198,1)',
                        type: 'line',
                        label: 'Meta',
                        data: [100,100,100],
                        options:  { tension:0.0, bezierCurve:false },borderWidth: 1,tension:0.25 },                         {backgroundColor:'rgba(51, 51, 26,0)',borderColor:'rgba(182, 87, 8,1)',
                        type: 'line',
                        label: 'Rango de aceptación ',
                        data: [90,90,90],
                        options:  { tension:0.0, bezierCurve:false },borderWidth: 1,tension:0.25 },                         {backgroundColor:['rgba(99, 33, 36 ,0.1)'],borderColor:['rgba(99, 33, 36, 1)'],
                        type: 'bar',
                        label: 'Porcentaje de proveedores evaluados satisfactoriamente ',
                        data: [100],
                        options:  { tension:0.0, bezierCurve:false },borderWidth: 1,tension:0.25 }          ]},
                options: {
                        tension:1,
                        scaleBeginAtZero: true,
                        scaleStartValue: 0,
                        scales: {
                            xAxes: [{
                                categorySpacing: 20,
                                gridLines: {
                                      color: "rgba(0, 0, 0, 0)",
                                  },
                            }],
                            yAxes: [{
                                categorySpacing: 20,
                                ticks: {
                                    beginAtZero:true
                                }
                            }]
                        }

                    }
                });
                </script>