从Chart.js的条形图的x轴上删除0(零)

时间:2019-01-14 17:12:01

标签: javascript chart.js

对于x轴(1月-5月)的零值,我什么也不想显示0。以下代码用于绘图。任何帮助/建议表示赞赏

$scope.showChart = function (finalLabels, finalDatasets, finalTitle, plotType,dateRange) {

    var ctx = document.getElementById("chart").getContext('2d');
    var plotConfig = {

        type: plotType,
        data: {
            labels: finalLabels,
            datasets: finalDatasets
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            layout: {
                padding: {left: 0, right: 0, top: 10, bottom: 0}
            },
            plugins: {
                labels: {
                    render: 'value',
                    fontSize: 10,

                }
            },
            legend: {
                display: false,
                labels: {
                    fontSize: 10,
                    boxWidth: 35
                }
            },
            scales: {
                xAxes: [{
                    ticks: { autoSkip: false,fontSize:10
                    }
                }],
                yAxes: [{
                    scaleLabel: {
                        display: true,
                        labelString: 'Testing',
                        fontSize:10
                    },
                    ticks: {
                        beginAtZero: true,
                        fontSize:10
                    }
                }]
            },

       /*     title: {
                display: true,
                text: [finalTitle.toUpperCase(),dateRange],
                fontSize:10,
                padding:0
            }*/
        }
    };
    if ($scope.chart) {
        $scope.chart.destroy();
    }

    $scope.chart = new Chart(ctx, plotConfig);
};

enter image description here

3 个答案:

答案 0 :(得分:2)

如果您使用的是DataLabels插件,请按照以下步骤操作scriptable display option

在插件选项中,您可以使用函数来指示DataLabels是否显示标签。因此,在这种情况下,可以简单地检查该值是否大于0。

var ctx = document.getElementById("myChart");
debugger;
var data = {
  labels: ["Jan 18", "Feb 18", "Mar18", "Apr 18", "May 18", "Jun 18", "Jul 18"],
  datasets: [{
    label: "# of Votes",
    data: [0, 0, 0, 0, 10, 12, 24]
  }]
}
var myChart = new Chart(ctx, {
  type: 'bar',
  data: data,
  legend: {
    "display": false
  },
  plugins: [ChartDataLabels],
  options: {
    plugins: {
      // Change options for ALL labels of THIS CHART
      datalabels: {
        color: "#36A2EB",
        anchor: "end",
        display: function(context) {
          var index = context.dataIndex;
          var value = context.dataset.data[index];
          return value > 0; // display labels with a value greater than 0
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@0.5.0/dist/chartjs-plugin-datalabels.min.js"></script>
<canvas id="myChart" width="100" height="100"></canvas>

答案 1 :(得分:1)

如何设置animation.onComplete上显示的文本的格式?

var ctx = document.getElementById("myChart");
debugger;
var data = {
  labels: ["2 May", "9 May", "16 May", "23 May", "30 May", "6 Jun", "13 Jun"],
  datasets: [{
    data: [0, 0, 56, 50, 88, 60, 45],
    backgroundColor: "#EEEEEE"
  }]
}
var myChart = new Chart(ctx, {
  type: 'bar',
  data: data,
  legend: {
    "display": false
  },
  options: {
    "hover": {
      "animationDuration": 0
    },
    "animation": {
      "duration": 1,
      "onComplete": function() {
        var chartInstance = this.chart,
          ctx = chartInstance.ctx;

        ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
        ctx.textAlign = 'center';
        ctx.textBaseline = 'bottom';

        this.data.datasets.forEach(function(dataset, i) {
          var meta = chartInstance.controller.getDatasetMeta(i);
          meta.data.forEach(function(bar, index) {
            var data = dataset.data[index];
            if (data) {
            ctx.fillText(data, bar._model.x, bar._model.y - 5);
            }
          });
        });
      }
    }
  }
});
<!doctype html>
<html>
<head>
  <title>example.com/test</title>
</head>
<body>
  <canvas id="myChart" width="100" height="100"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.js"></script>
</body>
</html>

答案 2 :(得分:0)

我在插件的渲染中仅添加了一个条件,并且效果很好。

plugins: {
           labels: {
                  render: function (args) {
                            if (args.value != 0)
                                return args.value;
                        },
                  fontSize: 10

                    }
          },