简体   繁体   English

Chartjs更改水平轴值

[英]Chartjs change horizontal axis value

What options to set in Chartjs to change the horizontal axis value, from the code below to have a base value of 100, instead of 0. 在Chartjs中设置哪些选项可更改水平轴值,以下代码将其基值为100,而不是0。

Chart to look like this, but y-axis base should be a 100 and not 0 as shown in the graph here. 图表看起来像这样,但y轴底应为100,而不是0,如此处的图形所示。

So, if a value of 70 is plotted on the bar chart, then it's y-axis should start at 100 to 70. While a value of 120, should start at 100 to 120 on it's y-axis. 因此,如果在条形图上绘制70的值,则其y轴应从100到70开始。而120的值应在其y轴上从100到120开始。

Mock up, Excel version of how it should look like. 模拟一下,Excel版本的外观如何。

I have tried some answers here but, unable to achieve this in Chartjs. 我在这里尝试了一些答案,但是无法在Chartjs中实现。

Note: I am working of the sample code from chartjs website. 注意:我正在处理来自chartjs网站的示例代码。 However, still can't get the specific scaling as needed. 但是,仍然无法获得所需的特定缩放比例。

Chartjs 2.6.0 Chartjs 2.6.0

var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var color = Chart.helpers.color;
    var barChartData = {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [{
            label: 'Dataset 1',
            backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
            borderColor: window.chartColors.red,
            borderWidth: 1,
            data: [140,10,60,69,56,110]

        }, {
            label: 'Dataset 2',
            backgroundColor: color(window.chartColors.blue).alpha(0.5).rgbString(),
            borderColor: window.chartColors.blue,
            borderWidth: 1,
            data: [50,120,70,98,130,34]
        }]

    };

    window.onload = function() {
        var ctx = document.getElementById("canvas").getContext("2d");
        window.myBar = new Chart(ctx, {
            type: 'bar',
            data: barChartData,
            options: {
                responsive: true,
                legend: {
                    position: 'top',
                },
                title: {
                    display: true,
                    text: 'Chart.js Bar Chart'
                }
            }
        });

    };

Edited Removed Random numbers, and edited constants for testing purposes. 编辑已删除的随机数,以及用于测试目的的已编辑常量。 I am currently looking into pluggins for Chartjs. 我目前正在研究Chartjs的插件。 This option might not be available in distributed package. 此选项在分布式程序包中可能不可用。

Just add 只需添加

ticks: {           
    min:100
}

in your options under the y-axis scales your option should look like this. 在y轴比例下的选项中,您的选项应如下所示。

options: {
    scales: {
        yAxes:[{
            ticks: {
                min:10
            }
        }],
    },
    responsive: true,
    legend: {
        position: 'top',
    },
    title: {
        display: true,
        text: 'Chart.js Bar Chart'
    }
}

Ps: check the indentation because i was not using the editor. 附:检查缩进,因为我没有使用编辑器。

Edit May be you have stacked in this case removed the stack type. 编辑在这种情况下,可能是您已经堆叠了,请移除堆叠类型。 See this fiddle 看到这个小提琴

Workaround Answer based of @adeel suggestions: 解决方法基于@adeel建议:

So from the fiddle below, I used stacking on both y and x-axis, to get the desired effect. 因此,从下面的小提琴中,我在y轴和x轴上都使用了叠加,以获得所需的效果。 The first dataset (just an offset dataset) is used as the basis to stack the second dataset (actual) to get the desired effect. 第一个数据集(仅是偏移数据集)用作堆叠第二个数据集(实际)以获得所需效果的基础。

fiddle 小提琴

var ctx = document.getElementById("myChart").getContext("2d");
var data = {
  labels: ["January", "February", "March", "April", "May", "June"],
  datasets: [{
    label: "Offset",
    backgroundColor: "transparent",
    data: [50,100,70,60,100,100]
  },{
    label: 'Actual Dataset',
    backgroundColor: 'Blue',
    borderWidth: 1,
    data: [50,20,30,40,30,70]
  }]
};

var myBarChart = new Chart(ctx, {
  type: 'bar',
  data: data,
  options: {
    scales: {
      xAxes: [{
        stacked: true
      }],
      yAxes: [{
        stacked: true,
      }]
    },
  }
});

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

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