Magnum π
Magnum π

Reputation: 55

ZingChart Y Axis Label Formatting

Is it possible in ZingChart to add a secondary y scale that uses the same values as the primary y scale, but just uses a simple conversion (e.g., anomaly degrees Celsius*1.8 = anomaly degrees Fahrenheit).

something like:

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

Or, perhaps a function, like:

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

I'm plotting temperature anomalies in degrees C on the primary y-axis. I'd like the degrees F to show up on the secondary y-axis.

Upvotes: 1

Views: 619

Answers (2)

nardecky
nardecky

Reputation: 2631

The above answer from @magnum-π is correct. Creating a formatting function is the easiest and most effective solution.

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';
}

I have also configured a working demo of this to assist the above answer:

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

Upvotes: 1

Magnum π
Magnum π

Reputation: 55

You do indeed use a function. I just had a syntax error.

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

Upvotes: 2

Related Questions