如何将Highchart放到html页面

时间:2014-08-05 08:23:36

标签: javascript php jquery html highcharts

非常新手的问题,我对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>

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

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

如何将这些高级图表显示在我的网站上?如果这对你们来说是一个愚蠢的问题,我非常抱歉。

3 个答案:

答案 0 :(得分:3)

首先你错过了你的目标div id“容器”..第二,你需要清理你的代码我注意到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>

答案 1 :(得分:2)

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

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

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

您应该将上述DIV添加到您希望图表显示的代码中。

答案 2 :(得分:1)

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

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