ZingChart Y轴标签格式

时间:2019-08-09 15:24:28

标签: zingchart

是否可以在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轴上以摄氏度为单位绘制温度异常。我希望度F出现在次要y轴上。

2 个答案:

答案 0 :(得分:2)

您确实确实使用了函数。我只是语法错误。

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

答案 1 :(得分:0)

以上来自@magnum-π的答案是正确的。创建格式化功能是最简单,最有效的解决方案。

enter image description here

// 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>