1

I have a program that will show the percentage in high charts when you input the value in the textbox. here is my code

<!DOCTYPE html>


<head>
   <script src="https://code.jquery.com/jquery-2.1.3.js" type="text/javascript"></script>
   <script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
   <script src="http://code.highcharts.com/modules/exporting.js" type="text/javascript"></script>
</head>

<body>
  <div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>
  <br>Number of Arson:
  <input type="text" id="arson" name="arson">
  <br>Number of Homicide:
  <input type="text" id="homicide" name="homicide">
  <br>Number of Physical Injuries:
  <input type="text" id="physicalinjuries" name="physicalinjuries">
  <br>Number of Carnapping:
  <input type="text" id="carnapping" name="carnapping">
  <br>Number of Ethical Law Violation:
  <input type="text" id="elv" name="elv">
  <br>
  <input type="submit" id="chart" value="Submit">
  <script>
$(document).ready(function() {
      ('#container').highcharts({
        chart: {
          plotBackgroundColor: null,
          plotBorderWidth: 1, //null,
          plotShadow: false
        },
        title: {
          text: 'Crimes for this Year'
        },
        tooltip: {
          pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
          pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
              enabled: true,
              format: '<b>{point.name}</b>: {point.percentage:.1f} %',
              style: {
                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
              }
            }
          }
        },
        series: [{
          type: 'pie',
          name: 'Browser share',
          data: [
            ['Arson', 0],
            ['Homicide', 0],
            ['Physical Injuries', 0],
            ['Carnapping', 0],
            ['Ethical Law Violation', 0]

          ]
        }]
      });


    $('#chart').on('click', function() {

      var myChart = $('#container').highcharts();

      var wrongValue = false;

      function setValue(type, slot) {
        var value = Number($('#' + type).val());
        if (isNaN(value) || value === undefined) {
          wrongValue = true;
          value = 0;
        }
        myChart.series[0].data[slot].update(value);
      }

      setValue('arson', 0);
      setValue('homicide', 1);
      setValue('physicalinjuries', 2);
      setValue('carnapping', 3);
      setValue('elv', 4);

      if (wrongValue) {
        // warn the user.
      }

    });
 });
  </script>
</body>

</html>

then i save it as chart.html but when i run it using google chrome the chart is not showing and i get the error of Uncaught ReferenceError: $ is not defined whats wrong with my code?

alacy
  • 4,972
  • 8
  • 30
  • 47
  • I'm sure you've figured it out from all of the questions very similar to this one, but you aren't loading the `jQuery` library anywhere which `$` is an alias for. – alacy Jan 08 '15 at 19:46
  • how can i fix it? this is my first time im using highcharts, i just want to show my chart – Alexiusjoe Coronel Jan 08 '15 at 19:59
  • you need to add the following line `` line and you should put all of your ` – alacy Jan 08 '15 at 20:01
  • ` ` thats my code and i put my javascript code between head tag and i get this error ` Uncaught ReferenceError: Highcharts is not defined Uncaught ReferenceError: $ is not defined` – Alexiusjoe Coronel Jan 08 '15 at 20:06
  • Since I can't answer the question I edited your original post with some code. Try that out. – alacy Jan 08 '15 at 20:10
  • do you mean the code within my question? i just copy it but the same error occurs `Uncaught ReferenceError: $ is not defined` – Alexiusjoe Coronel Jan 08 '15 at 20:15
  • You have to approve the edit that I made to the original question and then try to copy and paste that code in. – alacy Jan 08 '15 at 20:18
  • @aus_lacy I approve your edited post and try it, you can see it if you want but its still not working this is the error `Uncaught TypeError: undefined is not a function` `('#container').highcharts({` this is the part of the code with the error. i think – Alexiusjoe Coronel Jan 08 '15 at 20:25
  • Approve my most recent edit to see my changes. – alacy Jan 08 '15 at 20:29
  • now this is the part with the error `('#container').highcharts({` this is the message Uncaught TypeError: undefined is not a function – Alexiusjoe Coronel Jan 08 '15 at 20:34
  • You need a `$('#container').highcharts({`. You forgot the `$`. – alacy Jan 08 '15 at 20:35
  • Thank you lord and also thank you so much for the big help i really appreciate ty ty ty so much – Alexiusjoe Coronel Jan 08 '15 at 20:37

0 Answers0