使用C3JS仅在Y轴上显示最小/最大值

Show only the MIN/MAX value on Y-AXIS with C3JS

本文关键字:最大值 显示 C3JS 仅在 使用      更新时间:2023-09-26

我希望y轴只包含我的数据的最小/最大值。我尝试使用d3指令,但没有结果。

我看了一下谷歌,但没有找到实现这种行为的答案。

代码下方:

 $.getJSON('assets/json/chartc3.json', function(data) 
{ 
    scene=data;
    var chart = c3.generate({
    bindto: '#chartc3',
    data: 
        {
            json: scene,
            keys: 
            {
                    x: 'round',
                    value: ['Marketable', 'Total Requested Capacity', 'Your Bid'],
            },
            types: {
                Marketable: 'area'
            },
            colors: {
                Marketable: '#A09FA2',
                'Total Requested Capacity': '#272E80',
                'Your Bid': '#8EBF60'
            }
        },
    axis: 
    {
        x: {
            tick: 
            {
                culling: 
                {
                    max: 10
                }
            },
          type: 'category'
        },
        y: 
        {
            min: 0,
             padding : {
              bottom : 0
            },
            tick: 
            {
                values: [[0], [***d3.max(scene)]***],
                format:  function (d) { return d3.format(',f')(d) +' kWh/h' }
      //or format: function (d) { return '$' + d; }
            }
        }
      }.........

我如何才能达到上述结果?d3.max(场景)返回NaN。

问题是场景不是数组,而是json对象。

var k = d3.max([1,5,2])
k will be 5

因此,您需要传递一个元素数组,这些元素构成了您的y序数。

您需要使用

d3.max(arr, function(d){return numberic value});

var arr = scene.map(function(d){return ...the number value..})
y:{
    min:d3.min(arr),
    max:d3.max(arr)
},

函数取决于数据的数组元素。

我用了一个小函数来自己计算最大值。

var maxs=0;
    for (var j=0; j<scene.length; j++) {
        var maxtemp=Math.max(scene[j].Marketable, scene[j]['Your Bid'], scene[j]['Total Requested Capacity']);
        maxs=Math.max(maxs, maxtemp);
    }