js设置y轴标签格式以显示两个小数

Flot.js set y-axis labels format to show two decimals

本文关键字:小数 两个 显示 设置 标签 格式 js      更新时间:2023-09-26

我如何设置我的y轴标签来显示两个小数位置我检查了我的代码,发生的事情是,而不是显示14.59它显示15.00,下面是我的代码:

$.plot("#placeholder", data, {
                xaxis: {
                    min: minDate,
                    max: maxDate,
                    mode: "time",
                    tickSize: setTickSize,
                    monthNames: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
                },
                yaxis: {
                    min: yMin,
                    max: yMax,
                    ticks: 1,
                    tickSize: 1,
                    tickDecimals:2
                },
                series: {
                    lines: {
                        show: true,
                        fill: true
                    },
                    points: {
                        show: false
                    }
                },
                grid: {
                    borderWidth: 1,
                    hoverable: true
                }
            });

我读到我可以使用tickFormatter函数,但我不知道确切地把它放在哪里

你可以用你的y轴数据点构建一个刻度数组,因为它是动态的,并将它传递给y轴刻度。

yaxis: {
         ticks: [25.49, 28.49, 31.49, 34.49, 37.49, 40.49],
               
         tickDecimals:2
       }

var data = [];
data = [
  [3, 36.86],
  [5, 29.66],
  [8, 33.47]
];

dataLine = [];
dataLine.push(data);
$.plot($("#placeholder"), dataLine, {

  grid: {
    backgroundColor: "#E5E5E5",
    hoverable: true
  },
  points: {
    show: true,
    radius: 4
  },
  lines: {
    show: true
  },
  yaxis: {
    ticks: [25.49, 28.49, 31.49, 34.49, 37.49, 40.49],
    tickDecimals: 2
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://www.pureexample.com/js/flot/jquery.flot.min.js"></script>
<div id="placeholder" style="width:400px;height:300px;"></div>