【问题标题】:Hide label on xAxis in Line-Chart in canvas Html5在画布 Html5 中的折线图中隐藏 x 轴上的标签
【发布时间】:2017-10-06 18:45:12
【问题描述】:

我正在实现折线图,我想从折线图中隐藏 x'Axis 标签。我把 scaleFontSize: 0, ,比 x'Axis 和 Y'axis 标签是隐藏的。但我只想隐藏 x'Axis 标签。

var lineOptions = {
                ///Boolean - Whether grid lines are shown across the chart
                scaleShowGridLines : true,
                //String - Colour of the grid lines
                scaleGridLineColor : "rgba(0,0,0,.05)",
                //Number - Width of the grid lines
                scaleGridLineWidth : 1,
                //Boolean - Whether the line is curved between points
                bezierCurve : true,
                //Number - Tension of the bezier curve between points
                bezierCurveTension : 0.4,
                //Boolean - Whether to show a dot for each point
                pointDot : true,
                //Number - Radius of each point dot in pixels
                pointDotRadius : 4,
                //Number - Pixel width of point dot stroke
                pointDotStrokeWidth : 1,
                //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
                pointHitDetectionRadius : 20,
                //Boolean - Whether to show a stroke for datasets
                datasetStroke : true,
                //Number - Pixel width of dataset stroke
                datasetStrokeWidth : 2,
                //Boolean - Whether to fill the dataset with a colour
                datasetFill : true,
                //Boolean - Re-draw chart on page resize
                responsive: true,
                //String - A legend template
                legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].lineColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
            };
       var lineData = {
        labels: data,
        datasets: [
            {               
            pointHighlightStroke: "rgba(151,187,205,1)",
            data: []
        }

    ]
};

         var getElement = document.getElementById("departuresChart2");
                var ctx = getElement.getContext("2d");
                $scope.myNewChart = new Chart(ctx).Line(lineData, lineOptions);

我正在参考http://www.chartjs.org/docs/#line-chart-introduction。 我只想隐藏 A'axis 标签。我在 stackoverflow Remove x-axis label/text in chart.js 中看到了一个链接。但我仍然无法修复。谢谢

【问题讨论】:

  • 请告诉我们小提琴或图片有什么问题。
  • @artgb。我无法制作 jsfddle。因为我不能公开我的代码。但我会提供屏幕截图
  • 您使用的是哪个版本的 chart.js?
  • @GRUNT 我正在使用github.com/mengxiangbin/SmartAdmin,这是关注这个chartjs.org/docs/latest
  • 你的图表语法和你发布的一样吗?

标签: javascript html charts html5-canvas linechart


【解决方案1】:

您必须将图表(实例)的 scale.xLabels 属性设置为空数组 - [] (隐藏 x 轴网格线),或 @ 987654323@ (显示 x 轴网格线),隐藏 x 轴标签。

示例

var lineOptions = {
   //Boolean - Whether grid lines are shown across the chart
   scaleShowGridLines: true,
   //String - Colour of the grid lines
   scaleGridLineColor: "rgba(0,0,0,.05)",
   //Number - Width of the grid lines
   scaleGridLineWidth: 1,
   //Boolean - Whether the line is curved between points
   bezierCurve: true,
   //Number - Tension of the bezier curve between points
   bezierCurveTension: 0.4,
   //Boolean - Whether to show a dot for each point
   pointDot: true,
   //Number - Radius of each point dot in pixels
   pointDotRadius: 4,
   //Number - Pixel width of point dot stroke
   pointDotStrokeWidth: 1,
   //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
   pointHitDetectionRadius: 20,
   //Boolean - Whether to show a stroke for datasets
   datasetStroke: true,
   //Number - Pixel width of dataset stroke
   datasetStrokeWidth: 2,
   //Boolean - Whether to fill the dataset with a colour
   datasetFill: true,
   //Boolean - Re-draw chart on page resize
   responsive: true,
   //String - A legend template
   legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].lineColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
};
var lineData = {
   labels: ["January", "February", "March", "April", "May", "June", "July"],
   datasets: [{
      label: "My Second dataset",
      fillColor: "rgba(151,187,205,0.2)",
      strokeColor: "rgba(151,187,205,1)",
      pointColor: "rgba(151,187,205,1)",
      data: [28, 48, 40, 19, 86, 27, 90]
   }]
};

var getElement = document.getElementById("departuresChart2");
var ctx = getElement.getContext("2d");
myNewChart = new Chart(ctx).Line(lineData, lineOptions);
myNewChart.scale.xLabels = []; //or set -> myNewChart.scale.xLabels.map(e => '');
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<canvas id="departuresChart2"></canvas>

【讨论】:

  • @GRUNT 感谢您的回答。
  • @GRUNT 当然可以。它非常有用。如果我的问题对其他人有帮助,请点赞:)
  • 完成!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
相关资源
最近更新 更多