javascript - Highcharts 的数据由 php 生成的 html 表填充 - 只有一个会加载

标签 javascript php highcharts

我有一个使用 php/mysql 构建的程序,用户可以在开始时选择不同的选项,例如比较特定日期范围内的数据或通过选择要比较的年份来比较逐年数据。一旦用户选择一个选项并输入日期范围或选择要比较的年份,我就会使用 php 从 mysql 数据库中提取数据并将其显示在 html 表中。所有的 html 都是用 php 生成的。

我将 Highcharts 设置为使用 html 表中的数据通过在 php block 下包含一组函数来生成图表。问题是,我首先放置的图表函数会生成,而下面的则不会。

例如,当我将日期范围图表放在脚本中的第一位时,当用户选择日期范围选项时,这些图表将在页面上生成。但如果他们选择逐年选项,该图表将不会显示。如果我在脚本中切换图表函数的顺序,那么当用户选择日期范围函数时,他们会得到所有 html 表,但没有图表,而如果他们选择逐年选项,则图表显示得很好。

我的代码中有一些冲突,但我不知道哪里出了问题。有人可以帮忙吗?

<?php
if ($option == 'opt1') {
print "<div id='hour_chart' style='height:400px;width:100%;'></div>";
print "<div id='hour_data'><strong>Body Counts by Hour</strong>";
print "<table class='results_table table2excel' id='byhour'>";
print "<thead><tr><th>Hour</th>";
print $hour1a;
print "</tr></thead><tbody>";
print "<tr><th>Range 1</th>" . $hour1b . "</tr>";
print "</tbody></table><br><br><br></div>";
print "<button  class='submitex' onclick=\"tablesToExcel(['rangeTable','byhour','byday','bymonth','bylocation'], ['DateRangeTotals','CountsByHour','CountsByDay','CountsByMonth','CountsByLocation'], 'BodyCounts.xls', 'Excel')\">Export to Excel</button>";
}
if ($option == 'opt2') {
print "<div id='year_chart' style='height:400px;width:100%;'></div>";
print "<div id='year_data'><strong>Body Counts by Academic Year</strong>";
print "<table class='results_table table2excel' id='byyear'>";
print "<thead><tr>";
print $ay1;
print "</tr></thead><tbody>";
print "<tr>" . $ay2 . "</tr>";
print "</tbody></table><br><br><br></div>";
print "<button class='submitex' onclick=\"tablesToExcel(['byyear'], ['CountsByAcademicYear'], 'BodyCounts.xls', 'Excel')\">Export to Excel</button>";
}
?>

<script>
$(function () { 
    Highcharts.chart('year_chart', {
    data: {
        table: 'byyear',
        switchRowsAndColumns: true
    },
    chart: {
        type: 'column'
    },
    title: {
        text: 'Body Counts by Academic Year'
    },
    colors: ['#272264','#6AF9C4'],
     yAxis: {
        allowDecimals: false,
        title: {
            text: 'Counts'
        }
    },
       tooltip: {
        formatter: function () {
            return '<b>' + this.series.name + '</b><br/>' +
                this.point.name.toLowerCase() + '<br>' + this.point.y;
        }
    }
});
});
$(function () { 
    Highcharts.chart('hour_chart', {
    data: {
        table: 'byhour',
        switchRowsAndColumns: true
    },
    chart: {
        type: 'column'
    },
    title: {
        text: 'Body Counts by Hour'
    },
    colors: ['#005481','#ED561B'],
    yAxis: {
        allowDecimals: false,
        title: {
            text: 'Counts'
        }
    },
    tooltip: {
        formatter: function () {
            return '<b>' + this.series.name + '</b><br/>' +
                this.point.name.toLowerCase() + '<br>' + this.point.y;
        }
    }
});
});
</script>

最佳答案

我不知道您的所有代码,但我不明白 $option 如何等于 2 个不同的值 - opt1opt2 - 同时,您可以更改您的 javascript 代码,以免生成如下错误:

<script>
<?php if ($option == 'opt2') : ?>
$(function () { 
    Highcharts.chart('year_chart', {
    data: {
        table: 'byyear',
        switchRowsAndColumns: true
    },
    chart: {
        type: 'column'
    },
    title: {
        text: 'Body Counts by Academic Year'
    },
    colors: ['#272264','#6AF9C4'],
     yAxis: {
        allowDecimals: false,
        title: {
            text: 'Counts'
        }
    },
       tooltip: {
        formatter: function () {
            return '<b>' + this.series.name + '</b><br/>' +
                this.point.name.toLowerCase() + '<br>' + this.point.y;
        }
    }
});
<?php elseif ($option == 'opt1') : ?>
});
$(function () { 
    Highcharts.chart('hour_chart', {
    data: {
        table: 'byhour',
        switchRowsAndColumns: true
    },
    chart: {
        type: 'column'
    },
    title: {
        text: 'Body Counts by Hour'
    },
    colors: ['#005481','#ED561B'],
    yAxis: {
        allowDecimals: false,
        title: {
            text: 'Counts'
        }
    },
    tooltip: {
        formatter: function () {
            return '<b>' + this.series.name + '</b><br/>' +
                this.point.name.toLowerCase() + '<br>' + this.point.y;
        }
    }
});
});
<?php endif; ?>
</script>

关于javascript - Highcharts 的数据由 php 生成的 html 表填充 - 只有一个会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46345687/

相关文章:

javascript - 搜索并放大散点图点(Highcharts、react.js)

javascript - HighChart 图表切换到 StockChart 时显示不正确的数据

javascript - Jquery 如果选中复选框不起作用?

php - 从使用 inet_pton() 存储的 MySQL 中获取值

php - Laravel Unisharp 文件管理器 上传图像后 URL 错误

php - 如何通过PHP SDK在Amazon S3存储桶下创建文件夹?

javascript - 声明图表上 Highcharts 条形的位置?

javascript - jQuery隐藏ajax优化

javascript - 访问django for循环中的元素

Javascript 函数多种形式