我如何把高图到html页面


How can I put the Highchart to the html page

非常新手的问题,我是html的新手,特别是highcharts的东西。首先,我编写了test.php来显示图表。

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

使用apache服务器运行,图表出现。现在我想把这个图表出现在我的一个网站上。我要做的就是把javascript代码放到html页面中,像这样

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

但是图表不会出现。我甚至试图插入一个简单的,非SQL使用图表到html页面,但仍然失败。

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

我怎样才能使那些highcharts图表出现在我的网站上?如果这对你们来说是一个愚蠢的问题,我非常抱歉。

首先你错过了你的目标div id "container"..第二,你需要清理你的代码,我注意到jquery包含两次在你的代码。以下是带有示例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>

两者的区别在于,在第一个例子中,你有一个div,图表正在寻找,即

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

注意,它有id="container"和你的HighCharts代码正在寻找它:renderTo: 'container',

您只需要将上面的DIV添加到您希望图表显示的代码中。

你应该在你想要图表出现的位置添加这个:

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