简体   繁体   中英

How can I put the Highchart to the html page

Extremely newbie question, I'm very new to html and specifically highcharts stuff. So first things first, Ive made this test.php to make a chart appear.

 <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Highcharts Example</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">

$(function () {
var chart;
$(document).ready(function() {
    $.getJSON("data.php", function(json) {

        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'line',
                marginRight: 130,
                marginBottom: 25
            },
            title: {
                text: 'Disease',
                x: -20 //center
            },
            subtitle: {
                text: '',
                x: -20
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: {
                title: {
                    text: 'Patient Count'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        this.x +': '+ this.y;
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 100,
                borderWidth: 0
            },
            series: json
        });
    });

  });

 });
    </script>
  </head>
    <body>
  <script src="http://code.highcharts.com/highcharts.js"></script>
  <script src="http://code.highcharts.com/modules/exporting.js"></script>

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

   </body>
 </html>

Ran by using apache server, the chart appears. Now I want to make this chart appear in a website of mine. So what I do is put the javascript code to the html page, like so

 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">          </script>
 <script src="js/highcharts.js"></script>
 <script src="js/highcharts-more.js"></script>
 <script src="js/exporting.js"></script>
 <script src="js/jquery-1.8.3.min.js"></script>

 <title>Test Dash</title>
 </head>
 <body>

 <div style="width:500px"></div>

 <script type="text/javascript">

 //My highchart script

 $(function () {
 var chart;
 $(document).ready(function() {
    $.getJSON("data.php", function(json) {

        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'line',
                marginRight: 130,
                marginBottom: 25
            },
            title: {
                text: 'Disease',
                x: -20 //center
            },
            subtitle: {
                text: '',
                x: -20
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: {
                title: {
                    text: 'Patient Count'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        this.x +': '+ this.y;
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 100,
                borderWidth: 0
            },
            series: json
        });
         });

     });

 });

 </script>




 </body>
 </html>

But the chart wont appear. I even tried to insert a simple, non SQL using chart to the html page, but still failed.

 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
 <script src="js/highcharts.js"></script>
 <script src="js/highcharts-more.js"></script>
 <script src="js/exporting.js"></script>
 <script src="js/jquery-1.8.3.min.js"></script>

 <title>Test Dash</title>
 </head>
 <body>

 <div id="container" style="width:500px"></div>

 <script src="js/highcharts.js" type="text/javascript">

 //example script from highchart website

 $(function () { 
 $('container').highcharts({
    chart: {
        type: 'bar'
    },
    title: {
        text: 'Fruit Consumption'
    },
    xAxis: {
        categories: ['Apples', 'Bananas', 'Oranges']
    },
    yAxis: {
        title: {
            text: 'Fruit eaten'
        }
    },
    series: [{
        name: 'Jane',
        data: [1, 0, 4]
    }, {
        name: 'John',
        data: [5, 7, 3]
    }]
 });
  });

 </script>




 </body>
 </html>

How can I make those highcharts chart appear on my website? I am terribly sorry if this comes as an idiotic question for you guys.

First you missed out your target div id "container".. second you need to clean up your code I noticed jquery is including twice in your code. Here is the working code with example JSON.

 <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />       
     <title>Test Dash</title>
     </head>
     <body>

     <div id="container" style="width:500px;height:500px;"></div>

     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
     <script>window.jQuery || document.write('<script src="js/jquery-1.8.3.min.js"><\/script>')</script>
     <script src="http://code.highcharts.com/highcharts.js"></script>
     <script src="http://code.highcharts.com/highcharts-more.js"></script>
     <script src="http://code.highcharts.com/modules/exporting.js"></script>

     <script>
     //My highchart script

     $(function () {
        var chart;
        $(document).ready(function() {
            // $.getJSON("data.php", function(json) {

                chart = new Highcharts.Chart({
                    chart: {
                        renderTo: 'container',
                        type: 'line',
                        marginRight: 130,
                        marginBottom: 25
                    },
                    title: {
                        text: 'Disease',
                        x: -20 //center
                    },
                    subtitle: {
                        text: '',
                        x: -20
                    },
                    xAxis: {
                        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
                    },
                    yAxis: {
                        title: {
                            text: 'Patient Count'
                        },
                        plotLines: [{
                            value: 0,
                            width: 1,
                            color: '#808080'
                        }]
                    },
                    tooltip: {
                        formatter: function() {
                                return '<b>'+ this.series.name +'</b><br/>'+
                                this.x +': '+ this.y;
                        }
                    },
                    legend: {
                        layout: 'vertical',
                        align: 'right',
                        verticalAlign: 'top',
                        x: -10,
                        y: 100,
                        borderWidth: 0
                    },
                    series: [{
                        name: 'Jane',
                        data: [1, 0, 4]
                    }, {
                        name: 'John',
                        data: [5, 7, 3]
                    }]
                });
        //  });

        });

     });

     </script>
 </body>

The difference between the two is that in the first example you have a DIV that the chart is looking for ie

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

Note that is has id="container" and your HighCharts code is looking for it: renderTo: 'container',

You should just add the above DIV to your code where you want the chart to appear.

您应该将其添加到希望图表出现的位置:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM