1

HightCharts only accepts an array of integers. I want to convert arrVal array values from strings to ints. How can I do that?

$(document).ready(function(e) {
  $("#btn").on("click", function() {
    var arrVal = $("#selectVal").val(); // I try replace('"','')
    console.log(arrVal);
    
    Highcharts.chart('container', {
      chart: {
        type: 'line'
      },
      title: {
        text: 'Monthly Report Test'
      },
      xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
      },
      yAxis: {
        title: {
          text: 'Test'
        }
      },
      plotOptions: {
        line: {
          dataLabels: {
            enabled: true
          },
          enableMouseTracking: false
        }
      },
      series: [{
        name: 'Zaid Test',
        data: [arrVal] //Only accept int with comma sperated values. # 1,15,17,19,2,1,21,54
      }]
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<form id="myform" method="post">
  <select id="selectVal" name="selectVal[]" multiple="multiple">
    	<option value="2">2</option>
    	<option value="22">22</option>
    	<option value="65">65</option>
    	<option value="54">54</option>
    	<option value="32">32</option>
    	<option value="87">87</option>
    	<option value="9">9</option>
    	<option value="10">10</option>
    	<option value="60">60</option>
    </select>
  <input type="button" id="btn" value="submit" name="s">
</form>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

0

1 Answer 1

2

The issue is because the array holds string values whereas Highcharts expects the values to be integers. To convert the values, you can use map(), like this:

var arrVal = $("#selectVal").val().map(v => parseInt(v, 10));

$(document).ready(function(e) {
  $("#btn").on("click", function() {
    var arrVal = $("#selectVal").val().map(v => parseInt(v, 10));
    console.log(arrVal);
    
    Highcharts.chart('container', {
      chart: {
        type: 'line'
      },
      title: {
        text: 'Monthly Report Test'
      },
      xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
      },
      yAxis: {
        title: {
          text: 'Test'
        }
      },
      plotOptions: {
        line: {
          dataLabels: {
            enabled: true
          },
          enableMouseTracking: false
        }
      },
      series: [{
        name: 'Zaid Test',
        data: arrVal //Only accept int with comma sperated values. # 1,15,17,19,2,1,21,54
      }]
    });

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<form id="myform" method="post">
  <select id="selectVal" name="selectVal[]" multiple="multiple">
    	<option value="2">2</option>
    	<option value="22">22</option>
    	<option value="65">65</option>
    	<option value="54">54</option>
    	<option value="32">32</option>
    	<option value="87">87</option>
    	<option value="9">9</option>
    	<option value="10">10</option>
    	<option value="60">60</option>
    </select>
  <input type="button" id="btn" value="submit" name="s">
</form>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

What is meant by 10 in parseInt?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.