ZingChart Y 轴标签格式

ZingChart Y Axis Label Formatting

是否可以在 ZingChart 中添加一个辅助 y 刻度,它使用与主要 y 刻度相同的值,但只使用简单的转换(例如异常摄氏度*1.8 = 异常华氏度)。

类似于:

var chartConfig = {
  scaleY2: { format: %v*1.8 }
}

或者,也许是一个函数,例如:

var chartConfig = {
  scaleY2: { format: 'formatAxis()' }
}
...
formatAxis = function(p){ return { format:p.value*1.8 } }

我在主 y 轴上绘制以摄氏度为单位的温度异常。我希望华氏度显示在次要 y 轴上。

你确实用到了函数。我只是遇到语法错误。

var chartConfig = {
  scaleY2: { format: 'formatAxis()' }
}
...
window.formatAxis = function(v){
  return (v*1.8).toFixed(2)+'\u00B0F';
}

以上@magnum-π的回答是正确的。创建格式化函数是最简单有效的解决方案。

// how to call function from ZingChart
let chartConfig = {
  scaleY2: { format: 'formatAxis()' }
}

// defining function for ZingChart to find at the window scope
window.formatAxis = function(v){
  return (v*1.8).toFixed(2)+'\u00B0F';
}

我还配置了一个工作演示来辅助上述答案:

// window.onload event for Javascript to run after HTML
// because this Javascript is injected into the document head
window.addEventListener('load', () => {
  // Javascript code to execute after DOM content
  
  // full ZingChart schema can be found here:
  // https://www.zingchart.com/docs/api/json-configuration/
  let chartConfig = {
    type: 'bar',
    globals: {
      fontSize: '14px',
    },
    title: {
      text: 'Multiple Scales °C vs °F',
      fontSize: '24px',
      adjustLayout: true,
    },
    legend: {
      draggable: true,
    },
    // plot represents general series, or plots, styling
    plot: {
      // hoverstate
      tooltip: {
        // % symbol represents a token to insert a value. Full list here:
        // https://www.zingchart.com/docs/tutorials/chart-elements/zingchart-tokens/
        text: '%kl was %v° %plot-text',
        borderRadius: '3px',
        // htmlMode renders text attribute as html so
        // ° is rendered
        htmlMode: true
      },
      valueBox: {
        color: '#fff',
        placement: 'top-in'
      },
      // animation docs here:
      // https://www.zingchart.com/docs/tutorials/design-and-styling/chart-animation/#animation__effect
      animation: {
        effect: 'ANIMATION_EXPAND_BOTTOM', 
        method: 'ANIMATION_STRONG_EASE_OUT',
        sequence: 'ANIMATION_BY_NODE',
        speed: 275
      }
    },
    plotarea: { margin: 'dynamic',},
    scaleX: {
      // set scale label
      label: {
        text: 'Days'
      },
      // convert text on scale indices
      labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    scaleY: {
      // scale label with unicode character
      label: {
        text: 'Temperature (°C)'
      }
    },
    scaleY2: {
      label: {
        text: 'Temperature (°F)'
      },
      guide: { visible: false }
    },
    // plot values
    series: [
      {
        text: 'Celcius',
        values: [23, 20, 27, 29, 25, 17, 15],
        backgroundColor: '#448aff #64b5f6' ,
        scales: 'scale-x, scale-y'
      },
      {
        text: 'Farenheit',
        values: [35, 42, 33, 49, 35, 47, 35].map(v => Number((v*1.8).toFixed(2))),
        backgroundColor: '#ff5252 #e57373',
        scales: 'scale-x, scale-y-2'
      }
    ]
  };

  // render chart
  zingchart.render({ 
    id: 'myChart', 
    data: chartConfig,
    height: '100%',
    width: '100%',
  });
});
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

.chart--container {
  min-height: 150px;
  width: 100%;
  height: 100%;
}

.zc-ref {
  display: none;
}
<!DOCTYPE html>
<html>
 <head>
    <meta charset="utf-8">
    <title>ZingSoft Demo</title>
  <script src="https://cdn.zingchart.com/zingchart.min.js"></script>
  </head>
  <body>
    <!-- CHART CONTAINER -->
    <div id="myChart" class="chart--container">
      <a class="zc-ref" href="https://www.zingchart.com/">Powered by ZingChart</a>
    </div>
  </body>
</html>