change display format on y-axis when changing comparer in stock chart - Javascript highcharts

Javascript examples for highcharts:Chart Axis

Description

change display format on y-axis when changing comparer in stock chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Highcharts Stock Demo</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
      <script type="text/javascript">
$(function() {/*from   w  ww  . j ava  2 s . c  o  m*/
    var seriesOptions = [],
        yAxisOptions = [],
        seriesCounter = 0,
        names = ['MSFT', 'AAPL', 'GOOG'],
        colors = Highcharts.getOptions().colors;
    $.each(names, function(i, name) {
        $.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename='+ name.toLowerCase() +'-c.json&callback=?',    function(data) {
            seriesOptions[i] = {
                name: name,
                data: data
            };
            seriesCounter++;
            if (seriesCounter == names.length) {
                createChart();
            }
        });
    });
    function createChart() {
        $('#container').highcharts('StockChart', {
            rangeSelector: {
                selected: 4
            },
            yAxis: {
                labels: {
                    formatter: function() {
                        return (this.value > 0 ? '+' : '') + this.value;
                    }
                }
            },
            plotOptions: {
                series: {
                    compare: 'value'
                }
            },
            tooltip: {
                changeDecimals: 2,
                valueDecimals: 2,
                formatter: function() {
                    console.log(this);
return (this.y > 0 ? '+' : '') + this.value;
                }
            },
            series: seriesOptions
        });
    }
    // buttons behaviour
    $('button.compare').click(function () {
        var chart = $('#container').highcharts(),
            compare = $(this).data().compare;
        chart.yAxis[0].setCompare(compare);
    });
});

      </script> 
   </head> 
   <body> 
      <div id="container" style="height: 400px; min-width: 600px"></div> 
      <script src="https://code.highcharts.com/stock/highstock.js"></script> 
      <script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
       Set compare: 
      <button class="compare" data-compare="none">None</button> 
      <button class="compare" data-compare="value">Value</button> 
      <button class="compare" data-compare="percent">Percent</button>  
   </body>
</html>

Related Tutorials