简体   繁体   English

在雷达中设置最小值、最大值和步数 chart.js

[英]Set min, max and number of steps in radar chart.js

I am using chartjs.org 2.2.1 and have a radar chart that has values between 1..5.我正在使用 chartjs.org 2.2.1 并且有一个雷达图,其值在 1..5 之间。 I want to set the min value to 0 and max to 5 with a step of 1.我想将最小值设置为 0,将最大值设置为 5,步长为 1。

This seemed to be answered exactly here in this SO post .这似乎在这篇SO 帖子中得到了准确的回答。 However my charts still have a weird scale and not the one that I have defined as per my code below.然而,我的图表仍然有一个奇怪的比例,而不是我根据下面的代码定义的比例。

Can anyone see what I am doing wrong here?谁能看到我在这里做错了什么?

    var options = {
        responsive: false,
        maintainAspectRatio: true
    };

    var dataLiteracy = {
        labels: [
            @PointLabel("Literacy", 1), @PointLabel("Literacy", 2), @PointLabel("Literacy", 3),
            @PointLabel("Literacy", 4), @PointLabel("Literacy", 5)
        ],
        datasets: [
            {
                label: "Literacy",
                backgroundColor: "rgba(179,181,198,0.2)",
                borderColor: "rgba(179,181,198,1)",
                pointBackgroundColor: "rgba(179,181,198,1)",
                pointBorderColor: "#fff",
                pointHoverBackgroundColor: "#fff",
                pointHoverBorderColor: "rgba(179,181,198,1)",
                data: [
                    @PointValue("Literacy", 1), @PointValue("Literacy", 2), @PointValue("Literacy", 3),
                    @PointValue("Literacy", 4), @PointValue("Literacy", 5)
                ]
            }
        ]
    };

    var ctx = $("#chartLiteracy");
    var myRadarChart = new Chart(ctx,
    {
        type: 'radar',
        data: dataLiteracy,
        options: options,
        scaleOverride: true,
        scaleSteps: 5,
        scaleStepWidth: 1,
        scaleStartValue: 0
    });

You are right, but only if you are using Chart.js v1.x .你是对的,但前提是你使用的是 Chart.js v1.x

Ticks options have changed in v2.x (the one you are using). v2.x (您正在使用的那个)中的刻度选项已更改。


If you want to edit radar ticks, you will need to edit the ticks attribute in your chart options : 如果要编辑雷达刻度,则需要在图表选项中编辑ticks属性:

 var options = { scale: { ticks: { // changes here } } };

From what you need (mak a scale from 0 to 5), you can either :根据您的需要(从 0 到 5),您可以:

  • set the attribute beginAtZero to true and max to 5将属性beginAtZero设置为 true, max设置为 5
  • set the attribute min to 0 and max to 5将属性min设置为 0,将max设置为 5

You can see here the result.你可以在这里看到结果。

Set the value of stepSize设置stepSize的值

scale: {
    ticks: {
        beginAtZero: true,
        max: 5,
        min: 0,
        stepSize: 1
    }
}

Starting ChartJS 3 the min/max scale values are not specified in scale.ticks but in options.scale or options.scales[id] object, example :ChartJS 3开始,最小/最大比例值不在scale.ticks中指定,而是在options.scaleoptions.scales[id]对象中指定,例如:

new Chart(hostElement, {
    type: "radar",
    data,
    options: {
        scale: {
            min: 0,
            max: 100,
        },
    },
});

https://www.chartjs.org/docs/latest/axes/radial/linear.html#linear-radial-axis : https://www.chartjs.org/docs/latest/axes/radial/linear.html#linear-radial-axis

scales.[x/y]Axes.ticks.max was renamed to scales[id].max scales.[x/y]Axes.ticks.max 重命名为 scales[id].max
scales.[x/y]Axes.ticks.min was renamed to scales[id].min scales.[x/y]Axes.ticks.min 重命名为 scales[id].min

https://codepen.io/JCH77/pen/abNymae https://codepen.io/JCH77/pen/abNymae

Only this schema worked for me on v3.8.0只有这个架构在v3.8.0上对我有用

 options: {
   maintainAspectRatio: false,
   scales: {
     r: {
       min: 0,
       max: 5,
       beginAtZero: true,
       angleLines: {
         color: "red",
      },
    },
 }

In chart.js V3 you can do this for the radar type:在 chart.js V3 中,您可以为雷达类型执行此操作:

 options: { responsive: true, maintainAspectRatio: false, scales: { r: { min: 0, // MIN max: 5, // MAX beginAtZero: true, angleLines: { display: true, // color: 'red', }, grid: { circular: true, }, ticks: { stepSize: 1, // the number of step }, }, }, }

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

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