【问题标题】:Change the Labels of yAxis in chartJs更改chartJs中yAxis的标签
【发布时间】:2020-02-12 16:34:29
【问题描述】:

试图在 ChartJS 中获取折线图。我希望 yAxis 标签应该是 0、250k、500k、750k。

如何更改标签,使其始终至少为 0?

var lineChartData = {
  labels: ["Jun 1", "Jun 2", "jun 3", "Jun 4", "jun 5", "Jun 6", "Jun 7", "Jun 8", "Jun 9", "Jun 
         10", "Jun 11", "Jun 12", "Jun 13", "Jun 14", "Jun 15"],
  datasets: [{
               fillColor: "rgba(220,220,220,0)",
               strokeColor: "rgba(220,180,0,1)",
               pointColor: "#ffc000",
               data: [190, 200, 420, 390, 150, 250, 160, 155, 200, 260, 240, 500, 300, 210, 230, 270]
             },
             {
               fillColor: "rgba(151,187,205,0)",
               strokeColor: "#ff1e6d",
               pointColor: "#ff1e6d",
               data: [290, 390, 300, 440, 625, 375, 390, 325, 280, 295, 385, 295, 485, 280, 265]
             }]
   }

   Chart.defaults.global.animationSteps = 50;
   Chart.defaults.global.tooltipYPadding = 16;
   Chart.defaults.global.tooltipCornerRadius = 0;
   Chart.defaults.global.tooltipTitleFontStyle = "normal";
   Chart.defaults.global.tooltipFillColor = "rgba(0,160,0,0.8)";
   Chart.defaults.global.animationEasing = "easeOutBounce";
   Chart.defaults.global.responsive = true;
   Chart.defaults.global.scaleLineColor = "#816cab";
   Chart.defaults.global.scaleFontSize = 16;
   Chart.defaults.global.scaleFontColor = "white";

   var ctx = document.getElementById("canvas").getContext("2d");
   var LineChartDemo = new Chart(ctx).Line(lineChartData, {
         pointDotRadius: 10,
         bezierCurve: false,
         scaleShowVerticalLines: true,
         scaleGridLineColor: "#816cab"
   });

【问题讨论】:

    标签: javascript charts


    【解决方案1】:

    试试这样:

    var lineChartData = {
      labels: ["FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER", "JANUARY", "FEBRUARY", "MARCH", "APRIL"],
      datasets: [{
        label: "X",
        fillColor: "#ff1e6d",
        strokeColor: "#ff1e6d",
        pointColor: "#ff1e6d",
        data: [290, 390, 300, 440, 625, 375, 390, 325, 280, 295, 385, 295, 485, 280, 265]
      }, {
        label: "Y",
        fillColor: "#ffc000",
        strokeColor: "rgba(220,180,0,1)",
        pointColor: "#ffc000",
        data: [190, 200, 420, 390, 150, 250, 160, 155, 200, 260, 240, 500, 300, 210, 230, 270]
      }]
    }
    
    Chart.defaults.global.animationSteps = 50;
    Chart.defaults.global.tooltipYPadding = 0;
    Chart.defaults.global.tooltipCornerRadius = 0;
    Chart.defaults.global.tooltipTitleFontStyle = "normal";
    Chart.defaults.global.tooltipFillColor = "rgba(0,160,0,0.8)";
    Chart.defaults.global.animationEasing = "easeOutBounce";
    Chart.defaults.global.responsive = true;
    Chart.defaults.global.scaleLineColor = "#816cab";
    Chart.defaults.global.scaleFontSize = 11;
    Chart.defaults.global.scaleFontColor = "white";
    
    new Chart(document.getElementById("fill"), {
      type: 'line',
      data: lineChartData,
      options: {  
        pointDotRadius: 0,
        bezierCurve: true,
        scaleShowVerticalLines: false,
        scaleGridLineColor: "transparent",
        scales: {
          yAxes: [{
            ticks: {
                min:0,
                max:1000,
                stepSize: 250
            }
         }]
         }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
    <canvas id="fill"></canvas>

    【讨论】:

    • 是的,现在它工作得很好。非常感谢我的兄弟。尊敬的先生。
    • 先生还有一件事可以提供帮助。以下事情不起作用:pointDotRadius:10,bezierCurve:false,以及更改网格线颜色和删除图例,更改数据线颜色。标签字体颜色。这些东西应该可以工作。
    • 实际上先生,我无法理解如何修复并将其放置在上面的代码中
    猜你喜欢
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 2017-12-15
    相关资源
    最近更新 更多