简体   繁体   English

如何在chart.js上配置我的y轴?

[英]How can configure my y-axis on chart.js?

I am trying to build graph. 我正在尝试构建图形。

My y-axis start with 0 here, I dont know how to configure it and why it is talking 0 - I see other post which mentioned scaleOverride:true, scaleStartValue:0.1, scaleStepWidth:5 - I dont know how to use that in my below code , how can configure y-axis in chart.js. 我的y轴在这里以0开头,我不知道如何配置它以及为什么它在说0 - 我看到其他帖子提到了scaleOverride:true,scaleStartValue:0.1,scaleStepWidth:5 - 我不知道怎么用在我的在代码下面,如何在chart.js中配置y轴。

Any pointer would be 任何指针都是

I have following code 我有以下代码

var barChartData = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
        label: 'Dataset 1',
        backgroundColor: "rgba(220,220,220,0.5)",
        data: [6, 6, 6, 8, 6, 9, 8]
    }]
};
function barChart() {
    var context = document.getElementById('stacked').getContext('2d');
    var myBar = new Chart(context, {
        type: 'bar',
        data: barChartData,
        options: {
            title:{
                display:true,
                text:"Chart.js Bar Chart - Stacked"
            },
            tooltips: {
                mode: 'label'
            },
            responsive: true,
            scales: {
                xAxes: [{
                    stacked: true,
                }],
                yAxes: [{
                    stacked: true
                }]
            }
        }
    });
};
$(document).ready(function() {
    $(document).ready(barChart);
});

Thank you guys for helping, I observed that chart.js will automatically take value for y-axis based on your data, you can change y/x-axis setting under Options > Scales , I also needed to change step size of y-axis and get rid of x-axis grid line,"ticks" is something I was looking for, here is my sample code and steps to achieved all these. 谢谢大家帮忙,我观察到chart.js会根据你的数据自动获取y轴的值,你可以在Options> Scales下更改y / x轴设置,我还需要改变y轴的步长并且摆脱了x轴网格线,“ticks”是我正在寻找的东西,这是我的示例代码和实现所有这些的步骤。

Step 1) Canvas this is place holder where your chart will display on JSP 步骤1)Canvas这是占位符,您的图表将显示在JSP上

<canvas width="1393px;" height="500px;" id="stacked"></canvas>

Step 2) Create sample datasets (this is JSON you need to create based on your requirement but make sure you provide exact the same formated JSON response as given below along with your data. 步骤2)创建样本数据集(这是您需要根据您的要求创建的JSON,但请确保您提供与下面给出的完全相同的格式化JSON响应以及您的数据。

var barChartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
    label: 'Dataset 1',
    backgroundColor: "red",
    data: [9, 6, 6, 8, 6, 9, 8]
},
{
    label: 'Dataset 1',
        backgroundColor: "rgba(225,226,228,0.5)",
        data: [9, 6, 6, 8, 6, 9, 8]
    }]
};

or you can call JSON from controller inside script as below

var jsonArry = <%=request.getAttribute("jsonArry")%>;

Step 3) call that JSON which you have created at step 2 步骤3)调用您在步骤2中创建的JSON

  function barChart(){
          var context = document.getElementById('stacked').getContext('2d');
          var myBar = new Chart(context, {
          type: 'bar',
          data: jsonArry,

  options: {
         tooltips: {
          mode: 'label'
      },
      responsive: true,
      scales: {
          xAxes: [{
              ticks:{
                  stepSize : 10,

              },
              stacked: true,
            gridLines: {
                    lineWidth: 0,
                    color: "rgba(255,255,255,0)"
                }
          }],
          yAxes: [{

              stacked: true,
               ticks: {
                  min: 0,
                  stepSize: 1,
              }

          }]
      }
      }
  });

};

Hope this will help , for reference I have used following documentation Chart JS 希望这会有所帮助,作为参考,我使用了以下文档Chart JS

Thanks. 谢谢。

Just remove the stacked option and it will stop starting from 0 (unless your data starts from 0). 只需删除stacked选项,它将从0开始停止(除非您的数据从0开始)。

Related fiddle - http://jsfiddle.net/rsyk9he0/ 相关小提琴 - http://jsfiddle.net/rsyk9he0/

For stacked charts, Chart.js builds a list of positive and negative sums for each stack (bar) and then uses that to figure out the scale min and max values. 对于堆叠图表,Chart.js为每个堆栈(bar)构建正负和的列表,然后使用它来计算出最小值和最大值。 If there are no negative values, the list of negative sums is a list of 0s. 如果没有负值,则负和列表是0的列表。 This forces the scale min to be 0. 这会迫使刻度min为0。


scaleStartValue, scaleStepWidth, etc. are options from v1.x of Chart.js. scaleStartValue,scaleStepWidth等是Chart.js的v1.x中的选项。 You are using v2.x. 您正在使用v2.x. See How to make integer scale in Chartjs for the 2.x equivalents. 有关2.x等效 ,请参见如何在Chartjs中进行整数比例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM